1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.form;
16
17 import org.figure8.join.core.Configurable;
18 import org.figure8.join.core.ParameterDefinition;
19 import org.figure8.join.control.JoinForm;
20 import org.figure8.join.control.action.QuartzCronActions;
21 import org.figure8.join.services.scheduling.QuartzCronParameterInfo;
22 import org.figure8.join.util.LogUtil;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.struts.action.ActionMapping;
26 import org.quartz.Job;
27
28 import javax.servlet.http.HttpServletRequest;
29
30 import java.util.Map;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 /**
34 * Form to control the actions on Cron instances.
35 * @author <a href="mailto:jerome.evrard@gmail.com">Jerome Evrard</a>
36 * @version $Revision: 1.2 $
37 *
38 * @struts.form name="quartzCronForm"
39 */
40 public class QuartzCronForm extends JoinForm{
41
42
43
44 /** Get a commons logger. */
45 private static final Log log = LogUtil.getLog(QuartzCronForm.class);
46
47
48
49
50 /** <code>name</code>: The cron name to manage */
51 private String name;
52 /** <code>cronExpression</code>: The cron expression to schedule */
53 private String cronExpression;
54 /** <code>jobClass</code>: The job class name to instanciate on job execution */
55 private String jobClass;
56 /** <code>userProperties</code>: The user properties to parameter a job */
57 private String userProperties;
58
59 /**
60 * The map of parameters of the consumer bean instance if Configurabel.
61 * Keys are the name of parmaeter, values are string representing values.
62 */
63 private Map parameters = new HashMap();
64
65
66
67
68 /** Creates a new instance of QuartzCronForm */
69 public QuartzCronForm(){
70 }
71
72
73
74
75 /** @return Returns the name of the current cron. */
76 public String getName(){
77 return name;
78 }
79 /** @param name The name to set. */
80 public void setName(String name){
81 this.name = name;
82 }
83
84 /** @return Returns the cronExpression. */
85 public String getCronExpression(){
86 return cronExpression;
87 }
88 /** @param cronExpression The cronExpression to set. */
89 public void setCronExpression(String cronExpression){
90 this.cronExpression = cronExpression;
91 }
92
93 /** @return The name of class implementing the job of cron */
94 public String getJobClass(){
95 return jobClass;
96 }
97 /** @param jobClass The name of class implementing the job of cron */
98 public void setJobClass(String jobClass){
99 this.jobClass = jobClass;
100 }
101
102 /** @return Returns the userProperties. */
103 public String getUserProperties(){
104 return userProperties;
105 }
106 /** @param userProperties The userProperties to set. */
107 public void setUserProperties(String userProperties){
108 this.userProperties = userProperties;
109 }
110
111 /** @return Map of parameters associated to consumer. */
112 public Map getParameters(){
113 return parameters;
114 }
115 /**
116 * @param key Name of parameter to get value for
117 * @return string representing parameter value, null if unknown
118 */
119 public Object getMappedParameter(String key){
120 return parameters.get(key);
121 }
122 /**
123 * @param key Name of parameter to set value for
124 * @param value Value of parameters (String representation)
125 */
126 public void setMappedParameter(String key, Object value){
127 parameters.put(key, value);
128 }
129
130 /** @param parameter Existing consumer parameter to add to map ones. */
131 public void addParameter(QuartzCronParameterInfo parameter){
132 setMappedParameter(parameter.getName(), parameter.getValue());
133 }
134
135
136
137
138 /**
139 * Validation of the form attributes.
140 * @param operation String representing the operation to invoke on Action
141 * @param mapping Mapping between forwards name and path for this action
142 * @param request The servlet container request wrapper
143 */
144 public void doValidate(String operation, ActionMapping mapping, HttpServletRequest request){
145
146 if (QuartzCronActions.LOAD_OP.equals(operation)){
147
148 if (name == null || name.length() == 0)
149 addActionError("errors.required", getGuiMessageResources().getMessage("label.cron.name"));
150 }
151 else if (QuartzCronActions.SAVE_OP.equals(operation)){
152
153 if (name == null || name.trim().length() == 0)
154 addActionError("errors.required", getGuiMessageResources().getMessage("label.cron.name"));
155
156 if (cronExpression == null || cronExpression.trim().length() == 0)
157 addActionError("errors.required", getGuiMessageResources().getMessage("label.cron.expression"));
158
159 if (jobClass == null || jobClass.trim().length() == 0)
160 addActionError("errors.required", getGuiMessageResources().getMessage("label.cron.jobClass"));
161 else{
162 try{
163
164 Class clazz = Thread.currentThread().getContextClassLoader().loadClass(jobClass);
165 if (!Job.class.isAssignableFrom(clazz))
166 addActionError("errors.badimplementation", getGuiMessageResources().getMessage("label.cron.jobClasss"));
167 }
168 catch (ClassNotFoundException cnfe){
169 addActionError("errors.classnotfound", getGuiMessageResources().getMessage("label.cron.jobClass"));
170 }
171 }
172 }
173 else if (QuartzCronActions.SAVE_PARAM_OP.equals(operation)){
174
175 checkJobParameters(request);
176 }
177 }
178
179
180
181
182 /** Check if the mandatory job parameters (from Configurable definition) are set */
183 private void checkJobParameters(HttpServletRequest request){
184
185 Configurable job = (Configurable)request.getSession().getAttribute("configurable");
186
187 Iterator parameters = job.getParameterDefinitionsAsList().iterator();
188 while (parameters != null && parameters.hasNext()){
189 ParameterDefinition parameter = (ParameterDefinition)parameters.next();
190 if (parameter.isMandatory()){
191 String value = (String)getMappedParameter(parameter.getName());
192
193 if (value == null || value.trim().length() == 0)
194 addActionError("errors.required", parameter.getName());
195 }
196 }
197 }
198 }