KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > quartz > JobSchedulerComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Aug 1, 2005
14  * @author wseyler
15  */

16
17 package org.pentaho.plugin.quartz;
18
19 import java.io.IOException JavaDoc;
20 import java.text.ParseException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.pentaho.core.session.IPentahoSession;
27 import org.pentaho.messages.Messages;
28 import org.pentaho.plugin.ComponentBase;
29 import org.quartz.CronTrigger;
30 import org.quartz.JobDataMap;
31 import org.quartz.JobDetail;
32 import org.quartz.Scheduler;
33 import org.quartz.SchedulerException;
34 import org.quartz.SimpleTrigger;
35 import org.quartz.Trigger;
36
37 /**
38  * @author wseyler
39  *
40  * TODO To change the template for this generated type comment go to Window -
41  * Preferences - Java - Code Style - Code Templates
42  */

43 public class JobSchedulerComponent extends ComponentBase {
44
45     /**
46      *
47      */

48     private static final long serialVersionUID = -1770772140985331431L;
49
50     private static final String JavaDoc START_JOB_ACTION_STR = "startJob"; //$NON-NLS-1$
51

52     private static final String JavaDoc SUSPEND_JOB_ACTION_STR = "suspendJob"; //$NON-NLS-1$
53

54     private static final String JavaDoc DELETE_JOB_ACTION_STR = "deleteJob"; //$NON-NLS-1$
55

56     private static final String JavaDoc RESUME_JOB_ACTION_STR = "resumeJob"; //$NON-NLS-1$
57

58     private static final String JavaDoc JOB_ACTION_STR = "jobAction"; //$NON-NLS-1$
59

60     private static final String JavaDoc SOLUTION_STR = "solution"; //$NON-NLS-1$
61

62     private static final String JavaDoc PATH_STR = "path"; //$NON-NLS-1$
63

64     private static final String JavaDoc ACTION_STR = "action"; //$NON-NLS-1$
65

66     private static final String JavaDoc USER_STR = "username"; //$NON-NLS-1$
67

68     private static final String JavaDoc DEFAULT_STR = "default"; //$NON-NLS-1$
69

70     private static final String JavaDoc TRIGGER_TYPE_STR = "triggerType"; //$NON-NLS-1$
71

72     private static final String JavaDoc CRON_TRIGGER_TYPE_STR = "cron"; //$NON-NLS-1$
73

74     private static final String JavaDoc DEFAULT_CRON_EXP_STR = "0 0 12 * * ?"; // Everyday at Noon //$NON-NLS-1$
75

76     private static final String JavaDoc TRIGGER_CRON_STR = "cronString"; //$NON-NLS-1$
77

78     private static final String JavaDoc SIMPLE_TRIGGER_TYPE_STR = "simple"; //$NON-NLS-1$
79

80     private static final String JavaDoc TRIGGER_NAME_STR = "triggerName"; //$NON-NLS-1$
81

82     private static final String JavaDoc REPEAT_INTERVAL_STR = "repeatInterval"; //$NON-NLS-1$
83

84     private static final String JavaDoc REPEAT_COUNT_STR = "repeatCount"; //$NON-NLS-1$
85

86     private static final String JavaDoc JOB_NAME_STR = "jobName"; //$NON-NLS-1$
87

88     // Trigger misfire instructions
89
private static final String JavaDoc MISFIRE_POLICY = "misfirePolicy"; //$NON-NLS-1$
90

91     private static final String JavaDoc INSTRUCTION_NOOP = "INSTRUCTION_NOOP"; //$NON-NLS-1$
92

93     private static final String JavaDoc INSTRUCTION_RE_EXECUTE_JOB = "INSTRUCTION_RE_EXECUTE_JOB"; //$NON-NLS-1$
94

95     private static final String JavaDoc INSTRUCTION_DELETE_TRIGGER = "INSTRUCTION_DELETE_TRIGGER"; //$NON-NLS-1$
96

97     private static final String JavaDoc INSTRUCTION_SET_TRIGGER_COMPLETE = "INSTRUCTION_SET_TRIGGER_COMPLETE"; //$NON-NLS-1$
98

99     private static final String JavaDoc INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE = "INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE"; //$NON-NLS-1$
100

101     private static final String JavaDoc MISFIRE_INSTRUCTION_SMART_POLICY = "MISFIRE_INSTRUCTION_SMART_POLICY"; //$NON-NLS-1$
102

103     // Simple Trigger misfire instructions
104
private static final String JavaDoc MISFIRE_INSTRUCTION_FIRE_NOW = "MISFIRE_INSTRUCTION_FIRE_NOW"; //$NON-NLS-1$
105

106     private static final String JavaDoc MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT = "MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT"; //$NON-NLS-1$
107

108     private static final String JavaDoc MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT = "MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT"; //$NON-NLS-1$
109

110     private static final String JavaDoc MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT = "MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT"; //$NON-NLS-1$
111

112     private static final String JavaDoc MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT = "MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT"; //$NON-NLS-1$
113

114     // Cron Trigger misfire instructions
115
private static final String JavaDoc MISFIRE_INSTRUCTION_FIRE_ONCE_NOW = "MISFIRE_INSTRUCTION_FIRE_ONCE_NOW"; //$NON-NLS-1$
116

117     private static final String JavaDoc MISFIRE_INSTRUCTION_DO_NOTHING = "MISFIRE_INSTRUCTION_DO_NOTHING"; //$NON-NLS-1$
118

119     private Scheduler sched = null;
120
121     private List JavaDoc localInputNames = new ArrayList JavaDoc();
122
123     /*
124      * (non-Javadoc)
125      *
126      * @see org.pentaho.component.IComponent#init()
127      */

128     public boolean init() {
129         localInputNames.add(JOB_ACTION_STR);
130         localInputNames.add(SOLUTION_STR);
131         localInputNames.add(PATH_STR);
132         localInputNames.add(ACTION_STR);
133         localInputNames.add(TRIGGER_TYPE_STR);
134         localInputNames.add(TRIGGER_NAME_STR);
135         localInputNames.add(REPEAT_INTERVAL_STR);
136         localInputNames.add(REPEAT_COUNT_STR);
137         localInputNames.add(JOB_NAME_STR);
138         localInputNames.add(TRIGGER_CRON_STR);
139         try {
140             sched = QuartzSystemListener.getSchedulerInstance();
141         } catch (Exception JavaDoc e) {
142             error(Messages.getErrorString("JobSchedulerComponent.ERROR_0001_NoScheduler"), e); //$NON-NLS-1$
143
return false;
144         }
145         return true;
146     }
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see org.pentaho.component.ComponentBase#validateAction()
152      */

153     protected boolean validateAction() {
154         if (!isDefinedInput(JOB_ACTION_STR)) {
155             return false;
156         }
157         return true;
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see org.pentaho.component.ComponentBase#validateSystemSettings()
164      */

165     protected boolean validateSystemSettings() {
166         return true;
167     }
168
169     /*
170      * (non-Javadoc)
171      *
172      * @see org.pentaho.component.ComponentBase#done()
173      */

174     public void done() {
175         sched = null;
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.pentaho.component.ComponentBase#executeAction()
182      */

183     protected boolean executeAction() {
184
185         String JavaDoc jobAction = null;
186         jobAction = getInputStringValue(JOB_ACTION_STR);
187
188         if (START_JOB_ACTION_STR.equals(jobAction)) {
189             JobDetail jobDetail = createJobDetail();
190             Trigger trigger = createTrigger();
191             if (trigger == null || jobDetail == null) {
192                 error(Messages.getErrorString("JobSchedulerComponent.ERROR_0002_UnableToCreateTriggerOrJob")); //$NON-NLS-1$
193
return false;
194             }
195             return startJob(jobDetail, trigger);
196         } else if (SUSPEND_JOB_ACTION_STR.equals(jobAction)) {
197             return suspendJob(getJobName(), Scheduler.DEFAULT_GROUP);
198         } else if (DELETE_JOB_ACTION_STR.equals(jobAction)) {
199             return deleteJob(getJobName(), Scheduler.DEFAULT_GROUP);
200         } else if (RESUME_JOB_ACTION_STR.equals(jobAction)) {
201             return resumeJob(getJobName(), Scheduler.DEFAULT_GROUP);
202         } else {
203             return false;
204         }
205     }
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see org.pentaho.core.system.PentahoBase#getLogger()
211      */

212     public Log getLogger() {
213         return LogFactory.getLog(JobSchedulerComponent.class);
214     }
215
216     /**
217      * @return
218      */

219     private JobDetail createJobDetail() {
220         String JavaDoc jobName = getJobName();
221         JobDetail jobDetail = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, QuartzExecute.class);
222         JobDataMap jdm = jobDetail.getJobDataMap();
223         jdm.put(SOLUTION_STR, getInputStringValue(SOLUTION_STR));
224         // Prevents a null path if the action is at the root of the solution.
225
jdm.put(PATH_STR, getInputStringValue(PATH_STR) == null ? "" : getInputStringValue(PATH_STR)); //$NON-NLS-1$
226
jdm.put(ACTION_STR, getInputStringValue(ACTION_STR));
227         // Support scheduled actions in the portal and assigning the credential properly in the execute.
228
IPentahoSession session = this.getSession();
229         if ( (session != null) && (session.isAuthenticated()) ) {
230           jdm.put(USER_STR, session.getName());
231         }
232         Iterator JavaDoc inputNamesIterator = getInputNames().iterator();
233         while (inputNamesIterator.hasNext()) {
234             String JavaDoc inputName = (String JavaDoc) inputNamesIterator.next();
235             if (!localInputNames.contains(inputName)) {
236                 Object JavaDoc inputValue = getInputValue(inputName);
237                 jobDetail.getJobDataMap().put(inputName, inputValue);
238             }
239         }
240
241         return jobDetail;
242     }
243
244     /**
245      * @return
246      */

247     private Trigger createTrigger() {
248         Trigger trigger = null;
249         String JavaDoc triggerType = SIMPLE_TRIGGER_TYPE_STR;
250         if (isDefinedInput(TRIGGER_TYPE_STR)) {
251             triggerType = getInputStringValue(TRIGGER_TYPE_STR);
252         }
253         if (triggerType.equals(SIMPLE_TRIGGER_TYPE_STR)) {
254             trigger = createSimpleTrigger();
255         } else if (triggerType.equals(CRON_TRIGGER_TYPE_STR)) {
256             trigger = createCRONTrigger();
257         }
258         addMisfireInstruction(trigger);
259         return trigger;
260     }
261
262     private Trigger createSimpleTrigger() {
263         String JavaDoc triggerName = DEFAULT_STR;
264         if (isDefinedInput(TRIGGER_NAME_STR)) {
265             triggerName = getInputStringValue(TRIGGER_NAME_STR);
266         }
267         long repeatInterval = 0;
268         int repeatCount = SimpleTrigger.REPEAT_INDEFINITELY;
269         if (isDefinedInput(REPEAT_INTERVAL_STR)) {
270             repeatInterval = Long.valueOf(getInputStringValue(REPEAT_INTERVAL_STR)).longValue() * 1000; // convert
271
// it
272
// to
273
// seconds
274
}
275         if (isDefinedInput(REPEAT_COUNT_STR)) {
276             repeatCount = Integer.valueOf(getInputStringValue(REPEAT_COUNT_STR)).intValue();
277         }
278         Trigger trigger = new SimpleTrigger(triggerName, Scheduler.DEFAULT_GROUP, repeatCount, repeatInterval);
279         return trigger;
280     }
281
282     private Trigger createCRONTrigger() {
283
284         String JavaDoc triggerName = DEFAULT_STR;
285         if (isDefinedInput(TRIGGER_NAME_STR)) {
286             triggerName = getInputStringValue(TRIGGER_NAME_STR);
287         }
288         String JavaDoc cronExpression = DEFAULT_CRON_EXP_STR;
289         if (isDefinedInput(TRIGGER_CRON_STR)) {
290             cronExpression = getInputStringValue(TRIGGER_CRON_STR);
291         }
292         try {
293             Trigger trigger = new CronTrigger(triggerName, Scheduler.DEFAULT_GROUP, cronExpression);
294             return trigger;
295         } catch (ParseException JavaDoc e) {
296             error(Messages.getErrorString("JobSchedulerComponent.ERROR_0003_UnableToParse", cronExpression), e); //$NON-NLS-1$
297
return null;
298         }
299     }
300
301     /**
302      * @param trigger
303      */

304     private void addMisfireInstruction(Trigger trigger) {
305
306         String JavaDoc misfirePolicy = MISFIRE_INSTRUCTION_SMART_POLICY;
307         if (isDefinedInput(MISFIRE_POLICY)) {
308             misfirePolicy = getInputStringValue(MISFIRE_POLICY);
309         }
310
311         trigger.setMisfireInstruction(Trigger.MISFIRE_INSTRUCTION_SMART_POLICY); // Default
312
if (misfirePolicy.equalsIgnoreCase(INSTRUCTION_RE_EXECUTE_JOB)) {
313             trigger.setMisfireInstruction(Trigger.INSTRUCTION_RE_EXECUTE_JOB);
314         } else if (misfirePolicy.equals(INSTRUCTION_DELETE_TRIGGER)) {
315             trigger.setMisfireInstruction(Trigger.INSTRUCTION_DELETE_TRIGGER);
316         } else if (misfirePolicy.equals(INSTRUCTION_SET_TRIGGER_COMPLETE)) {
317             trigger.setMisfireInstruction(Trigger.INSTRUCTION_SET_TRIGGER_COMPLETE);
318         } else if (misfirePolicy.equals(INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE)) {
319             trigger.setMisfireInstruction(Trigger.INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE);
320         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_SMART_POLICY)) {
321             trigger.setMisfireInstruction(Trigger.MISFIRE_INSTRUCTION_SMART_POLICY);
322         } else if (misfirePolicy.equals(INSTRUCTION_NOOP)) {
323             trigger.setMisfireInstruction(Trigger.INSTRUCTION_NOOP);
324         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_FIRE_NOW) && (trigger instanceof SimpleTrigger)) {
325             trigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
326         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT) && (trigger instanceof SimpleTrigger)) {
327             trigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT);
328         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT) && (trigger instanceof SimpleTrigger)) {
329             trigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
330         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT) && (trigger instanceof SimpleTrigger)) {
331             trigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT);
332         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT) && (trigger instanceof SimpleTrigger)) {
333             trigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT);
334         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_FIRE_ONCE_NOW) && (trigger instanceof CronTrigger)) {
335             trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW);
336         } else if (misfirePolicy.equals(MISFIRE_INSTRUCTION_DO_NOTHING) && (trigger instanceof CronTrigger)) {
337             trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
338         }
339     }
340
341     /**
342      * @return
343      */

