KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import java.util.logging.Level JavaDoc;
35
36 import javax.ejb.EJBContext JavaDoc;
37 import javax.ejb.EJBException JavaDoc;
38 import javax.ejb.EJBLocalObject 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.WrapperGenerator;
43 import com.sun.ejb.containers.util.MethodMap;
44 import com.sun.enterprise.util.LocalStringManagerImpl;
45 import com.sun.logging.LogDomains;
46 import com.sun.ejb.spi.io.IndirectlySerializable;
47
48 /**
49  * Handler for EJBLocalObject invocations through EJBLocalObject proxy.
50  *
51  *
52  * @author Kenneth Saks
53  */

54
55 public final class EJBLocalObjectInvocationHandler
56     extends EJBLocalObjectImpl implements InvocationHandler JavaDoc {
57
58     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.EJB_LOGGER);
59
60     private static LocalStringManagerImpl localStrings =
61         new LocalStringManagerImpl(EJBLocalObjectInvocationHandler.class);
62     
63     // Our associated proxy object. Used when a caller needs EJBLocalObject
64
// but only has InvocationHandler.
65
private Object JavaDoc proxy_;
66
67     // Cache reference to invocation info. There is one of these per
68
// container. It's populated during container initialization and
69
// passed in when the InvocationHandler is created. This avoids the
70
// overhead of building the method info each time a LocalObject proxy
71
// is created.
72
private MethodMap invocationInfoMap_;
73
74     private Class JavaDoc localIntf_;
75
76     /**
77      * Constructor used for Local Home view
78      */

79     public EJBLocalObjectInvocationHandler(MethodMap invocationInfoMap,
80                                            Class JavaDoc localIntf)
81         throws Exception JavaDoc {
82
83         invocationInfoMap_ = invocationInfoMap;
84         
85         localIntf_ = localIntf;
86         setIsLocalHomeView(true);
87
88         // NOTE : Container is not set on super-class until after
89
// constructor is called.
90
}
91
92     /**
93      * Constructor used for Local Business view.
94      */

95       
96     public EJBLocalObjectInvocationHandler(MethodMap invocationInfoMap)
97         throws Exception JavaDoc {
98
99         invocationInfoMap_ = invocationInfoMap;
100         
101         setIsLocalHomeView(false);
102
103         // NOTE : Container is not set on super-class until after
104
// constructor is called.
105
}
106
107     public void setProxy(Object JavaDoc proxy) {
108         proxy_ = proxy;
109     }
110
111     public Object JavaDoc getClientObject() {
112         return proxy_;
113     }
114
115     /**
116      * This entry point is only used for Local Home view.
117      */

118     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
119         throws Throwable JavaDoc {
120
121         return invoke(localIntf_, method, args);
122     }
123
124     Object JavaDoc invoke(Class JavaDoc clientInterface, Method JavaDoc method, Object JavaDoc[] args)
125         throws Throwable JavaDoc {
126
127         // NOTE : be careful with "args" parameter. It is null
128
// if method signature has 0 arguments.
129
try {
130         container.onEnteringContainer();
131         Class JavaDoc methodClass = method.getDeclaringClass();
132         if( methodClass == java.lang.Object JavaDoc.class ) {
133             return InvocationHandlerUtil.invokeJavaObjectMethod(this, method, args);
134         } else if( methodClass == IndirectlySerializable.class ) {
135             return this.getSerializableObjectFactory();
136         }
137
138         // Use optimized version of get that takes param count as an argument.
139
InvocationInfo invInfo = (InvocationInfo)
140             invocationInfoMap_.get(method, ((args != null) ? args.length : 0));
141             
142         if( invInfo == null ) {
143             throw new IllegalStateException JavaDoc("Unknown method :" + method);
144         }
145
146         if( (methodClass == javax.ejb.EJBLocalObject JavaDoc.class) ||
147             invInfo.ejbIntfOverride ) {
148             return invokeEJBLocalObjectMethod(method.getName(), args);
149         } else if( invInfo.targetMethod1 == null ) {
150             Object JavaDoc [] params = new Object JavaDoc[]
151                 { invInfo.ejbName, "Local", invInfo.method.toString() };
152             String JavaDoc errorMsg = localStrings.getLocalString
153                 ("ejb.bean_class_method_not_found", "", params);
154             logger.log(Level.SEVERE, "ejb.bean_class_method_not_found",
155                        params);
156             throw new EJBException JavaDoc(errorMsg);
157         }
158
159         // Process application-specific method.
160

161         Object JavaDoc returnValue = null;
162
163         Invocation inv = new Invocation();
164         
165         inv.isLocal = true;
166         inv.isBusinessInterface = !isLocalHomeView();
167         inv.isHome = false;
168         inv.ejbObject = this;
169         inv.method = method;
170         inv.methodParams = args;
171
172         inv.clientInterface = clientInterface;
173
174         // Set cached invocation params. This will save additional lookups
175
// in BaseContainer.
176
inv.transactionAttribute = invInfo.txAttr;
177         inv.securityPermissions = invInfo.securityPermissions;
178         inv.invocationInfo = invInfo;
179         inv.beanMethod = invInfo.targetMethod1;
180
181         try {
182             container.preInvoke(inv);
183
184             returnValue = container.intercept(inv);
185
186         } catch(InvocationTargetException JavaDoc ite) {
187             inv.exception = ite.getCause();
188             inv.exceptionFromBeanMethod = inv.exception;
189         } catch(Throwable JavaDoc t) {
190             inv.exception = t;
191         } finally {
192             container.postInvoke(inv);
193         }
194         if (inv.exception != null) {
195             InvocationHandlerUtil.throwLocalException
196                 (inv.exception, method.getExceptionTypes());
197         }
198
199         return returnValue;
200         } finally {
201             container.onLeavingContainer();
202         }
203     }
204
205
206     private Object JavaDoc invokeEJBLocalObjectMethod(String JavaDoc methodName, Object JavaDoc[] args)
207         throws Exception JavaDoc
208     {
209         // Return value is null if target method returns void.
210
Object JavaDoc returnValue = null;
211
212         // Can only be remove, isIdentical, getEJBLocalHome, or getPrimaryKey,
213
// so optimize by comparing as few characters as possible.
214
switch(methodName.charAt(0)) {
215             case 'r' :
216
217                 // void remove();
218

219                 super.remove();
220                 break;
221
222             case 'i' :
223
224                 // boolean isIdentical(EJBLocalObject)
225

226                 // Convert the param into an EJBLocalObjectImpl. Can't
227
// assume it's an EJBLocalObject for an ejb that was deployed
228
// using dynamic proxies.
229
EJBLocalObject JavaDoc other = (EJBLocalObject JavaDoc) args[0];
230                 EJBLocalObjectImpl otherImpl =
231                     EJBLocalObjectImpl.toEJBLocalObjectImpl(other);
232                     
233                 returnValue = new Boolean JavaDoc(super.isIdentical(otherImpl));
234                 break;
235
236             case 'g' :
237
238                 if( methodName.charAt(3) == 'E' ) {
239                     // EJBLocalHome getEJBLocalHome();
240
returnValue = super.getEJBLocalHome();
241                 } else {
242                     // Object getPrimaryKey();
243
returnValue = super.getPrimaryKey();
244                 }
245                 break;
246
247             default :
248
249                 throw new EJBException JavaDoc("unknown method = " + methodName);
250         }
251
252         return returnValue;
253     }
254
255 }
256
Popular Tags