KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > crontab > modules > CrontabModule


1 /*
2  This software is OSI Certified Open Source Software.
3 OSI Certified is a certification mark of the Open Source Initiative.
4
5 The license (Mozilla version 1.0) can be read at the MMBase site.
6 See http://www.MMBase.org/license
7  */

8 package org.mmbase.applications.crontab.modules;
9
10 import java.util.*;
11 import org.mmbase.util.xml.UtilReader;
12 import org.mmbase.util.functions.*;
13 import org.mmbase.applications.crontab.*;
14 import org.mmbase.module.WatchedReloadableModule;
15 import org.mmbase.util.logging.*;
16
17 /**
18  * Starts a crontab for MMBase as a Module.
19  *
20  * @author Michiel Meeuwissen
21  * @version $Id: CrontabModule.java,v 1.6 2006/01/20 08:19:10 michiel Exp $
22  */

23 public class CrontabModule extends WatchedReloadableModule {
24
25     private static final Logger log = Logging.getLoggerInstance(CrontabModule.class);
26     protected CronDaemon cronDaemon = null;
27
28     /**
29      * Need to remember which crontab entries where 'mine', to known which must be removed if
30      * configuration changes.
31      */

32     private Set myEntries = new LinkedHashSet();
33
34     public CrontabModule() {
35         cronDaemon = CronDaemon.getInstance();
36     }
37
38     /**
39      * Interpretates all initParameters as crontab entries. The key is not very important but must
40      * be unique. The value are actually two or three or four values, separated by tabs newlines or '|',
41      * whatever you like most.
42      <pre>
43       &lt;cron time&gt;
44       &lt;class name of a CronJob&gt;
45       [&lt;description&gt;]
46       [&lt;configuration-string&gt;]
47       </pre>
48      */

49     public void init() {
50         Iterator i = getInitParameters().entrySet().iterator();
51         while (i.hasNext()) {
52             Map.Entry entry = (Map.Entry)i.next();
53             addJob(entry);
54         }
55         readMoreJobs();
56     }
57
58     protected void shutdown() {
59         cronDaemon.stop();
60     }
61
62     protected void addJob(Map.Entry entry) {
63         String JavaDoc value = (String JavaDoc)entry.getValue();
64         String JavaDoc[] tokens = value.trim().split("[\n|]");
65         String JavaDoc times;
66         if (tokens.length > 0) {
67             times = tokens[0].trim();
68         } else {
69             log.error("No times in " + value);
70             return;
71         }
72         String JavaDoc className;
73         if (tokens.length > 1) {
74             className = tokens[1].trim();
75         } else {
76             log.error("No className " + value);
77             return;
78         }
79         String JavaDoc description = null;
80         String JavaDoc configString = null;
81         String JavaDoc type = null;
82         if (tokens.length > 2) {
83             description = tokens[2].trim();
84         }
85         if (description == null || description.length() == 0) {
86             description = (String JavaDoc)entry.getKey();
87         }
88
89         if (tokens.length > 3) {
90             configString = tokens[3].trim();
91         }
92         if (tokens.length > 4) {
93             type = tokens[4].trim();
94         }
95
96         try {
97             CronEntry job = new CronEntry((String JavaDoc)entry.getKey(), times, description, className, configString, type);
98             log.debug("Found job: " + job);
99             myEntries.add(job);
100             cronDaemon.add(job);
101         } catch (Exception JavaDoc e) {
102             log.error("Could not add to CronDaemon " + entry.getKey() + "|" + times + "|" + description + "|" + className + " " + e.getClass().getName() + ": " + e.getMessage());
103         }
104     }
105
106
107
108     /**
109      * All previously added entries are removed from the cron-daemon and the currently configured
110      * ones are added (init is called).
111      */

112     public void reload() {
113         log.info("Reloading crontab");
114         Iterator i = myEntries.iterator();
115         while (i.hasNext()) {
116             cronDaemon.remove((CronEntry)i.next());
117         }
118         myEntries.clear();
119         init();
120     }
121
122     /**
123      * @since MMBase-1.8
124      */

125     private Map utilProperties = new UtilReader("crontab.xml", new Runnable JavaDoc() { public void run() { reload();}}).getProperties();
126
127     public void readMoreJobs() {
128         Iterator i = utilProperties.entrySet().iterator();
129         while (i.hasNext()) {
130             Map.Entry entry = (Map.Entry) i.next();
131             addJob(entry);
132         }
133
134     }
135     /**
136      * @since MMBase-1.8
137      */

138     protected Function listFunction = new AbstractFunction("list", Parameter.EMPTY, ReturnType.SET) {
139             public Object JavaDoc getFunctionValue(Parameters arguments) {
140                 return cronDaemon.getEntries();
141             }
142
143         };
144     {
145         addFunction(listFunction);
146     }
147
148     protected final static Parameter ENTRY = new Parameter("entry", String JavaDoc.class, true);
149     protected final static Parameter THREAD = new Parameter("thread", Integer JavaDoc.class, new Integer JavaDoc(0));
150     /**
151      * @since MMBase-1.8
152      */

153     protected Function kickFunction = new AbstractFunction("kick", new Parameter[] {ENTRY}, ReturnType.BOOLEAN) {
154             public Object JavaDoc getFunctionValue(Parameters arguments) {
155                 String JavaDoc id = (String JavaDoc) arguments.get(ENTRY);
156                 return Boolean.valueOf(cronDaemon.getCronEntry(id).kick());
157             }
158
159         };
160     {
161         addFunction(kickFunction);
162     }
163
164     /**
165      * @since MMBase-1.8
166      */

167     protected Function interruptFunction = new AbstractFunction("interrupt", new Parameter[] {ENTRY, THREAD}, ReturnType.BOOLEAN) {
168             public Object JavaDoc getFunctionValue(Parameters arguments) {
169                 String JavaDoc id = (String JavaDoc) arguments.get(ENTRY);
170                 Integer JavaDoc thread = (Integer JavaDoc) arguments.get(THREAD);
171                 Interruptable t = cronDaemon.getCronEntry(id).getThread(thread.intValue());
172                 return Boolean.valueOf(t != null && t.interrupt());
173             }
174
175         };
176     {
177         addFunction(interruptFunction);
178     }
179
180
181     /**
182      * @since MMBase-1.8
183      */

184     protected Function aliveFunction = new AbstractFunction("alive", Parameter.EMPTY, ReturnType.BOOLEAN) {
185             public Object JavaDoc getFunctionValue(Parameters arguments) {
186                 return Boolean.valueOf(cronDaemon.isAlive());
187             }
188
189         };
190     {
191         addFunction(aliveFunction);
192     }
193
194     /**
195      * @since MMBase-1.8
196      */

197     protected Function stopFunction = new AbstractFunction("stop", Parameter.EMPTY, ReturnType.BOOLEAN) {
198             public Object JavaDoc getFunctionValue(Parameters arguments) {
199                 cronDaemon.stop();
200                 return Boolean.valueOf(cronDaemon.isAlive());
201             }
202
203         };
204     {
205         addFunction(stopFunction);
206     }
207
208     /**
209      * @since MMBase-1.8
210      */

211     protected Function startFunction = new AbstractFunction("start", Parameter.EMPTY, ReturnType.BOOLEAN) {
212             public Object JavaDoc getFunctionValue(Parameters arguments) {
213                 cronDaemon.start();
214                 return Boolean.valueOf(cronDaemon.isAlive());
215             }
216
217         };
218     {
219         addFunction(startFunction);
220     }
221
222     /**
223      * @since MMBase-1.8
224      */

225     protected Function reloadFunction = new AbstractFunction("reload", Parameter.EMPTY, ReturnType.BOOLEAN) {
226             public Object JavaDoc getFunctionValue(Parameters arguments) {
227                 reload();
228                 return Boolean.valueOf(cronDaemon.isAlive());
229             }
230
231         };
232     {
233         addFunction(reloadFunction);
234     }
235
236
237 }
238
Popular Tags