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;
020
021import java.util.Collections;
022import java.util.Iterator;
023import java.util.List;
024import java.util.Properties;
025
026import org.apache.oozie.client.OozieClient;
027import org.apache.oozie.client.OozieClientException;
028import org.apache.oozie.client.WorkflowJob;
029import org.apache.oozie.util.XConfiguration;
030
031/**
032 * Client API to submit and manage Oozie workflow jobs against an Oozie intance. <p> This class is thread safe. <p>
033 * Syntax for filter for the {@link #getJobsInfo(String)}  {@link #getJobsInfo(String, int, int)}  methods:
034 * <code>[NAME=VALUE][;NAME=VALUE]*</code>. <p> Valid filter names are: <p> <ul> <li>name: the workflow application
035 * name from the workflow definition.</li> <li>user: the user that submitted the job.</li> <li>group: the group for the
036 * job.</li> <li>status: the status of the job.</li> </ul> <p> The query will do an AND among all the filter names. The
037 * query will do an OR among all the filter values for the same name. Multiple values must be specified as different
038 * name value pairs.
039 */
040public class LocalOozieClient extends OozieClient {
041
042    private DagEngine dagEngine;
043
044    /**
045     * Create a workflow client for Oozie local use. <p>
046     *
047     * @param dagEngine the dag engine instance to use.
048     */
049    public LocalOozieClient(DagEngine dagEngine) {
050        this.dagEngine = dagEngine;
051    }
052
053    /**
054     * Return the Oozie URL of the workflow client instance. <p> This URL is the base URL fo the Oozie system, with not
055     * protocol versioning.
056     *
057     * @return the Oozie URL of the workflow client instance.
058     */
059    @Override
060    public String getOozieUrl() {
061        return "localoozie";
062    }
063
064    /**
065     * Return the Oozie URL used by the client and server for WS communications. <p> This URL is the original URL plus
066     * the versioning element path.
067     *
068     * @return the Oozie URL used by the client and server for communication.
069     * @throws org.apache.oozie.client.OozieClientException thrown in the client and the server are not protocol
070     * compatible.
071     */
072    @Override
073    public String getProtocolUrl() throws OozieClientException {
074        return "localoozie";
075    }
076
077    /**
078     * Validate that the Oozie client and server instances are protocol compatible.
079     *
080     * @throws org.apache.oozie.client.OozieClientException thrown in the client and the server are not protocol
081     * compatible.
082     */
083    @Override
084    public synchronized void validateWSVersion() throws OozieClientException {
085    }
086
087    /**
088     * Create an empty configuration with just the {@link #USER_NAME} set to the JVM user name and the {@link
089     * #GROUP_NAME} set to 'other'.
090     *
091     * @return an empty configuration.
092     */
093    @Override
094    public Properties createConfiguration() {
095        Properties conf = new Properties();
096        if (dagEngine != null) {
097            conf.setProperty(USER_NAME, dagEngine.getUser());
098        }
099        conf.setProperty(GROUP_NAME, "users");
100        return conf;
101    }
102
103    /**
104     * Set a HTTP header to be used in the WS requests by the workflow instance.
105     *
106     * @param name header name.
107     * @param value header value.
108     */
109    @Override
110    public void setHeader(String name, String value) {
111    }
112
113    /**
114     * Get the value of a set HTTP header from the workflow instance.
115     *
116     * @param name header name.
117     * @return header value, <code>null</code> if not set.
118     */
119    @Override
120    public String getHeader(String name) {
121        return null;
122    }
123
124    /**
125     * Remove a HTTP header from the workflow client instance.
126     *
127     * @param name header name.
128     */
129    @Override
130    public void removeHeader(String name) {
131    }
132
133    /**
134     * Return an iterator with all the header names set in the workflow instance.
135     *
136     * @return header names.
137     */
138    @Override
139    @SuppressWarnings("unchecked")
140    public Iterator<String> getHeaderNames() {
141        return Collections.EMPTY_SET.iterator();
142    }
143
144
145    /**
146     * Submit a workflow job.
147     *
148     * @param conf job configuration.
149     * @return the job Id.
150     * @throws org.apache.oozie.client.OozieClientException thrown if the job could not be submitted.
151     */
152    @Override
153    public String submit(Properties conf) throws OozieClientException {
154        try {
155            return dagEngine.submitJob(new XConfiguration(conf), false);
156        }
157        catch (DagEngineException ex) {
158            throw new OozieClientException(ex.getErrorCode().toString(), ex);
159        }
160    }
161
162    /**
163     * Start a workflow job.
164     *
165     * @param jobId job Id.
166     * @throws org.apache.oozie.client.OozieClientException thrown if the job could not be started.
167     */
168    @Override
169    public void start(String jobId) throws OozieClientException {
170        try {
171            dagEngine.start(jobId);
172        }
173        catch (DagEngineException ex) {
174            throw new OozieClientException(ex.getErrorCode().toString(), ex);
175        }
176    }
177
178    /**
179     * Submit and start a workflow job.
180     *
181     * @param conf job configuration.
182     * @return the job Id.
183     * @throws org.apache.oozie.client.OozieClientException thrown if the job could not be submitted.
184     */
185    @Override
186    public String run(Properties conf) throws OozieClientException {
187        try {
188            return dagEngine.submitJob(new XConfiguration(conf), true);
189        }
190        catch (DagEngineException ex) {
191            throw new OozieClientException(ex.getErrorCode().toString(), ex);
192        }
193    }
194
195    /**
196     * Rerun a workflow job.
197     *
198     * @param jobId job Id to rerun.
199     * @param conf configuration information for the rerun.
200     * @throws org.apache.oozie.client.OozieClientException thrown if the job could not be started.
201     */
202    @Override
203    public void reRun(String jobId, Properties conf) throws OozieClientException {
204        try {
205            dagEngine.reRun(jobId, new XConfiguration(conf));
206        }
207        catch (DagEngineException ex) {
208            throw new OozieClientException(ex.getErrorCode().toString(), ex);
209        }
210    }
211
212    /**
213     * Suspend a workflow job.
214     *
215     * @param jobId job Id.
216     * @throws org.apache.oozie.client.OozieClientException thrown if the job could not be suspended.
217     */
218    @Override
219    public void suspend(String jobId) throws OozieClientException {
220        try {
221            dagEngine.suspend(jobId);
222        }
223        catch (DagEngineException ex) {
224            throw new OozieClientException(ex.getErrorCode().toString(), ex);
225        }
226    }
227
228    /**
229     * Resume a workflow job.
230     *
231     * @param jobId job Id.
232     * @throws org.apache.oozie.client.OozieClientException thrown if the job could not be resume.
233     */
234    @Override
235    public void resume(String jobId) throws OozieClientException {
236        try {
237            dagEngine.resume(jobId);
238        }
239        catch (DagEngineException ex) {
240            throw new OozieClientException(ex.getErrorCode().toString(), ex);
241        }
242    }
243
244    /**
245     * Kill a workflow job.
246     *
247     * @param jobId job Id.
248     * @throws org.apache.oozie.client.OozieClientException thrown if the job could not be killed.
249     */
250    @Override
251    public void kill(String jobId) throws OozieClientException {
252        try {
253            dagEngine.kill(jobId);
254        }
255        catch (DagEngineException ex) {
256            throw new OozieClientException(ex.getErrorCode().toString(), ex);
257        }
258    }
259
260    /**
261     * Get the info of a workflow job.
262     *
263     * @param jobId job Id.
264     * @return the job info.
265     * @throws org.apache.oozie.client.OozieClientException thrown if the job info could not be retrieved.
266     */
267    @Override
268    public WorkflowJob getJobInfo(String jobId) throws OozieClientException {
269        try {
270            return dagEngine.getJob(jobId);
271        }
272        catch (DagEngineException ex) {
273            throw new OozieClientException(ex.getErrorCode().toString(), ex);
274        }
275    }
276
277    /**
278     * Return the info of the workflow jobs that match the filter.
279     *
280     * @param filter job filter. Refer to the {@link LocalOozieClient} for the filter syntax.
281     * @param start jobs offset, base 1.
282     * @param len number of jobs to return.
283     * @return a list with the workflow jobs info, without node details.
284     * @throws org.apache.oozie.client.OozieClientException thrown if the jobs info could not be retrieved.
285     */
286    @Override
287    public List<WorkflowJob> getJobsInfo(String filter, int start, int len) throws OozieClientException {
288        try {
289            return (List) dagEngine.getJobs(filter, start, len).getWorkflows();
290        }
291        catch (DagEngineException ex) {
292            throw new OozieClientException(ex.getErrorCode().toString(), ex);
293        }
294    }
295
296    /**
297     * Return the info of the workflow jobs that match the filter. <p> It returns the first 100 jobs that match the
298     * filter.
299     *
300     * @param filter job filter. Refer to the {@link LocalOozieClient} for the filter syntax.
301     * @return a list with the workflow jobs info, without node details.
302     * @throws org.apache.oozie.client.OozieClientException thrown if the jobs info could not be retrieved.
303     */
304    @Override
305    public List<WorkflowJob> getJobsInfo(String filter) throws OozieClientException {
306        return getJobsInfo(filter, 1, 100);
307    }
308
309    /**
310     * Return the workflow job Id for an external Id. <p> The external Id must have provided at job creation time.
311     *
312     * @param externalId external Id given at job creation time.
313     * @return the workflow job Id for an external Id, <code>null</code> if none.
314     * @throws org.apache.oozie.client.OozieClientException thrown if the operation could not be done.
315     */
316    @Override
317    public String getJobId(String externalId) throws OozieClientException {
318        try {
319            return dagEngine.getJobIdForExternalId(externalId);
320        }
321        catch (DagEngineException ex) {
322            throw new OozieClientException(ex.getErrorCode().toString(), ex);
323        }
324    }
325
326    /**
327     * Returns if Oozie is in safe mode or not.
328     *
329     * @return true if safe mode is ON<br> false if safe mode is OFF
330     * @throws org.apache.oozie.client.OozieClientException throw if it could not obtain the safe mode status.
331     */
332    /*public SYSTEM_MODE isInSafeMode() throws OozieClientException {
333        //return Services.get().isSafeMode();
334        return Services.get().getSystemMode() ;
335    }*/
336
337}