KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > monitoring > WebServiceEngineImpl


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 /*
25  * TracingSystemHandlerFactory.java
26  *
27  * Created on August 12, 2004, 4:04 PM
28  */

29
30 package com.sun.enterprise.webservice.monitoring;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39 import java.util.logging.Level JavaDoc;
40
41 import java.io.File JavaDoc;
42 import java.io.BufferedInputStream JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44
45 import java.net.URL JavaDoc;
46 import java.net.URLDecoder JavaDoc;
47 import java.net.MalformedURLException JavaDoc;
48
49 import org.w3c.dom.Document JavaDoc;
50 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
51 import org.xml.sax.InputSource JavaDoc;
52 import javax.xml.xpath.XPathFactory JavaDoc;
53 import javax.xml.xpath.XPath JavaDoc;
54 import javax.xml.namespace.NamespaceContext JavaDoc;
55
56 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
57
58 import com.sun.logging.LogDomains;
59 import com.sun.enterprise.deployment.WebServiceEndpoint;
60
61 /**
62  * This class acts as a factory to create TracingSystemHandler
63  * instances. It also provides an API to register listeners
64  * of SOAP messages.
65  *
66  * @author Jerome Dochez
67  */

68 public class WebServiceEngineImpl implements WebServiceEngine {
69     
70     protected Map JavaDoc<String JavaDoc, Endpoint> endpoints = new HashMap JavaDoc<String JavaDoc, Endpoint>();
71     protected List JavaDoc<EndpointLifecycleListener> lifecycleListeners =
72             new ArrayList JavaDoc<EndpointLifecycleListener>();
73     protected List JavaDoc<AuthenticationListener> authListeners =
74             new ArrayList JavaDoc<AuthenticationListener>();
75     protected GlobalMessageListener globalMessageListener = null;
76            
77
78     static WebServiceEngineFactory factory = WebServiceEngineFactory.getInstance();
79     static ThreadLocal JavaDoc servletThreadLocal = new ThreadLocal JavaDoc();
80     public static Logger JavaDoc sLogger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
81     
82     
83     /** Creates a new instance of TracingSystemHandlerFactory */
84     protected WebServiceEngineImpl() {
85     }
86     
87     public static WebServiceEngineImpl getInstance() {
88             
89         if (factory.getEngine()==null) {
90             synchronized(factory) {
91                 if (factory.getEngine()==null) {
92                     factory.setEngine(new WebServiceEngineImpl());
93                     factory.getEngine().addAuthListener(new LogAuthenticationListener());
94                 }
95             }
96         }
97         return (WebServiceEngineImpl) factory.getEngine();
98     }
99     
100     private EndpointImpl createHandler(WebServiceEndpoint endpointDesc) {
101         
102         EndpointImpl newEndpoint = createEndpointInfo(endpointDesc);
103         if (newEndpoint==null) {
104             return null;
105         }
106         String JavaDoc key = pathFromURL(newEndpoint.getEndpointSelector());
107         endpoints.put(key, newEndpoint);
108         
109         // notify listeners
110
for (EndpointLifecycleListener listener : lifecycleListeners) {
111             listener.endpointAdded(newEndpoint);
112         }
113         
114         return newEndpoint;
115     }
116     
117     public EndpointImpl createHandler(com.sun.xml.rpc.spi.runtime.SystemHandlerDelegate parent,
118         WebServiceEndpoint endpointDesc) {
119         
120         EndpointImpl newEndpoint = createHandler(endpointDesc);
121         ((JAXRPCEndpointImpl)newEndpoint).setParent(parent);
122         return newEndpoint;
123     }
124
125     public EndpointImpl createHandler(com.sun.xml.ws.spi.runtime.SystemHandlerDelegate parent,
126         WebServiceEndpoint endpointDesc) {
127         
128         EndpointImpl newEndpoint = createHandler(endpointDesc);
129         ((JAXWSEndpointImpl)newEndpoint).setParent(parent);
130         return newEndpoint;
131     }
132     
133     public Endpoint getEndpoint(String JavaDoc url) {
134         return endpoints.get(pathFromURL(url));
135     }
136     
137     public Iterator JavaDoc<Endpoint> getEndpoints() {
138         return endpoints.values().iterator();
139     }
140     
141     public void removeHandler(WebServiceEndpoint endpointDesc) {
142
143         EndpointImpl endpoint = (EndpointImpl) endpointDesc.getExtraAttribute(EndpointImpl.NAME);
144         if (endpoint==null)
145             return;
146                 
147         // remove this endpoint from our list of endpoints
148
endpoints.remove(pathFromURL(endpoint.getEndpointSelector()));
149
150         // notify listeners
151
for (EndpointLifecycleListener listener : lifecycleListeners) {
152             listener.endpointRemoved(endpoint);
153         }
154         
155         // forcing the cleaning so we don't have DOL objects staying alive because
156
// some of our clients have not released the endpoint instance.
157
endpoint.setDescriptor(null);
158     }
159             
160     public void addLifecycleListener(EndpointLifecycleListener listener) {
161         lifecycleListeners.add(listener);
162     }
163     
164     public void removeLifecycleListener(EndpointLifecycleListener listener) {
165         lifecycleListeners.remove(listener);
166     }
167     
168     public void addAuthListener(AuthenticationListener listener) {
169         authListeners.add(listener);
170     }
171     
172     public void removeAuthListener(AuthenticationListener listener) {
173         authListeners.remove(listener);
174     }
175     
176     public Collection JavaDoc<AuthenticationListener> getAuthListeners() {
177         return authListeners;
178     }
179     
180     public GlobalMessageListener getGlobalMessageListener() {
181         return globalMessageListener;
182     }
183     
184     public void setGlobalMessageListener(GlobalMessageListener listener) {
185         globalMessageListener = listener;
186     }
187     
188     
189     public boolean hasGlobalMessageListener() {
190         return globalMessageListener!=null;
191     }
192                 
193     private EndpointImpl createEndpointInfo(WebServiceEndpoint endpoint) {
194         
195         try {
196             String JavaDoc decodedFileName = URLDecoder.decode(endpoint.getWebService().getGeneratedWsdlFilePath());
197             File JavaDoc wsdlFile = new File JavaDoc(decodedFileName);
198             if (!wsdlFile.exists()) {
199                 return null;
200             }
201
202             DocumentBuilderFactory JavaDoc dFactory = DocumentBuilderFactory.newInstance();
203             InputSource JavaDoc inputSource = new InputSource JavaDoc(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(wsdlFile)));
204             Document JavaDoc wsdlDoc = dFactory.newDocumentBuilder().parse(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(wsdlFile)));
205
206              
207             XPathFactory JavaDoc xPathFactory = XPathFactory.newInstance();
208             XPath JavaDoc xPath = xPathFactory.newXPath();
209             NamespaceContext JavaDoc context = new NamespaceContextImpl(wsdlDoc);
210             xPath.setNamespaceContext(context);
211
212                String JavaDoc xpathExpression = "/:definitions/:service/:port[@name='"+
213                     endpoint.getWsdlPort().getLocalPart()+"']/"+
214                      endpoint.getSoapAddressPrefix()+":address/@location";
215              
216             String JavaDoc endpointURL = xPath.evaluate(xpathExpression, inputSource);
217             if (endpointURL==null) {
218                 System.out.println("Cannot get endpoint URL from " + endpoint.getWsdlPort());
219             }
220         
221             EndpointType endpointType;
222             ModuleType JavaDoc moduleType = endpoint.getWebService().getWebServicesDescriptor().getModuleType();
223             if (moduleType.equals(ModuleType.EJB)) {
224                 endpointType = EndpointType.EJB_ENDPOINT;
225             } else {
226                 endpointType = EndpointType.SERVLET_ENDPOINT;
227             }
228
229             EndpointImpl newEndpoint;
230             // At this point, we can depend on presence of mapping file to distinguish between JAXRPC and JAXWS
231
// service
232
if(endpoint.getWebService().getMappingFileUri()==null) {
233                 newEndpoint = new JAXWSEndpointImpl(endpointURL, endpointType);
234             } else {
235                 newEndpoint = new JAXRPCEndpointImpl(endpointURL, endpointType);
236             }
237             newEndpoint.setDescriptor(endpoint);
238             return newEndpoint;
239         
240         } catch(Exception JavaDoc e) {
241             e.printStackTrace();
242         }
243         return null;
244     }
245         
246     /**
247      * Callback when a web service request entered the web service container
248      * before any processing is done.
249      * @return a message ID to trace the request in the subsequent callbacks
250      */

