01: /*
02: * Coefficient - facilitates project based collaboration
03: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
04: * PO Box 395
05: * Pretoria 0001, RSA
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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.webapp;
21:
22: import org.apache.commons.lang.StringUtils;
23:
24: import za.org.coefficient.invokers.base.BaseInvoker;
25: import za.org.coefficient.exception.ConfigurationException;
26: import za.org.coefficient.interfaces.ModuleLocal;
27: import za.org.coefficient.interfaces.ThemeLocalIf;
28:
29: /**
30: * This is an implementation of invoker. This allows us to invoke methods on
31: * modules, services, and themes within a webapp enviroment and to obtain useful
32: * reference to them.
33: *
34: * @version $Revision: 1.2 $ $Date: 2004/11/09 13:52:11 $
35: * @author <a href="mailto:detkin@csir.co.za">Dylan Etkin</a>
36: *
37: */
38: public class WebAppInvoker extends BaseInvoker {
39:
40: //~ Methods ================================================================
41:
42: /**
43: * NOTE: this method can only be used by those in the same class loader
44: * as the requested service. This is because this returns an EJB created
45: * through a local interface, not a remote interface.
46: */
47: public Object getService(String serviceName) throws Exception {
48: Object service = null;
49: if (serviceName != null) {
50: // This is left over from when the service name was lowercase
51: // there is too much html that will need to change to make this
52: // not be here
53: serviceName = StringUtils.capitalise(serviceName);
54: javax.naming.Context initCtx = new javax.naming.InitialContext();
55: javax.naming.Context envCtx = (javax.naming.Context) initCtx
56: .lookup("java:comp/env");
57: javax.naming.InitialContext initialContext = new javax.naming.InitialContext();
58: try {
59: service = envCtx.lookup(serviceName);
60: } catch (Exception e) {
61: e.printStackTrace();
62: throw new ConfigurationException("Requested service, "
63: + serviceName
64: + " is not available on the system!");
65: } finally {
66: initCtx.close();
67: envCtx.close();
68: }
69: }
70:
71: return service;
72: }
73:
74: protected ThemeLocalIf getTheme(String themeName) throws Exception {
75: Object theme = getService(themeName);
76: if (theme instanceof ThemeLocalIf) {
77: return (ThemeLocalIf) theme;
78: } else {
79: throw new ConfigurationException("Requested theme, "
80: + themeName + " is not a valid theme!");
81: }
82: }
83:
84: protected ModuleLocal getModule(String moduleName) throws Exception {
85: Object module = getService(moduleName);
86: if (module instanceof ModuleLocal) {
87: return (ModuleLocal) module;
88: } else {
89: throw new ConfigurationException("Requested module, "
90: + moduleName + " is not a system module!");
91: }
92: }
93:
94: }
|