KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > Invocation


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

24 package com.sun.ejb;
25
26 //XXX: import javax.xml.rpc.handler.MessageContext;
27
/* HARRY : JACC Changes */
28 import javax.xml.rpc.handler.MessageContext JavaDoc;
29
30 import javax.interceptor.InvocationContext;
31 import javax.ejb.EJBContext JavaDoc;
32 import javax.transaction.Transaction JavaDoc;
33 import java.security.Identity JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.rmi.Remote JavaDoc;
38
39 import com.sun.xml.ws.spi.runtime.WebServiceContext;
40 import com.sun.enterprise.deployment.MethodDescriptor;
41 import com.sun.enterprise.security.CachedPermission;
42
43 /**
44  * The Invocation object contains state associated with an invocation
45  * on an EJB or EJBHome (local/remote). It is usually created by generated code
46  * in *ObjectImpl and *HomeImpl classes. It is passed as a parameter to
47  * Container.preInvoke() * and postInvoke(), which are called by the
48  * EJB(Local)Object/EJB(Local)Home before and after an invocation.
49  */

50
51 public class Invocation
52     extends com.sun.enterprise.ComponentInvocation
53     implements InvocationContext
54 {
55     private static Map JavaDoc<Class JavaDoc, Class JavaDoc> primitive2Wrapper
56         = new HashMap JavaDoc<Class JavaDoc, Class JavaDoc>();
57
58     static {
59         primitive2Wrapper.put(byte.class, Byte JavaDoc.class);
60         primitive2Wrapper.put(boolean.class, Boolean JavaDoc.class);
61         primitive2Wrapper.put(char.class, Character JavaDoc.class);
62         primitive2Wrapper.put(double.class, Double JavaDoc.class);
63         primitive2Wrapper.put(float.class, Float JavaDoc.class);
64         primitive2Wrapper.put(int.class, Integer JavaDoc.class);
65         primitive2Wrapper.put(short.class, Short JavaDoc.class);
66     }
67     
68     public Invocation()
69     {}
70     
71     public Invocation(Object JavaDoc ejb, Object JavaDoc container) {
72         super(ejb, container);
73         this.ejb = ejb;
74     }
75
76     public Invocation(Object JavaDoc ejb, Object JavaDoc container, ComponentContext context) {
77         super(ejb, container, context);
78         this.ejb = ejb;
79     }
80     
81     /**
82      * The EJBObject/EJBLocalObject which created this Invocation object.
83      * This identifies the target bean.
84      */

85     public com.sun.ejb.containers.EJBLocalRemoteObject ejbObject;
86     
87     /**
88      * Local flag: true if this invocation was made through an ejb local
89      * interface or a local business interface.
90      */

91     public boolean isLocal=false;
92     
93     /**
94      * InvocationInfo object caches information about the current method
95      */

96     public InvocationInfo invocationInfo;
97     
98     /**
99      * True if this invocation was made through a local business interface or
100      * a remote business interface.
101      */

102     public boolean isBusinessInterface;
103
104     /**
105      * true if this is a web service invocation
106      */

107     public boolean isWebService=false;
108     
109     /**
110      * true if this is a message-driven bean invocation
111      */

112     public boolean isMessageDriven=false;
113     
114     /**
115      * true if this is an invocation on the home object
116      * this is required for jacc.
117      */

118     public boolean isHome=false;
119
120     /**
121      * Home, Remote, LocalHome, Local, WebService, or business interface
122      * through which a synchronous ejb invocation was made.
123      */

124     public Class JavaDoc clientInterface;
125     
126     /**
127      * Method to be invoked. This is a method of the EJB's local/remote
128      * component interface for invocations on EJB(Local)Objects,
129      * or of the local/remote Home interface
130      * for invocations on the EJBHome.
131      * Set by the EJB(Local)Object/EJB(Local)Home before calling
132      * Container.preInvoke().
133      */

134     public java.lang.reflect.Method JavaDoc method;
135     
136     /**
137      * The EJB instance to be invoked.
138      * Set by Container and used by EJBObject/EJBHome.
139      */

140     public Object JavaDoc ejb;
141
142     /**
143      * This reflects any exception that has occurred during this invocation,
144      * including preInvoke, bean method execution, and postInvoke.
145      */

146     public Throwable JavaDoc exception;
147
148     /**
149      * Set to any exception directly thrown from bean method invocation,
150      * which could be either an application exception or a runtime exception.
151      * This is set *in addition to* the this.exception field. Some container
152      * processing logic, e.g. @Remove, depends specifically on whether a
153      * bean method threw an exception.
154      */

155     public Throwable JavaDoc exceptionFromBeanMethod;
156     
157     
158     /**
159      * The client's transaction if any.
160      * Set by the Container during preInvoke() and used by the Container
161      * during postInvoke().
162      */

163     public Transaction JavaDoc clientTx;
164     
165     /**
166      * The EJBContext object of the bean instance being invoked.
167      * Set by the Container during preInvoke() and used by the Container
168      * during postInvoke().
169      */

170     // Moved to com/sun/enterprise/ComponentInvocation
171
// public ComponentContext context;
172

173     /**
174      * The transaction attribute of the bean method. Set in generated
175      * EJBObject/Home/LocalObject/LocalHome class.
176      */

177     public int transactionAttribute;
178     
179     /**
180      * The security attribute of the bean method. Set in generated
181      * EJBObject/Home/LocalObject/LocalHome class.
182      */

183     public int securityPermissions;
184     
185     
186     /**
187      * Used by MessageBeanContainer. true if container started
188      * a transaction for this invocation.
189      */

190     public boolean containerStartsTx;
191     
192     /**
193      * Used by MessageBeanContainer to keep track of the context class
194      * loader that was active before message delivery began.
195      */

196     public ClassLoader JavaDoc originalContextClassLoader;
197     
198     /**
199      * Used for web service invocations to hold SOAP message context.
200      * EJBs can access message context through SessionContext.
201      */

202     /* HARRY: JACC Related Changes */
203      public MessageContext JavaDoc messageContext;
204     
205     /**
206      * Used for JACC PolicyContextHandlers. The handler can query the container
207      * back for parameters on the ejb. This is set during the method invocation
208      * and is not available for preInvoke calls.
209      */

210     public Object JavaDoc[] methodParams;
211
212     /**
213      * Used by JACC implementation to get an enterprise bean
214      * instance for the EnterpriseBean policy handler. The jacc
215      * implementation should use this method rather than directly
216      * accessing the ejb field.
217      */

218     public Object JavaDoc getJaccEjb() {
219         Object JavaDoc bean = null;
220         if( container != null ) {
221             bean = ((Container) container).getJaccEjb(this);
222         }
223         return bean;
224     }
225     
226     /**
227      * This method returns the method interface constant for this Invocation.
228      */

229     public String JavaDoc getMethodInterface() {
230         if (isWebService) {
231             return MethodDescriptor.EJB_WEB_SERVICE;
232         } else if (isMessageDriven) {
233             return MethodDescriptor.EJB_BEAN;
234         } else if (isLocal) {
235             return (isHome) ? MethodDescriptor.EJB_LOCALHOME :
236                     MethodDescriptor.EJB_LOCAL;
237         } else {
238             return (isHome) ? MethodDescriptor.EJB_HOME :
239                     MethodDescriptor.EJB_REMOTE;
240         }
241     }
242
243     /**
244      * Returns CachedPermission associated with this invocation, or
245      * null if not available.
246      */

247     public CachedPermission getCachedPermission() {
248         return (invocationInfo != null) ? invocationInfo.cachedPermission :
249             null;
250     }
251
252     /**
253      * @return Returns the ejbCtx.
254      */

255     public EJBContext JavaDoc getEJBContext() {
256         return (EJBContext JavaDoc) this.context;
257     }
258     
259     
260     //Implementation of InvocationContext methods
261

262     private int interceptorIndex;
263
264     public Method JavaDoc beanMethod;
265
266     // Only set for web service invocations.
267
private WebServiceContext webServiceContext;
268
269     private Map JavaDoc contextData;
270
271     public InterceptorChain getInterceptorChain() {
272         return (invocationInfo == null)
273             ? null : invocationInfo.interceptorChain;
274     }
275
276     /**
277      * @return Returns the bean instance.
278      */

279     public Object JavaDoc getTarget() {
280         return this.ejb;
281     }
282  
283     
284     /**
285      * @return For AroundInvoke methods, returns the bean class
286      * method being invoked. For lifecycle callback methods,
287      * returns null.
288      */

289     public Method JavaDoc getMethod() {
290         return getBeanMethod();
291     }
292     public Method JavaDoc getBeanMethod() {
293         return this.beanMethod;
294     }
295
296     /**
297      * @return Returns the parameters that will be used to invoke
298      * the business method. If setParameters has been called,
299      * getParameters() returns the values to which the parameters
300      * have been set.
301      */

302     public Object JavaDoc[] getParameters() {
303         return this.methodParams;
304     }
305     
306     /**
307      * Set the parameters that will be used to invoke the business method.
308      *
309      */

310     public void setParameters(Object JavaDoc[] params) {
311         Method JavaDoc method = getMethod();
312         if (method != null) {
313             Class JavaDoc[] paramTypes = method.getParameterTypes();
314             if ((params == null) && (paramTypes.length != 0)) {
315                 throw new IllegalArgumentException JavaDoc("Wrong number of parameters for "
316                         + " method: " + method);
317             }
318             if (paramTypes.length != params.length) {
319                 throw new IllegalArgumentException JavaDoc("Wrong number of parameters for "
320                         + " method: " + method);
321             }
322             int index = 0 ;
323             for (Class JavaDoc type : paramTypes) {
324                 if (type.isPrimitive()) {
325                     Class JavaDoc allowedWrapper = primitive2Wrapper.get(type);
326                     if (allowedWrapper != params[index].getClass()) {
327                         throw new IllegalArgumentException JavaDoc("Parameter type mismatch for method "
328                                 + method.getName() + ". Arg["
329                             + index + "] type: " + params[index].getClass().getName()
330                             + " does not match the expected type: " + type.getName());
331                     }
332                 } else if (! type.isAssignableFrom(params[index].getClass())) {
333                     throw new IllegalArgumentException JavaDoc("Parameter type mismatch for method "
334                             + method.getName() + ". Arg["
335                         + index + "] type: " + params[index].getClass().getName()
336                         + " does not match the expected type: " + type.getName());
337                 }
338                 index++;
339             }
340         } else {
341             throw new IllegalStateException JavaDoc("Internal Error: Got null method");
342         }
343         this.methodParams = params;
344     }
345     
346     //The following method is not part of InvocationContext interface
347
// but needed for JAXWS message context propagation
348
public void setContextData(WebServiceContext context) {
349         this.webServiceContext = context;
350     }
351     
352     /**
353      * @return Returns the contextMetaData.
354      */

355     public Map JavaDoc<String JavaDoc, Object JavaDoc> getContextData() {
356         if (this.contextData == null) {
357             this.contextData = (webServiceContext != null) ?
358                 webServiceContext.getMessageContext() :
359                 new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
360         }
361         return contextData;
362     }
363
364     /* (non-Javadoc)
365      * @see javax.interceptor.InvocationContext#proceed()
366      */

367     public Object JavaDoc proceed()
368         throws Exception JavaDoc
369     {
370         try {
371             //TODO: Internal error if getInterceptorChain() is null
372
interceptorIndex++;
373             return getInterceptorChain().invokeNext(interceptorIndex, this);
374         } catch (Exception JavaDoc ex) {
375             throw ex;
376         } catch (Throwable JavaDoc th) {
377             throw new Exception JavaDoc(th);
378         } finally {
379             interceptorIndex--;
380         }
381     }
382
383     /*********************************************************/
384
385     public static interface InterceptorChain {
386     public Object JavaDoc invokeNext(int index, Invocation invCtx)
387         throws Throwable JavaDoc;
388     }
389
390 }
391
392
Popular Tags