251     public String JavaDoc preProcessRequest(Endpoint endpoint) {
252         
253         if (globalMessageListener==null)
254             return null;
255         
256         return globalMessageListener.preProcessRequest(endpoint);
257     }
258     
259     /**
260      * Callback when a web service request is received on
261      * the endpoint.
262      * @param messageID returned by preProcessRequest call
263      * @param msgContext the jaxrpc message trace, transport dependent.
264      */

265     public void processRequest(String JavaDoc messageID, com.sun.xml.rpc.spi.runtime.SOAPMessageContext context,
266             TransportInfo info) {
267         
268         if (globalMessageListener==null)
269             return;
270         
271         globalMessageListener.processRequest(messageID, context, info);
272     }
273     
274     /**
275      * Callback when a web service response is received on the
276      * endpoint.
277      * @param messageID returned by the preProcessRequest call
278      * @param trace jaxrpc message context
279      */

280     public void processResponse(String JavaDoc messageID, com.sun.xml.rpc.spi.runtime.SOAPMessageContext context) {
281         
282         if (globalMessageListener==null)
283             return;
284         
285         globalMessageListener.processResponse(messageID, context);
286     }
287     
288     /**
289      * Callback when a 2.0 web service request is received on
290      * the endpoint.
291      * @param messageID returned by preProcessRequest call
292      * @param msgContext the jaxrpc message trace, transport dependent.
293      */

