📄 service.java
字号:
* * @param portName The name of the service port * @param proxyInterface The Remote object returned by this * method will also implement the given proxyInterface * @return java.rmi.Remote The stub implementation. * @throws ServiceException If there's an error */ public Remote getPort(QName portName, Class proxyInterface) throws ServiceException { if (wsdlService == null) throw new ServiceException(Messages.getMessage("wsdlMissing00")); Port port = wsdlService.getPort(portName.getLocalPart()); if (port == null) throw new ServiceException(Messages.getMessage("noPort00", "" + portName)); // First, try to find a generated stub. If that // returns null, then find a dynamic stub. Remote stub = getGeneratedStub(portName, proxyInterface); return stub != null ? stub : getPort(null, portName, proxyInterface); } /** * With the proxyInterface and the service's portName, we have * ALMOST enough info to find a generated stub. The generated * stub is named after the binding, which we can get from the * service's port. This binding is likely in the same namespace * (ie, package) that the proxyInterface is in. So try to find * and instantiate <proxyInterfacePackage>.<bindingName>Stub. * If it doesn't exist, return null. */ private Remote getGeneratedStub(QName portName, Class proxyInterface) { try { String pkg = proxyInterface.getName(); pkg = pkg.substring(0, pkg.lastIndexOf('.')); Port port = wsdlService.getPort(portName.getLocalPart()); String binding = port.getBinding().getQName().getLocalPart(); Class stubClass = ClassUtils.forName( pkg + "." + binding + "Stub"); if (proxyInterface.isAssignableFrom(stubClass)) { Class[] formalArgs = {javax.xml.rpc.Service.class}; Object[] actualArgs = {this}; Constructor ctor = stubClass.getConstructor(formalArgs); Stub stub = (Stub) ctor.newInstance(actualArgs); stub._setProperty( Stub.ENDPOINT_ADDRESS_PROPERTY, WSDLUtils.getAddressFromPort(port)); stub.setPortName(portName); return (Remote) stub; } else { return null; } } catch (Throwable t) { return null; } } // getGeneratedStub /** * Return a dynamic proxy for the given proxy interface. * * @param proxyInterface The Remote object returned by this * method will also implement the given proxyInterface * @return java.rmi.Remote The stub implementation * @throws ServiceException If there's an error */ public Remote getPort(Class proxyInterface) throws ServiceException { if (wsdlService == null) throw new ServiceException(Messages.getMessage("wsdlMissing00")); Map ports = wsdlService.getPorts(); if (ports == null || ports.size() <= 0) throw new ServiceException(Messages.getMessage("noPort00", "")); // Get the name of the class (without package name) String clazzName = proxyInterface.getName(); if(clazzName.lastIndexOf('.')!=-1) { clazzName = clazzName.substring(clazzName.lastIndexOf('.')+1); } // Pick the port with the same name as the class Port port = (Port) ports.get(clazzName); if(port == null) { // If not found, just pick the first port. port = (Port) ports.values().iterator().next(); } // First, try to find a generated stub. If that // returns null, then find a dynamic stub. Remote stub = getGeneratedStub(new QName(port.getName()), proxyInterface); return stub != null ? stub : getPort(null, new QName(port.getName()), proxyInterface); } /** * Return an object which acts as a dynamic proxy for the passed * interface class. This is a more "dynamic" version in that it * doesn't actually require WSDL, simply an endpoint address. * * Note: Not part of the JAX-RPC spec. * * @param endpoint the URL which will be used as the SOAP endpoint * @param proxyInterface the interface class which we wish to mimic * via a dynamic proxy * @throws ServiceException */ public Remote getPort(String endpoint, Class proxyInterface) throws ServiceException { return getPort(endpoint, null, proxyInterface); } private Remote getPort(String endpoint, QName portName, Class proxyInterface) throws ServiceException { if (!proxyInterface.isInterface()) { throw new ServiceException(Messages.getMessage("mustBeIface00")); } if (!(Remote.class.isAssignableFrom(proxyInterface))) { throw new ServiceException( Messages.getMessage("mustExtendRemote00")); } // Validate the proxyInterface if (wsdlParser != null) { Port port = wsdlService.getPort(portName.getLocalPart()); if (port == null) throw new ServiceException(Messages.getMessage("noPort00", "" + proxyInterface.getName())); Binding binding = port.getBinding(); SymbolTable symbolTable = wsdlParser.getSymbolTable(); BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName()); if(bEntry.getParameters().size() != proxyInterface.getMethods().length) { throw new ServiceException(Messages.getMessage("incompatibleSEI00", "" + proxyInterface.getName())); } // TODO: Check the methods and the parameters as well. } try { Call call = null; if (portName == null) { call = (org.apache.axis.client.Call) createCall(); if (endpoint != null) { call.setTargetEndpointAddress(new URL(endpoint)); } } else { call = (org.apache.axis.client.Call) createCall(portName); } ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); javax.xml.rpc.Stub stub = (javax.xml.rpc.Stub) Proxy.newProxyInstance(classLoader, new Class[]{proxyInterface, javax.xml.rpc.Stub.class}, new AxisClientProxy(call, portName)); if(stub instanceof org.apache.axis.client.Stub){ ((org.apache.axis.client.Stub) stub).setPortName(portName); } return (Remote) stub; } catch (Exception e) { throw new ServiceException( Messages.getMessage("wsdlError00", "" + "", "\n" + e)); } } // getPort /** * Creates a new Call object - will prefill as much info from the WSDL * as it can. Right now it's just the target URL of the Web Service. * * @param portName PortName in the WSDL doc to search for * @return Call Used for invoking the Web Service * @throws ServiceException If there's an error */ public javax.xml.rpc.Call createCall(QName portName) throws ServiceException { Call call = (org.apache.axis.client.Call) createCall(); call.setPortName(portName); // We can't prefill information if WSDL is not specified, // So just return the call that we just created. if (wsdlParser == null) return call; Port port = wsdlService.getPort(portName.getLocalPart()); if (port == null) throw new ServiceException(Messages.getMessage("noPort00", "" + portName)); Binding binding = port.getBinding(); PortType portType = binding.getPortType(); if (portType == null) throw new ServiceException(Messages.getMessage("noPortType00", "" + portName)); // Get the URL //////////////////////////////////////////////////////////////////// List list = port.getExtensibilityElements(); for (int i = 0; list != null && i < list.size(); i++) { Object obj = list.get(i); if (obj instanceof SOAPAddress) { try { SOAPAddress addr = (SOAPAddress) obj; URL url = new URL(addr.getLocationURI()); call.setTargetEndpointAddress(url); } catch (Exception exp) { throw new ServiceException( Messages.getMessage("cantSetURI00", "" + exp)); } } } return (call); } /** * Creates a new Call object - will prefill as much info from the WSDL * as it can. Right now it's target URL, SOAPAction, Parameter types, * and return type of the Web Service. * * @param portName PortName in the WSDL doc to search for * @param operationName Operation(method) that's going to be invoked * @return Call Used for invoking the Web Service * @throws ServiceException If there's an error */ public javax.xml.rpc.Call createCall(QName portName, String operationName) throws ServiceException { Call call = (org.apache.axis.client.Call) createCall(); call.setOperation(portName, operationName); return (call); } /** * Creates a new Call object - will prefill as much info from the WSDL * as it can. Right now it's target URL, SOAPAction, Parameter types, * and return type of the Web Service. * * @param portName PortName in the WSDL doc to search for * @param operationName Operation(method) that's going to be invoked * @return Call Used for invoking the Web Service * @throws ServiceException If there's an error */ public javax.xml.rpc.Call createCall(QName portName, QName operationName) throws ServiceException { Call call = (org.apache.axis.client.Call) createCall(); call.setOperation(portName, operationName); return (call); } /** * Creates a new Call object with no prefilled data. This assumes * that the caller will set everything manually - no checking of * any kind will be done against the WSDL. * * @return Call Used for invoking the Web Service * @throws ServiceException If there's an error */ public javax.xml.rpc.Call createCall() throws ServiceException { _call = new org.apache.axis.client.Call(this); return _call; } /** * Gets an array of preconfigured Call objects for invoking operations * on the specified port. There is one Call object per operation that * can be invoked on the specified port. Each Call object is * pre-configured and does not need to be configured using the setter * methods on Call interface. * * This method requires the Service implementation class to have access * to the WSDL related metadata. * * @throws ServiceException - If this Service class does not have access * to the required WSDL metadata or if an illegal portName is specified. */ public javax.xml.rpc.Call[] getCalls(QName portName) throws ServiceException { if (portName == null) throw new ServiceException(Messages.getMessage("badPort00")); if (wsdlService == null) throw new ServiceException(Messages.getMessage("wsdlMissing00")); Port port = wsdlService.getPort(portName.getLocalPart()); if (port == null) throw new ServiceException(Messages.getMessage("noPort00", "" + portName)); Binding binding = port.getBinding(); SymbolTable symbolTable = wsdlParser.getSymbolTable(); BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName()); Iterator i = bEntry.getParameters().keySet().iterator(); Vector calls = new Vector(); while (i.hasNext()) { Operation operation = (Operation) i.next(); javax.xml.rpc.Call call = createCall(QName.valueOf(port.getName()), QName.valueOf(operation.getName())); calls.add(call); } javax.xml.rpc.Call[] array = new javax.xml.rpc.Call[calls.size()]; calls.toArray(array); return array; } /** * Returns the configured HandlerRegistry instance for this Service
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -