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.hadoop.util.ReflectionUtils;
019    import org.apache.oozie.workflow.WorkflowApp;
020    import org.apache.oozie.workflow.WorkflowException;
021    import org.apache.oozie.util.ParamChecker;
022    import org.apache.oozie.util.XLog;
023    import org.apache.oozie.ErrorCode;
024    
025    import java.io.DataInput;
026    import java.io.DataOutput;
027    import java.io.IOException;
028    import java.util.ArrayList;
029    import java.util.Collection;
030    import java.util.Collections;
031    import java.util.LinkedHashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    //TODO javadoc
036    public class LiteWorkflowApp implements Writable, WorkflowApp {
037        private String name;
038        private String definition;
039        private Map<String, NodeDef> nodesMap = new LinkedHashMap<String, NodeDef>();
040        private boolean complete = false;
041    
042        LiteWorkflowApp() {
043        }
044    
045        public LiteWorkflowApp(String name, String definition, StartNodeDef startNode) {
046            this.name = ParamChecker.notEmpty(name, "name");
047            this.definition = ParamChecker.notEmpty(definition, "definition");
048            nodesMap.put(StartNodeDef.START, startNode);
049        }
050    
051        public boolean equals(LiteWorkflowApp other) {
052            return !(other == null || getClass() != other.getClass() || !getName().equals(other.getName()));
053        }
054    
055        public int hashCode() {
056            return name.hashCode();
057        }
058    
059        public LiteWorkflowApp addNode(NodeDef node) throws WorkflowException {
060            ParamChecker.notNull(node, "node");
061            if (complete) {
062                throw new WorkflowException(ErrorCode.E0704,
063                                            XLog.format("Definition [{0}] already complete", name));
064            }
065            if (nodesMap.containsKey(node.getName())) {
066                throw new WorkflowException(ErrorCode.E0705,
067                                            XLog.format("Node [{0}] already defined", node.getName()));
068            }
069            if (node.getTransitions().contains(node.getName())) {
070                throw new WorkflowException(ErrorCode.E0706,
071                                            XLog.format("Node [{0}] cannot transition to itself", node.getName()));
072            }
073            nodesMap.put(node.getName(), node);
074            if (node instanceof EndNodeDef) {
075                complete = true;
076            }
077            return this;
078        }
079    
080        public String getName() {
081            return name;
082        }
083    
084        public String getDefinition() {
085            return definition;
086        }
087    
088        public Collection<NodeDef> getNodeDefs() {
089            return Collections.unmodifiableCollection(nodesMap.values());
090        }
091    
092        public NodeDef getNode(String name) {
093            return nodesMap.get(name);
094        }
095    
096        public void validateWorkflowIntegrity() {
097            //TODO traverse wf, ensure there are not cycles, no open paths, and one END
098        }
099    
100        public void validateTransition(String name, String transition) {
101            ParamChecker.notEmpty(name, "name");
102            ParamChecker.notEmpty(transition, "transition");
103            NodeDef node = getNode(name);
104            if (!node.getTransitions().contains(transition)) {
105                throw new IllegalArgumentException("invalid transition");
106            }
107        }
108    
109    
110        @Override
111        public void write(DataOutput dataOutput) throws IOException {
112            dataOutput.writeUTF(name);
113            //dataOutput.writeUTF(definition);
114            //writeUTF() has limit 65535, so split long string to multiple short strings
115            List<String> defList = divideStr(definition);
116            dataOutput.writeInt(defList.size());
117            for (String d : defList) {
118                dataOutput.writeUTF(d);
119            }
120            dataOutput.writeInt(nodesMap.size());
121            for (NodeDef n : getNodeDefs()) {
122                dataOutput.writeUTF(n.getClass().getName());
123                n.write(dataOutput);
124            }
125        }
126    
127        /**
128         * To split long string to a list of smaller strings.
129         *
130         * @param str
131         * @return List
132         */
133        private List<String> divideStr(String str) {
134            List<String> list = new ArrayList<String>();
135            int len = 20000;
136            int strlen = str.length();
137            int start = 0;
138            int end = len;
139    
140            while (end < strlen) {
141                list.add(str.substring(start, end));
142                start = end;
143                end += len;
144            }
145    
146            if (strlen <= end) {
147                list.add(str.substring(start, strlen));
148            }
149            return list;
150        }
151    
152        @Override
153        public void readFields(DataInput dataInput) throws IOException {
154            name = dataInput.readUTF();
155            //definition = dataInput.readUTF();
156            //read the full definition back
157            int defListSize = dataInput.readInt();
158            StringBuilder sb = new StringBuilder();
159            for (int i = 0; i < defListSize; i++) {
160                sb.append(dataInput.readUTF());
161            }
162            definition = sb.toString();
163    
164            int numNodes = dataInput.readInt();
165            for (int x = 0; x < numNodes; x++) {
166                try {
167                    String nodeDefClass = dataInput.readUTF();
168                    NodeDef node = (NodeDef) ReflectionUtils.newInstance(Class.forName(nodeDefClass), null);
169                    node.readFields(dataInput);
170                    addNode(node);
171                }
172                catch (WorkflowException ex) {
173                    throw new IOException(ex);
174                }
175                catch (ClassNotFoundException e) {
176                    throw new IOException(e);
177                }
178            }
179        }
180    
181    }