View Javadoc

1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.scheduler;
30  
31  import java.util.Date;
32  
33  /**
34   * Describes a <code>Job</code> that should be run periodicaly by the system.
35   *
36   * <p>Properties of a shcheduled job:</p>
37   * <ul>
38   *   <li>name: string 1) 2)</li>
39   *   <li>scheduleType: string 1) 2)</li>
40   *   <li>scheduleConfig: string 1) 2)</li>
41   *   <li>jobClass: string 1) 2)</li>
42   *   <li>argument: string 1)</li>
43   *   <li>runCount: int</li>
44   *   <li>runCountLimit: int 1)</li>
45   *   <li>lastRunTime:d ate</li>
46   *   <li>runTimeLimitStart: date 1)</li>
47   *   <li>runTimeLimitEnd: date 1)</li>
48   *   <li>reentrant: boolean 1)</li>
49   *   <li>enabled: boolean</li>
50   * </ul>
51   * <p> 1) can be specified in the config file driven implementation </p>
52   * <p> 2) required property </p>
53   */
54  public abstract class AbstractJobDescriptor
55  {
56      // instance variables ////////////////////////////////////////////////////
57  
58      /*** The scheduled job's name. */
59      protected String name;
60  
61      /*** The job's associated Schedule. */
62      protected Schedule schedule;
63  
64      /*** The execution argument. */
65      protected String argument;
66      
67      /*** The scheduled job's Job class name. */
68      protected String jobClassName;
69      
70      /*** The job's execution count to date. */
71      protected int runCount;
72  
73      /*** The job's execution count limit. */
74      protected int runCountLimit;
75      
76      /*** The job's last run time. */
77      protected Date lastRunTime;
78      
79      /*** The lower limit of job's execution period. */
80      protected Date runTimeLimitStart;
81      
82      /*** The upper limit of job's execution period. */
83      protected Date runTimeLimitEnd;
84  
85      /*** The job's reentrantness flag. */
86      protected boolean reentrant;
87      
88      /*** The job's currently running flag. */
89      protected boolean running;
90  
91      /*** The job's auto-clean flag. */
92      protected boolean autoClean;
93      
94      /*** The job's enabled flag. */
95      protected boolean enabled;
96  
97      /***
98       * Returns the name of the job.
99       *
100      * @return the name of the job.
101      */
102     public String getName()
103     {
104         return name;
105     }
106     
107     /***
108      * Returns the associated schedule.
109      *
110      * @return the associated schedule.
111      */    
112     public Schedule getSchedule()
113     {
114         return schedule;    
115     }    
116 
117     /***
118      * Changes the shedule schedule.
119      *
120      * @param schedule the new schedule.
121      * @throws JobModificationException if the job state could not be saved.
122      */
123     public synchronized void setSchedule(Schedule schedule)
124         throws JobModificationException
125     {
126         this.schedule = schedule;
127         saveChanges();
128     }
129 
130     /***
131      * Returns the job class name.
132      *
133      * 
134      * @return the job class name.
135      */
136     public String getJobClassName()
137     {
138         return jobClassName;
139     }
140 
141     /***
142      * Changes the job specification.
143      *
144      * @param jobClassName the job class name.
145      * @throws JobModificationException if the job state could not be saved. 
146      */
147     public synchronized void setJobClassName(String jobClassName)
148         throws JobModificationException
149     {
150         this.jobClassName = jobClassName;
151         saveChanges();
152     }
153     
154     /***
155      * Returns the job execution argument.
156      *
157      * <p>You can use the execution argument to specify the unit of work that
158      * the scheduled job should perform. You can have multiple parallel
159      * instanes of a Job class perfroming different units of work.</p>
160      *
161      * @return the execution argument, or <code>null</code> if undefined.
162      */
163     public String getArgument()
164     {
165         return argument;
166     }
167 
168     /***
169      * Sets the job execution argument.
170      *
171      * <p>You can use the execution argument to specify the unit of work that
172      * the scheduled job should perform. You can have multiple parallel
173      * instanes of a Job class perfroming different units of work.</p>
174      *
175      * @param argument the execution argument, or <code>null</code> if undefined.
176      * @throws JobModificationException if the job state could not be saved. 
177      */
178     public void setArgument(String argument)
179         throws JobModificationException
180     {
181         this.argument = argument;
182         saveChanges();
183     }
184 
185     /***
186      * Returns the count of job executions to date.
187      *
188      * @return the coutn of job executions to date.
189      */
190     public int getRunCount()
191     {
192         return runCount;
193     }
194 
195     /***
196      * Returns the maximum number of job executions.
197      *
198      * <p>When no limit is set, -1 will be returned. Newly created <code>
199      * ScheduledJobs</code> have no limit set.
200      *
201      * @return the maximum number of job executions, or -1 if disabled.
202      */
203     public int getRunCountLimit()
204     {
205         return runCountLimit;
206     }
207     
208     /***
209      * Sets the maximum number of job executions.
210      *
211      * <p>Use value of -1 to disable the limit.
212      *
213      * @param limit the maximum number of job executions, or -1 if disabled.
214      * @throws JobModificationException if the job state could not be saved. 
215      */
216     public synchronized void setRunCountLimit(int limit)
217         throws JobModificationException
218     {
219         this.runCountLimit = limit;
220         saveChanges();
221     }
222 
223     /***
224      * Return the job's last execution time.
225      *
226      * @return the job's last execution time, or <code>null</code> if unknown.
227      */
228     public Date getLastRunTime()
229     {
230         return lastRunTime;
231     }
232 
233     /***
234      * Returns lower limit of job execution period.
235      *
236      * <p>The job will not be run before the time returned. Newly created jobs
237      * have no limit set.</p>
238      *
239      * @return lower limit of job execution period, or <code>null</code> if 
240      *         disabled.
241      */  
242     public Date getTimeLimitStart()
243     {
244         return runTimeLimitStart;
245     }
246         
247     /***
248      * Returns upper limit of job execution period.
249      *
250      * <p>The job will not be run after the time returned. Newly created jobs
251      * have no limit set.</p>
252      *
253      * @return upper limit of job execution period, or <code>null</code> if 
254      *         disabled.
255      */  
256     public Date getTimeLimitEnd()
257     {
258         return runTimeLimitEnd;
259     }
260     
261     /***
262      * Sets the job execution period.
263      * 
264      * <p>The job will only be run in the time period specified.</p>
265      *
266      * @param start lower limit of job execution period, or <code>null</code> 
267      *        if disabled.
268      * @param end upper limit of job execution period, or <code>null</code> 
269      *        if disabled.
270      * @throws JobModificationException if the job state could not be saved.
271      */     
272     public void setTimeLimit(Date start, Date end)
273         throws JobModificationException
274     {
275         this.runTimeLimitStart = start;
276         this.runTimeLimitEnd = end;
277         saveChanges();
278     }
279 
280     /***
281      * Checks if the job is being executed at the moment.
282      *
283      * @return <code>true</code> if the job is being executed at the moment.
284      */
285     public boolean isRunning()
286     {
287         return running;
288     }
289 
290     /***
291      * Returns the job's auto-clean flag.
292      *
293      * <p>Jobs that have the auto-clean flag enabled are deleted once they
294      * exceed their run count limit, run time period, or their schedule's
295      * <code>getNextRunTime</code> method returns <code>null</code>.
296      *
297      * @return the job's auto-clean flag.
298      */
299     public boolean getAutoClean()
300     {
301         return autoClean;
302     }
303 
304     /***
305      * Returns the job's auto-clean flag.
306      *
307      * <p>Jobs that have the auto-clean flag enabled are deleted once they
308      * exceed their run count limit, run time period, or their schedule's
309      * <code>getNextRunTime</code> method returns <code>null</code>.
310      *
311      * @param autoClean the job's auto-clean flag.
312      * @throws JobModificationException if the job state could not be saved. 
313      */
314     public void setAutoClean(boolean autoClean)
315         throws JobModificationException
316     {
317         this.autoClean = autoClean;
318         saveChanges();
319     }
320 
321     /***
322      * Returns the job's reentrantness flag.
323      *
324      * <p>If the flag is set, execution of the job may be started even if the 
325      * pervious execution has not terminated yet. Newly created jobs are not
326      * reentrant by default.</p>
327      *
328      * @return the job's reentrantess flag state.
329      */
330     public boolean isReentrant()
331     {
332         return reentrant;
333     }
334 
335     /***
336      * Sets the job's reentrantness flag.
337      *
338      * @param reentrant the job's reentrantess flag state.
339      * @throws JobModificationException if the job state could not be saved.
340      */
341     public synchronized void setReentrant(boolean reentrant)
342         throws JobModificationException
343     {
344         this.reentrant = reentrant;
345         saveChanges();
346     }
347 
348     /***
349      * Checks if the job is enabled.
350      *
351      * <p>Newly created jobs are disabled by default. Only enabled jobs will
352      * be executed by the scheduler</p>
353      *
354      * @return <code>true</code> if the job is enabled.
355      */
356     public boolean isEnabled()
357     {
358         return enabled;
359     }
360 
361     // package private methods ///////////////////////////////////////////////
362 
363     /***
364      * Initializes the job descriptor.
365      *
366      * @param name the scheduled job name.
367      * @param schedule the job's schedule.
368      * @param jobClassName the job class name. 
369      */
370     public void init(String name, Schedule schedule, String jobClassName)
371     {
372         this.name = name;
373         this.schedule = schedule;
374         this.jobClassName = jobClassName;
375         this.runCount = 0;
376         this.runCountLimit = -1;
377         this.lastRunTime = null;
378         this.runTimeLimitStart = null;
379         this.runTimeLimitEnd = null;
380         this.running = false;
381         this.reentrant = false;
382         this.enabled = false;
383     }
384 
385     /***
386      * Sets the job's last execution time.
387      *
388      * @param lastRunTime the job's last execution time.
389      */
390     synchronized void setLastRunTime(Date lastRunTime)
391         throws JobModificationException
392     {
393         this.lastRunTime = lastRunTime;
394         saveChanges();
395     }
396     
397     /***
398      * Sets the count of job executions to date.
399      *
400      * @param runCount the count of job executions to date.
401      */
402     synchronized void setRunCount(int runCount)
403         throws JobModificationException
404     {
405         this.runCount = runCount;
406         saveChanges();
407     }
408 
409     /***
410      * Sets the 'enabled' flag for the job.
411      *
412      * @param enabled <code>true</code> if the job is to be enabled.
413      */
414     synchronized void setEnabled(boolean enabled)
415         throws JobModificationException
416     {
417         this.enabled = enabled;
418         saveChanges();
419     }
420 
421     /***
422      * Sets the 'currently running' flag for the job.
423      *
424      * @param running <code>true</code> if the job is being executed at the
425      *        moment. 
426      */
427     void setRunning(boolean running)
428     {
429         this.running = running;
430     }
431 
432     // defined by implementation /////////////////////////////////////////////    
433     
434     /***
435      * Updates the persistent state.
436      * 
437      * @throws JobModificationException if the job state could not be saved.
438      */
439     protected abstract void saveChanges()
440         throws JobModificationException;
441 }