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