KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > EJBHomeInvocationHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.ejb.containers;
24
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Proxy JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 import java.util.logging.Logger JavaDoc;
31 import java.util.logging.Level JavaDoc;
32
33 import java.util.Map JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import java.rmi.RemoteException JavaDoc;
38 import javax.rmi.CORBA.Tie JavaDoc;
39 import javax.ejb.EJBException JavaDoc;
40 import javax.ejb.EJBHome JavaDoc;
41 import com.sun.ejb.Invocation;
42 import com.sun.ejb.InvocationInfo;
43 import com.sun.ejb.Container;
44 import com.sun.ejb.containers.util.MethodMap;
45 import com.sun.enterprise.deployment.EjbDescriptor;
46 import com.sun.enterprise.deployment.EjbSessionDescriptor;
47 import com.sun.enterprise.util.LocalStringManagerImpl;
48 import com.sun.enterprise.util.Utility;
49
50 import com.sun.logging.LogDomains;
51
52 /**
53  * Handler for EJBHome invocations through EJBHome proxy.
54  *
55  * @author Kenneth Saks
56  */

57
58 final class EJBHomeInvocationHandler
59     extends ReadOnlyEJBHomeImpl implements InvocationHandler JavaDoc {
60
61     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.EJB_LOGGER);
62
63     private static LocalStringManagerImpl localStrings =
64         new LocalStringManagerImpl(EJBHomeInvocationHandler.class);
65
66     private boolean isStatelessSession_;
67     private boolean isEntity_;
68
69     // Our associated proxy object. Used when a caller needs EJBHome
70
// but only has InvocationHandler.
71
private EJBHome JavaDoc proxy_;
72
73     private Class JavaDoc homeIntfClass_;
74
75     // Cache reference to invocation info. There is one of these per
76
// container. It's populated during container initialization and
77
// passed in when the InvocationHandler is created. This avoids the
78
// overhead of building the method info each time a Home proxy
79
// is created.
80
private MethodMap invocationInfoMap_;
81
82     EJBHomeInvocationHandler(EjbDescriptor ejbDescriptor,
83                              Class JavaDoc homeIntfClass,
84                              MethodMap invocationInfoMap)
85         throws Exception JavaDoc {
86
87         if( ejbDescriptor instanceof EjbSessionDescriptor ) {
88             isEntity_ = false;
89             isStatelessSession_ =
90                 ((EjbSessionDescriptor)ejbDescriptor).isStateless();
91         } else {
92             isStatelessSession_ = false;
93             isEntity_ = true;
94         }
95
96         invocationInfoMap_ = invocationInfoMap;
97
98         homeIntfClass_ = homeIntfClass;
99
100         // NOTE : Container is not set on super-class until after
101
// constructor is called.
102
}
103
104     public void setProxy(EJBHome JavaDoc proxy) {
105         proxy_ = proxy;
106     }
107
108     protected EJBHome JavaDoc getEJBHome() {
109         return proxy_;
110     }
111
112     /**
113      * Called by EJBHome proxy.
114      */

