1 23 24 29 30 package com.sun.enterprise.webservice.monitoring; 31 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.List ; 35 import java.util.Collection ; 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.logging.Logger ; 39 import java.util.logging.Level ; 40 41 import java.io.File ; 42 import java.io.BufferedInputStream ; 43 import java.io.FileInputStream ; 44 45 import java.net.URL ; 46 import java.net.URLDecoder ; 47 import java.net.MalformedURLException ; 48 49 import org.w3c.dom.Document ; 50 import javax.xml.parsers.DocumentBuilderFactory ; 51 import org.xml.sax.InputSource ; 52 import javax.xml.xpath.XPathFactory ; 53 import javax.xml.xpath.XPath ; 54 import javax.xml.namespace.NamespaceContext ; 55 56 import javax.enterprise.deploy.shared.ModuleType ; 57 58 import com.sun.logging.LogDomains; 59 import com.sun.enterprise.deployment.WebServiceEndpoint; 60 61 68 public class WebServiceEngineImpl implements WebServiceEngine { 69 70 protected Map <String , Endpoint> endpoints = new HashMap <String , Endpoint>(); 71 protected List <EndpointLifecycleListener> lifecycleListeners = 72 new ArrayList <EndpointLifecycleListener>(); 73 protected List <AuthenticationListener> authListeners = 74 new ArrayList <AuthenticationListener>(); 75 protected GlobalMessageListener globalMessageListener = null; 76 77 78 static WebServiceEngineFactory factory = WebServiceEngineFactory.getInstance(); 79 static ThreadLocal servletThreadLocal = new ThreadLocal (); 80 public static Logger sLogger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 81 82 83 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 key = pathFromURL(newEndpoint.getEndpointSelector()); 107 endpoints.put(key, newEndpoint); 108 109 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 url) { 134 return endpoints.get(pathFromURL(url)); 135 } 136 137 public Iterator <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 endpoints.remove(pathFromURL(endpoint.getEndpointSelector())); 149 150 for (EndpointLifecycleListener listener : lifecycleListeners) { 152 listener.endpointRemoved(endpoint); 153 } 154 155 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 <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 decodedFileName = URLDecoder.decode(endpoint.getWebService().getGeneratedWsdlFilePath()); 197 File wsdlFile = new File (decodedFileName); 198 if (!wsdlFile.exists()) { 199 return null; 200 } 201 202 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 203 InputSource inputSource = new InputSource (new BufferedInputStream (new FileInputStream (wsdlFile))); 204 Document wsdlDoc = dFactory.newDocumentBuilder().parse(new BufferedInputStream (new FileInputStream (wsdlFile))); 205 206 207 XPathFactory xPathFactory = XPathFactory.newInstance(); 208 XPath xPath = xPathFactory.newXPath(); 209 NamespaceContext context = new NamespaceContextImpl(wsdlDoc); 210 xPath.setNamespaceContext(context); 211 212 String xpathExpression = "/:definitions/:service/:port[@name='"+ 213 endpoint.getWsdlPort().getLocalPart()+"']/"+ 214 endpoint.getSoapAddressPrefix()+":address/@location"; 215 216 String 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 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 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 e) { 241 e.printStackTrace(); 242 } 243 return null; 244 } 245 246 251 public String preProcessRequest(Endpoint endpoint) { 252 253 if (globalMessageListener==null) 254 return null; 255 256 return globalMessageListener.preProcessRequest(endpoint); 257 } 258 259 265 public void processRequest(String 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 280 public void processResponse(String 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 294 public void processRequest(String 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 309 public void processResponse(String messageID, com.sun.xml.ws.spi.runtime.SOAPMessageContext context) { 310 311 if (globalMessageListener==null) 312 return; 313 globalMessageListener.processResponse(messageID, context); 314 } 315 316 321 public void postProcessResponse(String messageID, TransportInfo info) { 322 323 if (globalMessageListener==null) 324 return; 325 326 globalMessageListener.postProcessResponse(messageID, info); 327 } 328 329 public ThreadLocal getThreadLocal() { 330 return servletThreadLocal; 331 } 332 333 private String pathFromURL(String urlString) { 334 URL url; 335 try { 336 url = new URL (java.net.URLDecoder.decode(urlString)); 337 } catch(MalformedURLException e) { 338 return null; 339 } 340 return url.getPath(); 341 } 342 } 343 | Popular Tags |