001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.workflow.lite;
016    
017    import org.apache.hadoop.io.Writable;
018    import org.apache.oozie.util.ParamChecker;
019    import org.apache.oozie.util.XLog;
020    
021    import java.io.DataInput;
022    import java.io.DataOutput;
023    import java.io.IOException;
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.List;
027    
028    //TODO javadoc
029    public class NodeDef implements Writable {
030        private String name;
031        private Class<? extends NodeHandler> handlerClass;
032        private String conf;
033        private List<String> transitions = new ArrayList<String>();
034        private String cred;
035    
036        NodeDef() {
037        }
038    
039        NodeDef(String name, String conf, Class<? extends NodeHandler> handlerClass, List<String> transitions) {
040            this.name = ParamChecker.notEmpty(name, "name");
041            this.conf = conf;
042            this.handlerClass = ParamChecker.notNull(handlerClass, "handlerClass");
043            this.transitions = Collections.unmodifiableList(ParamChecker.notEmptyElements(transitions, "transitions"));
044            this.cred = "null";
045        }
046    
047        NodeDef(String name, String conf, Class<? extends NodeHandler> handlerClass, List<String> transitions,String cred) {
048            this.name = ParamChecker.notEmpty(name, "name");
049            this.conf = conf;
050            this.handlerClass = ParamChecker.notNull(handlerClass, "handlerClass");
051            this.transitions = Collections.unmodifiableList(ParamChecker.notEmptyElements(transitions, "transitions"));
052            if(cred != null){
053                this.cred = cred;
054            }
055            else{
056                this.cred = "null";
057            }
058        }
059        
060        public boolean equals(NodeDef other) {
061            return !(other == null || getClass() != other.getClass() || !getName().equals(other.getName()));
062        }
063    
064        public int hashCode() {
065            return name.hashCode();
066        }
067    
068        public String getName() {
069            return name;
070        }
071    
072        /**
073         * @return the auth
074         */
075        public String getCred() {
076            return cred;
077        }
078    
079        public Class<? extends NodeHandler> getHandlerClass() {
080            return handlerClass;
081        }
082    
083        public List<String> getTransitions() {
084            return transitions;
085        }
086    
087        public String getConf() {
088            return conf;
089        }
090    
091        @Override
092        @SuppressWarnings("unchecked")
093        public void readFields(DataInput dataInput) throws IOException {
094            name = dataInput.readUTF();
095            cred = dataInput.readUTF();
096            String handlerClassName = dataInput.readUTF();
097            if ((handlerClassName != null) && (handlerClassName.length() > 0)) {
098                try {
099                    handlerClass = (Class<? extends NodeHandler>) Class.forName(handlerClassName);
100                }
101                catch (ClassNotFoundException ex) {
102                    throw new IOException(ex);
103                }
104            }
105            conf = dataInput.readUTF();
106            if (conf.equals("null")) {
107                conf = null;
108            }
109            int numTrans = dataInput.readInt();
110            transitions = new ArrayList<String>(numTrans);
111            for (int i = 0; i < numTrans; i++) {
112                transitions.add(dataInput.readUTF());
113            }
114        }
115    
116        @Override
117        public void write(DataOutput dataOutput) throws IOException {
118            dataOutput.writeUTF(name);
119            if(cred != null){
120                dataOutput.writeUTF(cred);
121            }else{
122                dataOutput.writeUTF("null");
123            }
124            XLog.getLog(getClass()).debug("write: Name:" + name +" Cred: "+ cred);
125            dataOutput.writeUTF(handlerClass.getName());
126            if (conf != null) {
127                dataOutput.writeUTF(conf);
128            }
129            else {
130                dataOutput.writeUTF("null");
131            }
132            dataOutput.writeInt(transitions.size());
133            for (String transition : transitions) {
134                dataOutput.writeUTF(transition);
135            }
136        }
137    
138    }