KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > invokers > ejb > EjbInvoker


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.ejb;
21
22 import org.apache.commons.beanutils.MethodUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.collections.LRUMap;
25
26 import za.org.coefficient.invokers.base.BaseInvoker;
27 import za.org.coefficient.exception.ConfigurationException;
28 import za.org.coefficient.interfaces.ModuleLocal;
29 import za.org.coefficient.interfaces.ThemeLocalIf;
30
31 import java.util.Collections JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * This is an implementation of invoker. This allows us to invoke methods on
36  * modules, services, and themes within an EJB enviroment and to obtain useful
37  * reference to them.
38  *
39  * @version $Revision: 1.4 $ $Date: 2004/11/09 13:52:11 $
40  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
41  *
42  * @pojo2ejb.class
43  * name="EjbInvokerFacade"
44  * jndi-prefix="za/org/coefficient/util/"
45  * interface-extends="za.co.csir.icomtek.workflow.interfaces.WorkflowModuleInvoker, za.org.coefficient.interfaces.Invoker"
46  *
47  * @web.resource-env-ref
48  * name="za/org/coefficient/util/EjbInvokerFacade"
49  * type="za.org.coefficient.invokers.EjbInvokerFacade"
50  * @web.resource-env-ref
51  * name="EjbInvokerFacade"
52  * type="za.org.coefficient.invokers.EjbInvokerFacade"
53  */

54 public class EjbInvoker extends BaseInvoker {
55     //~ Static fields/initializers =============================================
56

57     //private Map localHomeCache;
58
private static final String JavaDoc CREATE = "create";
59     private static final String JavaDoc LOCAL = "Local";
60
61     public EjbInvoker() {
62         //localHomeCache = Collections.synchronizedMap(new LRUMap(100));
63
}
64
65     //~ Methods ================================================================
66

67
68     /**
69      * NOTE: this method can only be used by those in the same class loader
70      * as the requested service. This is because this returns an EJB created
71      * through a local interface, not a remote interface.
72      */

73     public Object JavaDoc getService(String JavaDoc serviceName) throws Exception JavaDoc {
74         Object JavaDoc serviceVal = null;
75         boolean serviceFound = true;
76         if (serviceName != null) {
77             // This is left over from when the service name was lowercase
78
// there is too much html that will need to change to make this
79
// not be here
80
serviceName = StringUtils.capitalise(serviceName);
81             try {
82                 Object JavaDoc localHome = null;
83                 //localHome = localHomeCache.get(serviceName);
84
if(localHome == null) {
85                     javax.naming.InitialContext JavaDoc initialContext =
86                         new javax.naming.InitialContext JavaDoc();
87                     try {
88                         localHome =(javax.ejb.EJBLocalHome JavaDoc)
89                             initialContext.lookup(serviceName + LOCAL);
90                         //localHomeCache.put(serviceName, localHome);
91
} catch (Exception JavaDoc e) {
92                         e.printStackTrace();
93                         serviceFound = false;
94                     } finally {
95                         initialContext.close();
96                     }
97                 }
98
99                 if (serviceFound) {
100                     serviceVal = MethodUtils
101                         .invokeMethod(localHome, CREATE, null);
102                     if (serviceVal == null) {
103                         throw new ConfigurationException("Requested service, "
104                             + serviceName + " is not available on the system!");
105                     }
106                 }
107             } catch (NoSuchMethodException JavaDoc nsme) {
108                 nsme.printStackTrace();
109             } catch (IllegalAccessException JavaDoc iae) {
110                 iae.printStackTrace();
111             }
112         }
113
114         return serviceVal;
115     }
116
117     protected ThemeLocalIf getTheme(String JavaDoc theme) throws Exception JavaDoc {
118         ThemeLocalIf themeIf = null;
119         Object JavaDoc themeVal = getService(theme);
120         if (!(themeVal instanceof ThemeLocalIf)){
121             throw new ConfigurationException("Requested theme, "
122                                              + theme + " is not a valid theme!");
123         } else {
124             themeIf = (ThemeLocalIf) themeVal;
125         }
126         return themeIf;
127     }
128
129     protected ModuleLocal getModule(String JavaDoc module) throws Exception JavaDoc {
130         ModuleLocal mod = null;
131         Object JavaDoc modVal = getService(module);
132         if (!(modVal instanceof ModuleLocal)) {
133             throw new ConfigurationException("Requested module, "
134                                              + module +
135                                              " is not a system module!");
136         } else {
137             mod = (ModuleLocal) modVal;
138         }
139         return mod;
140     }
141
142 }
143
144
Popular Tags