KickJava   Java API By Example, From Geeks To Geeks.

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


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: TaskJob.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.scheduler;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.lenya.cms.task.DefaultTaskWrapper;
29 import org.apache.lenya.cms.task.ExecutionException;
30 import org.apache.lenya.cms.task.TaskParameters;
31 import org.apache.lenya.cms.task.TaskWrapper;
32 import org.apache.lenya.util.NamespaceMap;
33 import org.apache.lenya.xml.NamespaceHelper;
34 import org.apache.log4j.Category;
35 import org.quartz.JobDataMap;
36 import org.quartz.JobDetail;
37 import org.quartz.JobExecutionContext;
38 import org.quartz.JobExecutionException;
39 import org.quartz.SchedulerException;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * A TaskJob is a Job that executes a Task. The task ID is obtained from the <code>task-id</code>
44  * request parameter.
45  */

46 public class TaskJob extends ServletJob {
47     private static Category log = Category.getInstance(TaskJob.class);
48
49     /**
50      * Un-prefix the parameters.
51      *
52      * @param wrapperMap the prefixed parameters.
53      *
54      * @return the parameters
55      * @throws SchedulerException when something went wrong.
56      */

57     protected Map JavaDoc stripPrefixes(Map JavaDoc wrapperMap)
58         throws SchedulerException {
59         
60         NamespaceMap taskParameters = new NamespaceMap(TaskParameters.PREFIX);
61         taskParameters.putAll(wrapperMap);
62         wrapperMap.putAll(taskParameters.getPrefixedMap());
63         
64         DefaultTaskWrapper wrapper = new DefaultTaskWrapper(wrapperMap);
65         return wrapper.getParameters();
66     }
67
68     /**
69      * Creates the job data for a job.
70      *
71      * @param servletContextPath The servlet context path.
72      * @param request The request.
73      *
74      * @return A job data map.
75      * @throws SchedulerException when something went wrong.
76      */

77     public JobDataMap createJobData(HttpServletRequest JavaDoc request)
78         throws SchedulerException {
79         log.debug("Creating job data map:");
80         JobDataMap map = super.createJobData(request);
81
82         Enumeration JavaDoc parameters = request.getParameterNames();
83         Map JavaDoc wrapperMap = new HashMap JavaDoc();
84         while (parameters.hasMoreElements()) {
85             String JavaDoc key = (String JavaDoc) parameters.nextElement();
86             Object JavaDoc value;
87             String JavaDoc[] values = request.getParameterValues(key);
88             if (values.length == 1) {
89                 value = values[0];
90             }
91             else {
92                 value = values;
93             }
94             wrapperMap.put(key, value);
95         }
96
97         map.putAll(stripPrefixes(wrapperMap));
98         return map;
99     }
100
101     /**
102      * <p>
103      * Called by the <code>{@link org.quartz.Scheduler}</code> when a <code>{@link
104      * org.quartz.Trigger}</code> fires that is associated with the <code>Job</code>.
105      * </p>
106      *
107      * @param context DOCUMENT ME!
108      *
109      * @throws JobExecutionException if there is an exception while executing the job.
110      */

111     public void execute(JobExecutionContext context) throws JobExecutionException {
112         log.info("Executing job");
113         JobDetail jobDetail = context.getJobDetail();
114         
115         DefaultTaskWrapper wrapper = new DefaultTaskWrapper(jobDetail.getJobDataMap());
116         try {
117             wrapper.execute();
118         } catch (ExecutionException e) {
119             log.error("Task execution failed: ", e);
120         }
121     }
122
123     /**
124      * Loads a job details object from an XML element.
125      *
126      * @param jobElement The XML element.
127      * @param jobGroup The job group the job belongs to.
128      * @param servletContextPath The servlet context path.
129      * @throws SchedulerException when something went wrong.
130      *
131      * @return A job details object.
132      */

133     public JobDetail load(Element JavaDoc jobElement, String JavaDoc jobGroup, String JavaDoc servletContextPath) throws SchedulerException {
134         JobDetail jobDetail = super.load(jobElement, jobGroup, servletContextPath);
135         
136         NamespaceHelper helper = SchedulerStore.getNamespaceHelper();
137         DefaultTaskWrapper wrapper = new DefaultTaskWrapper(helper, jobElement);
138         wrapper.getTaskParameters().setServletContextPath(servletContextPath);
139         
140         JobDataMap map = new JobDataMap(wrapper.getParameters());
141         jobDetail.setJobDataMap(map);
142         
143         return jobDetail;
144     }
145
146     /**
147      * DOCUMENT ME!
148      *
149      * @param jobDetail DOCUMENT ME!
150      * @param helper namespace helper
151      * @throws SchedulerException when something went wrong.
152      *
153      * @return DOCUMENT ME!
154      */

155     public Element JavaDoc save(NamespaceHelper helper, JobDetail jobDetail) throws SchedulerException {
156         
157         Element JavaDoc jobElement = super.save(helper, jobDetail);
158         TaskWrapper wrapper = new DefaultTaskWrapper(jobDetail.getJobDataMap());
159         jobElement.appendChild(wrapper.save(helper));
160         
161         return jobElement;
162     }
163 }
164
Popular Tags