KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > invokers > base > BaseInvoker


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library 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. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.invokers.base;
21
22 import org.apache.commons.beanutils.MethodUtils;
23 import org.apache.commons.beanutils.PropertyUtils;
24 import org.apache.commons.lang.StringUtils;
25
26 import za.co.csir.icomtek.workflow.interfaces.WorkflowContext;
27 import za.co.csir.icomtek.workflow.interfaces.WorkflowModuleInvoker;
28 import za.org.coefficient.interfaces.Invoker;
29 import za.org.coefficient.core.Constants;
30
31 import za.org.coefficient.authentication.Role;
32 import za.org.coefficient.exception.ConfigurationException;
33 import za.org.coefficient.interfaces.CoefficientContext;
34 import za.org.coefficient.interfaces.ModuleLocal;
35 import za.org.coefficient.interfaces.ThemeLocalIf;
36 import za.org.coefficient.util.ejb.SecurityUtil;
37
38
39 /**
40  * This is an abstract base class that provides most of the funtionality
41  * an invoker requires. This allows us to invoke methods on
42  * modules, services, and themes without specifiying the methods that know
43  * about the environment.
44  *
45  * @version $Revision: 1.6 $ $Date: 2004/11/09 13:52:11 $
46  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
47  *
48  */

49 public abstract class BaseInvoker implements Invoker, WorkflowModuleInvoker {
50     //~ Static fields/initializers =============================================
51

52     private static final String JavaDoc CREATE = "create";
53     private static final String JavaDoc LOCAL = "Local";
54
55     //~ Methods ================================================================
56

57     /**
58      * This method will look for a request parameter named module and
59      * one named op and will try to fire that operation on the module.
60      */

61     public Object JavaDoc invoke(CoefficientContext ctx) throws Exception JavaDoc {
62         // Fire the module
63
String JavaDoc module = ctx.getParameter("module");
64         String JavaDoc operation = ctx.getParameter("op");
65         if (module == null || module.trim().equals("")) {
66             if(ctx.getCurrentUser() == null) {
67                 module = Constants.WELCOME_MODULE;
68             } else {
69                 // Default to the MyPage
70
module = "MyData";
71             }
72         }
73
74         return invokeOpOnModule(module, operation, ctx);
75     }
76
77     public Object JavaDoc invokeGetterOnModule(String JavaDoc module, String JavaDoc method)
78         throws Exception JavaDoc {
79         if ((method == null) || (module == null)) {
80             throw new ConfigurationException(
81                 "module and method must not be null");
82         }
83         Object JavaDoc retVal = null;
84
85         // Find the module
86
ModuleLocal mod = getModule(module);
87         try {
88             if (mod != null) {
89                 // invoke it
90
retVal = PropertyUtils.getProperty(mod, method);
91             }
92         } catch (NoSuchMethodException JavaDoc nsme) {
93             nsme.printStackTrace();
94         } catch (IllegalAccessException JavaDoc iae) {
95             iae.printStackTrace();
96         }
97
98         return retVal;
99     }
100
101     public Object JavaDoc invokeMethodOnModule(String JavaDoc module, String JavaDoc method,
102         Object JavaDoc[] paramVals) throws Exception JavaDoc {
103         if ((method == null) || (module == null)) {
104             throw new ConfigurationException(
105                 "module and method must not be null");
106         }
107         Object JavaDoc retVal = null;
108
109         // Find the module
110
ModuleLocal mod = getModule(module);
111         try {
112             if (mod != null) {
113                 // invoke it
114
retVal = MethodUtils.invokeMethod(mod, method, paramVals);
115             }
116         } catch (NoSuchMethodException JavaDoc nsme) {
117             nsme.printStackTrace();
118         } catch (IllegalAccessException JavaDoc iae) {
119             iae.printStackTrace();
120         }
121
122         return retVal;
123     }
124
125     public Object JavaDoc invokeMethodOnService(String JavaDoc serviceName, String JavaDoc method,
126         Object JavaDoc[] paramVals) throws Exception JavaDoc {
127         if ((method == null) || (serviceName == null)) {
128             throw new ConfigurationException(
129                 "serviceName and method must not be null");
130         }
131         Object JavaDoc retVal = null;
132
133         // Find the service
134
Object JavaDoc service = getService(serviceName);
135         try {
136             if (service != null) {
137                 // invoke it
138
retVal = MethodUtils.invokeMethod(service, method, paramVals);
139             }
140         } catch (NoSuchMethodException JavaDoc nsme) {
141             nsme.printStackTrace();
142         } catch (IllegalAccessException JavaDoc iae) {
143             iae.printStackTrace();
144         }
145
146         return retVal;
147     }
148
149     /**
150      * This method will invoke the named operation on the named module
151      * with the given context as a parameter
152      */

153     public Object JavaDoc invokeOpOnModule(String JavaDoc module, String JavaDoc operation,
154         CoefficientContext ctx) throws Exception JavaDoc {
155         Object JavaDoc retVal = null;
156
157         // Fire the module
158
ModuleLocal mod = getModule(module);
159         try {
160             if (mod != null) {
161
162                 if(mod.isProjectRequired() && ctx.getProject() == null) {
163                     ctx.setError("You must select a project to perform this operation");
164                 } else {
165                     // first thing to do is always set the context
166
PropertyUtils.setProperty(mod, "coefficientContext", ctx);
167                     if (operation == null) {
168                         // Get the "main" method from the module and invoke it
169
operation =
170                             (String JavaDoc) PropertyUtils.getProperty(mod, "mainMethod");
171                     }
172                     
173                     // perform the security check
174
Role role =
175                         SecurityUtil.getHighestRoleForUser(ctx.getCurrentUser(),
176                                                            ctx.getProject());
177                     String JavaDoc err = mod.canExecuteForRole(ctx, operation, role);
178                     if (err == null) {
179                         // invoke the operation on the module
180
retVal = MethodUtils.invokeMethod(mod, operation, ctx);
181                     } else {
182                         ctx.setError(err);
183                     }
184                 }
185             } else {
186                 ctx.setError("<b>No such module exists!</b>");
187             }
188         } catch (NoSuchMethodException JavaDoc nsme) {
189             nsme.printStackTrace();
190             ctx.setError("<b>No such operation exists on module " + module
191                 + "!</b>");
192         } catch (IllegalAccessException JavaDoc iae) {
193             iae.printStackTrace();
194             ctx.setError("<b>No such operation exists on module " + module
195                 + "!</b>");
196         }
197
198         if(ctx.isError()) {
199             retVal = ctx;
200         }
201         return retVal;
202     }
203
204     /**
205      * NOTE: This is bad and is only done to give the workflow some
206      * autonomy
207      * This method will invoke the named operation on the named module
208      * with the given a workflow context which is a CoefficientContext
209      */

210     public Object JavaDoc invokeOpOnModule(String JavaDoc module, String JavaDoc operation,
211         WorkflowContext ctx) throws Exception JavaDoc {
212         if (ctx instanceof CoefficientContext) {
213             CoefficientContext pctx = (CoefficientContext) ctx;
214
215             return invokeOpOnModule(module, operation, pctx);
216         } else {
217             return null;
218         }
219     }
220
221     public Object JavaDoc invokeMethodOnTheme(String JavaDoc theme, String JavaDoc method,
222                                       Object JavaDoc[] paramVals) throws Exception JavaDoc {
223         if ((theme == null) || (method == null)) {
224             throw new ConfigurationException("theme and method must not be null");
225         }
226         Object JavaDoc retVal = null;
227
228         // Find the module
229
ThemeLocalIf themeIf = getTheme(theme);
230         try {
231             if (themeIf != null) {
232                 // invoke it
233
retVal = MethodUtils.invokeMethod(themeIf, method, paramVals);
234             }
235         } catch (NoSuchMethodException JavaDoc nsme) {
236             nsme.printStackTrace();
237         } catch (IllegalAccessException JavaDoc iae) {
238             iae.printStackTrace();
239         }
240         return retVal;
241     }
242
243     public abstract Object JavaDoc getService(String JavaDoc serviceName) throws Exception JavaDoc;
244
245     protected abstract ThemeLocalIf getTheme(String JavaDoc theme) throws Exception JavaDoc;
246
247     protected abstract ModuleLocal getModule(String JavaDoc module) throws Exception JavaDoc;
248
249 }
250
Popular Tags