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.command.wf;
019
020import java.util.Properties;
021
022import org.apache.oozie.ErrorCode;
023import org.apache.oozie.WorkflowActionBean;
024import org.apache.oozie.action.ActionExecutor;
025import org.apache.oozie.command.CommandException;
026import org.apache.oozie.command.PreconditionException;
027import org.apache.oozie.executor.jpa.WorkflowActionQueryExecutor;
028import org.apache.oozie.executor.jpa.WorkflowActionQueryExecutor.WorkflowActionQuery;
029import org.apache.oozie.service.ActionService;
030import org.apache.oozie.service.Services;
031import org.apache.oozie.util.LogUtils;
032import org.apache.oozie.util.ParamChecker;
033
034/**
035 * This command is executed once the Workflow command is finished.
036 */
037public class CompletedActionXCommand extends WorkflowXCommand<Void> {
038    private final String actionId;
039    private final String externalStatus;
040    private WorkflowActionBean wfactionBean;
041
042    public CompletedActionXCommand(String actionId, String externalStatus, Properties actionData, int priority) {
043        super("callback", "callback", priority);
044        this.actionId = ParamChecker.notEmpty(actionId, "actionId");
045        this.externalStatus = ParamChecker.notEmpty(externalStatus, "externalStatus");
046    }
047
048    public CompletedActionXCommand(String actionId, String externalStatus, Properties actionData) {
049        this(actionId, externalStatus, actionData, 1);
050    }
051
052    /*
053     * (non-Javadoc)
054     *
055     * @see org.apache.oozie.command.XCommand#eagerLoadState()
056     */
057    @Override
058    protected void eagerLoadState() throws CommandException {
059        try {
060            this.wfactionBean = WorkflowActionQueryExecutor.getInstance().get(WorkflowActionQuery.GET_ACTION_COMPLETED,
061                    this.actionId);
062        }
063        catch (Exception ex) {
064            throw new CommandException(ErrorCode.E0603, ex.getMessage(), ex);
065        }
066        LogUtils.setLogInfo(this.wfactionBean, logInfo);
067    }
068
069    /*
070     * (non-Javadoc)
071     *
072     * @see org.apache.oozie.command.XCommand#eagerVerifyPrecondition()
073     */
074    @Override
075    protected void eagerVerifyPrecondition() throws CommandException, PreconditionException {
076        if (this.wfactionBean.getStatus() != WorkflowActionBean.Status.RUNNING) {
077            throw new CommandException(ErrorCode.E0800, actionId, this.wfactionBean.getStatus());
078        }
079    }
080
081    /*
082     * (non-Javadoc)
083     *
084     * @see org.apache.oozie.command.XCommand#execute()
085     */
086    @Override
087    protected Void execute() throws CommandException {
088        ActionExecutor executor = Services.get().get(ActionService.class).getExecutor(this.wfactionBean.getType());
089        // this is done because oozie notifications (of sub-wfs) is send
090        // every status change, not only on completion.
091        if (executor.isCompleted(externalStatus)) {
092            queue(new ActionCheckXCommand(this.wfactionBean.getId(), getPriority(), -1));
093        }
094        return null;
095    }
096
097    /*
098     * (non-Javadoc)
099     *
100     * @see org.apache.oozie.command.XCommand#getEntityKey()
101     */
102    @Override
103    public String getEntityKey() {
104        return null;
105    }
106
107    /*
108     * (non-Javadoc)
109     *
110     * @see org.apache.oozie.command.XCommand#isLockRequired()
111     */
112    @Override
113    protected boolean isLockRequired() {
114        return false;
115    }
116
117    /*
118     * (non-Javadoc)
119     *
120     * @see org.apache.oozie.command.XCommand#loadState()
121     */
122    @Override
123    protected void loadState() throws CommandException {
124        eagerLoadState();
125    }
126
127    /*
128     * (non-Javadoc)
129     *
130     * @see org.apache.oozie.command.XCommand#verifyPrecondition()
131     */
132    @Override
133    protected void verifyPrecondition() throws CommandException, PreconditionException {
134        eagerVerifyPrecondition();
135    }
136}