1 21 package com.essiembre.library.scheduler; 22 23 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.text.DateFormat ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 import java.util.Calendar ; 30 import java.util.Timer ; 31 import java.util.Vector ; 32 33 import javax.servlet.http.HttpServlet ; 34 35 import org.exolab.castor.mapping.Mapping; 36 import org.exolab.castor.xml.Unmarshaller; 37 38 import org.xml.sax.InputSource ; 39 40 41 58 public class SchedulerServlet 59 extends HttpServlet { 60 61 68 public void init() throws javax.servlet.ServletException { 69 70 try { 71 72 Mapping mapping = new Mapping( 73 this.getClass().getClassLoader()); 74 String config = 75 getServletConfig().getInitParameter("config"); 76 SchedulerTasks tasks = null; 77 Vector taskVector = null; 78 79 80 InputStreamReader reader = new InputStreamReader ( 82 getClass().getResourceAsStream("SchedulerMapping.xml")); 83 mapping.loadMapping(new InputSource (reader)); 84 85 InputStream input = getServletContext().getResourceAsStream(config); 87 Unmarshaller unmar = new Unmarshaller(mapping); 88 tasks = (SchedulerTasks) unmar.unmarshal(new InputSource (input)); 89 taskVector = tasks.getTasksList(); 90 91 for (int i = 0; i < taskVector.size(); i++) { 93 SchedulerTaskInfo taskInfo = 94 (SchedulerTaskInfo) taskVector.get(i); 95 SchedulerTask task = createSchedulerTask(taskInfo); 96 launchTimer(task, taskInfo); 97 } 98 99 } catch (Exception e) { 100 throw new RuntimeException ("ERROR in SchedulerServler.init():" + e); 101 } 102 } 103 104 105 113 private SchedulerTask createSchedulerTask(SchedulerTaskInfo taskInfo) { 114 115 try { 116 117 Class taskObject = Class.forName(taskInfo.getTask()); 118 SchedulerTask task = (SchedulerTask) taskObject.newInstance(); 119 task.setInitParameters(taskInfo.getParameters()); 120 task.setServletConfig(getServletConfig()); 121 return task; 122 123 } catch (java.lang.ClassNotFoundException e) { 124 throw new RuntimeException ( 125 "ERROR in SchedulerServlet.createSchedulerTask(). " 126 + "Provided class name does not exists. " 127 + "Exception is: " + e); 128 } catch (java.lang.IllegalAccessException e) { 129 throw new RuntimeException ( 130 "ERROR in SchedulerServlet.createSchedulerTask(). " 131 + "Cannot instantiate provided class name. " 132 + "Exception is: " + e); 133 } catch (java.lang.InstantiationException e) { 134 throw new RuntimeException ( 135 "ERROR in SchedulerServlet.createSchedulerTask(). " 136 + "Cannot instantiate provided class name. " 137 + "Exception is: " + e); 138 } 139 } 140 141 142 149 private void launchTimer(SchedulerTask task, SchedulerTaskInfo taskInfo) { 150 151 Timer timer = new Timer (true); 152 Date time = null; 153 154 157 if (taskInfo.getTime() != null) { 158 159 DateFormat formatter = null; 161 162 163 167 if (taskInfo.getTime().indexOf("-") > 0 168 && taskInfo.getTime().indexOf(":") > 0) { 169 170 formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 172 } else if (taskInfo.getTime().indexOf("-") > 0 173 && taskInfo.getTime().indexOf(":") < 0) { 174 175 formatter = new SimpleDateFormat ("yyyy-MM-dd"); 177 } else if (taskInfo.getTime().indexOf("-") < 0 178 && taskInfo.getTime().indexOf(":") > 0) { 179 180 formatter = new SimpleDateFormat ("HH:mm:ss"); 182 } 183 184 try { 185 time = formatter.parse(taskInfo.getTime()); 186 } catch (java.text.ParseException e) { 187 throw new IllegalArgumentException ( 188 "ERROR in SchedulerTask.setTime(String). " 189 + "Could not parse date provided date: '" + time + "'. " 190 + "Exception is: " + e); 191 } 192 193 if (taskInfo.getTime().indexOf("-") < 0 195 && taskInfo.getTime().indexOf(":") > 0) { 196 197 Calendar userCal = Calendar.getInstance(); 199 userCal.setTime(time); 200 201 Calendar sys = Calendar.getInstance(); 203 204 userCal.set(Calendar.YEAR, sys.get(java.util.Calendar.YEAR)); 206 userCal.set(Calendar.MONTH, sys.get(java.util.Calendar.MONTH)); 207 userCal.set(Calendar.DAY_OF_MONTH, 208 sys.get(Calendar.DAY_OF_MONTH)); 209 210 if (userCal.getTime().getTime() < sys.getTime().getTime()) { 212 213 userCal.add(Calendar.DAY_OF_MONTH, 1); 215 } 216 217 time = userCal.getTime(); 219 } 220 } 221 222 227 if (time != null && taskInfo.getPeriod() == null 228 && taskInfo.getDelay() == null) { 229 timer.schedule(task, time); 230 } else if (time != null && taskInfo.getPeriod() != null 231 && taskInfo.getDelay() == null) { 232 timer.schedule( 233 task, time, SchedulerUtil.parseTime(taskInfo.getPeriod())); 234 } else if (time == null && taskInfo.getPeriod() == null 235 && taskInfo.getDelay() != null) { 236 timer.schedule(task, SchedulerUtil.parseTime(taskInfo.getDelay())); 237 238 } else if (time == null && taskInfo.getPeriod() != null 239 && taskInfo.getDelay() != null) { 240 timer.schedule(task, SchedulerUtil.parseTime(taskInfo.getDelay()), 241 SchedulerUtil.parseTime(taskInfo.getPeriod())); 242 } else { 243 throw new RuntimeException ( 244 "ERROR in SchedulerServlet.launchTimer(). " 245 + "No Timer.shedule() method could be found matching " 246 + "supplied arguments. "); 247 } 248 } 249 } | Popular Tags |