1 55 56 package org.jboss.axis.client; 57 58 import org.jboss.axis.AxisEngine; 59 import org.jboss.axis.EngineConfiguration; 60 import org.jboss.axis.configuration.EngineConfigurationFactoryFinder; 61 import org.jboss.axis.utils.ClassUtils; 62 import org.jboss.axis.utils.Messages; 63 import org.jboss.axis.utils.WSDLUtils; 64 import org.jboss.axis.utils.XMLUtils; 65 import org.jboss.axis.wsdl.gen.Parser; 66 import org.jboss.axis.wsdl.symbolTable.BindingEntry; 67 import org.jboss.axis.wsdl.symbolTable.ServiceEntry; 68 import org.jboss.axis.wsdl.symbolTable.SymbolTable; 69 import org.jboss.logging.Logger; 70 import org.w3c.dom.Document ; 71 72 import javax.naming.Reference ; 73 import javax.naming.Referenceable ; 74 import javax.naming.StringRefAddr ; 75 import javax.wsdl.Binding; 76 import javax.wsdl.Operation; 77 import javax.wsdl.Port; 78 import javax.wsdl.PortType; 79 import javax.wsdl.extensions.soap.SOAPAddress; 80 import javax.xml.namespace.QName ; 81 import javax.xml.rpc.ServiceException ; 82 import javax.xml.rpc.encoding.TypeMappingRegistry ; 83 import javax.xml.rpc.handler.HandlerRegistry ; 84 import java.io.InputStream ; 85 import java.io.Serializable ; 86 import java.lang.reflect.Constructor ; 87 import java.lang.reflect.Proxy ; 88 import java.net.MalformedURLException ; 89 import java.net.URL ; 90 import java.rmi.Remote ; 91 import java.util.HashMap ; 92 import java.util.Hashtable ; 93 import java.util.Iterator ; 94 import java.util.List ; 95 import java.util.Map ; 96 import java.util.Vector ; 97 98 109 110 public class Service implements javax.xml.rpc.Service , Serializable , Referenceable 111 { 112 113 static final long serialVersionUID = 3948774801925955759L; 114 115 private transient static Logger log = Logger.getLogger(Service.class.getName()); 116 117 protected transient AxisEngine engine; 118 protected transient EngineConfiguration config; 119 120 protected QName serviceName; 121 protected String wsdlLocation; 122 protected javax.wsdl.Service wsdlService; 123 124 private boolean maintainSession; 125 private Parser wsdlParser; 126 127 private HandlerRegistryImpl registry = new HandlerRegistryImpl(); 128 129 protected static HashMap cachedWSDL = new HashMap (); 130 protected static boolean cachingWSDL = false; 131 132 135 private Hashtable transportImpls = new Hashtable (); 136 137 142 public Service() 143 { 144 engine = getAxisClient(); 145 } 146 147 152 public Service(QName serviceName) 153 { 154 this.serviceName = serviceName; 155 engine = getAxisClient(); 156 } 157 158 163 public Service(EngineConfiguration config) 164 { 165 this.config = config; 166 engine = getAxisClient(); 167 } 168 169 177 public Service(URL wsdlDoc, QName serviceName) throws ServiceException 178 { 179 this.serviceName = serviceName; 180 engine = getAxisClient(); 181 wsdlLocation = wsdlDoc.toString(); 182 Parser parser = null; 183 184 if (cachingWSDL && 185 (parser = (Parser)cachedWSDL.get(this.wsdlLocation.toString())) != null) 186 { 187 initService(parser, serviceName); 188 } 189 else 190 { 191 initService(wsdlDoc.toString(), serviceName); 192 } 193 } 194 195 202 public Service(Parser parser, QName serviceName) throws ServiceException 203 { 204 this.serviceName = serviceName; 205 engine = getAxisClient(); 206 initService(parser, serviceName); 207 } 208 209 220 public Service(String wsdlLocation, QName serviceName) 221 throws ServiceException 222 { 223 this.serviceName = serviceName; 224 this.wsdlLocation = wsdlLocation; 225 engine = getAxisClient(); 226 Parser parser = null; 228 if (cachingWSDL && 229 (parser = (Parser)cachedWSDL.get(wsdlLocation)) != null) 230 { 231 initService(parser, serviceName); 232 } 233 else 234 { 235 initService(wsdlLocation, serviceName); 236 } 237 } 238 239 249 public Service(InputStream wsdlInputStream, QName serviceName) 250 throws ServiceException 251 { 252 engine = getAxisClient(); 253 Document doc = null; 254 try 255 { 256 doc = XMLUtils.newDocument(wsdlInputStream); 257 } 258 catch (Exception exp) 259 { 260 throw new ServiceException (Messages.getMessage("wsdlError00", "" + "", "\n" + exp)); 261 } 262 initService(null, doc, serviceName); 263 } 264 265 272 private void initService(String url, QName serviceName) 273 throws ServiceException 274 { 275 try 276 { 277 Parser parser = getParser(); 279 parser.run(url); 280 281 if (cachingWSDL && this.wsdlLocation != null) 282 cachedWSDL.put(url, parser); 283 284 initService(parser, serviceName); 285 } 286 catch (Exception exp) 287 { 288 log.error(exp.getMessage(), exp); 289 throw new ServiceException (Messages.getMessage("wsdlError00", "" + "", "\n" + exp)); 290 } 291 } 292 293 297 protected Parser getParser() 298 { 299 Parser parser = new Parser(); 300 return parser; 301 } 302 303 311 private void initService(String context, Document doc, QName serviceName) 312 throws ServiceException 313 { 314 try 315 { 316 Parser parser = getParser(); 318 parser.run(context, doc); 319 320 initService(parser, serviceName); 321 } 322 catch (Exception exp) 323 { 324 throw new ServiceException (Messages.getMessage("wsdlError00", "" + "", "\n" + exp)); 325 } 326 } 327 328 335 private void initService(Parser parser, QName serviceName) throws ServiceException 336 { 337 this.wsdlParser = parser; 338 339 try 340 { 341 if (serviceName != null) 343 { 344 ServiceEntry serviceEntry = parser.getSymbolTable().getServiceEntry(serviceName); 345 if (serviceEntry != null) 346 this.wsdlService = serviceEntry.getService(); 347 if (this.wsdlService == null) 348 throw new ServiceException (Messages.getMessage("noService00", "" + serviceName)); 349 } 350 351 } 352 catch (Exception exp) 353 { 354 throw new ServiceException (Messages.getMessage("wsdlError00", "" + "", "\n" + exp)); 355 } 356 } 357 358 protected javax.wsdl.Service getWSDLService() 359 { 360 return (wsdlService); 361 } 362 363 public Parser getWSDLParser() 364 { 365 return (wsdlParser); 366 } 367 368 protected AxisClient getAxisClient() 369 { 370 return new AxisClient(getEngineConfiguration()); 371 } 372 373 383 public Remote getPort(QName portName, Class proxyInterface) 384 throws ServiceException 385 { 386 387 if (wsdlService == null) 388 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 389 390 Port port = wsdlService.getPort(portName.getLocalPart()); 391 if (port == null) 392 throw new ServiceException (Messages.getMessage("noPort00", "" + portName)); 393 394 Remote stub = getGeneratedStub(portName, proxyInterface); 397 return stub != null ? stub : getPort(null, portName, proxyInterface); 398 } 399 400 409 protected Remote getGeneratedStub(QName portName, Class proxyInterface) throws ServiceException 410 { 411 try 412 { 413 if (portName != null && proxyInterface != null) 414 { 415 String pkg = proxyInterface.getName(); 416 pkg = pkg.substring(0, pkg.lastIndexOf('.')); 417 Port port = wsdlService.getPort(portName.getLocalPart()); 418 String binding = port.getBinding().getQName().getLocalPart(); 419 Class stubClass = ClassUtils.forName(pkg + "." + binding + "Stub"); 420 if (proxyInterface.isAssignableFrom(stubClass)) 421 { 422 Class [] formalArgs = {javax.xml.rpc.Service .class}; 423 Object [] actualArgs = {this}; 424 Constructor ctor = stubClass.getConstructor(formalArgs); 425 Stub stub = (Stub)ctor.newInstance(actualArgs); 426 stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, 427 WSDLUtils.getAddressFromPort(port)); 428 stub.setPortName(portName); 429 return (Remote )stub; 430 } 431 } 432 return null; 433 } 434 catch (Throwable t) 435 { 436 return null; 437 } 438 } 440 448 public Remote getPort(Class proxyInterface) throws ServiceException 449 { 450 451 Port port = getWSDLPort(proxyInterface); 453 454 QName portName = (port != null ? new QName (port.getName()) : null); 456 457 Remote stub = getGeneratedStub(portName, proxyInterface); 460 return stub != null ? stub : getPort(null, portName, proxyInterface); 461 } 462 463 473 protected Port getWSDLPort(Class seiClass) throws ServiceException 474 { 475 476 if (wsdlService == null) 477 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 478 479 Map ports = wsdlService.getPorts(); 480 if (ports == null || ports.size() <= 0) 481 throw new ServiceException (Messages.getMessage("noPort00", "")); 482 483 String clazzName = seiClass.getName(); 485 if (clazzName.lastIndexOf('.') != -1) 486 { 487 clazzName = clazzName.substring(clazzName.lastIndexOf('.') + 1); 488 } 489 490 Port port = (Port)ports.get(clazzName); 492 if (port == null) 493 { 494 port = (Port)ports.values().iterator().next(); 496 } 497 498 return port; 499 } 500 501 513 public Remote getPort(String endpoint, Class proxyInterface) throws ServiceException 514 { 515 return getPort(endpoint, null, proxyInterface); 516 } 517 518 protected Remote getPort(String endpoint, QName portName, Class proxyInterface) 519 throws ServiceException 520 { 521 522 if (!proxyInterface.isInterface()) 523 { 524 throw new ServiceException (Messages.getMessage("mustBeIface00")); 525 } 526 527 if (!(Remote .class.isAssignableFrom(proxyInterface))) 528 { 529 throw new ServiceException (Messages.getMessage("mustExtendRemote00")); 530 } 531 532 try 533 { 534 Call call = null; 535 if (portName == null) 536 { 537 call = (org.jboss.axis.client.Call)createCall(); 538 if (endpoint != null) 539 { 540 call.setTargetEndpointAddress(new URL (endpoint)); 541 } 542 } 543 else 544 { 545 call = (org.jboss.axis.client.Call)createCall(portName); 546 } 547 ClassLoader classLoader = 548 Thread.currentThread().getContextClassLoader(); 549 return (Remote )Proxy.newProxyInstance(classLoader, 550 new Class []{proxyInterface, javax.xml.rpc.Stub .class}, 551 new AxisClientProxy(call, portName)); 552 } 553 catch (ServiceException e) 554 { 555 throw e; 556 } 557 catch (Exception e) 558 { 559 throw new ServiceException (e); 560 } 561 } 562 563 571 public javax.xml.rpc.Call createCall(QName portName) throws ServiceException 572 { 573 574 Call call = (org.jboss.axis.client.Call)createCall(); 575 call.setPortName(portName); 576 577 if (wsdlParser == null) 580 return call; 581 582 String targetEndpointAddress = getTargetEnpointAddress(); 584 585 if (targetEndpointAddress == null) 587 { 588 Port port = wsdlService.getPort(portName.getLocalPart()); 589 if (port == null) 590 throw new ServiceException (Messages.getMessage("noPort00", "" + portName)); 591 592 Binding binding = port.getBinding(); 593 PortType portType = binding.getPortType(); 594 if (portType == null) 595 throw new ServiceException (Messages.getMessage("noPortType00", "" + portName)); 596 597 List list = port.getExtensibilityElements(); 600 for (int i = 0; list != null && i < list.size(); i++) 601 { 602 Object obj = list.get(i); 603 if (obj instanceof SOAPAddress) 604 { 605 SOAPAddress addr = (SOAPAddress)obj; 606 targetEndpointAddress = addr.getLocationURI(); 607 } 608 } 609 } 610 611 try 612 { 613 call.setTargetEndpointAddress(new URL (targetEndpointAddress)); 614 } 615 catch (Exception exp) 616 { 617 throw new ServiceException (Messages.getMessage("cantSetURI00", "" + exp)); 618 } 619 620 return (call); 621 } 622 623 628 protected String getTargetEnpointAddress() 629 { 630 return null; 631 } 632 633 643 public javax.xml.rpc.Call createCall(QName portName, 644 String operationName) 645 throws ServiceException 646 { 647 648 Call call = (org.jboss.axis.client.Call)createCall(); 649 call.setOperation(portName, operationName); 650 return (call); 651 } 652 653 663 public javax.xml.rpc.Call createCall(QName portName, 664 QName operationName) 665 throws ServiceException 666 { 667 668 Call call = (org.jboss.axis.client.Call)createCall(); 669 call.setOperation(portName, operationName.getLocalPart()); 670 return (call); 671 } 672 673 681 public javax.xml.rpc.Call createCall() throws ServiceException 682 { 683 Call call = new org.jboss.axis.client.Call(this); 684 return call; 685 } 686 687 700 public javax.xml.rpc.Call [] getCalls(QName portName) throws ServiceException 701 { 702 if (portName == null) 703 throw new ServiceException (Messages.getMessage("badPort00")); 704 705 if (wsdlService == null) 706 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 707 708 Port port = wsdlService.getPort(portName.getLocalPart()); 709 if (port == null) 710 throw new ServiceException (Messages.getMessage("noPort00", "" + portName)); 711 712 Binding binding = port.getBinding(); 713 SymbolTable symbolTable = wsdlParser.getSymbolTable(); 714 BindingEntry bEntry = 715 symbolTable.getBindingEntry(binding.getQName()); 716 Iterator i = bEntry.getParameters().keySet().iterator(); 717 718 Vector calls = new Vector (); 719 while (i.hasNext()) 720 { 721 Operation operation = (Operation)i.next(); 722 javax.xml.rpc.Call call = createCall(QName.valueOf(port.getName()), 723 QName.valueOf(operation.getName())); 724 calls.add(call); 725 } 726 javax.xml.rpc.Call [] array = new javax.xml.rpc.Call [calls.size()]; 727 calls.toArray(array); 728 return array; 729 } 730 731 745 public HandlerRegistry getHandlerRegistry() 746 { 747 return registry; 748 } 749 750 756 public URL getWSDLDocumentLocation() 757 { 758 try 759 { 760 return new URL (wsdlLocation); 761 } 762 catch (MalformedURLException e) 763 { 764 return null; 765 } 766 } 767 768 773 public QName getServiceName() 774 { 775 776 if (serviceName != null) 777 return serviceName; 778 779 if (wsdlService == null) 780 return null; 781 782 QName qn = wsdlService.getQName(); 783 return (new QName (qn.getNamespaceURI(), qn.getLocalPart())); 784 } 785 786 796 public Iterator getPorts() throws ServiceException 797 { 798 799 if (wsdlService == null) 800 throw new ServiceException (Messages.getMessage("wsdlMissing00")); 801 802 if (wsdlService.getPorts() == null) 803 return new Vector ().iterator(); 804 805 return wsdlService.getPorts().keySet().iterator(); 806 } 807 808 814 public void setTypeMappingRegistry(TypeMappingRegistry registry) 815 throws ServiceException 816 { 817 } 818 819 824 public TypeMappingRegistry getTypeMappingRegistry() 825 { 826 return (engine.getTypeMappingRegistry()); 827 } 828 829 834 public Reference getReference() 835 { 836 String classname = this.getClass().getName(); 837 Reference reference = new Reference (classname, 838 "org.jboss.axis.client.ServiceFactory", null); 839 StringRefAddr addr = null; 840 if (!classname.equals("org.jboss.axis.client.Service")) 841 { 842 addr = new StringRefAddr (ServiceFactory.SERVICE_CLASSNAME, classname); 845 reference.add(addr); 846 } 847 else 848 { 849 if (wsdlLocation != null) 850 { 851 addr = new StringRefAddr (ServiceFactory.WSDL_LOCATION, wsdlLocation.toString()); 852 reference.add(addr); 853 } 854 QName serviceName = getServiceName(); 855 if (serviceName != null) 856 { 857 addr = new StringRefAddr (ServiceFactory.SERVICE_NAMESPACE, 858 serviceName.getNamespaceURI()); 859 reference.add(addr); 860 addr = new StringRefAddr (ServiceFactory.SERVICE_LOCAL_PART, 861 serviceName.getLocalPart()); 862 reference.add(addr); 863 } 864 } 865 if (maintainSession) 866 { 867 addr = new StringRefAddr (ServiceFactory.MAINTAIN_SESSION, "true"); 868 reference.add(addr); 869 } 870 return reference; 871 } 872 873 881 public void setEngine(AxisEngine engine) 882 { 883 this.engine = engine; 884 } 885 886 894 public AxisEngine getEngine() 895 { 896 return (engine); 897 } 898 899 932 public void setEngineConfiguration(EngineConfiguration config) 933 { 934 this.config = config; 935 } 936 937 940 protected EngineConfiguration getEngineConfiguration() 941 { 942 if (this.config == null) 943 { 944 this.config = EngineConfigurationFactoryFinder.newFactory().getClientEngineConfig(); 945 } 946 return config; 947 } 948 949 960 public void setMaintainSession(boolean yesno) 961 { 962 maintainSession = yesno; 963 } 964 965 968 public boolean getMaintainSession() 969 { 970 return maintainSession; 971 } 972 973 976 public boolean getCacheWSDL() 977 { 978 return cachingWSDL; 979 } 980 981 985 public void setCacheWSDL(boolean flag) 986 { 987 cachingWSDL = flag; 988 } 989 990 protected static class HandlerRegistryImpl implements HandlerRegistry 991 { 992 Map map = new HashMap (); 993 994 public List getHandlerChain(QName portName) 995 { 996 List list = (List )map.get(portName); 997 if (list == null) 998 { 999 list = new java.util.ArrayList (); 1000 setHandlerChain(portName, list); 1001 } 1002 return list; 1003 } 1004 1005 public void setHandlerChain(QName portName, List chain) 1006 { 1007 map.put(portName, chain); 1008 } 1009 } 1010 1011 1014 void registerTransportForURL(URL url, Transport transport) 1015 { 1016 transportImpls.put(url, transport); 1017 } 1018 1019 1022 Transport getTransportForURL(URL url) 1023 { 1024 return (Transport)transportImpls.get(url); 1025 } 1026 1027} 1028 | Popular Tags |