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