344     private String JavaDoc getJobName() {
345         String JavaDoc jobName = DEFAULT_STR;
346         if (isDefinedInput(JOB_NAME_STR)) {
347             jobName = getInputStringValue(JOB_NAME_STR);
348         }
349         return jobName;
350     }
351
352     public boolean startJob(JobDetail jobDetail, Trigger trigger) {
353         try { // If we got one already with the same name... overwrite it.
354
if (sched.getJobDetail(jobDetail.getName(), Scheduler.DEFAULT_GROUP) != null) {
355                 deleteJob(jobDetail.getName(), Scheduler.DEFAULT_GROUP);
356             }
357             sched.scheduleJob(jobDetail, trigger);
358             getFeedbackOutputStream().write(Messages.getString("JobSchedulerComponent.INFO_0001").getBytes()); //$NON-NLS-1$
359
} catch (SchedulerException e) {
360             error(e.getLocalizedMessage());
361             return false;
362         } catch (IOException JavaDoc e) {
363             error(e.getLocalizedMessage());
364             return false;
365         }
366         return true;
367     }
368
369     public boolean suspendJob(String JavaDoc jobName, String JavaDoc groupName) {
370         try {
371             sched.pauseJob(jobName, groupName);
372         } catch (SchedulerException e) {
373             error(e.getLocalizedMessage());
374             return false;
375         }
376         return true;
377     }
378
379     public boolean deleteJob(String JavaDoc jobName, String JavaDoc groupName) {
380         try {
381             sched.deleteJob(jobName, groupName);
382         } catch (SchedulerException e) {
383             error(e.getLocalizedMessage());
384             return false;
385         }
386         return true;
387     }
388
389     public boolean resumeJob(String JavaDoc jobName, String JavaDoc groupName) {
390         try {
391             sched.resumeJob(jobName, groupName);
392         } catch (SchedulerException e) {
393             error(e.getLocalizedMessage());
394             return false;
395         }
396         return true;
397     }
398 }
399
Popular Tags