KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.ejb.EJBException JavaDoc;
26 import javax.ejb.AccessLocalException JavaDoc;
27
28 import java.lang.reflect.InvocationHandler JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Proxy JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 import java.util.Map JavaDoc;
34 import com.sun.ejb.Invocation;
35 import com.sun.enterprise.Switch;
36 import com.sun.ejb.ComponentContext;
37 import com.sun.ejb.InvocationInfo;
38 import com.sun.enterprise.InvocationManager;
39 import com.sun.ejb.Container;
40 import com.sun.enterprise.deployment.EjbDescriptor;
41 import com.sun.enterprise.deployment.WebServiceEndpoint;
42
43 import com.sun.ejb.containers.util.InvocationHandlerUtil;
44
45 /**
46  * This is a proxy invocation handler for web service ejb invocations.
47  * A single instance of this invocation handler is used for all
48  * web service invocations to a particular ejb endpoint, so it must support
49  * concurrent use.
50  *
51  * @author Kenneth Saks
52  */

53
54 public final class WebServiceInvocationHandler extends EJBLocalRemoteObject
55     implements InvocationHandler JavaDoc {
56
57     private WebServiceEndpoint endpoint_;
58     private Class JavaDoc ejbClass_;
59     private Class JavaDoc serviceEndpointIntfClass_;
60     private InvocationManager invManager_;
61     private boolean hasHandlers_;
62     private Map JavaDoc invocationInfoMap_;
63
64     public WebServiceInvocationHandler(Class JavaDoc ejbClass,
65                                        WebServiceEndpoint endpoint,
66                                        Class JavaDoc serviceEndpointIntfClass,
67                                        Map JavaDoc invocationInfoMap) {
68         ejbClass_ = ejbClass;
69         serviceEndpointIntfClass_ = serviceEndpointIntfClass;
70         endpoint_ = endpoint;
71         hasHandlers_ = endpoint.hasHandlers();
72         Switch theSwitch = Switch.getSwitch();
73         invManager_ = theSwitch.getInvocationManager();
74         invocationInfoMap_ = invocationInfoMap;
75     }
76
77     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
78         throws Throwable JavaDoc {
79         try {
80         container.onEnteringContainer();
81         // NOTE : be careful with "args" parameter. It is null
82
// if method signature has 0 arguments.
83

84         Class JavaDoc methodClass = method.getDeclaringClass();
85         if( methodClass == java.lang.Object JavaDoc.class ) {
86             return InvocationHandlerUtil.
87                 invokeJavaObjectMethod(this, method, args);
88         }
89
90         Object JavaDoc returnValue = null;
91         Invocation inv = null;
92
93         try {
94             // Invocation was created earlier in the web service dispatching
95
inv = (Invocation) invManager_.getCurrentInvocation();
96
97             inv.ejbObject = this;
98             
99             // things can become hairy here. This handler may have been created
100
// with a dummy SEI to satisfy the EJB container. In such cases, we must
101
// find the right method object on the SIB.
102
if (endpoint_.getServiceEndpointInterface().equals(ejbClass_.getName())) {
103                 // we need to substiture the method object
104
method = ejbClass_.getMethod(method.getName(), method.getParameterTypes());
105             }
106             inv.method = method;
107             inv.clientInterface = serviceEndpointIntfClass_;
108
109             inv.invocationInfo = (InvocationInfo)
110                 invocationInfoMap_.get(inv.method);
111
112             if( inv.invocationInfo == null ) {
113                 throw new EJBException JavaDoc
114                     ("Web service Invocation Info lookup failed for " +
115                      "method " + inv.method);
116             }
117
118             inv.transactionAttribute = inv.invocationInfo.txAttr;
119
120             if( hasHandlers_ ) {
121                 // Handler performed method authorization already
122
} else {
123                 StatelessSessionContainer container =
124                     (StatelessSessionContainer) getContainer();
125
126                 boolean authorized = container.authorize(inv);
127                 if( !authorized ) {
128                     throw new AccessLocalException JavaDoc("Client not authorized " +
129                                                    "to access " + inv.method);
130                 }
131             }
132
133             ComponentContext ctx = container.getContext(inv);
134             inv.context = ctx;
135             inv.ejb = ctx.getEJB();
136             inv.instance = inv.ejb;
137
138             container.preInvokeTx(inv);
139
140             // Enterprise Bean class doesn't necessarily implement
141
// web service endpoint interface, so we can't directly
142
// dispatch through the given method object.
143
Method JavaDoc beanClassMethod = ejbClass_.getMethod
144                 (method.getName(), method.getParameterTypes());
145             inv.beanMethod = beanClassMethod;
146             inv.methodParams = args;
147             returnValue = container.intercept(inv);
148         } catch(NoSuchMethodException JavaDoc nsme) {
149             inv.exception = nsme;
150         } catch(InvocationTargetException JavaDoc ite) {
151             inv.exception = ite.getCause();
152         } catch(Throwable JavaDoc c) {
153             inv.exception = c;
154         } finally {
155             // ejb web service dispatcher handles postInvoke
156
}
157         if (inv.exception != null) {
158             if(inv.exception instanceof java.lang.RuntimeException JavaDoc) {
159                 throw (java.lang.RuntimeException JavaDoc)inv.exception;
160             } else if (inv.exception instanceof Exception JavaDoc) {
161                 throw inv.exception;
162             } else {
163                 EJBException JavaDoc ejbEx = new EJBException JavaDoc();
164                 ejbEx.initCause(inv.exception);
165                 throw ejbEx;
166             }
167         }
168         return returnValue;
169         } finally {
170             container.onLeavingContainer();
171         }
172     }
173     
174 }
175
Popular Tags