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.ErrorCode;
018    import org.apache.oozie.WorkflowJobBean;
019    import org.apache.oozie.XException;
020    import org.apache.oozie.command.CommandException;
021    import org.apache.oozie.command.PreconditionException;
022    import org.apache.oozie.executor.jpa.WorkflowJobGetJPAExecutor;
023    import org.apache.oozie.service.JPAService;
024    import org.apache.oozie.service.Services;
025    import org.apache.oozie.util.LogUtils;
026    import org.apache.oozie.util.ParamChecker;
027    
028    public class DefinitionXCommand extends WorkflowXCommand<String> {
029        private String jobId;
030        private WorkflowJobBean wfJob;
031    
032        public DefinitionXCommand(String jobId) {
033            super("definition", "definition", 1);
034            this.jobId = ParamChecker.notEmpty(jobId, "jobId");
035        }
036    
037        @Override
038        protected boolean isLockRequired() {
039            return false;
040        }
041    
042        @Override
043        protected String getEntityKey() {
044            return this.jobId;
045        }
046    
047        @Override
048        protected void loadState() throws CommandException {
049            try {
050                JPAService jpaService = Services.get().get(JPAService.class);
051    
052                if (jpaService != null) {
053                    this.wfJob = jpaService.execute(new WorkflowJobGetJPAExecutor(jobId));
054                    LogUtils.setLogInfo(wfJob, logInfo);
055                }
056                else {
057                    LOG.error(ErrorCode.E0610);
058                }
059            }
060            catch (XException ex) {
061                throw new CommandException(ex);
062            }
063        }
064    
065        @Override
066        protected void verifyPrecondition() throws CommandException, PreconditionException {
067        }
068    
069        @Override
070        protected String execute() throws CommandException {
071            if (wfJob != null) {
072                return wfJob.getWorkflowInstance().getApp().getDefinition();
073            }
074            else {
075                throw new CommandException(ErrorCode.E0604);
076            }
077        }
078    
079    }