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 final int start = 1;
036        private final 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        }
053    
054        /* (non-Javadoc)
055         * @see org.apache.oozie.command.XCommand#execute()
056         */
057        @Override
058        protected WorkflowJobBean execute() throws CommandException {
059            try {
060                JPAService jpaService = Services.get().get(JPAService.class);
061                if (jpaService != null) {
062                    this.workflow = jpaService.execute(new WorkflowInfoWithActionsSubsetGetJPAExecutor(this.id, this.start,
063                            this.len));
064                }
065                else {
066                    throw new CommandException(ErrorCode.E0610, this.id);
067                }
068                this.workflow.setConsoleUrl(getJobConsoleUrl(id));
069            }
070            catch (JPAExecutorException ex) {
071                throw new CommandException(ex);
072            }
073            catch (Exception ex) {
074                throw new CommandException(ErrorCode.E0603, ex.getMessage(), ex);
075            }
076    
077            return this.workflow;
078        }
079    
080        /**
081         * @param jobId : Job ID to retrieve console URL
082         * @return console URL
083         */
084        static String getJobConsoleUrl(String jobId) {
085            String consoleUrl = Services.get().getConf().get("oozie.JobCommand.job.console.url", null);
086            return (consoleUrl != null) ? consoleUrl + jobId : null;
087        }
088    
089        /* (non-Javadoc)
090         * @see org.apache.oozie.command.XCommand#getEntityKey()
091         */
092        @Override
093        public String getEntityKey() {
094            return this.id;
095        }
096    
097        /* (non-Javadoc)
098         * @see org.apache.oozie.command.XCommand#isLockRequired()
099         */
100        @Override
101        protected boolean isLockRequired() {
102            return false;
103        }
104    
105        /* (non-Javadoc)
106         * @see org.apache.oozie.command.XCommand#loadState()
107         */
108        @Override
109        protected void loadState() throws CommandException {
110        }
111    
112        /* (non-Javadoc)
113         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
114         */
115        @Override
116        protected void verifyPrecondition() throws CommandException, PreconditionException {
117        }
118    }