001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.workflow.lite;
019    
020    import org.apache.oozie.workflow.WorkflowException;
021    import org.apache.oozie.ErrorCode;
022    
023    import java.util.ArrayList;
024    import java.util.Arrays;
025    import java.util.List;
026    
027    //TODO javadoc
028    public class JoinNodeDef extends NodeDef {
029    
030        JoinNodeDef() {
031        }
032    
033        public JoinNodeDef(String name, String transition) {
034            super(name, null, JoinNodeHandler.class, Arrays.asList(transition));
035        }
036    
037        public static class JoinNodeHandler extends NodeHandler {
038    
039            public void loopDetection(Context context) throws WorkflowException {
040                String flag = getLoopFlag(context.getNodeDef().getName());
041                if (context.getVar(flag) != null) {
042                    throw new WorkflowException(ErrorCode.E0709, context.getNodeDef().getName());
043                }
044                String parentExecutionPath = context.getParentExecutionPath(context.getExecutionPath());
045                String forkCount = context.getVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath);
046                if (forkCount == null) {
047                    throw new WorkflowException(ErrorCode.E0720, context.getNodeDef().getName());
048                }
049                int count = Integer.parseInt(forkCount) - 1;
050                if (count == 0) {
051                    context.setVar(flag, "true");
052                }
053            }
054    
055            public boolean enter(Context context) throws WorkflowException {
056                String parentExecutionPath = context.getParentExecutionPath(context.getExecutionPath());
057                String forkCount = context.getVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath);
058                if (forkCount == null) {
059                    throw new WorkflowException(ErrorCode.E0720, context.getNodeDef().getName());
060                }
061                int count = Integer.parseInt(forkCount) - 1;
062                if (count > 0) {
063                    context.setVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath, "" + count);
064                    context.deleteExecutionPath();
065                }
066                else {
067                    context.setVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath, null);
068                }
069                return (count == 0);
070            }
071    
072            public List<String> multiExit(Context context) {
073                String parentExecutionPath = context.getParentExecutionPath(context.getExecutionPath());
074                // NOW we delete..
075                context.deleteExecutionPath();
076    
077                String transition = context.getNodeDef().getTransitions().get(0);
078                String fullTransition = context.createFullTransition(parentExecutionPath, transition);
079                List<String> transitions = new ArrayList<String>(1);
080                transitions.add(fullTransition);
081                return transitions;
082            }
083    
084            public String exit(Context context) {
085                throw new UnsupportedOperationException();
086            }
087    
088            public void kill(Context context) {
089            }
090    
091            public void fail(Context context) {
092            }
093        }
094    
095    }