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