115     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
116         throws Throwable JavaDoc {
117
118         ClassLoader JavaDoc originalClassLoader = null;
119
120         // NOTE : be careful with "args" parameter. It is null
121
// if method signature has 0 arguments.
122
try {
123             ((BaseContainer) getContainer()).onEnteringContainer();
124
125             // In some cases(e.g. if the Home/Remote interfaces appear in
126
// a parent of the application's classloader),
127
// ServantLocator.preinvoke() will not be called by the
128
// ORB, and therefore BaseContainer.externalPreInvoke will not have
129
// been called for this invocation. In those cases we need to set
130
// the context classloader to the application's classloader before
131
// proceeding. Otherwise, the context classloader could still
132
// reflect the caller's class loader.
133

134             if( Thread.currentThread().getContextClassLoader() !=
135                 getContainer().getClassLoader() ) {
136                 originalClassLoader = Utility.setContextClassLoader
137                     (getContainer().getClassLoader());
138             }
139
140             Class JavaDoc methodClass = method.getDeclaringClass();
141
142             if( methodClass == java.lang.Object JavaDoc.class ) {
143                 return InvocationHandlerUtil.invokeJavaObjectMethod
144                     (this, method, args);
145             } else if( methodClass == ReadOnlyEJBHome.class ) {
146                 if( method.getName().equals("_refresh_All") ) {
147                     super._refresh_All();
148                 } else {
149                     super._refresh_com_sun_ejb_containers_read_only_bean_
150                         (args[0]);
151                 }
152                 return null;
153             }
154
155             // Use optimized version of get that takes param count as an
156
// argument.
157
InvocationInfo invInfo = (InvocationInfo)
158                 invocationInfoMap_.get(method,
159                                        ((args != null) ? args.length : 0) );
160             
161             if( invInfo == null ) {
162
163                 throw new RemoteException JavaDoc("Unknown Home interface method :"
164                                           + method);
165
166             } else if( (methodClass == javax.ejb.EJBHome JavaDoc.class) ||
167                        invInfo.ejbIntfOverride ) {
168                 
169                 return invokeEJBHomeMethod(method.getName(), args);
170                 
171             } else if( GenericEJBHome.class.isAssignableFrom(methodClass) ) {
172                 
173                 // This is an internal creation request through the EJB 3.0
174
// client view, so just create an business object and return it
175
EJBObjectImpl busObjectImpl = createRemoteBusinessObjectImpl();
176                 return busObjectImpl.getStub((String JavaDoc) args[0]);
177                 
178             }
179             
180             // Process finder, create method, or home method.
181
EJBObjectImpl ejbObjectImpl = null;
182             Object JavaDoc returnValue = null;
183             
184             if( !isEntity_ && invInfo.startsWithCreate ) {
185                 ejbObjectImpl = createEJBObjectImpl();
186                 returnValue = ejbObjectImpl.getStub();
187             }
188             
189             if( !isStatelessSession_ ) {
190                 
191                 if( invInfo.targetMethod1 == null ) {
192                     
193                     String JavaDoc errorMsg = localStrings.getLocalString
194                         ("ejb.bean_class_method_not_found", "", new Object JavaDoc[]
195                             { invInfo.ejbName, "Home",
196                               invInfo.method.toString() });
197                     logger.log(Level.SEVERE, errorMsg);
198                     throw new RemoteException JavaDoc(errorMsg);
199                     
200                 }
201                 
202                 Invocation inv = new Invocation();
203                 
204                 inv.isLocal = false;
205                 inv.method = method;
206                 inv.isHome = true;
207                 
208                 inv.clientInterface = homeIntfClass_;
209
210                 // Set cached invocation params. This will save
211
// additional lookups in BaseContainer.
212

213                 inv.transactionAttribute = invInfo.txAttr;
214                 inv.securityPermissions = invInfo.securityPermissions;
215                 inv.invocationInfo = invInfo;
216                 
217                 if( !isEntity_ && invInfo.startsWithCreate ) {
218                     inv.ejbObject = (EJBLocalRemoteObject) ejbObjectImpl;
219                 }
220                 
221                 BaseContainer container = (BaseContainer) getContainer();
222                 
223                 try {
224                     
225                     container.preInvoke(inv);
226                     
227                     if( invInfo.startsWithCreate ) {
228                         
229                         Object JavaDoc ejbCreateReturnValue = container.
230                             invokeTargetBeanMethod(invInfo.targetMethod1,
231                                                    inv, inv.ejb, args, null);
232                         if( isEntity_ ) {
233                             container.postCreate(inv, ejbCreateReturnValue);
234                             container.invokeTargetBeanMethod
235                                 (invInfo.targetMethod2,
236                                  inv, inv.ejb, args, null);
237                         }
238                         if( inv.ejbObject != null ) {
239                             returnValue = ((EJBObjectImpl)inv.ejbObject).
240                                 getStub();
241                         }
242                         
243                     } else if (invInfo.startsWithFindByPrimaryKey) {
244                         EntityContainer entityContainer = (EntityContainer)
245                             container;
246                         returnValue = entityContainer.invokeFindByPrimaryKey
247                             (invInfo.targetMethod1, inv, args);
248                                                                              
249                     } else if ( invInfo.startsWithFind ) {
250                         
251                         Object JavaDoc pKeys = container.invokeTargetBeanMethod
252                             (invInfo.targetMethod1, inv, inv.ejb, args, null);
253                                                                         
254                         returnValue = container.postFind(inv, pKeys, null);
255                         
256                     } else {
257                         
258                         returnValue = container.invokeTargetBeanMethod
259                             (invInfo.targetMethod1, inv, inv.ejb, args, null);
260                     }
261                 } catch(InvocationTargetException JavaDoc ite) {
262                     inv.exception = ite.getCause();
263                 } catch(Throwable JavaDoc c) {
264                     inv.exception = c;
265                 } finally {
266                     container.postInvoke(inv);
267                 }
268                 
269                 if (inv.exception != null) {
270                     InvocationHandlerUtil.throwRemoteException
271                         (inv.exception, method.getExceptionTypes());
272                 }
273             }
274             
275             return returnValue;
276         } finally {
277             
278             if( originalClassLoader != null ) {
279                 Utility.setContextClassLoader(originalClassLoader);
280             }
281
282             ((BaseContainer) getContainer()).onLeavingContainer();
283         }
284     }
285
286     private Object JavaDoc invokeEJBHomeMethod(String JavaDoc methodName, Object JavaDoc[] args)
287         throws Exception JavaDoc
288     {
289         // Return value is null if target method returns void.
290
Object JavaDoc returnValue = null;
291
292         // NOTE : Might be worth optimizing this method check if it
293
// turns out to be a bottleneck. I don't think these methods
294
// are called with the frequency that this would be an issue,
295
// but it's worth considering.
296

297         if( methodName.equals("getEJBMetaData") ) {
298
299             returnValue = super.getEJBMetaData();
300
301         } else if( methodName.equals("getHomeHandle") ) {
302
303             returnValue = super.getHomeHandle();
304
305         } else if( methodName.equals("remove") ) {
306
307             if( args[0] instanceof javax.ejb.Handle JavaDoc ) {
308                 super.remove((javax.ejb.Handle JavaDoc)args[0]);
309             } else {
310                 super.remove(args[0]);
311             }
312
313         } else {
314
315            throw new RemoteException JavaDoc("unknown EJBHome method = " + methodName);
316
317         }
318
319         return returnValue;
320     }
321 }
322
Popular Tags