1 17 18 19 20 package org.apache.lenya.cms.task; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 31 import org.apache.avalon.framework.parameters.Parameters; 32 import org.apache.cocoon.environment.Request; 33 import org.apache.lenya.ac.Identity; 34 import org.apache.lenya.ac.Role; 35 import org.apache.lenya.cms.publication.Publication; 36 import org.apache.lenya.util.NamespaceMap; 37 import org.apache.lenya.xml.NamespaceHelper; 38 import org.apache.log4j.Category; 39 import org.w3c.dom.Element ; 40 41 public class DefaultTaskWrapper implements TaskWrapper { 42 43 private static Category log = Category.getInstance(DefaultTaskWrapper.class); 44 45 private Map parameters = new HashMap (); 46 private TaskWrapperParameters wrapperParameters = 47 new TaskWrapperParameters(getParameterObject()); 48 private TaskParameters taskParameters = new TaskParameters(getParameterObject()); 49 50 53 protected DefaultTaskWrapper() { 54 } 55 56 61 public DefaultTaskWrapper(Map parameters) { 62 log.debug("Creating"); 63 64 List keys = new ArrayList (); 65 for (Iterator i = parameters.keySet().iterator(); i.hasNext();) { 66 String key = (String ) i.next(); 67 keys.add(key); 68 } 69 70 Collections.sort(keys); 71 72 for (Iterator i = keys.iterator(); i.hasNext();) { 73 String key = (String ) i.next(); 74 String value = null; 75 Object valueObject = parameters.get(key); 76 if (valueObject instanceof String ) { 77 value = ((String ) valueObject).trim(); 78 } else if (valueObject instanceof String []) { 79 String [] values = (String []) valueObject; 80 value = ""; 81 for (int j = 0; j < values.length; j++) { 82 if (j > 0 && !"".equals(value)) { 83 value += ","; 84 } 85 value += values[j].trim(); 86 } 87 } else { 88 log.debug("Not a string value: [" + key + "] = [" + valueObject + "]"); 89 } 90 91 if (value != null) { 92 log.debug("Setting parameter: [" + key + "] = [" + value + "]"); 93 this.parameters.put(key, value); 94 } 95 } 96 97 } 98 99 105 public DefaultTaskWrapper(NamespaceHelper helper, Element parent) { 106 log.debug("Creating"); 107 restore(helper, parent); 108 } 109 110 118 protected void initialize( 119 String taskId, 120 Publication publication, 121 String webappUrl, 122 Parameters parameters) 123 throws ExecutionException { 124 log.debug("Initializing"); 125 126 getTaskParameters().setPublication(publication); 127 getWrapperParameters().setWebappUrl(webappUrl); 128 129 getWrapperParameters().setTaskId(taskId); 130 getTaskParameters().parameterize(parameters); 131 } 132 133 140 protected Parameters extractTaskParameters( 141 Parameters parameters, 142 Publication publication, 143 Request request) { 144 Parameters taskParameters = new Parameters(); 145 taskParameters.setParameter( 146 Task.PARAMETER_SERVLET_CONTEXT, 147 publication.getServletContext().getAbsolutePath()); 148 taskParameters.setParameter(Task.PARAMETER_CONTEXT_PREFIX, request.getContextPath()); 149 taskParameters.setParameter( 150 Task.PARAMETER_SERVER_PORT, 151 Integer.toString(request.getServerPort())); 152 taskParameters.setParameter(Task.PARAMETER_SERVER_URI, "http://" + request.getServerName()); 153 taskParameters.setParameter(Task.PARAMETER_PUBLICATION_ID, publication.getId()); 154 155 for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) { 156 String key = (String ) e.nextElement(); 157 String value = request.getParameter(key); 158 if (value != null) { 159 taskParameters.setParameter(key, value); 160 } 161 } 162 163 String [] names = parameters.getNames(); 164 for (int i = 0; i < names.length; i++) { 165 String name = names[i]; 166 String value = parameters.getParameter(name, ""); 167 if (value != null) { 168 taskParameters.setParameter(name, value); 169 } 170 } 171 return taskParameters; 172 } 173 174 180 public void setWorkflowAware(String eventName, Identity identity, Role[] roles) { 181 NamespaceMap workflowParameters = 182 WorkflowInvoker.extractParameters(eventName, identity, roles); 183 getParameterObject().putAll(workflowParameters.getPrefixedMap()); 184 } 185 186 190 public void execute() throws ExecutionException { 191 192 String taskId = getWrapperParameters().getTaskId(); 193 194 if (taskId == null) { 195 throw new ExecutionException("No task id provided!"); 196 } 197 198 log.info("==================================="); 199 log.info(" Executing task [" + taskId + "]"); 200 log.info("-----------------------------------"); 201 202 if (!wrapperParameters.isComplete()) { 203 204 String [] missingKeys = getWrapperParameters().getMissingKeys(); 205 String keyString = ""; 206 for (int i = 0; i < missingKeys.length; i++) { 207 if (i > 0) { 208 keyString += ", "; 209 } 210 keyString += missingKeys[i]; 211 } 212 throw new ExecutionException("Parameters missing: [" + keyString + "]"); 213 } 214 215 TaskManager manager; 216 217 Publication publication = getTaskParameters().getPublication(); 218 219 WorkflowInvoker workflowInvoker = new WorkflowInvoker(getParameters()); 220 workflowInvoker.setup(publication, getWrapperParameters().getWebappUrl()); 221 222 Task task; 223 try { 224 manager = new TaskManager(publication.getDirectory().getAbsolutePath()); 225 task = manager.getTask(taskId); 226 227 Properties properties = new Properties (); 228 properties.putAll(getTaskParameters().getMap()); 229 Parameters parameters = Parameters.fromProperties(properties); 230 231 task.parameterize(parameters); 232 } catch (Exception e) { 233 throw new ExecutionException(e); 234 } 235 236 log.debug("-----------------------------------"); 237 log.debug(" Triggering workflow"); 238 log.debug("-----------------------------------"); 239 240 workflowInvoker.invokeTransition(); 243 244 log.debug("-----------------------------------"); 245 log.debug(" Triggering task"); 246 log.debug("-----------------------------------"); 247 248 task.execute(publication.getServletContext().getAbsolutePath()); 249 250 log.debug("-----------------------------------"); 251 log.debug(" Triggering notification"); 252 log.debug("-----------------------------------"); 253 Notifier notifier = new Notifier(manager, getParameters()); 254 notifier.sendNotification(getTaskParameters()); 255 256 log.debug("-----------------------------------"); 257 log.debug(" Executing task finished."); 258 log.debug("===================================\n\n"); 259 } 260 261 265 public TaskWrapperParameters getWrapperParameters() { 266 return wrapperParameters; 267 } 268 269 273 public TaskParameters getTaskParameters() { 274 return taskParameters; 275 } 276 277 protected static final String ELEMENT_TASK = "task"; 278 protected static final String ELEMENT_PARAMETER = "parameter"; 279 protected static final String ATTRIBUTE_NAME = "name"; 280 protected static final String ATTRIBUTE_VALUE = "value"; 281 282 287 public Element save(NamespaceHelper helper) { 288 org.w3c.dom.Document document = helper.getDocument(); 289 NamespaceHelper taskHelper = 290 new NamespaceHelper(Task.NAMESPACE, Task.DEFAULT_PREFIX, document); 291 Element element = taskHelper.createElement(ELEMENT_TASK); 292 293 List keys = new ArrayList (getParameters().keySet()); 294 Collections.sort(keys); 295 296 for (Iterator i = keys.iterator(); i.hasNext();) { 297 String key = (String ) i.next(); 298 Element parameterElement = taskHelper.createElement(ELEMENT_PARAMETER); 299 parameterElement.setAttribute(ATTRIBUTE_NAME, key); 300 parameterElement.setAttribute(ATTRIBUTE_VALUE, (String ) getParameters().get(key)); 301 element.appendChild(parameterElement); 302 } 303 304 return element; 305 } 306 307 312 public void restore(NamespaceHelper helper, Element parent) { 313 org.w3c.dom.Document document = helper.getDocument(); 314 NamespaceHelper taskHelper = 315 new NamespaceHelper(Task.NAMESPACE, Task.DEFAULT_PREFIX, document); 316 Element taskElement = taskHelper.getFirstChild(parent, ELEMENT_TASK); 317 Element [] parameterElements = taskHelper.getChildren(taskElement, ELEMENT_PARAMETER); 318 for (int i = 0; i < parameterElements.length; i++) { 319 String key = parameterElements[i].getAttribute(ATTRIBUTE_NAME); 320 String value = parameterElements[i].getAttribute(ATTRIBUTE_VALUE); 321 getParameterObject().put(key, value); 322 } 323 } 324 325 329 public Map getParameters() { 330 return Collections.unmodifiableMap(parameters); 331 } 332 333 337 protected Map getParameterObject() { 338 return parameters; 339 } 340 341 345 protected void setNotifying(NamespaceMap notificationParameters) { 346 log.info("Enabling notification"); 347 getParameterObject().putAll(notificationParameters.getPrefixedMap()); 348 } 349 350 } 351 | Popular Tags |