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