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