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