KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > scheduler > xml > TriggerHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: TriggerHelper.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.scheduler.xml;
21
22 import java.io.IOException JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.GregorianCalendar JavaDoc;
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 JavaDoc;
36
37
38 /**
39  * Utility class to work with trigger XML.
40  */

41 public final class TriggerHelper {
42     
43     /**
44      * Ctor.
45      */

46     private TriggerHelper() {
47     }
48
49     private static Category log = Category.getInstance(TriggerHelper.class);
50     public static final String JavaDoc YEAR = "year";
51     public static final String JavaDoc MONTH = "month";
52     public static final String JavaDoc DAY = "day";
53     public static final String JavaDoc HOUR = "hour";
54     public static final String JavaDoc MINUTE = "minute";
55     public static final String JavaDoc TRIGGER_TYPE = "type";
56     public static final String JavaDoc ONCE = "once";
57     public static final String JavaDoc REPEATED = "repeated";
58     public static final String JavaDoc CRON_EXPRESSION = "expression";
59     private static int id = 0;
60     public static final String JavaDoc PREFIX = "trigger";
61
62     /**
63      * Creates a trigger from an XML element.
64      *
65      * @param element The XML element.
66      * @param jobName The job name.
67      * @param jobGroup The job group.
68      *
69      * @return A trigger.
70      */

71     public static Trigger createTrigger(Element JavaDoc element, String JavaDoc jobName, String JavaDoc jobGroup) {
72         if (!element.getLocalName().equals("trigger")) {
73             throw new IllegalArgumentException JavaDoc();
74         }
75
76         String JavaDoc triggerType = element.getAttribute(TRIGGER_TYPE);
77
78         NamespaceHelper helper = SchedulerStore.getNamespaceHelper();
79
80         // SimpleTrigger
81
if (triggerType.equals(ONCE)) {
82             Element JavaDoc[] parameterElements = helper.getChildren(element, "parameter");
83             GregorianCalendar JavaDoc date = new GregorianCalendar JavaDoc();
84
85             for (int i = 0; i < parameterElements.length; i++) {
86                 String JavaDoc name = parameterElements[i].getAttribute("name");
87                 String JavaDoc 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         // CronTrigger
118
if (triggerType.equals(REPEATED)) {
119             Element JavaDoc[] parameterElements = helper.getChildren(element, "parameter");
120             String JavaDoc name = parameterElements[0].getAttribute("name");
121             String JavaDoc value = parameterElements[0].getAttribute("value");
122             String JavaDoc 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 JavaDoc("Trigger type '" + triggerType + "' not defined!");
134     }
135
136     /**
137      * Creates a unique trigger ID.
138      * @return A string.
139      */

140     protected static String JavaDoc createUniqueTriggerId() {
141         return "trigger_" + id++;
142     }
143
144     /**
145      * Creates a simple trigger.
146      *
147      * @param jobName The job name.
148      * @param jobGroup The job group.
149      * @param date The trigger date.
150      *
151      * @return DOCUMENT ME!
152      */

153     public static Trigger createSimpleTrigger(String JavaDoc jobName, String JavaDoc jobGroup, Date JavaDoc date) {
154         return new SimpleTrigger(createUniqueTriggerId(), "triggerGroup1", jobName, jobGroup, date,
155             null, 0, 0);
156     }
157
158     /**
159      * Creates a cron trigger.
160      *
161      * @param jobName The job name.
162      * @param jobGroup The job group.
163      * @param cron_expression Seconds, Minutes, Hours, Day of Month, Months, Day of Week (e.g. 34
164      * ?)
165      *
166      * @return A trigger.
167      */

168     public static Trigger createCronTrigger(String JavaDoc jobName, String JavaDoc jobGroup, String JavaDoc cron_expression) {
169         try {
170             return new CronTrigger(createUniqueTriggerId(), "triggerGroup1", jobName, jobGroup,
171                 cron_expression);
172         } catch (ParseException JavaDoc e) {
173             log.error(".createCronTrigger(): " + e);
174         }
175
176         return null;
177     }
178
179     /**
180      * Creates an XML element containing trigger information.
181      * @param helper The namespace helper to use.
182      * @param trigger The trigger.
183      * @return An XML element.
184      */

185     public static Element JavaDoc createElement(NamespaceHelper helper, Trigger trigger) {
186         Element JavaDoc triggerElement = helper.createElement("trigger");
187         triggerElement.setAttribute("type", ONCE);
188
189         if (trigger == null) {
190             return triggerElement;
191         }
192
193         GregorianCalendar JavaDoc startTime = new GregorianCalendar JavaDoc();
194         startTime.setTime(trigger.getStartTime());
195
196         Element JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
225      * Extracts the date from the scheduler parameters.
226      * @param schedulerParameters The scheduler parameters.
227      * @return A date.
228      * @throws IOException when something went wrong.
229      */

230     public static Date JavaDoc getDate(NamespaceMap schedulerParameters) throws IOException JavaDoc {
231         NamespaceMap triggerParameters =
232             new NamespaceMap(schedulerParameters.getMap(), PREFIX);
233         String JavaDoc startDay = (String JavaDoc) triggerParameters.get(DAY);
234         String JavaDoc startMonth = (String JavaDoc) triggerParameters.get(MONTH);
235         String JavaDoc startYear = (String JavaDoc) triggerParameters.get(YEAR);
236         String JavaDoc startHour = (String JavaDoc) triggerParameters.get(HOUR);
237         String JavaDoc startMin = (String JavaDoc) triggerParameters.get(MINUTE);
238
239         Date JavaDoc startTime = null;
240
241         try {
242             // Month value is 0-based
243
startTime =
244                 new GregorianCalendar JavaDoc(
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 JavaDoc 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 JavaDoc("Parsing scheduling date/time failed!");
266         }
267         return startTime;
268     }
269
270 }
271
Popular Tags