KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > metadata > WebserviceDescriptionMetaData


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

7 package org.jboss.webservice.metadata;
8
9 // $Id: WebserviceDescriptionMetaData.java,v 1.19.4.5 2005/06/17 22:33:06 tdiesler Exp $
10

11 import org.jboss.logging.Logger;
12 import org.jboss.webservice.ServiceDeployer;
13 import org.jboss.webservice.metadata.wsdl.WSDL11DefinitionFactory;
14 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping;
15 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMappingFactory;
16
17 import javax.wsdl.Definition;
18 import javax.wsdl.Port;
19 import javax.wsdl.Service;
20 import javax.wsdl.WSDLException;
21 import javax.wsdl.extensions.soap.SOAPAddress;
22 import javax.xml.rpc.JAXRPCException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * XML Binding element for <code>webservices/webservice-description</code>
33  *
34  * @author Thomas.Diesler@jboss.org
35  * @version $Revision: 1.19.4.5 $
36  * @since 15-April-2004
37  */

38 public class WebserviceDescriptionMetaData
39 {
40    // provide logging
41
private static final Logger log = Logger.getLogger(WebserviceDescriptionMetaData.class);
42
43    // The parent <webservices> element
44
private WebservicesMetaData webservices;
45
46    // The required <webservice-description-name> element
47
private String JavaDoc webserviceDescriptionName;
48    // The required <wsdl-file> element
49
private String JavaDoc wsdlFile;
50    // The required <jaxrpc-mapping-file> element
51
private String JavaDoc jaxrpcMappingFile;
52    // The required <port-component> elements
53
private ArrayList JavaDoc portComponents = new ArrayList JavaDoc();
54
55    // Where the wsdl file is published to
56
private String JavaDoc wsdlPublishLocation;
57
58    public WebserviceDescriptionMetaData(WebservicesMetaData webservices)
59    {
60       this.webservices = webservices;
61    }
62
63    public WebservicesMetaData getWebservices()
64    {
65       return webservices;
66    }
67
68    public void addPortComponent(PortComponentMetaData portComponent)
69    {
70       portComponents.add(portComponent);
71    }
72
73    public PortComponentMetaData[] getPortComponents()
74    {
75       PortComponentMetaData[] array = new PortComponentMetaData[portComponents.size()];
76       portComponents.toArray(array);
77       return array;
78    }
79
80    /**
81     * Lookup a PortComponentMetaData by wsdl-port local part
82     *
83     * @param name - the wsdl-port local part
84     * @return PortComponentMetaData if found, null otherwise
85     */

86    public PortComponentMetaData getPortComponentByWsdlPort(String JavaDoc name)
87    {
88       ArrayList JavaDoc pcNames = new ArrayList JavaDoc();
89       Iterator JavaDoc it = portComponents.iterator();
90       while (it.hasNext())
91       {
92          PortComponentMetaData pc = (PortComponentMetaData)it.next();
93          String JavaDoc wsdlPortName = pc.getWsdlPort().getLocalPart();
94          if (wsdlPortName.equals(name))
95          {
96             return pc;
97          }
98          pcNames.add(wsdlPortName);
99       }
100
101       log.error("Cannot get port component name '" + name + "', we have: " + pcNames);
102       return null;
103    }
104
105    /**
106     * Lookup a PortComponentMetaData by port-component-name
107     *
108     * @param name - the port-component-name
109     * @return PortComponentMetaData if found, null otherwise
110     */

111    public PortComponentMetaData getPortComponentByName(String JavaDoc name)
112    {
113       ArrayList JavaDoc pcNames = new ArrayList JavaDoc();
114       Iterator JavaDoc it = portComponents.iterator();
115       while (it.hasNext())
116       {
117          PortComponentMetaData pc = (PortComponentMetaData)it.next();
118          String JavaDoc portName = pc.getPortComponentName();
119          if (portName.equals(name))
120          {
121             return pc;
122          }
123          pcNames.add(portName);
124       }
125
126       log.error("Cannot get port component name '" + name + "', we have: " + pcNames);
127       return null;
128    }
129
130    public String JavaDoc getWebserviceDescriptionName()
131    {
132       return webserviceDescriptionName;
133    }
134
135    public void setWebserviceDescriptionName(String JavaDoc webserviceDescriptionName)
136    {
137       this.webserviceDescriptionName = webserviceDescriptionName;
138    }
139
140    public String JavaDoc getWsdlFile()
141    {
142       return wsdlFile;
143    }
144
145    public void setWsdlFile(String JavaDoc wsdlFile)
146    {
147       this.wsdlFile = wsdlFile;
148    }
149
150    public String JavaDoc getWsdlPublishLocation()
151    {
152       return wsdlPublishLocation;
153    }
154
155    public void setWsdlPublishLocation(String JavaDoc wsdlPublishLocation)
156    {
157       this.wsdlPublishLocation = wsdlPublishLocation;
158    }
159
160    public String JavaDoc getJaxrpcMappingFile()
161    {
162       return jaxrpcMappingFile;
163    }
164
165    public JavaWsdlMapping getJavaWsdlMapping()
166    {
167       JavaWsdlMapping javaWsdlMapping = (JavaWsdlMapping)webservices.jaxrpcMappingFileMap.get(jaxrpcMappingFile);
168       if (javaWsdlMapping == null)
169       {
170          try
171          {
172             // setup the XML binding Unmarshaller
173
URL JavaDoc location = webservices.getResourceLoader().findResource(jaxrpcMappingFile);
174             JavaWsdlMappingFactory mappingFactory = JavaWsdlMappingFactory.newInstance();
175             javaWsdlMapping = mappingFactory.parse(location);
176             webservices.jaxrpcMappingFileMap.put(jaxrpcMappingFile, javaWsdlMapping);
177          }
178          catch (Exception JavaDoc e)
179          {
180             throw new JAXRPCException JavaDoc("Cannot unmarshal jaxrpc-mapping-file: " + jaxrpcMappingFile, e);
181          }
182       }
183       return javaWsdlMapping;
184    }
185
186    public void setJaxrpcMappingFile(String JavaDoc jaxrpcMappingFile)
187    {
188       this.jaxrpcMappingFile = jaxrpcMappingFile;
189    }
190
191    /**
192     * Get the wsdl definition that corresponds to the wsdl-file element.
193     */

194    public Definition getWsdlDefinition()
195    {
196       WSDLMapEntry wsdlEntry = (WSDLMapEntry)webservices.wsdlFileMap.get(wsdlFile);
197       if (wsdlEntry == null)
198       {
199          try
200          {
201             URL JavaDoc wsdlLocation = webservices.getResourceLoader().findResource(wsdlFile);
202             if (wsdlLocation == null)
203                throw new IllegalArgumentException JavaDoc("Cannot find wsdl in deployment: " + wsdlFile);
204
205             WSDL11DefinitionFactory factory = WSDL11DefinitionFactory.newInstance();
206             Definition wsdlDefinition = factory.parse(wsdlLocation);
207
208             wsdlEntry = new WSDLMapEntry(wsdlDefinition);
209             webservices.wsdlFileMap.put(wsdlFile, wsdlEntry);
210          }
211          catch (WSDLException e)
212          {
213             throw new IllegalStateException JavaDoc("Cannot obtain WSDL definition, cause: " + e.toString());
214          }
215       }
216
217       return wsdlEntry.wsdlDefinition;
218    }
219
220    public static boolean isValidServiceUrl(String JavaDoc url)
221    {
222       URI JavaDoc uri;
223       try
224       {
225           uri = new URI JavaDoc(url);
226       }
227       catch (URISyntaxException JavaDoc e)
228       {
229          return false;
230       }
231
232       String JavaDoc scheme = uri.getScheme();
233       if (! "http".equals(scheme) && ! "https".equals(scheme))
234          return false;
235
236       if (uri.getHost() == null)
237          return false;
238
239       return true;
240    }
241
242    /**
243     * Replace the port location with the actual one in JBoss
244     */

245    public void updateServiceAddress(ServiceDeployer.ServiceLocationResolver locationResolver)
246    {
247       // make sure we have the document
248
Definition wsdlDefinition = getWsdlDefinition();
249
250       WSDLMapEntry wsdlEntry = (WSDLMapEntry)webservices.wsdlFileMap.get(wsdlFile);
251       if (wsdlEntry.locationReplaced == false)
252       {
253          replaceAddressLocations(wsdlDefinition, locationResolver);
254          wsdlEntry.locationReplaced = true;
255       }
256    }
257
258    /**
259     * Replace the address locations for all ports that that are referenced from this webservices.xml
260     */

261    private void replaceAddressLocations(Definition wsdlDefinition, ServiceDeployer.ServiceLocationResolver locationResolver)
262    {
263       // process all webservice-description(s) that use the same wsdl-file
264
WebserviceDescriptionMetaData[] wsdArr = webservices.getWebserviceDescriptions();
265       for (int i = 0; i < wsdArr.length; i++)
266       {
267          WebserviceDescriptionMetaData wsdMetaData = wsdArr[i];
268          if (wsdlFile.equals(wsdMetaData.wsdlFile))
269          {
270             PortComponentMetaData[] pcarr = wsdMetaData.getPortComponents();
271             for (int pcIndex = 0; pcIndex < pcarr.length; pcIndex++)
272             {
273                PortComponentMetaData portComponent = pcarr[pcIndex];
274                String JavaDoc pcWsdlPortName = portComponent.getWsdlPort().getLocalPart();
275                Port wsdlPort = null;
276
277                Iterator JavaDoc itServices = wsdlDefinition.getServices().values().iterator();
278                while (wsdlPort == null && itServices.hasNext())
279                {
280                   Service wsdlService = (Service)itServices.next();
281
282                   Map JavaDoc wsdlPorts = wsdlService.getPorts();
283                   Iterator JavaDoc itPorts = wsdlPorts.keySet().iterator();
284                   while (wsdlPort == null && itPorts.hasNext())
285                   {
286                      String JavaDoc wsdlPortName = (String JavaDoc)itPorts.next();
287                      if (wsdlPortName.equals(pcWsdlPortName))
288                      {
289                         wsdlPort = (Port)wsdlPorts.get(wsdlPortName);
290                         Iterator JavaDoc itElements = wsdlPort.getExtensibilityElements().iterator();
291                         while (itElements.hasNext())
292                         {
293                            Object JavaDoc obj = itElements.next();
294                            if (obj instanceof SOAPAddress)
295                            {
296                               SOAPAddress address = (SOAPAddress)obj;
297                               String JavaDoc wsdlURI = address.getLocationURI();
298                               String JavaDoc addressUrl = null;
299                               String JavaDoc schema = null;
300
301                               if (wsdlURI.startsWith("http://") || wsdlURI.startsWith("https://"))
302                                  schema = wsdlURI.substring(0, wsdlURI.indexOf("://") + 3);
303
304                               String JavaDoc endpointUrl = locationResolver.getServiceLocation(schema, portComponent);
305
306                               if (isValidServiceUrl(wsdlURI) && ! locationResolver.alwaysResolve())
307                                  addressUrl = wsdlURI;
308                               else
309                                  addressUrl = endpointUrl;
310
311                               try
312                               {
313                                  portComponent.setServiceEndpointURL(new URL JavaDoc(endpointUrl));
314                                  log.debug("Replace port location '" + wsdlURI + "' with '" + addressUrl + "'");
315                                  address.setLocationURI(addressUrl);
316                               }
317                               catch (MalformedURLException JavaDoc e)
318                               {
319                                  log.error("Invalid service URL: " + endpointUrl);
320                               }
321                            }
322                         }
323                      }
324                   }
325                }
326
327                if (wsdlPort == null)
328                   throw new IllegalArgumentException JavaDoc("Cannot find port with name '" + pcWsdlPortName + "' in wsdl document");
329             }
330          }
331       }
332    }
333
334    /**
335     * An entry in the map of wsdl files that are referenced from webservices.xml
336     * <p/>
337     * This implementation maintains a flag for the wsdl definition that indicates
338     * whether the address locations have already been replaced.
339     */

340    class WSDLMapEntry
341    {
342       Definition wsdlDefinition;
343       boolean locationReplaced;
344
345       public WSDLMapEntry(Definition wsdlDefinition)
346       {
347          this.wsdlDefinition = wsdlDefinition;
348       }
349    }
350 }
351
Popular Tags