1 16 17 package org.apache.axis.client; 18 19 import org.apache.axis.AxisEngine; 20 import org.apache.axis.EngineConfiguration; 21 import org.apache.axis.configuration.EngineConfigurationFactoryFinder; 22 import org.apache.axis.encoding.TypeMappingRegistryImpl; 23 import org.apache.axis.utils.ClassUtils; 24 import org.apache.axis.utils.Messages; 25 import org.apache.axis.utils.WSDLUtils; 26 import org.apache.axis.utils.XMLUtils; 27 import org.apache.axis.wsdl.gen.Parser; 28 import org.apache.axis.wsdl.symbolTable.BindingEntry; 29 import org.apache.axis.wsdl.symbolTable.ServiceEntry; 30 import org.apache.axis.wsdl.symbolTable.SymbolTable; 31 import org.w3c.dom.Document ; 32 33 import javax.naming.Reference ; 34 import javax.naming.Referenceable ; 35 import javax.naming.StringRefAddr ; 36 import javax.wsdl.Binding; 37 import javax.wsdl.Operation; 38 import javax.wsdl.Port; 39 import javax.wsdl.PortType; 40 import javax.wsdl.extensions.soap.SOAPAddress; 41 import javax.xml.namespace.QName ; 42 import javax.xml.rpc.ServiceException ; 43 import javax.xml.rpc.encoding.TypeMappingRegistry ; 44 import javax.xml.rpc.handler.HandlerRegistry ; 45 import java.io.InputStream ; 46 import java.io.Serializable ; 47 import java.lang.reflect.Constructor ; 48 import java.lang.reflect.Proxy ; 49 import java.net.MalformedURLException ; 50 import java.net.URL ; 51 import java.rmi.Remote ; 52 import java.util.HashMap ; 53 import java.util.Hashtable ; 54 import java.util.Iterator ; 55 import java.util.List ; 56 import java.util.Map ; 57 import java.util.Vector ; 58 59 70 71 public class Service implements javax.xml.rpc.Service , Serializable , Referenceable { 72 private transient AxisEngine engine = null; 73 private transient EngineConfiguration config = null; 74 75 private QName serviceName = null; 76 private String wsdlLocation = null; 77 private javax.wsdl.Service wsdlService = null; 78 private boolean maintainSession = false; 79 private HandlerRegistryImpl registry = new HandlerRegistryImpl(); 80 private Parser wsdlParser = null; 81 82 private static HashMap cachedWSDL = new HashMap (); 83 private static boolean cachingWSDL = true; 84 85 protected Call _call = null; 87 88 91 private Hashtable transportImpls = new Hashtable (); 92 93 94 protected javax.wsdl.Service getWSDLService() { 95 return (wsdlService); 96 } 97 98 public Parser getWSDLParser() { 99 return (wsdlParser); 100 } 101 102 protected AxisClient getAxisClient() { 103 return new AxisClient(getEngineConfiguration()); 104 } 105 106 111 public Service() { 112 engine = getAxisClient(); 113 } 114 115 120 public Service(QName serviceName) { 121 this.serviceName = serviceName; 122 engine = getAxisClient(); 123 } 124 125 131 public Service(EngineConfiguration engineConfiguration, AxisClient axisClient) { 132 this.config = engineConfiguration; 133 this.engine = axisClient; 134 } 135 136 141 public Service(EngineConfiguration config) { 142 this.config = config; 143 engine = getAxisClient(); 144 } 145 146 154 public Service(URL wsdlDoc, QName serviceName) throws ServiceException { 155 this.serviceName = serviceName; 156 engine = getAxisClient(); 157 wsdlLocation = wsdlDoc.toString(); 158 Parser parser = null; 159 160 if (cachingWSDL && 161 (parser = (Parser) cachedWSDL.get(this.wsdlLocation.toString())) != null) { 162 initService(parser, serviceName); 163 } else { 164 initService(wsdlDoc.toString(), serviceName); 165 } 166 } 167 168 175 public Service(Parser parser, QName serviceName) throws ServiceException { 176 this.serviceName = serviceName; 177 engine = getAxisClient(); 178 initService(parser, serviceName); 179 } 180 181 192 public Service(String wsdlLocation, QName serviceName) 193 throws ServiceException { 194 this.serviceName = serviceName; 195 this.wsdlLocation = wsdlLocation; 196 engine = getAxisClient(); 197 Parser parser = null; 199 if (cachingWSDL && 200 (parser = (Parser) cachedWSDL.get(wsdlLocation)) != null) { 201 initService(parser, serviceName); 202 } else { 203 initService(wsdlLocation, serviceName); 204 } 205 } 206 207 217 public Service(InputStream wsdlInputStream, QName serviceName) 218 throws ServiceException { 219 engine = getAxisClient(); 220 Document doc = null; 221 try { 222 doc = XMLUtils.newDocument(wsdlInputStream); 223 } catch (Exception exp) { 224 throw new ServiceException ( 225 Messages.getMessage("wsdlError00", "" + "", "\n" + exp)); 226 } 227 initService(null, doc, serviceName); 228 } 229 230 237 private void initService(String url, QName serviceName) 238 throws ServiceException { 239 try { 240 Parser parser = new Parser(); 242 parser.run(url); 243 244 if (cachingWSDL && this.wsdlLocation != null) 245 cachedWSDL.put(url, parser); 246 247 initService(parser, serviceName); 248 } catch (Exception exp) { 249 throw new ServiceException ( 250 Messages.getMessage("wsdlError00", "" + "", "\n" + exp), 251 exp); 252 } 253 } 254 255 263 private void initService(String context, Document doc, QName serviceName) 264 throws ServiceException { 265 try { 266 Parser parser = new Parser(); 268 parser.run(context, doc); 269 270 initService(parser, serviceName); 271 } catch (Exception exp) { 272 throw new ServiceException ( 273 Messages.getMessage("wsdlError00", "" + "", "\n" + exp)); 274 } 275 } 276 277 284 private void initService(Parser parser, QName serviceName) 285 throws ServiceException { 286 try { 287 this.wsdlParser = parser; 288 ServiceEntry serviceEntry = parser.getSymbolTable().getServiceEntry(serviceName); 289 if (serviceEntry != null) 290 this.wsdlService = serviceEntry.getService(); 291 if (this.wsdlService == null) 292 throw new ServiceException ( 293 Messages.getMessage("noService00", "" + serviceName)); 294 } catch (Exception exp) { 295 throw new ServiceException ( 296 Messages.getMessage("wsdlError00", "" + "", "\n" + exp)); 297 } 298 } 299 300 310 public Remote getPort(QName portName, Class proxyInterface) 311 throws ServiceException { 312 313 if (wsdlService == null) 314 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 315 316 Port port = wsdlService.getPort(portName.getLocalPart()); 317 if (port == null) 318 throw new ServiceException (Messages.getMessage("noPort00", "" + portName)); 319 320 Remote stub = getGeneratedStub(portName, proxyInterface); 323 return stub != null ? stub : getPort(null, portName, proxyInterface); 324 } 325 326 335 private Remote getGeneratedStub(QName portName, Class proxyInterface) { 336 try { 337 String pkg = proxyInterface.getName(); 338 pkg = pkg.substring(0, pkg.lastIndexOf('.')); 339 Port port = wsdlService.getPort(portName.getLocalPart()); 340 String binding = port.getBinding().getQName().getLocalPart(); 341 Class stubClass = ClassUtils.forName( 342 pkg + "." + binding + "Stub"); 343 if (proxyInterface.isAssignableFrom(stubClass)) { 344 Class [] formalArgs = {javax.xml.rpc.Service .class}; 345 Object [] actualArgs = {this}; 346 Constructor ctor = stubClass.getConstructor(formalArgs); 347 Stub stub = (Stub) ctor.newInstance(actualArgs); 348 stub._setProperty( 349 Stub.ENDPOINT_ADDRESS_PROPERTY, 350 WSDLUtils.getAddressFromPort(port)); 351 stub.setPortName(portName); 352 return (Remote ) stub; 353 } else { 354 return null; 355 } 356 } catch (Throwable t) { 357 return null; 358 } 359 } 361 369 public Remote getPort(Class proxyInterface) throws ServiceException { 370 if (wsdlService == null) 371 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 372 373 Map ports = wsdlService.getPorts(); 374 if (ports == null || ports.size() <= 0) 375 throw new ServiceException (Messages.getMessage("noPort00", "")); 376 377 String clazzName = proxyInterface.getName(); 379 if(clazzName.lastIndexOf('.')!=-1) { 380 clazzName = clazzName.substring(clazzName.lastIndexOf('.')+1); 381 } 382 383 Port port = (Port) ports.get(clazzName); 385 if(port == null) { 386 port = (Port) ports.values().iterator().next(); 388 } 389 390 Remote stub = getGeneratedStub(new QName (port.getName()), proxyInterface); 393 return stub != null ? stub : getPort(null, new QName (port.getName()), proxyInterface); 394 } 395 396 408 public Remote getPort(String endpoint, Class proxyInterface) 409 throws ServiceException { 410 return getPort(endpoint, null, proxyInterface); 411 } 412 413 private Remote getPort(String endpoint, QName portName, 414 Class proxyInterface) throws ServiceException { 415 if (!proxyInterface.isInterface()) { 416 throw new ServiceException (Messages.getMessage("mustBeIface00")); 417 } 418 419 if (!(Remote .class.isAssignableFrom(proxyInterface))) { 420 throw new ServiceException ( 421 Messages.getMessage("mustExtendRemote00")); 422 } 423 424 if (wsdlParser != null) { 426 Port port = wsdlService.getPort(portName.getLocalPart()); 427 if (port == null) 428 throw new ServiceException (Messages.getMessage("noPort00", "" + proxyInterface.getName())); 429 430 Binding binding = port.getBinding(); 431 SymbolTable symbolTable = wsdlParser.getSymbolTable(); 432 BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName()); 433 if(bEntry.getParameters().size() != proxyInterface.getMethods().length) { 434 throw new ServiceException (Messages.getMessage("incompatibleSEI00", "" + proxyInterface.getName())); 435 } 436 } 438 439 try { 440 Call call = null; 441 if (portName == null) { 442 call = (org.apache.axis.client.Call) createCall(); 443 if (endpoint != null) { 444 call.setTargetEndpointAddress(new URL (endpoint)); 445 } 446 } else { 447 call = (org.apache.axis.client.Call) createCall(portName); 448 } 449 ClassLoader classLoader = 450 Thread.currentThread().getContextClassLoader(); 451 javax.xml.rpc.Stub stub = (javax.xml.rpc.Stub ) Proxy.newProxyInstance(classLoader, 452 new Class []{proxyInterface, javax.xml.rpc.Stub .class}, 453 new AxisClientProxy(call, portName)); 454 if(stub instanceof org.apache.axis.client.Stub){ 455 ((org.apache.axis.client.Stub) stub).setPortName(portName); 456 } 457 return (Remote ) stub; 458 } catch (Exception e) { 459 throw new ServiceException ( 460 Messages.getMessage("wsdlError00", "" + "", "\n" + e)); 461 } 462 } 464 472 public javax.xml.rpc.Call createCall(QName portName) 473 throws ServiceException { 474 Call call = (org.apache.axis.client.Call) createCall(); 475 call.setPortName(portName); 476 477 if (wsdlParser == null) 480 return call; 481 482 Port port = wsdlService.getPort(portName.getLocalPart()); 483 if (port == null) 484 throw new ServiceException (Messages.getMessage("noPort00", "" + portName)); 485 486 Binding binding = port.getBinding(); 487 PortType portType = binding.getPortType(); 488 if (portType == null) 489 throw new ServiceException (Messages.getMessage("noPortType00", "" + portName)); 490 491 List list = port.getExtensibilityElements(); 494 for (int i = 0; list != null && i < list.size(); i++) { 495 Object obj = list.get(i); 496 if (obj instanceof SOAPAddress) { 497 try { 498 SOAPAddress addr = (SOAPAddress) obj; 499 URL url = new URL (addr.getLocationURI()); 500 call.setTargetEndpointAddress(url); 501 } catch (Exception exp) { 502 throw new ServiceException ( 503 Messages.getMessage("cantSetURI00", "" + exp)); 504 } 505 } 506 } 507 508 return (call); 509 } 510 511 521 public javax.xml.rpc.Call createCall(QName portName, 522 String operationName) 523 throws ServiceException { 524 525 Call call = (org.apache.axis.client.Call) createCall(); 526 call.setOperation(portName, operationName); 527 return (call); 528 } 529 530 540 public javax.xml.rpc.Call createCall(QName portName, 541 QName operationName) 542 throws ServiceException { 543 544 Call call = (org.apache.axis.client.Call) createCall(); 545 call.setOperation(portName, operationName); 546 return (call); 547 } 548 549 557 public javax.xml.rpc.Call createCall() throws ServiceException { 558 _call = new org.apache.axis.client.Call(this); 559 return _call; 560 } 561 562 575 public javax.xml.rpc.Call [] getCalls(QName portName) throws ServiceException { 576 if (portName == null) 577 throw new ServiceException (Messages.getMessage("badPort00")); 578 579 if (wsdlService == null) 580 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 581 582 Port port = wsdlService.getPort(portName.getLocalPart()); 583 if (port == null) 584 throw new ServiceException (Messages.getMessage("noPort00", "" + portName)); 585 586 Binding binding = port.getBinding(); 587 SymbolTable symbolTable = wsdlParser.getSymbolTable(); 588 BindingEntry bEntry = 589 symbolTable.getBindingEntry(binding.getQName()); 590 Iterator i = bEntry.getParameters().keySet().iterator(); 591 592 Vector calls = new Vector (); 593 while (i.hasNext()) { 594 Operation operation = (Operation) i.next(); 595 javax.xml.rpc.Call call = createCall(QName.valueOf(port.getName()), 596 QName.valueOf(operation.getName())); 597 calls.add(call); 598 } 599 javax.xml.rpc.Call [] array = new javax.xml.rpc.Call [calls.size()]; 600 calls.toArray(array); 601 return array; 602 } 603 604 617 public HandlerRegistry getHandlerRegistry() { 618 return registry; 619 } 620 621 627 public URL getWSDLDocumentLocation() { 628 try { 629 return new URL (wsdlLocation); 630 } catch (MalformedURLException e) { 631 return null; 632 } 633 } 634 635 640 public QName getServiceName() { 641 if (serviceName != null) return serviceName; 642 if (wsdlService == null) return (null); 643 QName qn = wsdlService.getQName(); 644 return (new QName (qn.getNamespaceURI(), qn.getLocalPart())); 645 } 646 647 657 public Iterator getPorts() throws ServiceException { 658 if (wsdlService == null) 659 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 660 661 if (wsdlService.getPorts() == null) { 662 return new Vector ().iterator(); 664 } 665 666 Map portmap = wsdlService.getPorts(); 667 List portlist = new java.util.ArrayList (portmap.size()); 668 Iterator portiterator = portmap.values().iterator(); 674 while (portiterator.hasNext()) { 675 Port port = (Port) portiterator.next(); 676 portlist.add(new QName (wsdlService.getQName().getNamespaceURI(), port.getName())); 682 } 683 684 return portlist.iterator(); 686 } 687 688 694 public void setTypeMappingRegistry(TypeMappingRegistry registry) 695 throws ServiceException { 696 } 697 698 703 public TypeMappingRegistry getTypeMappingRegistry() { 704 return (engine.getTypeMappingRegistry()); 705 } 706 707 712 public Reference getReference() { 713 String classname = this.getClass().getName(); 714 Reference reference = new Reference (classname, 715 "org.apache.axis.client.ServiceFactory", null); 716 StringRefAddr addr = null; 717 if (!classname.equals("org.apache.axis.client.Service")) { 718 addr = new StringRefAddr ( 721 ServiceFactory.SERVICE_CLASSNAME, classname); 722 reference.add(addr); 723 } else { 724 if (wsdlLocation != null) { 725 addr = new StringRefAddr ( 726 ServiceFactory.WSDL_LOCATION, wsdlLocation.toString()); 727 reference.add(addr); 728 } 729 QName serviceName = getServiceName(); 730 if (serviceName != null) { 731 addr = new StringRefAddr (ServiceFactory.SERVICE_NAMESPACE, 732 serviceName.getNamespaceURI()); 733 reference.add(addr); 734 addr = new StringRefAddr (ServiceFactory.SERVICE_LOCAL_PART, 735 serviceName.getLocalPart()); 736 reference.add(addr); 737 } 738 } 739 if (maintainSession) { 740 addr = new StringRefAddr (ServiceFactory.MAINTAIN_SESSION, "true"); 741 reference.add(addr); 742 } 743 return reference; 744 } 745 746 754 public void setEngine(AxisEngine engine) { 755 this.engine = engine; 756 } 757 758 766 public AxisEngine getEngine() { 767 return (engine); 768 } 769 770 803 public void setEngineConfiguration(EngineConfiguration config) { 804 this.config = config; 805 } 806 807 810 protected EngineConfiguration getEngineConfiguration() { 811 if (this.config == null) { 812 this.config = EngineConfigurationFactoryFinder.newFactory().getClientEngineConfig(); 813 } 814 return config; 815 } 816 817 828 public void setMaintainSession(boolean yesno) { 829 maintainSession = yesno; 830 } 831 832 835 public boolean getMaintainSession() { 836 return maintainSession; 837 } 838 839 846 public Call getCall() throws ServiceException { 847 return _call; 848 } 849 850 853 public boolean getCacheWSDL() { 854 return cachingWSDL; 855 } 856 857 861 public void setCacheWSDL(boolean flag) { 862 cachingWSDL = flag; 863 } 864 865 protected static class HandlerRegistryImpl implements HandlerRegistry { 866 Map map = new HashMap (); 867 868 public List getHandlerChain(QName portName) { 869 String key = portName.getLocalPart(); 871 List list = (List ) map.get(key); 872 if (list == null) { 873 list = new java.util.ArrayList (); 874 setHandlerChain(portName, list); 875 } 876 return list; 877 } 878 879 public void setHandlerChain(QName portName, List chain) { 880 map.put(portName.getLocalPart(), chain); 882 } 883 } 884 885 888 void registerTransportForURL(URL url, Transport transport) { 889 transportImpls.put(url.toString(), transport); 890 } 891 892 895 Transport getTransportForURL(URL url) { 896 return (Transport) transportImpls.get(url.toString()); 897 } 898 899 903 public void setTypeMappingVersion(String version) { 904 ((TypeMappingRegistryImpl)getTypeMappingRegistry()).doRegisterFromVersion(version); 905 } 906 } 907 | Popular Tags |