294     public void processRequest(String JavaDoc messageID, com.sun.xml.ws.spi.runtime.SOAPMessageContext context,
295             TransportInfo info) {
296         
297         if (globalMessageListener==null)
298             return;
299
300         globalMessageListener.processRequest(messageID, context, info);
301     }
302     
303     /**
304      * Callback when a 2.0 web service response is received on the
305      * endpoint.
306      * @param messageID returned by the preProcessRequest call
307      * @param trace jaxrpc message context
308      */

309     public void processResponse(String JavaDoc messageID, com.sun.xml.ws.spi.runtime.SOAPMessageContext context) {
310         
311         if (globalMessageListener==null)
312             return;
313         globalMessageListener.processResponse(messageID, context);
314     }
315     
316     /**
317      * Callback when a web service response has finished being processed
318      * by the container and was sent back to the client
319      * @param messageID returned by the preProcessRequest call
320      */

321     public void postProcessResponse(String JavaDoc messageID, TransportInfo info) {
322         
323         if (globalMessageListener==null)
324             return;
325         
326         globalMessageListener.postProcessResponse(messageID, info);
327     }
328     
329     public ThreadLocal JavaDoc getThreadLocal() {
330         return servletThreadLocal;
331     }
332     
333     private String JavaDoc pathFromURL(String JavaDoc urlString) {
334         URL JavaDoc url;
335         try {
336             url = new URL JavaDoc(java.net.URLDecoder.decode(urlString));
337         } catch(MalformedURLException JavaDoc e) {
338             return null;
339         }
340         return url.getPath();
341     }
342 }
343
Popular Tags