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