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.command.wf;
016    
017    import org.apache.oozie.WorkflowActionBean;
018    import org.apache.oozie.ErrorCode;
019    import org.apache.oozie.command.Command;
020    import org.apache.oozie.command.CommandException;
021    import org.apache.oozie.service.ActionService;
022    import org.apache.oozie.action.ActionExecutor;
023    import org.apache.oozie.store.StoreException;
024    import org.apache.oozie.store.WorkflowStore;
025    import org.apache.oozie.store.Store;
026    import org.apache.oozie.util.ParamChecker;
027    import org.apache.oozie.util.XLog;
028    import org.apache.oozie.service.Services;
029    
030    import java.util.Properties;
031    
032    public class CompletedActionCommand extends WorkflowCommand<Void> {
033        private String actionId;
034        private String externalStatus;
035        private Properties actionData;
036    
037        public CompletedActionCommand(String actionId, String externalStatus, Properties actionData, int priority) {
038            super("callback", "callback", priority, XLog.STD);
039            this.actionId = ParamChecker.notEmpty(actionId, "actionId");
040            this.externalStatus = ParamChecker.notEmpty(externalStatus, "externalStatus");
041            this.actionData = actionData;
042        }
043    
044        public CompletedActionCommand(String actionId, String externalStatus, Properties actionData) {
045            this(actionId, externalStatus, actionData, 1);
046        }
047    
048        @Override
049        protected Void call(WorkflowStore store) throws StoreException, CommandException {
050            WorkflowActionBean action = store.getAction(actionId, false);
051            setLogInfo(action);
052            if (action.getStatus() == WorkflowActionBean.Status.RUNNING) {
053                ActionExecutor executor = Services.get().get(ActionService.class).getExecutor(action.getType());
054                // this is done because oozie notifications (of sub-wfs) is send
055                // every status change, not only on completion.
056                if (executor.isCompleted(externalStatus)) {
057                    queueCallable(new ActionCheckCommand(action.getId(), getPriority(), -1));
058                }
059            }
060            else {
061                throw new CommandException(ErrorCode.E0800, actionId, action.getStatus());
062            }
063            return null;
064        }
065    
066    }