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 */
018package org.apache.oozie.workflow.lite;
019
020import org.apache.oozie.workflow.WorkflowException;
021import org.apache.oozie.workflow.WorkflowInstance;
022import org.apache.oozie.ErrorCode;
023
024import java.util.ArrayList;
025import java.util.List;
026
027//TODO javadoc
028public abstract class NodeHandler {
029
030    public interface Context {
031
032        public NodeDef getNodeDef();
033
034        public String getExecutionPath();
035
036        public String getParentExecutionPath(String executionPath);
037
038        public String getSignalValue();
039
040        public void setVar(String name, String value);
041
042        public String getVar(String name);
043
044        public void setTransientVar(String name, Object value);
045
046        public Object getTransientVar(String name);
047
048        public String createExecutionPath(String name);
049
050        //can be called only from exit(), creation of execPaths is automatic
051        //when a handler returns more than one transition.
052        public void deleteExecutionPath();
053
054        //must be used by multiExit
055        public String createFullTransition(String executionPath, String transition);
056
057        public void killJob();
058
059        public void completeJob();
060
061        public LiteWorkflowInstance getProcessInstance();
062    }
063
064    private static final String VISITED = "visited";
065
066    public static String getLoopFlag(String nodeName) {
067        return nodeName + WorkflowInstance.NODE_VAR_SEPARATOR + VISITED;
068    }
069
070    public void loopDetection(Context context) throws WorkflowException {
071        String flag = getLoopFlag(context.getNodeDef().getName());
072        if (context.getVar(flag) != null) {
073            throw new WorkflowException(ErrorCode.E0709, context.getNodeDef().getName());
074        }
075        context.setVar(flag, "true");
076    }
077
078    // TRUE means immediate exit, false means has to be signal
079    public abstract boolean enter(Context context) throws WorkflowException;
080
081    // the return list contains executionPath#transition, important for fork
082    public List<String> multiExit(Context context) throws WorkflowException {
083        List<String> transitions = new ArrayList<String>(1);
084        String transition = exit(context);
085        if (transition != null) {
086            transitions.add(context.createFullTransition(context.getExecutionPath(), transition));
087        }
088        return transitions;
089    }
090
091
092    public abstract String exit(Context context) throws WorkflowException;
093
094    public void kill(Context context) {
095    }
096
097    public void fail(Context context) {
098    }
099}