1 17 18 19 20 package org.apache.lenya.cms.scheduler; 21 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import javax.servlet.http.HttpServletRequest ; 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 ; 41 42 46 public class TaskJob extends ServletJob { 47 private static Category log = Category.getInstance(TaskJob.class); 48 49 57 protected Map stripPrefixes(Map 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 77 public JobDataMap createJobData(HttpServletRequest request) 78 throws SchedulerException { 79 log.debug("Creating job data map:"); 80 JobDataMap map = super.createJobData(request); 81 82 Enumeration parameters = request.getParameterNames(); 83 Map wrapperMap = new HashMap (); 84 while (parameters.hasMoreElements()) { 85 String key = (String ) parameters.nextElement(); 86 Object value; 87 String [] 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 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 133 public JobDetail load(Element jobElement, String jobGroup, String 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 155 public Element save(NamespaceHelper helper, JobDetail jobDetail) throws SchedulerException { 156 157 Element 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 |