KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > Ejb3MessageDispatcher


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
24 package com.sun.enterprise.webservice;
25
26 import java.io.IOException JavaDoc;
27
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import com.sun.logging.LogDomains;
31
32 import javax.xml.soap.SOAPMessage JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import javax.xml.ws.handler.MessageContext.Scope;
37 import javax.xml.ws.handler.MessageContext;
38 import javax.xml.ws.handler.soap.SOAPMessageContext;
39
40 import com.sun.xml.ws.spi.runtime.RuntimeEndpointInfo;
41 import com.sun.xml.ws.server.Tie;
42 import com.sun.xml.ws.spi.runtime.WSRtObjectFactory;
43 import com.sun.xml.ws.spi.runtime.WSConnection;
44 import com.sun.xml.ws.spi.runtime.WebServiceContext;
45
46 import com.sun.enterprise.webservice.monitoring.WebServiceEngineImpl;
47 import com.sun.enterprise.webservice.monitoring.EndpointImpl;
48 import com.sun.enterprise.webservice.monitoring.HttpRequestInfoImpl;
49 import com.sun.enterprise.webservice.monitoring.HttpResponseInfoImpl;
50 import com.sun.enterprise.webservice.monitoring.ThreadLocalInfo;
51
52 /**
53  * Implementation of the Ejb Message Dispatcher for EJB3 endpoints.
54  *
55  * @author Jerome Dochez
56  */

57 public class Ejb3MessageDispatcher implements EjbMessageDispatcher {
58     
59     private static Logger JavaDoc logger =
60         LogDomains.getLogger(LogDomains.EJB_LOGGER);
61     
62     private static WsUtil wsUtil = new WsUtil();
63     
64     // all jaxrpc invocations of all endpoints go through the same tie
65
private static Tie tie = new Tie();
66     private static WSRtObjectFactory rpcFactory = WSRtObjectFactory.newInstance();
67     private WebServiceEngineImpl wsEngine;
68     
69     /** Creates a new instance of Ejb3MessageDispatcher */
70     public Ejb3MessageDispatcher() {
71         wsEngine = WebServiceEngineImpl.getInstance();
72     }
73     
74     public void invoke(HttpServletRequest JavaDoc req,
75                        HttpServletResponse JavaDoc resp,
76                        EjbRuntimeEndpointInfo endpointInfo) {
77         
78         
79         if (logger.isLoggable(Level.FINE)) {
80             logger.log(Level.FINE, "JAXWS WebServiceDispatcher " + req.getMethod() +
81                    " entering for " + req.getRequestURI() + " and query string " + req.getQueryString());
82         }
83        
84         String JavaDoc method = req.getMethod();
85         
86         try {
87             if( method.equals("POST") ) {
88                 
89                 handlePost(req, resp, endpointInfo);
90                 
91             } else if( method.equals("GET") ) {
92                 
93                 handleGet(req, resp, endpointInfo);
94                 
95             } else {
96                 
97                 String JavaDoc errorMessage = "Unsupported method request = ["
98                     + method + "] for endpoint " +
99                     endpointInfo.getEndpoint().getEndpointName() + " at " +
100                     endpointInfo.getEndpointAddressUri();
101                 logger.warning(errorMessage);
102                 wsUtil.writeInvalidMethodType(resp, errorMessage);
103             }
104         } catch(Exception JavaDoc e) {
105             logger.log(Level.WARNING, "ejb endpoint exception", e);
106         }
107     }
108     
109     private void handlePost(HttpServletRequest JavaDoc req,
110                             HttpServletResponse JavaDoc resp,
111                             EjbRuntimeEndpointInfo endpointInfo)
112         throws IOException JavaDoc {
113         
114         EndpointImpl endpoint = null;
115         String JavaDoc messageID = null;
116         
117         try {
118             
119             try {
120                 
121                 RuntimeEndpointInfo jaxwsInfo = endpointInfo.prepareInvocation(true);
122                 if (jaxwsInfo != null) {
123                     
124                     // get the endpoint info
125
endpoint = (EndpointImpl) endpointInfo.getEndpoint().getExtraAttribute(EndpointImpl.NAME);
126                     
127                     if (endpoint!=null) {
128                         // first global notification
129
if (wsEngine.hasGlobalMessageListener()) {
130                             messageID = wsEngine.preProcessRequest(endpoint);
131                         }
132                     } else {
133                         logger.fine("Missing internal monitoring info to trace " + req.getRequestURI());
134                     }
135                     
136
137                     
138                     WSConnection connection = rpcFactory.createWSConnection(req, resp);
139                     WebServiceContext wsCtxt = jaxwsInfo.getWebServiceContext();
140                     MessageContext msgCtxt = wsCtxt.getMessageContext();
141                     msgCtxt.put(MessageContext.SERVLET_REQUEST, req);
142                     msgCtxt.setScope(MessageContext.SERVLET_REQUEST, Scope.APPLICATION);
143                     msgCtxt.put(MessageContext.SERVLET_RESPONSE, resp);
144                     msgCtxt.setScope(MessageContext.SERVLET_RESPONSE, Scope.APPLICATION);
145                     msgCtxt.put(MessageContext.HTTP_REQUEST_METHOD, req.getMethod());
146                     msgCtxt.setScope(MessageContext.HTTP_REQUEST_METHOD, Scope.APPLICATION);
147                     if (req.getQueryString() != null) {
148                         msgCtxt.put(MessageContext.QUERY_STRING, req.getQueryString());
149                         msgCtxt.setScope(MessageContext.QUERY_STRING, Scope.APPLICATION);
150                     }
151                     if (req.getPathInfo() != null) {
152                         msgCtxt.put(MessageContext.PATH_INFO, req.getPathInfo());
153                         msgCtxt.setScope(MessageContext.PATH_INFO, Scope.APPLICATION);
154                     }
155                     msgCtxt.put(MessageContext.HTTP_REQUEST_HEADERS, connection.getHeaders());
156                     msgCtxt.setScope(MessageContext.HTTP_REQUEST_HEADERS, Scope.APPLICATION);
157
158                     // our container related information
159
msgCtxt.put(EndpointImpl.MESSAGE_ID, messageID);
160                     tie.handle(connection, jaxwsInfo);
161                     
162                 }
163                                 
164             } finally {
165                 
166                 // Always call release, even if an error happened
167
// during getImplementor(), since some of the
168
// preInvoke steps might have occurred. It's ok
169
// if implementor is null.
170
endpointInfo.releaseImplementor();
171                 
172             }
173              
174         } catch (Throwable JavaDoc e) {
175             
176             String JavaDoc errorMessage = "invocation error on ejb endpoint " +
177             endpointInfo.getEndpoint().getEndpointName() + " at " +
178             endpointInfo.getEndpointAddressUri() + " : " + e.getMessage();
179             logger.log(Level.WARNING, errorMessage, e);
180
181             String JavaDoc binding = endpointInfo.getEndpoint().getProtocolBinding();
182             WsUtil.raiseException(resp, binding, errorMessage);
183         }
184         
185         // final tracing notification
186
if (messageID!=null) {
187             HttpResponseInfoImpl response = new HttpResponseInfoImpl(resp);
188             wsEngine.postProcessResponse(messageID, response);
189         }
190     }
191     
192     private void handleGet(HttpServletRequest JavaDoc req,
193                            HttpServletResponse JavaDoc resp,
194                            EjbRuntimeEndpointInfo endpointInfo)
195         throws IOException JavaDoc
196     {
197        
198         wsUtil.handleGet(req, resp, endpointInfo.getEndpoint());
199
200     }
201     
202 }
203
Popular Tags