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    
020    import java.io.DataInput;
021    import java.io.DataOutput;
022    import java.io.IOException;
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.List;
026    
027    //TODO javadoc
028    public class NodeDef implements Writable {
029        private String name;
030        private Class<? extends NodeHandler> handlerClass;
031        private String conf;
032        private List<String> transitions = new ArrayList<String>();
033    
034        NodeDef() {
035        }
036    
037        NodeDef(String name, String conf, Class<? extends NodeHandler> handlerClass, List<String> transitions) {
038            this.name = ParamChecker.notEmpty(name, "name");
039            this.conf = conf;
040            this.handlerClass = ParamChecker.notNull(handlerClass, "handlerClass");
041            this.transitions = Collections.unmodifiableList(ParamChecker.notEmptyElements(transitions, "transitions"));
042        }
043    
044        public boolean equals(NodeDef other) {
045            return !(other == null || getClass() != other.getClass() || !getName().equals(other.getName()));
046        }
047    
048        public int hashCode() {
049            return name.hashCode();
050        }
051    
052        public String getName() {
053            return name;
054        }
055    
056        public Class<? extends NodeHandler> getHandlerClass() {
057            return handlerClass;
058        }
059    
060        public List<String> getTransitions() {
061            return transitions;
062        }
063    
064        public String getConf() {
065            return conf;
066        }
067    
068        @Override
069        @SuppressWarnings("unchecked")
070        public void readFields(DataInput dataInput) throws IOException {
071            name = dataInput.readUTF();
072            String handlerClassName = dataInput.readUTF();
073            if ((handlerClassName != null) && (handlerClassName.length() > 0)) {
074                try {
075                    handlerClass = (Class<? extends NodeHandler>) Class.forName(handlerClassName);
076                }
077                catch (ClassNotFoundException ex) {
078                    throw new IOException(ex);
079                }
080            }
081            conf = dataInput.readUTF();
082            if (conf.equals("null")) {
083                conf = null;
084            }
085            int numTrans = dataInput.readInt();
086            transitions = new ArrayList<String>(numTrans);
087            for (int i = 0; i < numTrans; i++) {
088                transitions.add(dataInput.readUTF());
089            }
090        }
091    
092        @Override
093        public void write(DataOutput dataOutput) throws IOException {
094            dataOutput.writeUTF(name);
095            dataOutput.writeUTF(handlerClass.getName());
096            if (conf != null) {
097                dataOutput.writeUTF(conf);
098            }
099            else {
100                dataOutput.writeUTF("null");
101            }
102            dataOutput.writeInt(transitions.size());
103            for (String transition : transitions) {
104                dataOutput.writeUTF(transition);
105            }
106        }
107    
108    }