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.oozie.workflow.WorkflowException;
018    import org.apache.oozie.ErrorCode;
019    
020    import java.util.ArrayList;
021    import java.util.Arrays;
022    import java.util.List;
023    
024    //TODO javadoc
025    public class JoinNodeDef extends NodeDef {
026    
027        JoinNodeDef() {
028        }
029    
030        public JoinNodeDef(String name, String transition) {
031            super(name, null, JoinNodeHandler.class, Arrays.asList(transition));
032        }
033    
034        public static class JoinNodeHandler extends NodeHandler {
035    
036            public void loopDetection(Context context) throws WorkflowException {
037                String flag = getLoopFlag(context.getNodeDef().getName());
038                if (context.getVar(flag) != null) {
039                    throw new WorkflowException(ErrorCode.E0709, context.getNodeDef().getName());
040                }
041                String parentExecutionPath = context.getParentExecutionPath(context.getExecutionPath());
042                String forkCount = context.getVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath);
043                if (forkCount == null) {
044                    throw new WorkflowException(ErrorCode.E0720, context.getNodeDef().getName());
045                }
046                int count = Integer.parseInt(forkCount) - 1;
047                if (count == 0) {
048                    context.setVar(flag, "true");
049                }
050            }
051    
052            public boolean enter(Context context) throws WorkflowException {
053                String parentExecutionPath = context.getParentExecutionPath(context.getExecutionPath());
054                String forkCount = context.getVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath);
055                if (forkCount == null) {
056                    throw new WorkflowException(ErrorCode.E0720, context.getNodeDef().getName());
057                }
058                int count = Integer.parseInt(forkCount) - 1;
059                if (count > 0) {
060                    context.setVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath, "" + count);
061                    context.deleteExecutionPath();
062                }
063                else {
064                    context.setVar(ForkNodeDef.FORK_COUNT_PREFIX + parentExecutionPath, null);
065                }
066                return (count == 0);
067            }
068    
069            public List<String> multiExit(Context context) {
070                String parentExecutionPath = context.getParentExecutionPath(context.getExecutionPath());
071                // NOW we delete..
072                context.deleteExecutionPath();
073    
074                String transition = context.getNodeDef().getTransitions().get(0);
075                String fullTransition = context.createFullTransition(parentExecutionPath, transition);
076                List<String> transitions = new ArrayList<String>(1);
077                transitions.add(fullTransition);
078                return transitions;
079            }
080    
081            public String exit(Context context) {
082                throw new UnsupportedOperationException();
083            }
084    
085            public void kill(Context context) {
086            }
087    
088            public void fail(Context context) {
089            }
090        }
091    
092    }