KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > common > InvokerFactory


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.util.common;
21
22 import org.apache.commons.beanutils.MethodUtils;
23
24 import za.org.coefficient.exception.ConfigurationException;
25 import za.org.coefficient.interfaces.Invoker;
26 import za.org.coefficient.core.Constants;
27
28 /**
29  * This is used to get a handle on an object capable of providing us with
30  * a module invoker
31  */

32 public class InvokerFactory {
33
34     public static boolean RUN_AS_EJB = true;
35
36     private static String JavaDoc INVOKER_LOCAL_EJB_IMPLEMENTATION =
37         "za.org.coefficient.invokers.ejb.EjbInvoker";
38     private static String JavaDoc INVOKER_LOCAL_WEBAPP_IMPLEMENTATION =
39         "za.org.coefficient.invokers.webapp.WebAppInvoker";
40     private static String JavaDoc INVOKER_REMOTE_EJB_IMPLEMENTATION =
41         "za.org.coefficient.invokers.ejb.interfaces.EjbInvokerFacadeHome";
42     private static String JavaDoc INVOKER_REMOTE_EJB_JNDI_NAME =
43         "za/org/coefficient/util/EjbInvokerFacade";
44     private static final String JavaDoc CREATE = "create";
45
46     private static Invoker local_mi = null;
47     private static Object JavaDoc remote_mi = null;
48     private static Object JavaDoc lock = new Object JavaDoc();
49
50     static {
51         init();
52     }
53
54     public static Invoker getInvoker() throws Exception JavaDoc {
55         Class JavaDoc cls = null;
56         try {
57             if(RUN_AS_EJB) {
58                 cls = Class.forName(INVOKER_LOCAL_EJB_IMPLEMENTATION);
59             } else {
60                 cls = Class.forName(INVOKER_LOCAL_WEBAPP_IMPLEMENTATION);
61             }
62         } catch(Exception JavaDoc e) {
63             throw new Exception JavaDoc("The implementation provided to create a Module Invoker is not on the classpath");
64         }
65
66         // don't let anyone change the reference out from under another thread
67
synchronized(lock) {
68
69             // Check to make sure the invoker impl hasn't changed
70
// out from under us
71
if(local_mi == null || !(local_mi.getClass().equals(cls))) {
72                 try {
73                     Object JavaDoc obj = cls.newInstance();
74                     if(obj instanceof Invoker) {
75                         local_mi = (Invoker)obj;
76                         return (Invoker)obj;
77                     } else {
78                         throw new Exception JavaDoc("The implementation provided to create a Module Invoker, " + cls.getName() + " is not of type Invoker");
79                     }
80                 } catch(Exception JavaDoc e) {
81                     throw new Exception JavaDoc("Unable to instantiate: "+ cls.getName());
82                 }
83             } else {
84                 return local_mi;
85             }
86         }
87     }
88
89     /**
90      * This should be used by objects that are separated by a remote boundary.
91      * You should also recognize that the parameters passed into a remote call
92      * will be passed by value and not by reference. In the case of a module
93      * invocation this means that the CoefficientContext that is sent across
94      * and modified will not be modified by reference. Therefore you should set
95      * your reference to the context to the returned value so that all changes
96      * will be propagated back across the remote boundary.
97      */

98     public static Invoker getRemoteInvoker() throws Exception JavaDoc {
99         if(RUN_AS_EJB) {
100             if(remote_mi == null) {
101                 javax.naming.InitialContext JavaDoc initialContext =
102                     new javax.naming.InitialContext JavaDoc();
103                 try {
104                     java.lang.Object JavaDoc objRef =
105                         initialContext.lookup(INVOKER_REMOTE_EJB_JNDI_NAME);
106                     remote_mi = javax.rmi.PortableRemoteObject.narrow(objRef, Class.forName(INVOKER_REMOTE_EJB_IMPLEMENTATION));
107                 } finally {
108                     initialContext.close();
109                 }
110             }
111             Object JavaDoc modInvoker = MethodUtils.invokeMethod(remote_mi, CREATE, null);
112             if(modInvoker != null && modInvoker instanceof Invoker) {
113                 return (Invoker)modInvoker;
114             } else {
115                 throw new ConfigurationException("Unable to create a remote module invoker");
116             }
117         } else {
118             return getInvoker();
119         }
120     }
121
122     private static void init() {
123         try {
124             Class.forName(INVOKER_LOCAL_EJB_IMPLEMENTATION);
125         } catch (ClassNotFoundException JavaDoc cnfe) {
126             RUN_AS_EJB = false;
127         }
128     }
129     
130 }
131
Popular Tags