KickJava   Java API By Example, From Geeks To Geeks.

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


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: ServletJob.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.scheduler;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23
24 import org.apache.lenya.util.NamespaceMap;
25 import org.apache.lenya.xml.NamespaceHelper;
26 import org.apache.log4j.Category;
27 import org.quartz.Job;
28 import org.quartz.JobDataMap;
29 import org.quartz.JobDetail;
30 import org.quartz.SchedulerException;
31 import org.w3c.dom.Element JavaDoc;
32
33 /**
34  * Scheduling job that uses an HttpServletRequest to obtain its Job data.
35  */

36 public abstract class ServletJob implements Job {
37
38     private static Category log = Category.getInstance(ServletJob.class);
39     
40     /**
41      * Creates the job data from an HTTP request.
42      * @param request The request.
43      * @return A job data map.
44      * @throws SchedulerException when something went wrong.
45      */

46     public JobDataMap createJobData(HttpServletRequest JavaDoc request)
47         throws SchedulerException {
48         JobDataMap map = new JobDataMap();
49         String JavaDoc key = NamespaceMap.getFullName(LoadQuartzServlet.PREFIX, PARAMETER_DOCUMENT_URL);
50         String JavaDoc documentUrl = request.getParameter(key);
51         if (documentUrl == null) {
52             throw new SchedulerException("Document URL must not be null!");
53         }
54         map.put(key, documentUrl);
55         return map;
56     }
57
58     /**
59      * Loads the job data from an XML element.
60      * @param element An XML element.
61      * @param jobGroup The job group the job belongs to.
62      * @param servletContextPath The servlet context path.
63      * @return A job detail object.
64      * @throws SchedulerException when something went wrong.
65      */

66     public JobDetail load(Element JavaDoc element, String JavaDoc jobGroup, String JavaDoc servletContextPath)
67         throws SchedulerException {
68         String JavaDoc jobId = element.getAttribute(ATTRIBUTE_ID);
69         JobDetail jobDetail = new JobDetail(jobId, jobGroup, getClass());
70         return jobDetail;
71
72     }
73
74     public static final String JavaDoc ELEMENT_JOB = "job";
75     public static final String JavaDoc ATTRIBUTE_ID = "id";
76     public static final String JavaDoc ATTRIBUTE_CLASS = "class";
77     public static final String JavaDoc ATTRIBUTE_DOCUMENT_URL = "url";
78     public static final String JavaDoc ATTRIBUTE_SERVLET_CONTEXT = "servletcontext";
79     public static final String JavaDoc PARAMETER_DOCUMENT_URL = "document-url";
80
81     /**
82      * Saves the job data to an XML element.
83      * @param helper The namespace helper of the document the element shall belong to.
84      * @param jobDetail The job detail to save.
85      * @return An XML element.
86      * @throws SchedulerException when something went wrong.
87      */

88     public Element JavaDoc save(NamespaceHelper helper, JobDetail jobDetail) throws SchedulerException {
89         log.debug("Saving job");
90
91         Element JavaDoc jobElement = helper.createElement(ELEMENT_JOB);
92         jobElement.setAttribute(ATTRIBUTE_ID, jobDetail.getName());
93         jobElement.setAttribute(ATTRIBUTE_CLASS, getClass().getName());
94
95         String JavaDoc documentUrl = getDocumentUrl(jobDetail);
96         jobElement.setAttribute(ATTRIBUTE_DOCUMENT_URL, documentUrl);
97         
98         return jobElement;
99     }
100
101     /**
102      * Returns the document URL of a certain job.
103      * @param jobDetail The job detail.
104      * @return A string.
105      */

106     public String JavaDoc getDocumentUrl(JobDetail jobDetail) {
107         JobDataMap map = jobDetail.getJobDataMap();
108         NamespaceMap wrapper = new NamespaceMap(map, LoadQuartzServlet.PREFIX);
109         String JavaDoc documentUrl = (String JavaDoc) wrapper.get(PARAMETER_DOCUMENT_URL);
110         return documentUrl;
111     }
112     
113     /**
114      * Sets the document URL of a job.
115      * @param jobDetail The job detail.
116      * @param url The URL.
117      */

118     public void setDocumentUrl(JobDetail jobDetail, String JavaDoc url) {
119         JobDataMap map = jobDetail.getJobDataMap();
120         NamespaceMap wrapper = new NamespaceMap(map, LoadQuartzServlet.PREFIX);
121         wrapper.put(PARAMETER_DOCUMENT_URL, url);
122         jobDetail.setJobDataMap(map);
123     }
124
125 }
126
Popular Tags