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.ArrayList;
021    import java.util.Collections;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Properties;
025    
026    import org.apache.oozie.client.CoordinatorAction;
027    import org.apache.oozie.client.CoordinatorJob;
028    import org.apache.oozie.client.OozieClient;
029    import org.apache.oozie.client.OozieClientException;
030    import org.apache.oozie.client.WorkflowJob;
031    import org.apache.oozie.client.rest.JsonCoordinatorAction;
032    import org.apache.oozie.client.rest.JsonCoordinatorJob;
033    import org.apache.oozie.util.XConfiguration;
034    
035    /**
036     * Client API to submit and manage Oozie coordinator jobs against an Oozie
037     * intance.
038     * <p/>
039     * This class is thread safe.
040     * <p/>
041     * Syntax for filter for the {@link #getJobsInfo(String)}
042     * {@link #getJobsInfo(String, int, int)} methods:
043     * <code>[NAME=VALUE][;NAME=VALUE]*</code>.
044     * <p/>
045     * Valid filter names are:
046     * <p/>
047     * <ul/>
048     * <li>name: the coordinator application name from the coordinator definition.</li>
049     * <li>user: the user that submitted the job.</li>
050     * <li>group: the group for the job.</li>
051     * <li>status: the status of the job.</li>
052     * </ul>
053     * <p/>
054     * The query will do an AND among all the filter names. The query will do an OR
055     * among all the filter values for the same name. Multiple values must be
056     * specified as different name value pairs.
057     */
058    public class LocalOozieClientCoord extends OozieClient {
059    
060        private final CoordinatorEngine coordEngine;
061    
062        /**
063         * Create a coordinator client for Oozie local use.
064         * <p/>
065         *
066         * @param coordEngine the engine instance to use.
067         */
068        public LocalOozieClientCoord(CoordinatorEngine coordEngine) {
069            this.coordEngine = coordEngine;
070        }
071    
072        /**
073         * Return the Oozie URL of the coordinator client instance.
074         * <p/>
075         * This URL is the base URL fo the Oozie system, with not protocol
076         * versioning.
077         *
078         * @return the Oozie URL of the coordinator client instance.
079         */
080        @Override
081        public String getOozieUrl() {
082            return "localoozie";
083        }
084    
085        /**
086         * Return the Oozie URL used by the client and server for WS communications.
087         * <p/>
088         * This URL is the original URL plus the versioning element path.
089         *
090         * @return the Oozie URL used by the client and server for communication.
091         * @throws org.apache.oozie.client.OozieClientException thrown in the client
092         *         and the server are not protocol compatible.
093         */
094        @Override
095        public String getProtocolUrl() throws OozieClientException {
096            return "localoozie";
097        }
098    
099        /**
100         * Validate that the Oozie client and server instances are protocol
101         * compatible.
102         *
103         * @throws org.apache.oozie.client.OozieClientException thrown in the client
104         *         and the server are not protocol compatible.
105         */
106        @Override
107        public synchronized void validateWSVersion() throws OozieClientException {
108        }
109    
110        /**
111         * Create an empty configuration with just the {@link #USER_NAME} set to the
112         * JVM user name and the {@link #GROUP_NAME} set to 'other'.
113         *
114         * @return an empty configuration.
115         */
116        @Override
117        public Properties createConfiguration() {
118            Properties conf = new Properties();
119            if (coordEngine != null) {
120                conf.setProperty(USER_NAME, coordEngine.getUser());
121            }
122            conf.setProperty(GROUP_NAME, "users");
123            return conf;
124        }
125    
126        /**
127         * Set a HTTP header to be used in the WS requests by the coordinator
128         * instance.
129         *
130         * @param name header name.
131         * @param value header value.
132         */
133        @Override
134        public void setHeader(String name, String value) {
135        }
136    
137        /**
138         * Get the value of a set HTTP header from the coordinator instance.
139         *
140         * @param name header name.
141         * @return header value, <code>null</code> if not set.
142         */
143        @Override
144        public String getHeader(String name) {
145            return null;
146        }
147    
148        /**
149         * Remove a HTTP header from the coordinator client instance.
150         *
151         * @param name header name.
152         */
153        @Override
154        public void removeHeader(String name) {
155        }
156    
157        /**
158         * Return an iterator with all the header names set in the coordinator
159         * instance.
160         *
161         * @return header names.
162         */
163        @Override
164        @SuppressWarnings("unchecked")
165        public Iterator<String> getHeaderNames() {
166            return Collections.EMPTY_SET.iterator();
167        }
168    
169        /**
170         * Submit a coordinator job.
171         *
172         * @param conf job configuration.
173         * @return the job Id.
174         * @throws org.apache.oozie.client.OozieClientException thrown if the job
175         *         could not be submitted.
176         */
177        @Override
178        public String submit(Properties conf) throws OozieClientException {
179            try {
180                return coordEngine.submitJob(new XConfiguration(conf), false);
181            }
182            catch (CoordinatorEngineException ex) {
183                throw new OozieClientException(ex.getErrorCode().toString(), ex);
184            }
185        }
186    
187        /**
188         * Start a coordinator job.
189         *
190         * @param jobId job Id.
191         * @throws org.apache.oozie.client.OozieClientException thrown if the job
192         *         could not be started.
193         */
194        @Override
195        @Deprecated
196        public void start(String jobId) throws OozieClientException {
197            try {
198                coordEngine.start(jobId);
199            }
200            catch (CoordinatorEngineException ex) {
201                throw new OozieClientException(ex.getErrorCode().toString(), ex);
202            }
203            catch (BaseEngineException bex) {
204                throw new OozieClientException(bex.getErrorCode().toString(), bex);
205            }
206        }
207    
208        /**
209         * Submit and start a coordinator job.
210         *
211         * @param conf job configuration.
212         * @return the job Id.
213         * @throws org.apache.oozie.client.OozieClientException thrown if the job
214         *         could not be submitted.
215         */
216        @Override
217        public String run(Properties conf) throws OozieClientException {
218            try {
219                return coordEngine.submitJob(new XConfiguration(conf), true);
220            }
221            catch (CoordinatorEngineException ex) {
222                throw new OozieClientException(ex.getErrorCode().toString(), ex);
223            }
224        }
225    
226        /**
227         * Rerun a workflow job.
228         *
229         * @param jobId job Id to rerun.
230         * @param conf configuration information for the rerun.
231         * @throws org.apache.oozie.client.OozieClientException thrown if the job
232         *         could not be started.
233         */
234        @Override
235        @Deprecated
236        public void reRun(String jobId, Properties conf) throws OozieClientException {
237            throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
238        }
239    
240        /**
241         * Rerun coordinator actions.
242         *
243         * @param jobId coordinator jobId
244         * @param rerunType rerun type 'date' if -date is used, 'action-id' if
245         *        -action is used
246         * @param scope rerun scope for date or actionIds
247         * @param refresh true if -refresh is given in command option
248         * @param noCleanup true if -nocleanup is given in command option
249         * @throws OozieClientException
250         */
251        @Override
252        public List<CoordinatorAction> reRunCoord(String jobId, String rerunType, String scope, boolean refresh,
253                boolean noCleanup) throws OozieClientException {
254            try {
255                CoordinatorActionInfo coordInfo = coordEngine.reRun(jobId, rerunType, scope, Boolean.valueOf(refresh),
256                        Boolean.valueOf(noCleanup));
257                List<CoordinatorActionBean> actionBeans = coordInfo.getCoordActions();
258                List<CoordinatorAction> actions = new ArrayList<CoordinatorAction>();
259                for (CoordinatorActionBean actionBean : actionBeans) {
260                    actions.add(actionBean);
261                }
262                return actions;
263            }
264            catch (BaseEngineException ex) {
265                throw new OozieClientException(ex.getErrorCode().toString(), ex);
266            }
267        }
268    
269        /**
270         * Suspend a coordinator job.
271         *
272         * @param jobId job Id.
273         * @throws org.apache.oozie.client.OozieClientException thrown if the job
274         *         could not be suspended.
275         */
276        @Override
277        public void suspend(String jobId) throws OozieClientException {
278            try {
279                coordEngine.suspend(jobId);
280            }
281            catch (CoordinatorEngineException ex) {
282                throw new OozieClientException(ex.getErrorCode().toString(), ex);
283            }
284        }
285    
286        /**
287         * Resume a coordinator job.
288         *
289         * @param jobId job Id.
290         * @throws org.apache.oozie.client.OozieClientException thrown if the job
291         *         could not be resume.
292         */
293        @Override
294        public void resume(String jobId) throws OozieClientException {
295            try {
296                coordEngine.resume(jobId);
297            }
298            catch (CoordinatorEngineException ex) {
299                throw new OozieClientException(ex.getErrorCode().toString(), ex);
300            }
301        }
302    
303        /**
304         * Kill a coordinator job.
305         *
306         * @param jobId job Id.
307         * @throws org.apache.oozie.client.OozieClientException thrown if the job
308         *         could not be killed.
309         */
310        @Override
311        public void kill(String jobId) throws OozieClientException {
312            try {
313                coordEngine.kill(jobId);
314            }
315            catch (CoordinatorEngineException ex) {
316                throw new OozieClientException(ex.getErrorCode().toString(), ex);
317            }
318        }
319    
320        /**
321         * Get the info of a workflow job.
322         *
323         * @param jobId job Id.
324         * @return the job info.
325         * @throws org.apache.oozie.client.OozieClientException thrown if the job
326         *         info could not be retrieved.
327         */
328        @Override
329        @Deprecated
330        public WorkflowJob getJobInfo(String jobId) throws OozieClientException {
331            throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
332        }
333    
334        /**
335         * Get the info of a coordinator job.
336         *
337         * @param jobId job Id.
338         * @return the job info.
339         * @throws org.apache.oozie.client.OozieClientException thrown if the job
340         *         info could not be retrieved.
341         */
342        @Override
343        public CoordinatorJob getCoordJobInfo(String jobId) throws OozieClientException {
344            try {
345                return coordEngine.getCoordJob(jobId);
346            }
347            catch (CoordinatorEngineException ex) {
348                throw new OozieClientException(ex.getErrorCode().toString(), ex);
349            }
350            catch (BaseEngineException bex) {
351                throw new OozieClientException(bex.getErrorCode().toString(), bex);
352            }
353        }
354    
355        /**
356         * Get the info of a coordinator action.
357         * 
358         * @param actionId Id.
359         * @return the coordinator action info.
360         * @throws OozieClientException thrown if the job info could not be
361         *         retrieved.
362         */
363        @Override
364        public CoordinatorAction getCoordActionInfo(String actionId) throws OozieClientException {
365            try {
366                return coordEngine.getCoordAction(actionId);
367            }
368            catch (CoordinatorEngineException ex) {
369                throw new OozieClientException(ex.getErrorCode().toString(), ex);
370            }
371            catch (BaseEngineException bex) {
372                throw new OozieClientException(bex.getErrorCode().toString(), bex);
373            }
374        }
375    
376        /**
377         * Return the info of the workflow jobs that match the filter.
378         *
379         * @param filter job filter. Refer to the {@link OozieClient} for the filter
380         *        syntax.
381         * @param start jobs offset, base 1.
382         * @param len number of jobs to return.
383         * @return a list with the workflow jobs info, without node details.
384         * @throws OozieClientException thrown if the jobs info could not be
385         *         retrieved.
386         */
387        @Override
388        @Deprecated
389        public List<WorkflowJob> getJobsInfo(String filter, int start, int len) throws OozieClientException {
390            throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
391        }
392    
393        /**
394         * Return the info of the coordinator jobs that match the filter.
395         *
396         * @param filter job filter. Refer to the {@link OozieClient} for the filter
397         *        syntax.
398         * @param start jobs offset, base 1.
399         * @param len number of jobs to return.
400         * @return a list with the coordinator jobs info
401         * @throws OozieClientException thrown if the jobs info could not be
402         *         retrieved.
403         */
404        @Override
405        public List<CoordinatorJob> getCoordJobsInfo(String filter, int start, int len) throws OozieClientException {
406            try {
407                CoordinatorJobInfo info = coordEngine.getCoordJobs(filter, start, len);
408                List<CoordinatorJob> jobs = new ArrayList<CoordinatorJob>();
409                List<CoordinatorJobBean> jobBeans = info.getCoordJobs();
410                for (CoordinatorJobBean jobBean : jobBeans) {
411                    jobs.add(jobBean);
412                }
413                return jobs;
414    
415            }
416            catch (CoordinatorEngineException ex) {
417                throw new OozieClientException(ex.getErrorCode().toString(), ex);
418            }
419        }
420    
421        /**
422         * Return the info of the workflow jobs that match the filter.
423         * <p/>
424         * It returns the first 100 jobs that match the filter.
425         *
426         * @param filter job filter. Refer to the {@link LocalOozieClient} for the
427         *        filter syntax.
428         * @return a list with the workflow jobs info, without node details.
429         * @throws org.apache.oozie.client.OozieClientException thrown if the jobs
430         *         info could not be retrieved.
431         */
432        @Override
433        @Deprecated
434        public List<WorkflowJob> getJobsInfo(String filter) throws OozieClientException {
435            throw new OozieClientException(ErrorCode.E0301.toString(), "no-op");
436        }
437    
438    }