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: ServletJob.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.scheduler;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.lenya.util.NamespaceMap;
026: import org.apache.lenya.xml.NamespaceHelper;
027: import org.apache.log4j.Logger;
028: import org.quartz.Job;
029: import org.quartz.JobDataMap;
030: import org.quartz.JobDetail;
031: import org.quartz.SchedulerException;
032: import org.w3c.dom.DOMException;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Scheduling job that uses an HttpServletRequest to obtain its Job data.
037: */
038: public abstract class ServletJob implements Job {
039:
040: private static Logger log = Logger.getLogger(ServletJob.class);
041:
042: /**
043: * <code>ELEMENT_JOB</code> The job element
044: */
045: public static final String ELEMENT_JOB = "job";
046: /**
047: * <code>ATTRIBUTE_ID</code> The id attribute
048: */
049: public static final String ATTRIBUTE_ID = "id";
050: /**
051: * <code>ATTRIBUTE_CLASS</code> The class attribute
052: */
053: public static final String ATTRIBUTE_CLASS = "class";
054: /**
055: * <code>ATTRIBUTE_DOCUMENT_URL</code> The document url attribute
056: */
057: public static final String ATTRIBUTE_DOCUMENT_URL = "url";
058: /**
059: * <code>ATTRIBUTE_SERVLET_CONTEXT</code> The servlet context attribute
060: */
061: public static final String ATTRIBUTE_SERVLET_CONTEXT = "servletcontext";
062: /**
063: * <code>PARAMETER_DOCUMENT_URL</code> The document URL parameter
064: */
065: public static final String PARAMETER_DOCUMENT_URL = "document-url";
066:
067: /**
068: * Creates the job data from an HTTP request.
069: * @param request The request.
070: * @return A job data map.
071: * @throws SchedulerException when something went wrong.
072: */
073: public JobDataMap createJobData(HttpServletRequest request)
074: throws SchedulerException {
075: JobDataMap map = new JobDataMap();
076: String key = NamespaceMap.getFullName(LoadQuartzServlet.PREFIX,
077: PARAMETER_DOCUMENT_URL);
078: String documentUrl = request.getParameter(key);
079: if (documentUrl == null) {
080: throw new SchedulerException(
081: "Document URL must not be null!");
082: }
083: map.put(key, documentUrl);
084: return map;
085: }
086:
087: /**
088: * Loads the job data from an XML element.
089: * @param element An XML element.
090: * @param jobGroup The job group the job belongs to.
091: * @param servletContextPath The servlet context path.
092: * @return A job detail object.
093: * @throws SchedulerException when something went wrong.
094: */
095: public JobDetail load(Element element, String jobGroup,
096: String servletContextPath) throws SchedulerException {
097: String jobId = element.getAttribute(ATTRIBUTE_ID);
098: JobDetail jobDetail = new JobDetail(jobId, jobGroup, getClass());
099: return jobDetail;
100:
101: }
102:
103: /**
104: * Saves the job data to an XML element.
105: * @param helper The namespace helper of the document the element shall belong to.
106: * @param jobDetail The job detail to save.
107: * @return An XML element.
108: * @throws SchedulerException when something went wrong.
109: */
110: public Element save(NamespaceHelper helper, JobDetail jobDetail)
111: throws SchedulerException {
112: log.debug("Saving job");
113:
114: try {
115: Element jobElement = helper.createElement(ELEMENT_JOB);
116: jobElement.setAttribute(ATTRIBUTE_ID, jobDetail.getName());
117: jobElement.setAttribute(ATTRIBUTE_CLASS, getClass()
118: .getName());
119:
120: String documentUrl = getDocumentUrl(jobDetail);
121: jobElement
122: .setAttribute(ATTRIBUTE_DOCUMENT_URL, documentUrl);
123:
124: return jobElement;
125: } catch (final DOMException e) {
126: log.error("" + e.toString());
127: throw new SchedulerException(e);
128: }
129: }
130:
131: /**
132: * Returns the document URL of a certain job.
133: * @param jobDetail The job detail.
134: * @return A string.
135: */
136: public String getDocumentUrl(JobDetail jobDetail) {
137: JobDataMap map = jobDetail.getJobDataMap();
138: NamespaceMap wrapper = new NamespaceMap(map,
139: LoadQuartzServlet.PREFIX);
140: String documentUrl = (String) wrapper
141: .get(PARAMETER_DOCUMENT_URL);
142: return documentUrl;
143: }
144:
145: /**
146: * Sets the document URL of a job.
147: * @param jobDetail The job detail.
148: * @param url The URL.
149: */
150: public void setDocumentUrl(JobDetail jobDetail, String url) {
151: JobDataMap map = jobDetail.getJobDataMap();
152: NamespaceMap wrapper = new NamespaceMap(map,
153: LoadQuartzServlet.PREFIX);
154: wrapper.put(PARAMETER_DOCUMENT_URL, url);
155: jobDetail.setJobDataMap(map);
156: }
157:
158: }
|