KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > DefaultTaskWrapper


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: DefaultTaskWrapper.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.task;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
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 JavaDoc;
40
41 public class DefaultTaskWrapper implements TaskWrapper {
42
43     private static Category log = Category.getInstance(DefaultTaskWrapper.class);
44
45     private Map JavaDoc parameters = new HashMap JavaDoc();
46     private TaskWrapperParameters wrapperParameters =
47         new TaskWrapperParameters(getParameterObject());
48     private TaskParameters taskParameters = new TaskParameters(getParameterObject());
49
50     /**
51      * Default ctor for subclasses.
52      */

53     protected DefaultTaskWrapper() {
54     }
55
56     /**
57      * Ctor to be called when all task wrapper parameters are known.
58      * All keys and values must be strings or string arrays.
59      * @param parameters The prefixed parameters.
60      */

61     public DefaultTaskWrapper(Map JavaDoc parameters) {
62         log.debug("Creating");
63
64         List JavaDoc keys = new ArrayList JavaDoc();
65         for (Iterator JavaDoc i = parameters.keySet().iterator(); i.hasNext();) {
66             String JavaDoc key = (String JavaDoc) i.next();
67             keys.add(key);
68         }
69
70         Collections.sort(keys);
71
72         for (Iterator JavaDoc i = keys.iterator(); i.hasNext();) {
73             String JavaDoc key = (String JavaDoc) i.next();
74             String JavaDoc value = null;
75             Object JavaDoc valueObject = parameters.get(key);
76             if (valueObject instanceof String JavaDoc) {
77                 value = ((String JavaDoc) valueObject).trim();
78             } else if (valueObject instanceof String JavaDoc[]) {
79                 String JavaDoc[] values = (String JavaDoc[]) 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     /**
100      * Ctor.
101      * Restores the wrapper parameters from an XML element.
102      * @param parent The parent of the task wrapper element.
103      * @param helper The namespace helper of the document.
104      */

105     public DefaultTaskWrapper(NamespaceHelper helper, Element JavaDoc parent) {
106         log.debug("Creating");
107         restore(helper, parent);
108     }
109
110     /**
111      * Initializes the task wrapper.
112      * @param taskId The task ID.
113      * @param publication The publication.
114      * @param webappUrl The webapp URL.
115      * @param parameters The task parameters.
116      * @throws ExecutionException when the task ID is null.
117      */

118     protected void initialize(
119         String JavaDoc taskId,
120         Publication publication,
121         String JavaDoc 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     /**
134      * Extracts the task parameters from the given objects.
135      * @param parameters A parameters object.
136      * @param publication A publication.
137      * @param request A request.
138      * @return A parameters object.
139      */

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 JavaDoc e = request.getParameterNames(); e.hasMoreElements();) {
156             String JavaDoc key = (String JavaDoc) e.nextElement();
157             String JavaDoc value = request.getParameter(key);
158             if (value != null) {
159                 taskParameters.setParameter(key, value);
160             }
161         }
162
163         String JavaDoc[] names = parameters.getNames();
164         for (int i = 0; i < names.length; i++) {
165             String JavaDoc name = names[i];
166             String JavaDoc value = parameters.getParameter(name, "");
167             if (value != null) {
168                 taskParameters.setParameter(name, value);
169             }
170         }
171         return taskParameters;
172     }
173
174     /**
175      * Enables workflow transition invocation.
176      * @param eventName The event name.
177      * @param identity The identity that executes the task.
178      * @param roles The roles of the identity.
179      */

180     public void setWorkflowAware(String JavaDoc eventName, Identity identity, Role[] roles) {
181         NamespaceMap workflowParameters =
182             WorkflowInvoker.extractParameters(eventName, identity, roles);
183         getParameterObject().putAll(workflowParameters.getPrefixedMap());
184     }
185
186     /**
187      * Executes the task.
188      * @throws ExecutionException when something went wrong.
189      */

190     public void execute() throws ExecutionException {
191
192         String JavaDoc 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 JavaDoc[] missingKeys = getWrapperParameters().getMissingKeys();
205             String JavaDoc 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 JavaDoc properties = new Properties JavaDoc();
228             properties.putAll(getTaskParameters().getMap());
229             Parameters parameters = Parameters.fromProperties(properties);
230
231             task.parameterize(parameters);
232         } catch (Exception JavaDoc e) {
233             throw new ExecutionException(e);
234         }
235
236         log.debug("-----------------------------------");
237         log.debug(" Triggering workflow");
238         log.debug("-----------------------------------");
239         
240         //FIXME The new workflow is set before the end of the transition because the document id
241
// and so the document are sometimes changing during the transition (ex archiving , ...)
242
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     /**
262      * Returns the task wrapper parameters.
263      * @return A task wrapper parameters object.
264      */

265     public TaskWrapperParameters getWrapperParameters() {
266         return wrapperParameters;
267     }
268
269     /**
270      * Returns the task parameters.
271      * @return A task parameters object.
272      */

273     public TaskParameters getTaskParameters() {
274         return taskParameters;
275     }
276
277     protected static final String JavaDoc ELEMENT_TASK = "task";
278     protected static final String JavaDoc ELEMENT_PARAMETER = "parameter";
279     protected static final String JavaDoc ATTRIBUTE_NAME = "name";
280     protected static final String JavaDoc ATTRIBUTE_VALUE = "value";
281
282     /**
283      * Saves the wrapper parameters to an XML element.
284      * @param helper The namespace helper of the document.
285      * @return An XML element.
286      */

287     public Element JavaDoc save(NamespaceHelper helper) {
288         org.w3c.dom.Document JavaDoc document = helper.getDocument();
289         NamespaceHelper taskHelper =
290             new NamespaceHelper(Task.NAMESPACE, Task.DEFAULT_PREFIX, document);
291         Element JavaDoc element = taskHelper.createElement(ELEMENT_TASK);
292         
293         List JavaDoc keys = new ArrayList JavaDoc(getParameters().keySet());
294         Collections.sort(keys);
295
296         for (Iterator JavaDoc i = keys.iterator(); i.hasNext();) {
297             String JavaDoc key = (String JavaDoc) i.next();
298             Element JavaDoc parameterElement = taskHelper.createElement(ELEMENT_PARAMETER);
299             parameterElement.setAttribute(ATTRIBUTE_NAME, key);
300             parameterElement.setAttribute(ATTRIBUTE_VALUE, (String JavaDoc) getParameters().get(key));
301             element.appendChild(parameterElement);
302         }
303
304         return element;
305     }
306
307     /**
308      * Restores the wrapper parameters from an XML element.
309      * @param parent The parent of the task wrapper element.
310      * @param helper The namespace helper of the document.
311      */

312     public void restore(NamespaceHelper helper, Element JavaDoc parent) {
313         org.w3c.dom.Document JavaDoc document = helper.getDocument();
314         NamespaceHelper taskHelper =
315             new NamespaceHelper(Task.NAMESPACE, Task.DEFAULT_PREFIX, document);
316         Element JavaDoc taskElement = taskHelper.getFirstChild(parent, ELEMENT_TASK);
317         Element JavaDoc[] parameterElements = taskHelper.getChildren(taskElement, ELEMENT_PARAMETER);
318         for (int i = 0; i < parameterElements.length; i++) {
319             String JavaDoc key = parameterElements[i].getAttribute(ATTRIBUTE_NAME);
320             String JavaDoc value = parameterElements[i].getAttribute(ATTRIBUTE_VALUE);
321             getParameterObject().put(key, value);
322         }
323     }
324
325     /**
326      * Returns all prefixed parameters.
327      * @return A map.
328      */

329     public Map JavaDoc getParameters() {
330         return Collections.unmodifiableMap(parameters);
331     }
332
333     /**
334      * Returns all prefixed parameters.
335      * @return A map.
336      */

337     protected Map JavaDoc getParameterObject() {
338         return parameters;
339     }
340
341     /**
342      * Sets the notification parameters.
343      * @param notificationParameters The notification parameters.
344      */

345     protected void setNotifying(NamespaceMap notificationParameters) {
346         log.info("Enabling notification");
347         getParameterObject().putAll(notificationParameters.getPrefixedMap());
348     }
349
350 }
351
Popular Tags