KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > scheduler > SchedulerStore


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: SchedulerStore.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.scheduler;
21
22 import java.io.File JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.GregorianCalendar JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.lenya.cms.publication.Publication;
31 import org.apache.lenya.cms.scheduler.xml.TriggerHelper;
32 import org.apache.lenya.xml.DocumentHelper;
33 import org.apache.lenya.xml.NamespaceHelper;
34 import org.apache.log4j.Category;
35 import org.quartz.JobDetail;
36 import org.quartz.SchedulerException;
37 import org.quartz.Trigger;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  * Store for scheduler jobs.
43  */

44 public class SchedulerStore {
45
46     public static final String JavaDoc ELEMENT_JOB_GROUP = "job-group";
47     public static final String JavaDoc ELEMENT_JOB = "job";
48     public static final String JavaDoc TITLE_ELEMENT = "title";
49
50     private static final Category log = Category.getInstance(SchedulerStore.class);
51
52     public static final String JavaDoc SNAPSHOT_FILE =
53         "config/scheduler/jobs.xml".replace('/', File.separatorChar);
54
55     /**
56      * Ctor.
57      * @param publication The publication.
58      */

59     public SchedulerStore() {
60     }
61
62     /**
63      * Returns the job snapshot file for a publication..
64      * @param Publication The publication.
65      * @return A file.
66      * @throws SchedulerException when the publication could not be built.
67      */

68     protected File JavaDoc getJobsFile(Publication publication) throws SchedulerException {
69         File JavaDoc jobsFile;
70         jobsFile = new File JavaDoc(publication.getDirectory(), SNAPSHOT_FILE);
71         log.debug("Resolved job snapshot file: [" + jobsFile.getAbsolutePath() + "]");
72         return jobsFile;
73     }
74
75     /**
76      * Writes a job snapshot.
77      * @param publication The publication.
78      * @throws SchedulerException when something went wrong.
79      */

80     protected void writeSnapshot(Publication publication, JobWrapper[] jobs)
81         throws SchedulerException {
82
83         log.debug("Writing job snapshot for publication [" + publication.getId() + "]");
84         File JavaDoc jobsFile = getJobsFile(publication);
85
86         try {
87             File JavaDoc directory = jobsFile.getParentFile();
88
89             if (!directory.exists()) {
90                 directory.mkdirs();
91                 log.debug("Creating job snapshot directory: " + directory.getPath());
92             }
93
94             jobsFile.createNewFile();
95             DocumentHelper.writeDocument(getSnapshot(publication, jobs), jobsFile);
96         } catch (Exception JavaDoc e) {
97             log.error("Writing job snapshot failed: ", e);
98         }
99     }
100
101     /**
102      * Return an xml description of all scheduled jobs for the given publication.
103      *
104      * @param publication The publication.
105      * @return An XML document.
106      * @exception SchedulerException if an error occurs
107      */

108     public Document JavaDoc getSnapshot(Publication publication, JobWrapper[] jobs)
109         throws SchedulerException {
110         NamespaceHelper helper = SchedulerStore.getNamespaceHelper();
111         Document JavaDoc document = helper.getDocument();
112         Element JavaDoc root = document.getDocumentElement();
113
114         log.debug("Creating job snapshot for publication [" + publication.getId() + "]");
115         root.appendChild(createSnapshot(helper, publication, jobs));
116
117         return document;
118     }
119
120     /** The namespace for the <code>jobs.xml</code> file. */
121     public static final String JavaDoc NAMESPACE = "http://apache.org/cocoon/lenya/scheduler/1.0";
122
123     /**
124      * Returns a scheduler namespace helper for a document.
125      * @param document The XML document.
126      * @return a namespace helper.
127      */

128     public static NamespaceHelper getNamespaceHelper(Document JavaDoc document) {
129         return new NamespaceHelper(NAMESPACE, "sch", document);
130     }
131
132     /**
133      * Returns a new scheduler namespace helper with an document containing
134      * a &lt;sch:scheduler&gt; element.
135      * @return a namespace helper.
136      */

137     public static NamespaceHelper getNamespaceHelper() {
138         try {
139             return new NamespaceHelper(NAMESPACE, "sch", "scheduler");
140         } catch (Exception JavaDoc e) {
141             log.error("Could not create namespace helper: ", e);
142
143             return null;
144         }
145     }
146
147     /**
148      * Creates an XML element containting a snapshot of a job group.
149      * @param helper The namespace helper to use.
150      * @param jobGroup The job group.
151      * @return An XMl element.
152      * @throws SchedulerException when something went wrong.
153      */

154     protected Element JavaDoc createSnapshot(
155         NamespaceHelper helper,
156         Publication publication,
157         JobWrapper[] jobs)
158         throws SchedulerException {
159         Element JavaDoc jobGroupElement = helper.createElement(ELEMENT_JOB_GROUP);
160         jobGroupElement.setAttribute("name", publication.getId());
161
162         for (int i = 0; i < jobs.length; i++) {
163
164             ServletJob job = jobs[i].getJob();
165             Element JavaDoc jobElement = job.save(helper, jobs[i].getJobDetail());
166             jobGroupElement.appendChild(jobElement);
167
168             Trigger trigger = jobs[i].getTrigger();
169
170             if (trigger != null) {
171                 Element JavaDoc triggerElement = TriggerHelper.createElement(helper, trigger);
172                 jobElement.appendChild(triggerElement);
173             }
174         }
175
176         return jobGroupElement;
177     }
178
179     /**
180      * Restores the jobs of a certain job group from the snapshot file.
181      * @param jobGroup The job group.
182      * @throws SchedulerException when something went wrong.
183      */

184     public JobWrapper[] restoreJobs(Publication publication) throws SchedulerException {
185         
186         log.debug("Restoring jobs for publication [" + publication.getId() + "]");
187
188         List JavaDoc wrappers = new ArrayList JavaDoc();
189         File JavaDoc jobsFile = getJobsFile(publication);
190         
191         if (jobsFile.exists()) {
192             Element JavaDoc[] jobElements = getJobElements(publication);
193             Document JavaDoc document;
194             try {
195                 document = DocumentHelper.readDocument(jobsFile);
196             } catch (Exception JavaDoc e) {
197                 throw new SchedulerException(e);
198             }
199             NamespaceHelper helper = SchedulerStore.getNamespaceHelper(document);
200
201             for (int i = 0; i < jobElements.length; i++) {
202                 wrappers.add(restoreJob(helper, jobElements[i], publication));
203             }
204         }
205         else {
206             log.debug("Could not restore jobs for publication [" + publication.getId() + "] - jobs file does not exist.");
207         }
208
209         return (JobWrapper[]) wrappers.toArray(new JobWrapper[wrappers.size()]);
210     }
211
212     /**
213      * Restores the jobs from a certain XML element.
214      * @param jobElement The XML element.
215      * @param jobGroup The job group the job belongs to.
216      */

217     protected JobWrapper restoreJob(
218         NamespaceHelper helper,
219         Element JavaDoc jobElement,
220         Publication publication)
221         throws SchedulerException {
222         log.debug("Restoring job ");
223         JobWrapper wrapper;
224
225         try {
226             String JavaDoc jobClassName = jobElement.getAttribute(ServletJob.ATTRIBUTE_CLASS);
227             ServletJob job = ServletJobFactory.createJob(jobClassName);
228             JobDetail jobDetail =
229                 job.load(
230                     jobElement,
231                     publication.getId(),
232                     publication.getServletContext().getAbsolutePath());
233
234             Trigger trigger = null;
235
236             Element JavaDoc triggerElement = helper.getFirstChild(jobElement, "trigger");
237             if (triggerElement != null) {
238                 trigger =
239                     TriggerHelper.createTrigger(
240                         triggerElement,
241                         jobDetail.getName(),
242                         jobDetail.getGroup());
243
244                 Date JavaDoc now = new GregorianCalendar JavaDoc().getTime();
245                 if (log.isDebugEnabled()) {
246                     DateFormat JavaDoc format = new SimpleDateFormat JavaDoc();
247                     log.debug(
248                         " Trigger time: [" + format.format(trigger.getFinalFireTime()) + "]");
249                     log.debug(" Current time: [" + format.format(now) + "]");
250                 }
251                 if (!trigger.getFinalFireTime().after(now)) {
252                     trigger = null;
253                 }
254             }
255             wrapper = new JobWrapper(jobDetail, trigger);
256
257         } catch (Exception JavaDoc e) {
258             throw new SchedulerException(e);
259         }
260         return wrapper;
261     }
262
263     /**
264      * Returns the job elements of a publication.
265      * @param publication
266      * @return
267      * @throws SchedulerException when something went wrong.
268      */

269     protected Element JavaDoc[] getJobElements(Publication publication) throws SchedulerException {
270         Element JavaDoc[] jobElements;
271         try {
272             File JavaDoc jobsFile = getJobsFile(publication);
273             if (jobsFile.exists()) {
274                 Document JavaDoc document = DocumentHelper.readDocument(jobsFile);
275                 Element JavaDoc schedulerElement = document.getDocumentElement();
276                 NamespaceHelper helper = SchedulerStore.getNamespaceHelper(document);
277
278                 Element JavaDoc jobGroupElement =
279                     helper.getFirstChild(schedulerElement, SchedulerStore.ELEMENT_JOB_GROUP);
280                 if (jobGroupElement == null) {
281                     throw new SchedulerException("No <job-group> element found!");
282                 }
283
284                 String JavaDoc jobGroupAttribute = jobGroupElement.getAttribute("name");
285
286                 if (!jobGroupAttribute.equals(publication.getId())) {
287                     throw new SchedulerException(
288                         "The jobs.xml file contains a wrong job group: ["
289                             + jobGroupAttribute
290                             + "]");
291                 } else {
292                     jobElements = helper.getChildren(jobGroupElement, SchedulerStore.ELEMENT_JOB);
293
294                 }
295             } else {
296                 throw new SchedulerException(
297                     "The jobs file [" + jobsFile.getAbsolutePath() + "] does not exist!");
298             }
299         } catch (SchedulerException e) {
300             throw e;
301         } catch (Exception JavaDoc e) {
302             throw new SchedulerException(e);
303         }
304         return jobElements;
305     }
306 }
307
Popular Tags