KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import java.util.logging.Logger JavaDoc;
35 import java.util.logging.Level JavaDoc;
36
37 import javax.ejb.EJBException JavaDoc;
38 import javax.ejb.EJBLocalHome JavaDoc;
39 import com.sun.ejb.Invocation;
40 import com.sun.ejb.InvocationInfo;
41 import com.sun.ejb.Container;
42 import com.sun.ejb.codegen.HomeGenerator;
43 import com.sun.enterprise.deployment.EjbDescriptor;
44 import com.sun.enterprise.deployment.EjbSessionDescriptor;
45 import com.sun.ejb.containers.util.MethodMap;
46 import com.sun.ejb.spi.io.IndirectlySerializable;
47
48 import com.sun.enterprise.util.LocalStringManagerImpl;
49 import com.sun.logging.LogDomains;
50
51 /**
52  * Handler for EJBLocalHome invocations through EJBLocalHome proxy.
53  *
54  * @author Kenneth Saks
55  */

56
57 final class EJBLocalHomeInvocationHandler
58     extends ReadOnlyEJBLocalHomeImpl implements InvocationHandler JavaDoc {
59
60     private static Logger JavaDoc logger =
61         LogDomains.getLogger(LogDomains.EJB_LOGGER);
62
63     private static LocalStringManagerImpl localStrings =
64         new LocalStringManagerImpl(EJBLocalHomeInvocationHandler.class);
65
66     private boolean isStatelessSession_;
67     private boolean isEntity_;
68
69     // Our associated proxy object. Used when a caller needs EJBLocalObject
70
// but only has InvocationHandler.
71
private EJBLocalHome JavaDoc proxy_;
72
73     private Class JavaDoc localHomeIntfClass_;
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 LocalHome proxy
79
// is created.
80
private MethodMap invocationInfoMap_;
81
82     EJBLocalHomeInvocationHandler(EjbDescriptor ejbDescriptor,
83                                   Class JavaDoc localHomeIntf,
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         localHomeIntfClass_ = localHomeIntf;
99
100         // NOTE : Container is not set on super-class until after
101
// constructor is called.
102
}
103
104     public void setProxy(EJBLocalHome JavaDoc proxy) {
105         proxy_ = proxy;
106     }
107
108     protected EJBLocalHome JavaDoc getEJBLocalHome() {
109         return proxy_;
110     }
111
112     /**
113      * Called by EJBLocalHome proxy.
114      */

115     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
116         throws Throwable JavaDoc {
117
118         // NOTE : be careful with "args" parameter. It is null
119
// if method signature has 0 arguments.
120
try {
121         ((BaseContainer) getContainer()).onEnteringContainer();
122
123         Class JavaDoc methodClass = method.getDeclaringClass();
124
125         if( methodClass == java.lang.Object JavaDoc.class ) {
126             return InvocationHandlerUtil.invokeJavaObjectMethod
127                 (this, method, args);
128         } else if( methodClass == IndirectlySerializable.class ) {
129             return this.getSerializableObjectFactory();
130         } else if( methodClass == ReadOnlyEJBLocalHome.class ) {
131             // ReadOnlyBeanLocalNotifier getReadOnlyBeanLocalNotifier();
132
return super.getReadOnlyBeanLocalNotifier();
133         }
134
135         // Use optimized version of get that takes param count as an argument.
136
InvocationInfo invInfo = (InvocationInfo)
137             invocationInfoMap_.get(method, ((args != null) ? args.length : 0) );
138             
139         if( invInfo == null ) {
140             throw new IllegalStateException JavaDoc("Unknown method :" + method);
141         }
142
143         if( (methodClass == javax.ejb.EJBLocalHome JavaDoc.class) ||
144             invInfo.ejbIntfOverride ) {
145             // There is only one method on javax.ejb.EJBLocalHome
146
super.remove(args[0]);
147             return null;
148
149         } else if(methodClass == GenericEJBLocalHome.class) {
150
151             // This is a creation request through the EJB 3.0
152
// client view, so just create a local business object and
153
// return it.
154
EJBLocalObjectImpl localImpl =
155                 createEJBLocalBusinessObjectImpl();
156             return localImpl.getClientObject((String JavaDoc) args[0]);
157             
158         }
159
160         // Process finder, create method, or home method.
161
EJBLocalObjectImpl localObjectImpl = null;
162         Object JavaDoc returnValue = null;
163
164         if( !isEntity_ && invInfo.startsWithCreate ) {
165             localObjectImpl = createEJBLocalObjectImpl();
166             returnValue = localObjectImpl.getClientObject();
167         }
168  
169         if( !isStatelessSession_ ) {
170
171             if( invInfo.targetMethod1 == null ) {
172
173                 Object JavaDoc [] params = new Object JavaDoc[]
174                     { invInfo.ejbName, "LocalHome",
175                       invInfo.method.toString() };
176                 String JavaDoc errorMsg = localStrings.getLocalString
177                     ("ejb.bean_class_method_not_found", "", params);
178                 logger.log(Level.SEVERE, "ejb.bean_class_method_not_found",
179                            params);
180                 throw new EJBException JavaDoc(errorMsg);
181             }
182
183             Invocation inv = new Invocation();
184
185             inv.isLocal = true;
186             inv.isHome = true;
187             inv.method = method;
188
189             inv.clientInterface = localHomeIntfClass_;
190
191             // Set cached invocation params. This will save additional lookups
192
// in BaseContainer.
193
inv.transactionAttribute = invInfo.txAttr;
194             inv.securityPermissions = invInfo.securityPermissions;
195             inv.invocationInfo = invInfo;
196
197             if( !isEntity_ && invInfo.startsWithCreate ) {
198                 inv.ejbObject = (EJBLocalRemoteObject) localObjectImpl;
199             }
200
201             try {
202
203                 container.preInvoke(inv);
204
205                 if( invInfo.startsWithCreate ) {
206
207                     Object JavaDoc ejbCreateReturnValue = container.invokeTargetBeanMethod(
208                         invInfo.targetMethod1, inv, inv.ejb, args, null);
209                     if( isEntity_ ) {
210                         container.postCreate(inv, ejbCreateReturnValue);
211                         container.invokeTargetBeanMethod(invInfo.targetMethod2,
212                                             inv, inv.ejb, args, null);
213                     }
214                     if( inv.ejbObject != null ) {
215                         returnValue = ((EJBLocalObjectImpl)inv.ejbObject)
216                             .getClientObject();
217                     }
218                 } else if (invInfo.startsWithFindByPrimaryKey) {
219             EntityContainer entityContainer = (EntityContainer) container;
220             returnValue = entityContainer.invokeFindByPrimaryKey(
221             invInfo.targetMethod1, inv, args);
222                 } else if ( invInfo.startsWithFind ) {
223
224                     Object JavaDoc pKeys = container.invokeTargetBeanMethod(invInfo.targetMethod1,
225                                       inv, inv.ejb, args, null);
226                     returnValue = container.postFind(inv, pKeys, null);
227                 } else {
228
229                     returnValue = container.invokeTargetBeanMethod(invInfo.targetMethod1,
230                                       inv, inv.ejb, args, null);
231
232                 }
233             } catch(InvocationTargetException JavaDoc ite) {
234                 inv.exception = ite.getCause();
235             } catch(Throwable JavaDoc c) {
236                 inv.exception = c;
237             } finally {
238                 container.postInvoke(inv);
239             }
240
241             if (inv.exception != null) {
242                 InvocationHandlerUtil.throwLocalException
243                     (inv.exception, method.getExceptionTypes());
244             }
245         }
246
247         return returnValue;
248         } finally {
249             ((BaseContainer) getContainer()).onLeavingContainer();
250         }
251     }
252 }
253
Popular Tags