001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: TriggerHelper.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.scheduler.xml;
022:
023: import java.io.IOException;
024: import java.text.ParseException;
025: import java.util.Calendar;
026: import java.util.Date;
027: import java.util.GregorianCalendar;
028:
029: import org.apache.lenya.cms.scheduler.SchedulerStore;
030: import org.apache.lenya.util.NamespaceMap;
031: import org.apache.lenya.xml.NamespaceHelper;
032: import org.apache.log4j.Logger;
033: import org.quartz.CronTrigger;
034: import org.quartz.SimpleTrigger;
035: import org.quartz.Trigger;
036: import org.w3c.dom.Element;
037:
038: /**
039: * Utility class to work with trigger XML.
040: */
041: public final class TriggerHelper {
042:
043: /**
044: * Ctor.
045: */
046: private TriggerHelper() {
047: // do nothing
048: }
049:
050: private static Logger log = Logger.getLogger(TriggerHelper.class);
051: /**
052: * <code>YEAR</code> The year
053: */
054: public static final String YEAR = "year";
055: /**
056: * <code>MONTH</code> The month
057: */
058: public static final String MONTH = "month";
059: /**
060: * <code>DAY</code> The day
061: */
062: public static final String DAY = "day";
063: /**
064: * <code>HOUR</code> The hour
065: */
066: public static final String HOUR = "hour";
067: /**
068: * <code>MINUTE</code> The minute
069: */
070: public static final String MINUTE = "minute";
071: /**
072: * <code>TRIGGER_TYPE</code> The trigger type
073: */
074: public static final String TRIGGER_TYPE = "type";
075: /**
076: * <code>ONCE</code> Once
077: */
078: public static final String ONCE = "once";
079: /**
080: * <code>REPEATED</code> Repeated
081: */
082: public static final String REPEATED = "repeated";
083: /**
084: * <code>CRON_EXPRESSION</code> The cron expression
085: */
086: public static final String CRON_EXPRESSION = "expression";
087: private static int id = 0;
088: /**
089: * <code>PREFIX</code> The prefix
090: */
091: public static final String PREFIX = "trigger";
092:
093: /**
094: * Creates a trigger from an XML element.
095: * @param element The XML element.
096: * @param jobName The job name.
097: * @param jobGroup The job group.
098: * @return A trigger.
099: */
100: public static Trigger createTrigger(Element element,
101: String jobName, String jobGroup) {
102: if (!element.getLocalName().equals("trigger")) {
103: throw new IllegalArgumentException();
104: }
105:
106: String triggerType = element.getAttribute(TRIGGER_TYPE);
107: NamespaceHelper helper = SchedulerStore.getNamespaceHelper();
108:
109: // SimpleTrigger
110: if (triggerType.equals(ONCE)) {
111: Element[] parameterElements = helper.getChildren(element,
112: "parameter");
113: GregorianCalendar date = new GregorianCalendar();
114:
115: for (int i = 0; i < parameterElements.length; i++) {
116: String name = parameterElements[i].getAttribute("name");
117: String value = parameterElements[i]
118: .getAttribute("value");
119:
120: if (name.equals(YEAR)) {
121: date.set(Calendar.YEAR, Integer.parseInt(value));
122: }
123:
124: if (name.equals(MONTH)) {
125: date.set(Calendar.MONTH,
126: Integer.parseInt(value) - 1);
127: }
128:
129: if (name.equals(DAY)) {
130: date.set(Calendar.DAY_OF_MONTH, Integer
131: .parseInt(value));
132: }
133:
134: if (name.equals(HOUR)) {
135: date.set(Calendar.HOUR_OF_DAY, Integer
136: .parseInt(value));
137: }
138:
139: if (name.equals(MINUTE)) {
140: date.set(Calendar.MINUTE, Integer.parseInt(value));
141: }
142:
143: if (name.equals(HOUR)) {
144: date.set(Calendar.HOUR_OF_DAY, Integer
145: .parseInt(value));
146: }
147: }
148:
149: return createSimpleTrigger(jobName, jobGroup, date
150: .getTime());
151: }
152:
153: // CronTrigger
154: if (triggerType.equals(REPEATED)) {
155: Element[] parameterElements = helper.getChildren(element,
156: "parameter");
157: String name = parameterElements[0].getAttribute("name");
158: String value = parameterElements[0].getAttribute("value");
159: String cron_expression;
160:
161: if (name.equals(CRON_EXPRESSION)) {
162: cron_expression = value;
163: } else {
164: cron_expression = "45 * * * * ?";
165: }
166:
167: return createCronTrigger(jobName, jobGroup, cron_expression);
168: }
169:
170: throw new IllegalStateException("Trigger type '" + triggerType
171: + "' not defined!");
172: }
173:
174: /**
175: * Creates a unique trigger ID.
176: * @return A string.
177: */
178: protected static String createUniqueTriggerId() {
179: return "trigger_" + id++;
180: }
181:
182: /**
183: * Creates a simple trigger.
184: * @param jobName The job name.
185: * @param jobGroup The job group.
186: * @param date The trigger date.
187: * @return The trigger
188: */
189: public static Trigger createSimpleTrigger(String jobName,
190: String jobGroup, Date date) {
191: return new SimpleTrigger(createUniqueTriggerId(),
192: "triggerGroup1", jobName, jobGroup, date, null, 0, 0);
193: }
194:
195: /**
196: * Creates a cron trigger.
197: * @param jobName The job name.
198: * @param jobGroup The job group.
199: * @param cron_expression Seconds, Minutes, Hours, Day of Month, Months, Day of Week (e.g. 34
200: * ?)
201: * @return A trigger.
202: */
203: public static Trigger createCronTrigger(String jobName,
204: String jobGroup, String cron_expression) {
205: try {
206: return new CronTrigger(createUniqueTriggerId(),
207: "triggerGroup1", jobName, jobGroup, cron_expression);
208: } catch (ParseException e) {
209: log.error(".createCronTrigger(): " + e);
210: }
211:
212: return null;
213: }
214:
215: /**
216: * Creates an XML element containing trigger information.
217: * @param helper The namespace helper to use.
218: * @param trigger The trigger.
219: * @return An XML element.
220: */
221: public static Element createElement(NamespaceHelper helper,
222: Trigger trigger) {
223: Element triggerElement = helper.createElement("trigger");
224: triggerElement.setAttribute("type", ONCE);
225:
226: if (trigger == null) {
227: return triggerElement;
228: }
229:
230: GregorianCalendar startTime = new GregorianCalendar();
231: startTime.setTime(trigger.getStartTime());
232:
233: Element yearElement = helper.createElement("parameter");
234: yearElement.setAttribute("name", YEAR);
235: yearElement.setAttribute("value", Integer.toString(startTime
236: .get(Calendar.YEAR)));
237: triggerElement.appendChild(yearElement);
238:
239: Element monthElement = helper.createElement("parameter");
240: monthElement.setAttribute("name", MONTH);
241: monthElement.setAttribute("value", Integer.toString(startTime
242: .get(Calendar.MONTH) + 1));
243: triggerElement.appendChild(monthElement);
244:
245: Element dayElement = helper.createElement("parameter");
246: dayElement.setAttribute("name", DAY);
247: dayElement.setAttribute("value", Integer.toString(startTime
248: .get(Calendar.DAY_OF_MONTH)));
249: triggerElement.appendChild(dayElement);
250:
251: Element hourElement = helper.createElement("parameter");
252: hourElement.setAttribute("name", HOUR);
253: hourElement.setAttribute("value", Integer.toString(startTime
254: .get(Calendar.HOUR_OF_DAY)));
255: triggerElement.appendChild(hourElement);
256:
257: Element minuteElement = helper.createElement("parameter");
258: minuteElement.setAttribute("name", MINUTE);
259: minuteElement.setAttribute("value", Integer.toString(startTime
260: .get(Calendar.MINUTE)));
261: triggerElement.appendChild(minuteElement);
262:
263: return triggerElement;
264: }
265:
266: /**
267: * Extracts the date from the scheduler parameters.
268: * @param schedulerParameters The scheduler parameters.
269: * @return A date.
270: * @throws IOException when something went wrong.
271: */
272: public static Date getDate(NamespaceMap schedulerParameters)
273: throws IOException {
274: NamespaceMap triggerParameters = new NamespaceMap(
275: schedulerParameters.getMap(), PREFIX);
276: String startDay = (String) triggerParameters.get(DAY);
277: String startMonth = (String) triggerParameters.get(MONTH);
278: String startYear = (String) triggerParameters.get(YEAR);
279: String startHour = (String) triggerParameters.get(HOUR);
280: String startMin = (String) triggerParameters.get(MINUTE);
281:
282: Date startTime = null;
283:
284: try {
285: // Month value is 0-based
286: startTime = new GregorianCalendar(Integer
287: .parseInt(startYear),
288: Integer.parseInt(startMonth) - 1, Integer
289: .parseInt(startDay), Integer
290: .parseInt(startHour), Integer
291: .parseInt(startMin)).getTime();
292: } catch (NumberFormatException e) {
293: log
294: .error(
295: "NumberFormatException with parameters "
296: + "startYear, startMonth, startDay, startHour, startMin: "
297: + startDay + ", " + startMonth
298: + ", " + startDay + ", "
299: + startHour + ", " + startMin, e);
300: throw new IOException(
301: "Parsing scheduling date/time failed!");
302: }
303: return startTime;
304: }
305:
306: }
|