KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > client > EJBInvocationHandler


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: EJBInvocationHandler.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.client;
46
47 import java.io.Serializable JavaDoc;
48 import java.lang.reflect.Method JavaDoc;
49 import java.rmi.NoSuchObjectException JavaDoc;
50 import java.util.HashSet JavaDoc;
51 import java.util.Hashtable JavaDoc;
52 import java.util.Iterator JavaDoc;
53
54 import org.openejb.client.proxy.InvocationHandler;
55
56 /**
57  * TODO: Add comments
58  *
59  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
60  */

61 public abstract class EJBInvocationHandler implements InvocationHandler, Serializable JavaDoc, ResponseCodes, RequestMethods {
62
63     protected static final Method JavaDoc EQUALS = getMethod(Object JavaDoc.class, "equals", null);
64     protected static final Method JavaDoc HASHCODE = getMethod(Object JavaDoc.class, "hashCode", null);
65     protected static final Method JavaDoc TOSTRING = getMethod(Object JavaDoc.class, "toString", null);
66
67     /**
68      * Keeps track of live EJBInvocationHandler in this VM. So that
69      * related handlers can be removed if necessary.
70      *
71      * Currently this registry is only used to track live
72      * EJBInvocationHandlers. The EJBObjectHandlers are tracked to allow
73      * remove() operations and invalidate exceptions to be propagated to
74      * the proper handler instances.
75      *
76      * There are several scenarios where this is useful:
77      * <ul>
78      * <li>
79      * If an EJBHome.remove( )method is invoked, the EJBHomeHandler will
80      * use this registry to notify the EJBObjectHandler associated with
81      * the removed identity that the EJBObjectHandler should invalidate
82      * itself.
83      * <li>
84      * When two EjbObjectProxy handlers are both associated with the same
85      * bean identity and one is removed, the EJBObjectHandler executing
86      * the remove will notify the other EJBObjectHandler objects
87      * associated with the same identity that they are to be invalidated.
88      * <li>
89      * When an EjbObjectProxyHanlder performs an operation that results
90      * in a an InvalidateReferenceException the EJBObjectHandler will use
91      * the registry to ensure that all EJBObjectHandlers associated the
92      * identity are invalidated.
93      */

94     protected static final Hashtable JavaDoc liveHandleRegistry = new Hashtable JavaDoc();
95
96     /**
97      * TODO: Add comments
98      */

99     protected transient boolean inProxyMap = false;
100
101     /**
102      * TODO: Add comments
103      */

104     protected transient boolean isInvalidReference = false;
105
106
107     //-------------------------------------------------------//
108
// All of the following are not serialized as objects.
109
// The data in them is written to the stream in the
110
// writeExternal method. The raw data is read in the
111
// readExternal method, the data is used to create new
112
// instances of the objects.
113
//-------------------------------------------------------//
114

115     /**
116      * TODO: Add comments
117      */

118     protected transient EJBRequest request;
119
120     /**
121      * The EJBMetaDataImpl object of the bean deployment that this
122      * invocation handler represents.
123      */

124     protected transient EJBMetaDataImpl ejb;
125     /**
126      * The ServerMetaData object containing the information needed to
127      * send invokations to the EJB Server.
128      */

129     protected transient ServerMetaData server;
130     /**
131      * The ClientMetaData object containing the information aquired from
132      * the server after authenticating.
133      */

134     protected transient ClientMetaData client;
135
136     /**
137      * The primary key of the bean deployment or null if the deployment
138      * is a bean type that doesn't require a primary key
139      */

140     protected transient Object JavaDoc primaryKey;
141
142     /**
143      * TODO: Add comments
144      */

145     public EJBInvocationHandler(){
146     }
147
148     public EJBInvocationHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client){
149         this.ejb = ejb;
150         this.server = server;
151         this.client = client;
152     }
153
154     public EJBInvocationHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client, Object JavaDoc primaryKey){
155         this(ejb, server, client);
156         this.primaryKey = primaryKey;
157     }
158
159     protected static Method JavaDoc getMethod(Class JavaDoc c, String JavaDoc method, Class JavaDoc[] params){
160         try{
161             return c.getMethod(method, params );
162         }catch(NoSuchMethodException JavaDoc nse){
163             //TODO:3: If this happens, it would cause bizarre problems
164
// Do something to handle it, just in case.
165
}
166         return null;
167     }
168
169     /**
170      * TODO: Add comments
171      *
172      * @param proxy The Proxy object that represents this bean deployment's EJBObject
173      * or EJBHome.
174      * @param method The EJBHome or EJBObject method the caller is invoking.
175      * @param args The parameters to the mehtod being invoked
176      * @return The result of invoking the appropriate method on the bean instance.
177      * @exception Throwable
178      */

179     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc{
180         if (isInvalidReference) throw new NoSuchObjectException JavaDoc("reference is invalid");
181
182         Object JavaDoc returnObj = null;
183         returnObj = _invoke(proxy,method,args);
184         return returnObj;
185     }
186
187     /**
188      * Overridden by subclasses and called by {@link #invoke}.
189      * Subclasses implement the main behavior of calling invoke on the
190      * Container that the bean deployment lives in.
191      *
192      * @param proxy The Proxy subclass that is the bean's EJBObject or EJBHome.
193      * @param method The bean method that the caller is attempting to invoke.
194      * @param args The arguments to the method being invoked.
195      * @return The result of invoking the appropriate method on the bean instance.
196      * @exception Throwable
197      */

198     protected abstract Object JavaDoc _invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc;
199
200     public static void print(String JavaDoc s){
201         //System.out.println();
202
}
203
204     public static void println(String JavaDoc s){
205         //System.out.print(s+'\n');
206
}
207
208     protected EJBResponse request(EJBRequest req) throws Exception JavaDoc {
209         return (EJBResponse) Client.request(req, new EJBResponse(), server);
210     }
211
212     /**
213      * Invalidates this reference so that it can not be used as a proxy
214      * for the bean identity. This method may be called when an
215      * InvalidateReferenceException is thrown by the container or when
216      * the bean identity associated with this proxy is explicitly
217      * removed, by calling one of the remove( ) methods.
218      */

219     protected void invalidateReference(){
220         this.server = null;
221         this.client = null;
222         this.ejb = null;
223         this.inProxyMap = false;
224         this.isInvalidReference = true;
225         this.primaryKey = null;
226     }
227
228     /**
229      * TODO: Add comments
230      *
231      * @param key
232      */

233     protected static void invalidateAllHandlers(Object JavaDoc key){
234
235         HashSet JavaDoc set = (HashSet JavaDoc)liveHandleRegistry.remove( key );
236         if ( set == null ) return;
237
238         synchronized ( set ) {
239             Iterator JavaDoc handlers = set.iterator();
240             while(handlers.hasNext()){
241                 EJBInvocationHandler handler = (EJBInvocationHandler)handlers.next();
242                 handler.invalidateReference();
243             }
244             set.clear();
245         }
246     }
247
248     /**
249      * TODO: Add comments
250      *
251      * @param key
252      * @param handler
253      */

254     protected static void registerHandler(Object JavaDoc key, EJBInvocationHandler handler){
255         HashSet JavaDoc set = (HashSet JavaDoc)liveHandleRegistry.get(key);
256
257         if ( set == null ) {
258             set = new HashSet JavaDoc();
259             liveHandleRegistry.put( key, set );
260         }
261
262         synchronized (set) {
263             set.add(handler);
264         }
265     }
266 }
Popular Tags