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