1 17 18 19 20 package org.apache.lenya.cms.scheduler.xml; 21 22 import java.io.IOException ; 23 import java.text.ParseException ; 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import java.util.GregorianCalendar ; 27 28 import org.apache.lenya.cms.scheduler.SchedulerStore; 29 import org.apache.lenya.util.NamespaceMap; 30 import org.apache.lenya.xml.NamespaceHelper; 31 import org.apache.log4j.Category; 32 import org.quartz.CronTrigger; 33 import org.quartz.SimpleTrigger; 34 import org.quartz.Trigger; 35 import org.w3c.dom.Element ; 36 37 38 41 public final class TriggerHelper { 42 43 46 private TriggerHelper() { 47 } 48 49 private static Category log = Category.getInstance(TriggerHelper.class); 50 public static final String YEAR = "year"; 51 public static final String MONTH = "month"; 52 public static final String DAY = "day"; 53 public static final String HOUR = "hour"; 54 public static final String MINUTE = "minute"; 55 public static final String TRIGGER_TYPE = "type"; 56 public static final String ONCE = "once"; 57 public static final String REPEATED = "repeated"; 58 public static final String CRON_EXPRESSION = "expression"; 59 private static int id = 0; 60 public static final String PREFIX = "trigger"; 61 62 71 public static Trigger createTrigger(Element element, String jobName, String jobGroup) { 72 if (!element.getLocalName().equals("trigger")) { 73 throw new IllegalArgumentException (); 74 } 75 76 String triggerType = element.getAttribute(TRIGGER_TYPE); 77 78 NamespaceHelper helper = SchedulerStore.getNamespaceHelper(); 79 80 if (triggerType.equals(ONCE)) { 82 Element [] parameterElements = helper.getChildren(element, "parameter"); 83 GregorianCalendar date = new GregorianCalendar (); 84 85 for (int i = 0; i < parameterElements.length; i++) { 86 String name = parameterElements[i].getAttribute("name"); 87 String value = parameterElements[i].getAttribute("value"); 88 89 if (name.equals(YEAR)) { 90 date.set(Calendar.YEAR, Integer.parseInt(value)); 91 } 92 93 if (name.equals(MONTH)) { 94 date.set(Calendar.MONTH, Integer.parseInt(value) - 1); 95 } 96 97 if (name.equals(DAY)) { 98 date.set(Calendar.DAY_OF_MONTH, Integer.parseInt(value)); 99 } 100 101 if (name.equals(HOUR)) { 102 date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(value)); 103 } 104 105 if (name.equals(MINUTE)) { 106 date.set(Calendar.MINUTE, Integer.parseInt(value)); 107 } 108 109 if (name.equals(HOUR)) { 110 date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(value)); 111 } 112 } 113 114 return createSimpleTrigger(jobName, jobGroup, date.getTime()); 115 } 116 117 if (triggerType.equals(REPEATED)) { 119 Element [] parameterElements = helper.getChildren(element, "parameter"); 120 String name = parameterElements[0].getAttribute("name"); 121 String value = parameterElements[0].getAttribute("value"); 122 String cron_expression; 123 124 if (name.equals(CRON_EXPRESSION)) { 125 cron_expression = value; 126 } else { 127 cron_expression = "45 * * * * ?"; 128 } 129 130 return createCronTrigger(jobName, jobGroup, cron_expression); 131 } 132 133 throw new IllegalStateException ("Trigger type '" + triggerType + "' not defined!"); 134 } 135 136 140 protected static String createUniqueTriggerId() { 141 return "trigger_" + id++; 142 } 143 144 153 public static Trigger createSimpleTrigger(String jobName, String jobGroup, Date date) { 154 return new SimpleTrigger(createUniqueTriggerId(), "triggerGroup1", jobName, jobGroup, date, 155 null, 0, 0); 156 } 157 158 168 public static Trigger createCronTrigger(String jobName, String jobGroup, String cron_expression) { 169 try { 170 return new CronTrigger(createUniqueTriggerId(), "triggerGroup1", jobName, jobGroup, 171 cron_expression); 172 } catch (ParseException e) { 173 log.error(".createCronTrigger(): " + e); 174 } 175 176 return null; 177 } 178 179 185 public static Element createElement(NamespaceHelper helper, Trigger trigger) { 186 Element triggerElement = helper.createElement("trigger"); 187 triggerElement.setAttribute("type", ONCE); 188 189 if (trigger == null) { 190 return triggerElement; 191 } 192 193 GregorianCalendar startTime = new GregorianCalendar (); 194 startTime.setTime(trigger.getStartTime()); 195 196 Element yearElement = helper.createElement("parameter"); 197 yearElement.setAttribute("name", YEAR); 198 yearElement.setAttribute("value", Integer.toString(startTime.get(Calendar.YEAR))); 199 triggerElement.appendChild(yearElement); 200 201 Element monthElement = helper.createElement("parameter"); 202 monthElement.setAttribute("name", MONTH); 203 monthElement.setAttribute("value", Integer.toString(startTime.get(Calendar.MONTH) + 1)); 204 triggerElement.appendChild(monthElement); 205 206 Element dayElement = helper.createElement("parameter"); 207 dayElement.setAttribute("name", DAY); 208 dayElement.setAttribute("value", Integer.toString(startTime.get(Calendar.DAY_OF_MONTH))); 209 triggerElement.appendChild(dayElement); 210 211 Element hourElement = helper.createElement("parameter"); 212 hourElement.setAttribute("name", HOUR); 213 hourElement.setAttribute("value", Integer.toString(startTime.get(Calendar.HOUR_OF_DAY))); 214 triggerElement.appendChild(hourElement); 215 216 Element minuteElement = helper.createElement("parameter"); 217 minuteElement.setAttribute("name", MINUTE); 218 minuteElement.setAttribute("value", Integer.toString(startTime.get(Calendar.MINUTE))); 219 triggerElement.appendChild(minuteElement); 220 221 return triggerElement; 222 } 223 224 230 public static Date getDate(NamespaceMap schedulerParameters) throws IOException { 231 NamespaceMap triggerParameters = 232 new NamespaceMap(schedulerParameters.getMap(), PREFIX); 233 String startDay = (String ) triggerParameters.get(DAY); 234 String startMonth = (String ) triggerParameters.get(MONTH); 235 String startYear = (String ) triggerParameters.get(YEAR); 236 String startHour = (String ) triggerParameters.get(HOUR); 237 String startMin = (String ) triggerParameters.get(MINUTE); 238 239 Date startTime = null; 240 241 try { 242 startTime = 244 new GregorianCalendar ( 245 Integer.parseInt(startYear), 246 Integer.parseInt(startMonth) - 1, 247 Integer.parseInt(startDay), 248 Integer.parseInt(startHour), 249 Integer.parseInt(startMin)) 250 .getTime(); 251 } catch (NumberFormatException e) { 252 log.error( 253 "NumberFormatException with parameters " 254 + "startYear, startMonth, startDay, startHour, startMin: " 255 + startDay 256 + ", " 257 + startMonth 258 + ", " 259 + startDay 260 + ", " 261 + startHour 262 + ", " 263 + startMin, 264 e); 265 throw new IOException ("Parsing scheduling date/time failed!"); 266 } 267 return startTime; 268 } 269 270 } 271 | Popular Tags |