KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > CustomClassExecutor


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.cms.util.workflow;
25
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import org.apache.log4j.Logger;
34
35 import webwork.action.ActionContext;
36
37 import com.opensymphony.module.propertyset.PropertySet;
38 import com.opensymphony.workflow.FunctionProvider;
39 import com.opensymphony.workflow.WorkflowContext;
40 import com.opensymphony.workflow.WorkflowException;
41
42
43 /**
44  * Executes a WebWork function and restores the old ActionContext when finished
45  * (but does not provide chaining support yet). The following conversion is done:
46  * <ul>
47  * <li>inputs -> ActionContext#parameters</li>
48  * <li>variables -> ActionContext#session</li>
49  * <li>args -> ActionContext#application</li>
50  * </ul>
51  * <p>
52  *
53  * <ul>
54  * <li><b>action.name</b> - the actionName to ask from the ActionFactory</li>
55  * </ul>
56  */

57 public class CustomClassExecutor implements FunctionProvider
58 {
59     private final static Logger logger = Logger.getLogger(CustomClassExecutor.class.getName());
60
61     public void execute(Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps) throws WorkflowException
62     {
63         logger.info("CustomClassExecutor.execute........");
64         final WorkflowContext wfContext = (WorkflowContext) transientVars.get("context");
65
66         String JavaDoc className = (String JavaDoc) args.get("customClass.name");
67         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) transientVars.get("request");
68         logger.info("className:" + className);
69         
70         Iterator JavaDoc paramsIterator = transientVars.keySet().iterator();
71         while(paramsIterator.hasNext())
72         {
73             String JavaDoc key = (String JavaDoc)paramsIterator.next();
74             logger.info("transientVars key:" + key);
75             Object JavaDoc value = args.get(key);
76             logger.info("transientVars value:" + value);
77         }
78         
79         Map JavaDoc params = new HashMap JavaDoc(transientVars);
80         params.putAll(args);
81         ActionContext.setParameters(Collections.unmodifiableMap(params));
82         
83         CustomWorkflowAction customWorkflowAction = getCustomWorkflowActionWithName(className);
84         if(customWorkflowAction != null)
85             customWorkflowAction.invokeAction(wfContext.getCaller(), request, Collections.unmodifiableMap(params), ps);
86         else
87         {
88             logger.warn("Could not find custom class " + className + ". Is it in the classpath?");
89             throw new WorkflowException("Could not find custom class " + className + ". Is it in the classpath?");
90         }
91     }
92     
93     /**
94      * This method instansiate a new object of the given class
95      */

96     
97     public CustomWorkflowAction getCustomWorkflowActionWithName(String JavaDoc className)
98     {
99         try
100         {
101             Class JavaDoc theClass = null;
102             try
103             {
104                 theClass = Thread.currentThread().getContextClassLoader().loadClass( className );
105             }
106             catch (ClassNotFoundException JavaDoc e)
107             {
108                 theClass = getClass().getClassLoader().loadClass( className );
109             }
110             return (CustomWorkflowAction)theClass.newInstance();
111             
112         }
113         catch (InstantiationException JavaDoc e)
114         {
115             e.printStackTrace();
116         }
117         catch (IllegalAccessException JavaDoc e)
118         {
119             e.printStackTrace();
120         }
121         catch (ClassNotFoundException JavaDoc e)
122         {
123             e.printStackTrace();
124         }
125         
126         return null;
127     }
128 }
Popular Tags