001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package za.org.coefficient.invokers.base;
021:
022: import org.apache.commons.beanutils.MethodUtils;
023: import org.apache.commons.beanutils.PropertyUtils;
024: import org.apache.commons.lang.StringUtils;
025:
026: import za.co.csir.icomtek.workflow.interfaces.WorkflowContext;
027: import za.co.csir.icomtek.workflow.interfaces.WorkflowModuleInvoker;
028: import za.org.coefficient.interfaces.Invoker;
029: import za.org.coefficient.core.Constants;
030:
031: import za.org.coefficient.authentication.Role;
032: import za.org.coefficient.exception.ConfigurationException;
033: import za.org.coefficient.interfaces.CoefficientContext;
034: import za.org.coefficient.interfaces.ModuleLocal;
035: import za.org.coefficient.interfaces.ThemeLocalIf;
036: import za.org.coefficient.util.ejb.SecurityUtil;
037:
038: /**
039: * This is an abstract base class that provides most of the funtionality
040: * an invoker requires. This allows us to invoke methods on
041: * modules, services, and themes without specifiying the methods that know
042: * about the environment.
043: *
044: * @version $Revision: 1.6 $ $Date: 2004/11/09 13:52:11 $
045: * @author <a href="mailto:detkin@csir.co.za">Dylan Etkin</a>
046: *
047: */
048: public abstract class BaseInvoker implements Invoker,
049: WorkflowModuleInvoker {
050: //~ Static fields/initializers =============================================
051:
052: private static final String CREATE = "create";
053: private static final String LOCAL = "Local";
054:
055: //~ Methods ================================================================
056:
057: /**
058: * This method will look for a request parameter named module and
059: * one named op and will try to fire that operation on the module.
060: */
061: public Object invoke(CoefficientContext ctx) throws Exception {
062: // Fire the module
063: String module = ctx.getParameter("module");
064: String operation = ctx.getParameter("op");
065: if (module == null || module.trim().equals("")) {
066: if (ctx.getCurrentUser() == null) {
067: module = Constants.WELCOME_MODULE;
068: } else {
069: // Default to the MyPage
070: module = "MyData";
071: }
072: }
073:
074: return invokeOpOnModule(module, operation, ctx);
075: }
076:
077: public Object invokeGetterOnModule(String module, String method)
078: throws Exception {
079: if ((method == null) || (module == null)) {
080: throw new ConfigurationException(
081: "module and method must not be null");
082: }
083: Object retVal = null;
084:
085: // Find the module
086: ModuleLocal mod = getModule(module);
087: try {
088: if (mod != null) {
089: // invoke it
090: retVal = PropertyUtils.getProperty(mod, method);
091: }
092: } catch (NoSuchMethodException nsme) {
093: nsme.printStackTrace();
094: } catch (IllegalAccessException iae) {
095: iae.printStackTrace();
096: }
097:
098: return retVal;
099: }
100:
101: public Object invokeMethodOnModule(String module, String method,
102: Object[] paramVals) throws Exception {
103: if ((method == null) || (module == null)) {
104: throw new ConfigurationException(
105: "module and method must not be null");
106: }
107: Object 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,
115: paramVals);
116: }
117: } catch (NoSuchMethodException nsme) {
118: nsme.printStackTrace();
119: } catch (IllegalAccessException iae) {
120: iae.printStackTrace();
121: }
122:
123: return retVal;
124: }
125:
126: public Object invokeMethodOnService(String serviceName,
127: String method, Object[] paramVals) throws Exception {
128: if ((method == null) || (serviceName == null)) {
129: throw new ConfigurationException(
130: "serviceName and method must not be null");
131: }
132: Object retVal = null;
133:
134: // Find the service
135: Object service = getService(serviceName);
136: try {
137: if (service != null) {
138: // invoke it
139: retVal = MethodUtils.invokeMethod(service, method,
140: paramVals);
141: }
142: } catch (NoSuchMethodException nsme) {
143: nsme.printStackTrace();
144: } catch (IllegalAccessException iae) {
145: iae.printStackTrace();
146: }
147:
148: return retVal;
149: }
150:
151: /**
152: * This method will invoke the named operation on the named module
153: * with the given context as a parameter
154: */
155: public Object invokeOpOnModule(String module, String operation,
156: CoefficientContext ctx) throws Exception {
157: Object retVal = null;
158:
159: // Fire the module
160: ModuleLocal mod = getModule(module);
161: try {
162: if (mod != null) {
163:
164: if (mod.isProjectRequired() && ctx.getProject() == null) {
165: ctx
166: .setError("You must select a project to perform this operation");
167: } else {
168: // first thing to do is always set the context
169: PropertyUtils.setProperty(mod,
170: "coefficientContext", ctx);
171: if (operation == null) {
172: // Get the "main" method from the module and invoke it
173: operation = (String) PropertyUtils.getProperty(
174: mod, "mainMethod");
175: }
176:
177: // perform the security check
178: Role role = SecurityUtil.getHighestRoleForUser(ctx
179: .getCurrentUser(), ctx.getProject());
180: String err = mod.canExecuteForRole(ctx, operation,
181: role);
182: if (err == null) {
183: // invoke the operation on the module
184: retVal = MethodUtils.invokeMethod(mod,
185: operation, ctx);
186: } else {
187: ctx.setError(err);
188: }
189: }
190: } else {
191: ctx.setError("<b>No such module exists!</b>");
192: }
193: } catch (NoSuchMethodException nsme) {
194: nsme.printStackTrace();
195: ctx.setError("<b>No such operation exists on module "
196: + module + "!</b>");
197: } catch (IllegalAccessException iae) {
198: iae.printStackTrace();
199: ctx.setError("<b>No such operation exists on module "
200: + module + "!</b>");
201: }
202:
203: if (ctx.isError()) {
204: retVal = ctx;
205: }
206: return retVal;
207: }
208:
209: /**
210: * NOTE: This is bad and is only done to give the workflow some
211: * autonomy
212: * This method will invoke the named operation on the named module
213: * with the given a workflow context which is a CoefficientContext
214: */
215: public Object invokeOpOnModule(String module, String operation,
216: WorkflowContext ctx) throws Exception {
217: if (ctx instanceof CoefficientContext) {
218: CoefficientContext pctx = (CoefficientContext) ctx;
219:
220: return invokeOpOnModule(module, operation, pctx);
221: } else {
222: return null;
223: }
224: }
225:
226: public Object invokeMethodOnTheme(String theme, String method,
227: Object[] paramVals) throws Exception {
228: if ((theme == null) || (method == null)) {
229: throw new ConfigurationException(
230: "theme and method must not be null");
231: }
232: Object retVal = null;
233:
234: // Find the module
235: ThemeLocalIf themeIf = getTheme(theme);
236: try {
237: if (themeIf != null) {
238: // invoke it
239: retVal = MethodUtils.invokeMethod(themeIf, method,
240: paramVals);
241: }
242: } catch (NoSuchMethodException nsme) {
243: nsme.printStackTrace();
244: } catch (IllegalAccessException iae) {
245: iae.printStackTrace();
246: }
247: return retVal;
248: }
249:
250: public abstract Object getService(String serviceName)
251: throws Exception;
252:
253: protected abstract ThemeLocalIf getTheme(String theme)
254: throws Exception;
255:
256: protected abstract ModuleLocal getModule(String module)
257: throws Exception;
258:
259: }
|