1 7 8 package org.jboss.webservice; 10 11 13 import org.jboss.axis.MessageContext; 14 import org.jboss.axis.server.AxisServer; 15 import org.jboss.axis.utils.Admin; 16 import org.jboss.deployment.DeploymentInfo; 17 import org.jboss.logging.Logger; 18 import org.jboss.metadata.WebMetaData; 19 import org.jboss.system.ServiceMBeanSupport; 20 import org.jboss.system.server.ServerConfig; 21 import org.jboss.webservice.deployment.MetaDataRegistry; 22 import org.jboss.webservice.deployment.ServiceDescription; 23 import org.jboss.webservice.deployment.TypeMappingDescription; 24 import org.jboss.webservice.deployment.WSDDGenerator; 25 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping; 26 import org.jboss.webservice.metadata.PortComponentMetaData; 27 import org.jboss.webservice.metadata.WebserviceDescriptionMetaData; 28 import org.jboss.webservice.server.InvokerProviderJMX; 29 import org.jboss.webservice.server.JMXInvokerEndpoint; 30 import org.jboss.webservice.server.ServerEngine; 31 import org.w3c.dom.Document ; 32 33 import javax.management.ObjectName ; 34 import javax.wsdl.Definition; 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 import java.io.ByteArrayInputStream ; 39 import java.io.File ; 40 import java.io.FileWriter ; 41 import java.io.PrintWriter ; 42 import java.io.StringWriter ; 43 import java.net.InetAddress ; 44 import java.net.MalformedURLException ; 45 import java.net.URL ; 46 import java.net.UnknownHostException ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.Map ; 50 51 64 public class AxisService extends ServiceMBeanSupport 65 implements AxisServiceMBean 66 { 67 private static final Logger log = Logger.getLogger(AxisService.class); 69 70 private Map wsRegistry = new HashMap (); 72 73 private ServerEngine axisServer; 75 76 private MetaDataRegistry metaDataRegistry = new MetaDataRegistry(); 78 79 private String webServiceHost; 81 private int webServicePort; 83 private int webServiceSecurePort; 85 private boolean alwaysModifySOAPAddress; 87 private String invokerProviderEJB; 89 private String invokerProviderJSE; 91 92 100 protected void startService() throws Exception 101 { 102 super.startService(); 103 } 104 105 113 protected void stopService() throws Exception 114 { 115 super.stopService(); 116 } 117 118 121 public String getWebServiceHost() 122 { 123 return webServiceHost; 124 } 125 126 129 public int getWebServicePort() 130 { 131 return webServicePort; 132 } 133 134 137 public int getWebServiceSecurePort() 138 { 139 return webServiceSecurePort; 140 } 141 142 145 public boolean isAlwaysModifySOAPAddress() 146 { 147 return alwaysModifySOAPAddress; 148 } 149 150 153 public void setWebServiceHost(String host) 154 { 155 if ("0.0.0.0".equals(host)) 156 { 157 try 158 { 159 InetAddress localHost = InetAddress.getLocalHost(); 160 host = localHost.getHostName(); 161 } 162 catch (UnknownHostException e) 163 { 164 log.error("Cannot map host: " + host, e); 165 } 166 } 167 168 this.webServiceHost = host; 169 } 170 171 174 public void setWebServicePort(int port) 175 { 176 this.webServicePort = port; 177 } 178 179 182 public void setWebServiceSecurePort(int port) 183 { 184 this.webServiceSecurePort = port; 185 } 186 187 190 public void setAlwaysModifySOAPAddress(boolean modify) 191 { 192 this.alwaysModifySOAPAddress = modify; 193 } 194 195 198 public String getInvokerProviderEJB() 199 { 200 return invokerProviderEJB; 201 } 202 203 206 public void setInvokerProviderEJB(String invokerProviderEJB) 207 { 208 this.invokerProviderEJB = invokerProviderEJB; 209 } 210 211 214 public String getInvokerProviderJSE() 215 { 216 return invokerProviderJSE; 217 } 218 219 222 public void setInvokerProviderJSE(String invokerProviderJSE) 223 { 224 this.invokerProviderJSE = invokerProviderJSE; 225 } 226 227 232 public AxisServer getAxisServer() 233 { 234 if (axisServer == null) 235 axisServer = new ServerEngine(EngineConfigurationFinder.getServerEngineConfiguration()); 236 237 return axisServer; 238 } 239 240 245 public MetaDataRegistry getMetaDataRegistry() 246 { 247 return metaDataRegistry; 248 } 249 250 270 public PortComponentInfo getPortComponentInfo(String wsID) 271 { 272 PortComponentInfo pcInfo = (PortComponentInfo)wsRegistry.get(wsID); 273 274 if (pcInfo == null) 275 { 276 log.debug("No PortComponentInfo found for serviceID: " + wsID); 277 PortComponentInfo singleMatch = null; 278 Iterator it = wsRegistry.keySet().iterator(); 279 while (it.hasNext()) 280 { 281 String key = (String )it.next(); 282 if (key.endsWith(wsID) && singleMatch != null) 283 { 284 log.warn("Too many possible serviceID matches: " + key); 285 return null; 286 } 287 if (key.endsWith(wsID) && singleMatch == null) 288 { 289 log.debug("Found possible match: " + key); 290 singleMatch = (PortComponentInfo)wsRegistry.get(key); 291 } 292 } 293 pcInfo = singleMatch; 294 } 295 296 return pcInfo; 297 } 298 299 303 public PortComponentInfo[] listServiceEndpointInfos() 304 { 305 PortComponentInfo[] arr = new PortComponentInfo[wsRegistry.size()]; 306 wsRegistry.values().toArray(arr); 307 return arr; 308 } 309 310 314 public String listServiceEndpoints() 315 { 316 StringWriter sw = new StringWriter (); 317 PrintWriter pw = new PrintWriter (sw); 318 319 pw.println("<table>"); 320 pw.println("<tr><th>ID</th><th>Address</th></tr>"); 321 322 PortComponentInfo[] pcInfoArr = listServiceEndpointInfos(); 323 for (int i = 0; i < pcInfoArr.length; i++) 324 { 325 PortComponentInfo pcInfo = pcInfoArr[i]; 326 String wsID = pcInfo.getServiceID(); 327 PortComponentMetaData pcMetaData = pcInfo.getPortComponentMetaData(); 328 pw.println("<tr><td>" + wsID + "</td><td>" + pcMetaData.getServiceEndpointURL() + "</td></tr>"); 329 } 330 pw.println("</table>"); 331 pw.close(); 332 333 return sw.toString(); 334 } 335 336 341 public void deployService(PortComponentInfo pcInfo) throws Exception 342 { 343 DeploymentInfo di = pcInfo.getDeploymentInfo(); 344 PortComponentMetaData pcMetaData = pcInfo.getPortComponentMetaData(); 345 346 ObjectName oname = pcInfo.getObjectName(); 347 if (server.isRegistered(oname)) 348 throw new IllegalStateException ("Service already registerd, maybe the port-component-name is not unique: " + oname); 349 350 String serviceID = pcInfo.getServiceID(); 351 log.debug("deployService: " + serviceID); 352 353 ServiceDescription serviceDesc = getServiceDescription(pcInfo); 354 pcInfo.setServiceDesc(serviceDesc); 355 356 String wsdd = generateDeploymentWSDD(pcInfo); 357 Document genDoc = getDocumentBuilder().parse(new ByteArrayInputStream (wsdd.getBytes())); 358 359 String deploymentName = di.getCanonicalName(); 360 String dataDir = System.getProperty(ServerConfig.SERVER_DATA_DIR); 361 File wsddFile = new File (dataDir + "/wsdl/" + deploymentName + "/" + pcMetaData.getPortComponentName() + ".wsdd"); 362 wsddFile.getParentFile().mkdirs(); 363 364 FileWriter out = new FileWriter (wsddFile); 365 out.write(wsdd); 366 out.close(); 367 368 log.info("WSDD published to: " + wsddFile.getCanonicalPath()); 369 370 ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader(); 373 try 374 { 375 ClassLoader deploymentCL = (di.metaData instanceof WebMetaData ? ((WebMetaData)di.metaData).getContextLoader() : di.ucl); 376 Thread.currentThread().setContextClassLoader(deploymentCL); 377 378 MessageContext msgContext = new MessageContext(getAxisServer()); 379 new Admin().process(msgContext, genDoc.getDocumentElement()); 380 381 registerBeanMetaData(serviceDesc); 382 } 383 finally 384 { 385 Thread.currentThread().setContextClassLoader(ctxClassLoader); 386 } 387 388 PortComponent pc = new PortComponent(pcInfo); 390 server.registerMBean(pc, oname); 391 392 wsRegistry.put(serviceID, pcInfo); 393 394 String endpointURL = pcInfo.getServiceEndpointURL(); 395 log.info("Web Service deployed: " + endpointURL); 396 } 397 398 400 private void registerBeanMetaData(ServiceDescription serviceDesc) 401 { 402 Iterator it = serviceDesc.getTypMappings(); 403 while (it.hasNext()) 404 { 405 TypeMappingDescription typeMapping = (TypeMappingDescription)it.next(); 406 if (typeMapping.getMetaData() != null) 407 metaDataRegistry.registerTypeMappingMetaData(typeMapping); 408 } 409 } 410 411 418 private URL findTypeMappingMetaData(DeploymentInfo di) 419 { 420 URL resourceURL = null; 421 422 String dataDir = System.getProperty(ServerConfig.SERVER_DATA_DIR); 424 File resourceFile = new File (dataDir + "/wsdl/ws4ee-deployment.xml"); 425 if (resourceFile.exists()) 426 { 427 try 428 { 429 resourceURL = resourceFile.toURL(); 430 } 431 catch (MalformedURLException e) 432 { 433 log.warn("Could not get url to ws4ee-deployment.xml.", e); 434 } 435 } 436 437 if (resourceURL == null) 438 { 439 String infDir = (di.shortName.endsWith(".war") ? "WEB-INF" : "META-INF"); 440 String resName = infDir + "/ws4ee-deployment.xml"; 441 resourceURL = di.localCl.findResource(resName); 442 } 443 444 return resourceURL; 445 } 446 447 452 public void undeployService(String serviceID) throws Exception 453 { 454 PortComponentInfo pcInfo = getPortComponentInfo(serviceID); 455 if (pcInfo == null) 456 throw new IllegalStateException ("Cannot find port component info for: " + serviceID); 457 458 StringBuffer buffer = new StringBuffer (); 459 buffer.append("<undeployment"); 460 buffer.append(" xmlns='http://xml.apache.org/axis/wsdd/'"); 461 buffer.append(" xmlns:java='http://xml.apache.org/axis/wsdd/providers/java'"); 462 buffer.append(" xmlns:xsi='http://www.w3.org/2000/10/XMLSchema-instance'>"); 463 buffer.append(" <service name='" + serviceID + "'/>"); 464 buffer.append("</undeployment>"); 465 466 try 467 { 468 DocumentBuilder builder = getDocumentBuilder(); 469 Document doc = builder.parse(new ByteArrayInputStream (buffer.toString().getBytes())); 470 471 MessageContext msgContext = new MessageContext(getAxisServer()); 472 new Admin().process(msgContext, doc.getDocumentElement()); 473 } 474 catch (Exception e) 475 { 476 log.error("Cannot unregister Axis service: " + serviceID); 477 } 478 479 try 481 { 482 server.unregisterMBean(pcInfo.getObjectName()); 483 } 484 catch (Exception e) 485 { 486 log.error("Cannot unregister port component MBean: " + serviceID); 487 } 488 489 unregisterBeanMetaData(pcInfo.getServiceDescription()); 490 491 wsRegistry.remove(serviceID); 492 493 log.info("WebService undeployed: " + pcInfo.getServiceEndpointURL()); 494 } 495 496 498 private void unregisterBeanMetaData(ServiceDescription serviceDesc) 499 { 500 Iterator it = serviceDesc.getTypMappings(); 501 while (it.hasNext()) 502 { 503 TypeMappingDescription typeMapping = (TypeMappingDescription)it.next(); 504 if (typeMapping.getMetaData() != null) 505 metaDataRegistry.unregisterTypeMappingMetaData(typeMapping.getTypeQName()); 506 } 507 } 508 509 512 private ServiceDescription getServiceDescription(PortComponentInfo pcInfo) throws Exception 513 { 514 DeploymentInfo di = pcInfo.getDeploymentInfo(); 515 PortComponentMetaData pcMetaData = pcInfo.getPortComponentMetaData(); 516 WebserviceDescriptionMetaData wsdMetaData = pcMetaData.getWebserviceDescription(); 517 JavaWsdlMapping javaWsdlMapping = wsdMetaData.getJavaWsdlMapping(); 518 519 String portName = pcMetaData.getWsdlPort().getLocalPart(); 520 521 Definition wsdlDefinition = wsdMetaData.getWsdlDefinition(); 523 524 URL ws4eeMetaData = findTypeMappingMetaData(di); 526 527 ServiceDescription serviceDesc = new ServiceDescription(wsdlDefinition, javaWsdlMapping, ws4eeMetaData, portName); 528 return serviceDesc; 529 } 530 531 534 private String generateDeploymentWSDD(PortComponentInfo pcInfo) throws Exception 535 { 536 StringWriter strWriter = new StringWriter (1024); 537 PrintWriter out = new PrintWriter (strWriter); 538 539 DeploymentInfo di = pcInfo.getDeploymentInfo(); 540 PortComponentMetaData pcMetaData = pcInfo.getPortComponentMetaData(); 541 ServiceDescription serviceDesc = pcInfo.getServiceDescription(); 542 String serviceID = pcInfo.getServiceID(); 543 544 WSDDGenerator wsddGenerator = new WSDDGenerator(serviceDesc); 545 546 wsddGenerator.appendHeader(out); 547 wsddGenerator.appendServiceElement(out, serviceID, "Handler"); 548 549 String wsID = pcInfo.getServiceID(); 550 out.println(" <parameter name='" + PortComponentMetaData.PARAMETER_WEBSERVICE_ID + "' value='" + wsID + "' />"); 551 552 String ejbLink = pcMetaData.getEjbLink(); 553 String servletLink = pcMetaData.getServletLink(); 554 if (ejbLink != null) 555 { 556 out.println(" <parameter name='handlerClass' value='" + invokerProviderEJB + "' />"); 557 } 558 else if (servletLink != null) 559 { 560 if (servletLink.equals(JMXInvokerEndpoint.class.getName())) 561 out.println(" <parameter name='handlerClass' value='" + InvokerProviderJMX.class.getName() + "' />"); 562 else 563 out.println(" <parameter name='handlerClass' value='" + invokerProviderJSE + "' />"); 564 } 565 else 566 { 567 throw new IllegalArgumentException ("Cannot find <ejb-link> nor <servlet-link> in webservices.xml"); 568 } 569 570 out.println(); 571 572 wsddGenerator.appendOperations(out); 574 575 ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader(); 576 try 577 { 578 ClassLoader deploymentCL = (di.metaData instanceof WebMetaData ? ((WebMetaData)di.metaData).getContextLoader() : di.ucl); 579 Thread.currentThread().setContextClassLoader(deploymentCL); 580 581 wsddGenerator.appendTypeMappings(out); 583 } 584 finally 585 { 586 Thread.currentThread().setContextClassLoader(ctxClassLoader); 587 } 588 589 wsddGenerator.appendFooter(out); 590 out.close(); 591 592 return strWriter.toString(); 593 } 594 595 598 private DocumentBuilder getDocumentBuilder() 599 throws ParserConfigurationException 600 { 601 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 602 factory.setNamespaceAware(true); 603 DocumentBuilder builder = factory.newDocumentBuilder(); 604 return builder; 605 } 606 } 607 | Popular Tags |