View Javadoc

1   /*
2    * Copyright (c) 2004 Peter Antman, Mogul  <peter.antman@mogul.com>
3    *
4    * $Id: CronJob.java,v 1.1.1.1 2004/05/19 12:22:46 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.amsterdam.cron;
21  
22  import org.backsource.amsterdam.metadata.ContentMapEntry;
23  /***
24  <p>
25  Represents a Job/Task to be run by a CronManager.
26  </p>
27  
28  <p>A CronJob takes a string describing when it is to be run. The string
29  hase the same format as the date and time fields in a  Unix/Vixie Crontab 
30  entry. It also takes a ContentMapEntry. This is an opaque object that may be
31  accessed in the listener that is called when the CronManager "executes" the 
32  job.
33  </p>
34  
35  <p>The <strong>sceduleAt</strong> string follows the syntax of a cronlike Date/Timestring
36  <p>
37  
38  <pre>
39       String: minute hour day-of-month month day-of-week
40  
41              field          allowed values
42                -----          --------------
43                minute         0-59
44                hour           0-23
45                day of month   1-31
46                month          1-12 (no names)
47                day of week    1-7 (1 is Sun)
48    </pre>
49  <p>
50  Currently ranges are not alowed, only skips, ie 0-12 is not allowed, 
51  but *\/5 wich for minutes means every fifth minute.
52  <p>
53  
54  <p>
55  Some examples:
56  </p>
57  
58  <p>Run every minute: <strong>* * * * *</strong>.
59  <p>
60  
61  <p>Run every fith minute: <strong>*\/5 * * * *</strong>.
62  <p>
63  
64  <p>Run at three a clock every day: <strong>0 3 * * *</strong>.
65  <p>
66  
67  <p>Run at three a clock every monday: <strong>0 3 * * 2</strong>.
68  <p>
69  
70   *
71   * @author <a href="mailto:pra@mogul.com">Peter Antman</a>
72   * @version $Revision: 1.1.1.1 $
73   */
74  
75  public class CronJob {
76   
77     ContentMapEntry contentEntry;
78     String scheduledAt;
79     
80     public CronJob(String scheduleAt, ContentMapEntry contentEntry) {
81        this.scheduledAt = scheduleAt;
82        this.contentEntry = contentEntry;
83      }
84  
85      public String getCronDateTime() {
86  	return scheduledAt;
87      }
88  
89      public ContentMapEntry getContentEntry() {
90  	return contentEntry;
91      }
92      
93      public void setContentEntry(ContentMapEntry contentEntry) {
94  	this.contentEntry = contentEntry;
95      }
96  
97     public String toString() {
98        return contentEntry.getName()+":"+scheduledAt;
99     }
100 
101     public String getContentAsXML(boolean prettyPrint){
102 	StringBuffer sBuffer=new StringBuffer();
103 	String newLine=prettyPrint?"\n":"";
104 	sBuffer.append("<CronJob>").append(newLine);
105 	sBuffer.append("<ScheduledAt>").append(scheduledAt).append("</ScheduledAt>" + newLine);
106 	String indent = prettyPrint?"\t":"";
107 	sBuffer.append(contentEntry.getContentAsXML(prettyPrint));
108 	sBuffer.append("</CronJob>").append(newLine);
109 	return sBuffer.toString();
110     }
111 }// CronJob