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
019package org.apache.oozie.action.decision;
020
021import org.apache.oozie.client.WorkflowAction;
022import org.apache.oozie.action.ActionExecutor;
023import org.apache.oozie.action.ActionExecutorException;
024import org.apache.oozie.util.XLog;
025import org.apache.oozie.util.XmlUtils;
026import org.jdom.Element;
027import org.jdom.JDOMException;
028import org.jdom.Namespace;
029
030import java.util.List;
031
032public class DecisionActionExecutor extends ActionExecutor {
033    public static final String ACTION_TYPE = "switch";
034
035    private static final String TRUE = "true";
036
037    public static final String XML_ERROR = "XML_ERROR";
038
039    public DecisionActionExecutor() {
040        super(ACTION_TYPE);
041    }
042
043    @SuppressWarnings("unchecked")
044    public void start(Context context, WorkflowAction action) throws ActionExecutorException {
045        XLog log = XLog.getLog(getClass());
046        log.trace("start() begins");
047        try {
048            String confStr = action.getConf();
049            context.setStartData("-", "-", "-");
050            Element conf = XmlUtils.parseXml(confStr);
051            Namespace ns = conf.getNamespace();
052
053            String externalState = null;
054
055            for (Element eval : (List<Element>) conf.getChildren("case", ns)) {
056                if (TRUE.equals(eval.getTextTrim())) {
057                    externalState = eval.getAttributeValue("to");
058                    break;
059                }
060            }
061            if (externalState == null) {
062                Element def = conf.getChild("default", ns);
063                if (def != null) {
064                    externalState = def.getAttributeValue("to");
065                }
066            }
067
068            if (externalState == null) {
069                throw new IllegalStateException("Transition cannot be NULL");
070            }
071            // for decision we are piggybacking on external status to transfer the transition,
072            // the {@link ActionEndCommand} does the special handling of setting it as signal value.
073            context.setExecutionData(externalState, null);
074        }
075        catch (JDOMException ex) {
076            throw new ActionExecutorException(ActionExecutorException.ErrorType.FAILED, XML_ERROR, ex.getMessage(), ex);
077        }
078        finally {
079            log.trace("start() ends");
080        }
081    }
082
083    public void end(Context context, WorkflowAction action) throws ActionExecutorException {
084        context.setEndData(WorkflowAction.Status.OK, action.getExternalStatus());
085    }
086
087    public void check(Context context, WorkflowAction action) throws ActionExecutorException {
088        throw new UnsupportedOperationException();
089    }
090
091    public void kill(Context context, WorkflowAction action) throws ActionExecutorException {
092        throw new UnsupportedOperationException();
093    }
094
095    public boolean isCompleted(String externalStatus) {
096        return true;
097    }
098
099}