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.command.CommandException;
020    import org.apache.oozie.command.PreconditionException;
021    import org.apache.oozie.executor.jpa.JPAExecutorException;
022    import org.apache.oozie.executor.jpa.WorkflowInfoWithActionsSubsetGetJPAExecutor;
023    import org.apache.oozie.service.JPAService;
024    import org.apache.oozie.service.Services;
025    import org.apache.oozie.util.ParamChecker;
026    
027    /**
028     * This Xcommand is returning the workflow with action within the range.
029     */
030    public class JobXCommand extends WorkflowXCommand<WorkflowJobBean> {
031        private final String id;
032        private final int start = 1;
033        private final int len = Integer.MAX_VALUE;
034        private WorkflowJobBean workflow;
035    
036        public JobXCommand(String id) {
037            this(id, 1, Integer.MAX_VALUE);
038        }
039    
040        /**
041         * Constructor used to retrieve WF Job
042         * @param id wf jobId
043         * @param start starting index in the list of actions belonging to the job
044         * @param length number of actions to be returned
045         */
046        public JobXCommand(String id, int start, int length) {
047            super("job.info", "job.info", 1, true);
048            this.id = ParamChecker.notEmpty(id, "id");
049        }
050    
051        /* (non-Javadoc)
052         * @see org.apache.oozie.command.XCommand#execute()
053         */
054        @Override
055        protected WorkflowJobBean execute() throws CommandException {
056            try {
057                JPAService jpaService = Services.get().get(JPAService.class);
058                if (jpaService != null) {
059                    this.workflow = jpaService.execute(new WorkflowInfoWithActionsSubsetGetJPAExecutor(this.id, this.start,
060                            this.len));
061                }
062                else {
063                    throw new CommandException(ErrorCode.E0610, this.id);
064                }
065                this.workflow.setConsoleUrl(getJobConsoleUrl(id));
066            }
067            catch (JPAExecutorException ex) {
068                throw new CommandException(ex);
069            }
070            catch (Exception ex) {
071                throw new CommandException(ErrorCode.E0603, ex);
072            }
073    
074            return this.workflow;
075        }
076    
077        /**
078         * @param jobId : Job ID to retrieve console URL
079         * @return console URL
080         */
081        static String getJobConsoleUrl(String jobId) {
082            String consoleUrl = Services.get().getConf().get("oozie.JobCommand.job.console.url", null);
083            return (consoleUrl != null) ? consoleUrl + jobId : null;
084        }
085    
086        /* (non-Javadoc)
087         * @see org.apache.oozie.command.XCommand#getEntityKey()
088         */
089        @Override
090        protected String getEntityKey() {
091            return this.id;
092        }
093    
094        /* (non-Javadoc)
095         * @see org.apache.oozie.command.XCommand#isLockRequired()
096         */
097        @Override
098        protected boolean isLockRequired() {
099            return false;
100        }
101    
102        /* (non-Javadoc)
103         * @see org.apache.oozie.command.XCommand#loadState()
104         */
105        @Override
106        protected void loadState() throws CommandException {
107        }
108    
109        /* (non-Javadoc)
110         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
111         */
112        @Override
113        protected void verifyPrecondition() throws CommandException, PreconditionException {
114        }
115    }