KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > util > WSDLUtilities


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: WSDLUtilities.java,v 1.6.4.2 2005/03/23 11:35:02 tdiesler Exp $
9
package org.jboss.webservice.util;
10
11 // $Id: WSDLUtilities.java,v 1.6.4.2 2005/03/23 11:35:02 tdiesler Exp $
12

13 import org.jboss.logging.Logger;
14 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping;
15 import org.jboss.webservice.metadata.jaxrpcmapping.ServiceEndpointInterfaceMapping;
16 import org.jboss.webservice.metadata.jaxrpcmapping.ServiceEndpointMethodMapping;
17
18 import javax.wsdl.Definition;
19 import javax.wsdl.Import;
20 import javax.wsdl.Operation;
21 import javax.wsdl.Port;
22 import javax.wsdl.PortType;
23 import javax.wsdl.Service;
24 import javax.wsdl.extensions.soap.SOAPAddress;
25 import javax.xml.rpc.ServiceException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * A utility class for common wsdl operations.
35  *
36  * @author Thomas.Diesler@jboss.org
37  * @since 15-May-2004
38  */

39 public final class WSDLUtilities
40 {
41    // provide logging
42
private static final Logger log = Logger.getLogger(WSDLUtilities.class);
43
44    public static final String JavaDoc WSDL_NAMESPACE_URI = "http://schemas.xmlsoap.org/wsdl/".intern();
45    public static final String JavaDoc XSD_NAMESPACE_URI = "http://www.w3.org/2001/XMLSchema".intern();
46
47    // hide the constructor
48
private WSDLUtilities()
49    {
50    }
51
52    /**
53     * Validate that the operations in the given SEI correspond to the wsdl operations
54     */

55    public static void endorseServiceEndpointInterface(Definition wsdlDefinition, Class JavaDoc seiClass, JavaWsdlMapping jaxrpcMapping)
56            throws ServiceException JavaDoc
57    {
58       if (wsdlDefinition == null)
59          throw new IllegalArgumentException JavaDoc("WSDL definition cannot be null");
60
61       if (seiClass == null)
62          throw new IllegalArgumentException JavaDoc("Service endpoint interface cannot be null");
63
64       if (jaxrpcMapping == null)
65          throw new IllegalArgumentException JavaDoc("JavaWsdlMapping cannot be null");
66
67       if (java.rmi.Remote JavaDoc.class.isAssignableFrom(seiClass) == false)
68          throw new IllegalArgumentException JavaDoc("The service endpoint interface does not implement java.rmi.Remote: " + seiClass.getName());
69
70       // Get the list of method names in the SEI
71
List JavaDoc seiMethodNames = new ArrayList JavaDoc();
72       Method JavaDoc[] seiMethods = seiClass.getDeclaredMethods();
73       for (int i = 0; i < seiMethods.length; i++)
74       {
75          Method JavaDoc method = seiMethods[i];
76          seiMethodNames.add(method.getName());
77       }
78
79       // Map the wsdl-operation to the java-method-name
80
Map JavaDoc semMap = new HashMap JavaDoc();
81       ServiceEndpointInterfaceMapping[] seiMappings = jaxrpcMapping.getServiceEndpointInterfaceMappings();
82       for (int i = 0; i < seiMappings.length; i++)
83       {
84          ServiceEndpointInterfaceMapping seiMapping = seiMappings[i];
85          if (seiClass.getName().equals(seiMapping.getServiceEndpointInterface()))
86          {
87             ServiceEndpointMethodMapping[] semMappings = seiMapping.getServiceEndpointMethodMappings();
88             for (int j = 0; j < semMappings.length; j++)
89             {
90                ServiceEndpointMethodMapping semMapping = semMappings[j];
91                semMap.put(semMapping.getWsdlOperation(), semMapping.getJavaMethodName());
92             }
93          }
94       }
95
96       // Map the port type qname to the list of its operations
97
Map JavaDoc portTypeOperationsMap = new HashMap JavaDoc();
98
99       // init WSDL operation names
100
Iterator JavaDoc itPortType = getPortTypesWithImport(wsdlDefinition).iterator();
101       while (itPortType.hasNext())
102       {
103          List JavaDoc ptMethods = new ArrayList JavaDoc();
104          PortType portType = (PortType)itPortType.next();
105          Iterator JavaDoc itop = portType.getOperations().iterator();
106          while (itop.hasNext())
107          {
108             String JavaDoc wsdlOp = ((Operation)itop.next()).getName();
109             String JavaDoc javaOp = (String JavaDoc)semMap.get(wsdlOp);
110             ptMethods.add(javaOp != null ? javaOp : wsdlOp);
111          }
112          portTypeOperationsMap.put(portType.getQName(), ptMethods);
113       }
114
115       boolean portTypeFound = false;
116
117       Iterator JavaDoc it = portTypeOperationsMap.values().iterator();
118       while (it.hasNext())
119       {
120          boolean seiOpInPortType = true;
121          List JavaDoc opList = (List JavaDoc)it.next();
122          for (int i = 0; i < seiMethods.length; i++)
123          {
124             Method JavaDoc seiMethod = seiMethods[i];
125             seiOpInPortType &= opList.contains(seiMethod.getName());
126          }
127
128          // all sei methods were found in this wsdl port type
129
if (seiOpInPortType == true)
130          {
131             portTypeFound = true;
132             break;
133          }
134       }
135
136       if (portTypeFound == false)
137          throw new ServiceException JavaDoc("Cannot find SEI operations " + seiMethodNames + " in any wsdl port type, we have " + portTypeOperationsMap);
138    }
139
140    /**
141     * Get the complete list of PortTypes including all the imports as well
142     */

143    private static List JavaDoc getPortTypesWithImport(Definition wsdl)
144    {
145       ArrayList JavaDoc pTypes = new ArrayList JavaDoc(wsdl.getPortTypes().values());
146       Iterator JavaDoc it = wsdl.getImports().values().iterator();
147       while (it.hasNext())
148       {
149          List JavaDoc imports = (List JavaDoc)it.next();
150          for (int i = 0; i < imports.size(); i++)
151          {
152             Import imp = (Import)imports.get(i);
153             Definition subdef = imp.getDefinition();
154             pTypes.addAll(subdef.getPortTypes().values());
155             pTypes.addAll(getPortTypesWithImport(subdef));
156          }
157       }
158       return pTypes;
159    }
160
161    /**
162     * Get the list of available service locations
163     */

164    public static String JavaDoc[] getServiceLocations(Definition wsdlDefinition)
165    {
166       ArrayList JavaDoc locations = new ArrayList JavaDoc();
167
168       Service wsdlService = (Service)wsdlDefinition.getServices().values().iterator().next();
169
170       Map JavaDoc wsdlPorts = wsdlService.getPorts();
171       Iterator JavaDoc it = wsdlPorts.values().iterator();
172       while (it.hasNext())
173       {
174          Port wsdlPort = (Port)it.next();
175          Iterator JavaDoc itElements = wsdlPort.getExtensibilityElements().iterator();
176          while (itElements.hasNext())
177          {
178             Object JavaDoc obj = itElements.next();
179             if (obj instanceof SOAPAddress)
180             {
181                SOAPAddress address = (SOAPAddress)obj;
182                String JavaDoc wsdlURI = address.getLocationURI();
183                locations.add(wsdlURI);
184             }
185          }
186       }
187
188       String JavaDoc[] arr = new String JavaDoc[locations.size()];
189       locations.toArray(arr);
190       return arr;
191    }
192 }
Popular Tags