2ronwalf// The MIT License
2ronwalf//
2ronwalf// Copyright (c) 2004 Evren Sirin
2ronwalf//
2ronwalf// Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf// of this software and associated documentation files (the "Software"), to
2ronwalf// deal in the Software without restriction, including without limitation the
2ronwalf// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf// sell copies of the Software, and to permit persons to whom the Software is
2ronwalf// furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf// The above copyright notice and this permission notice shall be included in
2ronwalf// all copies or substantial portions of the Software.
2ronwalf//
2ronwalf// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf// IN THE SOFTWARE.
2ronwalf
2ronwalfpackage org.mindswap.wsdl;
2ronwalf
2ronwalfimport java.net.URI;
2ronwalfimport java.util.ArrayList;
2ronwalfimport java.util.HashMap;
2ronwalfimport java.util.Iterator;
2ronwalfimport java.util.List;
2ronwalfimport java.util.Map;
2ronwalfimport java.util.Vector;
2ronwalf
2ronwalfimport javax.wsdl.Binding;
2ronwalfimport javax.wsdl.Message;
2ronwalfimport javax.wsdl.Operation;
2ronwalfimport javax.wsdl.Port;
2ronwalfimport javax.wsdl.Service;
2ronwalfimport javax.wsdl.extensions.soap.SOAPAddress;
2ronwalfimport javax.xml.namespace.QName;
2ronwalf
2ronwalfimport org.apache.axis.client.Call;
2ronwalfimport org.apache.axis.wsdl.gen.Parser;
2ronwalfimport org.apache.axis.wsdl.symbolTable.BindingEntry;
2ronwalfimport org.apache.axis.wsdl.symbolTable.Parameter;
2ronwalfimport org.apache.axis.wsdl.symbolTable.Parameters;
2ronwalfimport org.apache.axis.wsdl.symbolTable.ServiceEntry;
2ronwalfimport org.apache.axis.wsdl.symbolTable.SymTabEntry;
2ronwalfimport org.apache.axis.wsdl.symbolTable.SymbolTable;
2ronwalfimport org.w3c.dom.Node;
2ronwalf
2ronwalf
2ronwalf/**
2ronwalf * Represents a WSDL service
2ronwalf *
2ronwalf * @author Evren Sirin
2ronwalf */
2ronwalfpublic class WSDLService {
2ronwalf public static boolean DEBUG = false;
2ronwalf //private boolean USE_TCPMON = false;
2ronwalf
2ronwalf private Parser wsdlParser = null;
2ronwalf private URI uri = null;
2ronwalf
2ronwalf private Map operations = new HashMap();
2ronwalf
2ronwalf public WSDLService(URI wsdlURL) throws Exception {
2ronwalf uri = wsdlURL;
2ronwalf
2ronwalf // Start by reading in the WSDL using Parser
2ronwalf wsdlParser = new Parser();
2ronwalf if(DEBUG) System.out.println("Reading WSDL document from '" + wsdlURL + "'");
2ronwalf wsdlParser.run(wsdlURL.toString());
2ronwalf
2ronwalf readOperations();
2ronwalf }
2ronwalf
2ronwalf static public WSDLService createService(String wsdlLoc) throws Exception {
2ronwalf return createService(URI.create(wsdlLoc));
2ronwalf }
2ronwalf
2ronwalf static public WSDLService createService(URI wsdlLoc) throws Exception {
2ronwalf return new WSDLService(wsdlLoc);
2ronwalf }
2ronwalf
2ronwalf private String createURI(QName qname) {
2ronwalf return qname.getNamespaceURI() + "#" + qname.getLocalPart();
2ronwalf }
2ronwalf
2ronwalf private String createURI(String localName) {
2ronwalf return uri + "#" + localName;
2ronwalf }
2ronwalf
2ronwalf private void readOperations() {
2ronwalf try {
2ronwalf String serviceNS = null;
2ronwalf String serviceName = null;
2ronwalf String operationName = null;
2ronwalf String portName = null;
2ronwalf
2ronwalf Service service = selectService(serviceNS, serviceName);
2ronwalf org.apache.axis.client.Service dpf =
2ronwalf new org.apache.axis.client.Service(wsdlParser, service.getQName());
2ronwalf
2ronwalf Port port = selectPort(service.getPorts(), portName);
2ronwalf if (portName == null) {
2ronwalf portName = port.getName();
2ronwalf }
2ronwalf Binding binding = port.getBinding();
2ronwalf
2ronwalf SymbolTable symbolTable = wsdlParser.getSymbolTable();
2ronwalf BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
2ronwalf Parameters parameters = null;
2ronwalf Iterator i = bEntry.getParameters().keySet().iterator();
2ronwalf
2ronwalf while (i.hasNext()) {
2ronwalf Operation o = (Operation) i.next();
2ronwalf operationName = o.getName();
2ronwalf
2ronwalf Call call = (Call) dpf.createCall(QName.valueOf(portName),
2ronwalf QName.valueOf(operationName));
2ronwalf WSDLOperation op = new WSDLOperation(call);
2ronwalf op.setService(this);
2ronwalf
2ronwalf operations.put(operationName, op);
2ronwalf
2ronwalf Message inputMessage = o.getInput().getMessage();
2ronwalf Message outputMessage = o.getOutput().getMessage();
2ronwalf
2ronwalf op.setOperationName(createURI(operationName));
2ronwalf op.setInputMessageName(createURI(inputMessage.getQName()));
2ronwalf op.setOutputMessageName(createURI(outputMessage.getQName()));
2ronwalf op.setPortName(createURI(port.getName()));
2ronwalf
2ronwalf if(DEBUG) {
2ronwalf System.out.println(" Operation : " + operationName);
2ronwalf System.out.println(" Port : " + portName + " -> " + op.getPortName());
2ronwalf System.out.println(" Input Msg : " + inputMessage.getQName() + " -> " + op.getInputMessageName());
2ronwalf System.out.println(" Output Msg: " + outputMessage.getQName() + " -> " + op.getOutputMessageName());
2ronwalf }
2ronwalf
2ronwalf
2ronwalf if(o.getDocumentationElement() != null) {
2ronwalf Node doc = o.getDocumentationElement().getFirstChild();
2ronwalf if(doc != null)
2ronwalf op.setDocumentation(doc.getNodeValue());
2ronwalf }
2ronwalf
2ronwalf parameters = (Parameters) bEntry.getParameters().get(o);
2ronwalf
2ronwalf // loop over parameters and set up in/out params
2ronwalf for (int j = 0; j < parameters.list.size(); ++j) {
2ronwalf Parameter p = (Parameter) parameters.list.get(j);
2ronwalf String name = createURI(p.getName());
2ronwalf QName type = p.getType().getQName();
2ronwalf
2ronwalf if (p.getMode() == Parameter.IN) { // IN
2ronwalf op.addInput(name, type);
2ronwalf
2ronwalf if(DEBUG)
2ronwalf System.out.println(" Input : " + name + " " + type);
2ronwalf } else if (p.getMode() == Parameter.OUT) { // OUT
2ronwalf op.addOutput(name, type);
2ronwalf
2ronwalf if(DEBUG)
2ronwalf System.out.println(" Output : " + name + " " + type);
2ronwalf } else if (p.getMode() == Parameter.INOUT) { // INOUT
2ronwalf op.addInput(name, type);
2ronwalf op.addOutput(name, type);
2ronwalf System.err.println("WARNING: A wsdl parameter is defined as INOUT is not tested yet");
2ronwalf System.err.println(" Parameter = " + name);
2ronwalf
2ronwalf if(DEBUG)
2ronwalf System.out.println(" InOut : " + name + " " + type);
2ronwalf }
2ronwalf
2ronwalf }
2ronwalf
2ronwalf // set output type
2ronwalf if (parameters.returnParam != null) {
2ronwalf Parameter p = parameters.returnParam;
2ronwalf String name = createURI(p.getName());
2ronwalf QName type = p.getType().getQName();
2ronwalf
2ronwalf op.addOutput(name, type);
2ronwalf
2ronwalf if(DEBUG)
2ronwalf System.out.println(" Return : " + name + " " + type);
2ronwalf }
2ronwalf
2ronwalf if(DEBUG) {
2ronwalf System.out.println(" Inputs : " + op.getInputs().size());
2ronwalf System.out.println(" Outputs : " + op.getOutputs().size());
2ronwalf System.out.println(" Document : " + op.getDocumentation());
2ronwalf System.out.println();
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf } catch(Exception e) {
2ronwalf System.err.println(e);
2ronwalf e.printStackTrace();
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf private Service selectService(String serviceNS, String serviceName)
2ronwalf throws Exception {
2ronwalf QName serviceQName = (((serviceNS != null)
2ronwalf && (serviceName != null))
2ronwalf ? new QName(serviceNS, serviceName)
2ronwalf : null);
2ronwalf ServiceEntry serviceEntry = (ServiceEntry) getSymTabEntry(serviceQName,
2ronwalf ServiceEntry.class);
2ronwalf return serviceEntry.getService();
2ronwalf }
2ronwalf
2ronwalf private SymTabEntry getSymTabEntry(QName qname, Class cls) {
2ronwalf HashMap map = wsdlParser.getSymbolTable().getHashMap();
2ronwalf Iterator iterator = map.entrySet().iterator();
2ronwalf
2ronwalf while (iterator.hasNext()) {
2ronwalf Map.Entry entry = (Map.Entry) iterator.next();
2ronwalf Vector v = (Vector) entry.getValue();
2ronwalf
2ronwalf if ((qname == null) || qname.equals(qname)) {
2ronwalf for (int i = 0; i < v.size(); ++i) {
2ronwalf SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
2ronwalf
2ronwalf if (cls.isInstance(symTabEntry)) {
2ronwalf return symTabEntry;
2ronwalf }
2ronwalf }
2ronwalf }
2ronwalf }
2ronwalf return null;
2ronwalf }
2ronwalf
2ronwalf private Port selectPort(Map ports, String portName) throws Exception {
2ronwalf Iterator valueIterator = ports.keySet().iterator();
2ronwalf while (valueIterator.hasNext()) {
2ronwalf String name = (String) valueIterator.next();
2ronwalf
2ronwalf if ((portName == null) || (portName.length() == 0)) {
2ronwalf Port port = (Port) ports.get(name);
2ronwalf List list = port.getExtensibilityElements();
2ronwalf
2ronwalf for (int i = 0; (list != null) && (i < list.size()); i++) {
2ronwalf Object obj = list.get(i);
2ronwalf if (obj instanceof SOAPAddress) {
2ronwalf return port;
2ronwalf }
2ronwalf }
2ronwalf } else if ((name != null) && name.equals(portName)) {
2ronwalf return (Port) ports.get(name);
2ronwalf }
2ronwalf }
2ronwalf return null;
2ronwalf }
2ronwalf
2ronwalf public URI getFileURI() {
2ronwalf return uri;
2ronwalf }
2ronwalf
2ronwalf public List getOperations() {
2ronwalf return new ArrayList(operations.values());
2ronwalf }
2ronwalf
2ronwalf public WSDLOperation getOperation(String opName) {
2ronwalf return (WSDLOperation) operations.get(opName);
2ronwalf }
2ronwalf}