1 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 23 public class CrontabModule extends WatchedReloadableModule { 24 25 private static final Logger log = Logging.getLoggerInstance(CrontabModule.class); 26 protected CronDaemon cronDaemon = null; 27 28 32 private Set myEntries = new LinkedHashSet(); 33 34 public CrontabModule() { 35 cronDaemon = CronDaemon.getInstance(); 36 } 37 38 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 value = (String )entry.getValue(); 64 String [] tokens = value.trim().split("[\n|]"); 65 String 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 className; 73 if (tokens.length > 1) { 74 className = tokens[1].trim(); 75 } else { 76 log.error("No className " + value); 77 return; 78 } 79 String description = null; 80 String configString = null; 81 String type = null; 82 if (tokens.length > 2) { 83 description = tokens[2].trim(); 84 } 85 if (description == null || description.length() == 0) { 86 description = (String )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 )entry.getKey(), times, description, className, configString, type); 98 log.debug("Found job: " + job); 99 myEntries.add(job); 100 cronDaemon.add(job); 101 } catch (Exception 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 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 125 private Map utilProperties = new UtilReader("crontab.xml", new Runnable () { 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 138 protected Function listFunction = new AbstractFunction("list", Parameter.EMPTY, ReturnType.SET) { 139 public Object 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 .class, true); 149 protected final static Parameter THREAD = new Parameter("thread", Integer .class, new Integer (0)); 150 153 protected Function kickFunction = new AbstractFunction("kick", new Parameter[] {ENTRY}, ReturnType.BOOLEAN) { 154 public Object getFunctionValue(Parameters arguments) { 155 String id = (String ) arguments.get(ENTRY); 156 return Boolean.valueOf(cronDaemon.getCronEntry(id).kick()); 157 } 158 159 }; 160 { 161 addFunction(kickFunction); 162 } 163 164 167 protected Function interruptFunction = new AbstractFunction("interrupt", new Parameter[] {ENTRY, THREAD}, ReturnType.BOOLEAN) { 168 public Object getFunctionValue(Parameters arguments) { 169 String id = (String ) arguments.get(ENTRY); 170 Integer thread = (Integer ) 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 184 protected Function aliveFunction = new AbstractFunction("alive", Parameter.EMPTY, ReturnType.BOOLEAN) { 185 public Object getFunctionValue(Parameters arguments) { 186 return Boolean.valueOf(cronDaemon.isAlive()); 187 } 188 189 }; 190 { 191 addFunction(aliveFunction); 192 } 193 194 197 protected Function stopFunction = new AbstractFunction("stop", Parameter.EMPTY, ReturnType.BOOLEAN) { 198 public Object getFunctionValue(Parameters arguments) { 199 cronDaemon.stop(); 200 return Boolean.valueOf(cronDaemon.isAlive()); 201 } 202 203 }; 204 { 205 addFunction(stopFunction); 206 } 207 208 211 protected Function startFunction = new AbstractFunction("start", Parameter.EMPTY, ReturnType.BOOLEAN) { 212 public Object getFunctionValue(Parameters arguments) { 213 cronDaemon.start(); 214 return Boolean.valueOf(cronDaemon.isAlive()); 215 } 216 217 }; 218 { 219 addFunction(startFunction); 220 } 221 222 225 protected Function reloadFunction = new AbstractFunction("reload", Parameter.EMPTY, ReturnType.BOOLEAN) { 226 public Object 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 |