KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > deployment > api > WSDLFile


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: WSDLFile.java,v 1.11 2004/10/13 14:04:22 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ws.deployment.api;
27
28 import java.io.StringWriter JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.wsdl.Definition;
37 import javax.wsdl.Message;
38 import javax.wsdl.Part;
39 import javax.wsdl.Port;
40 import javax.wsdl.Service;
41 import javax.wsdl.WSDLException;
42 import javax.wsdl.extensions.soap.SOAPAddress;
43 import javax.wsdl.extensions.soap.SOAPBinding;
44 import javax.wsdl.factory.WSDLFactory;
45 import javax.wsdl.xml.WSDLReader;
46 import javax.wsdl.xml.WSDLWriter;
47 import javax.xml.namespace.QName JavaDoc;
48
49 import org.objectweb.jonas_lib.I18n;
50
51 /**
52  * Gives methods to get WSDL informations and to validate these one.
53  * @author Xavier Delplanque
54  * @author Guillaume Sauthier
55  */

56 public class WSDLFile {
57
58
59     /** list of each ports of each wsdl services */
60     private String JavaDoc name;
61
62     /** list of each ports of each wsdl services */
63     private List JavaDoc wsdlPorts = null;
64
65     /** wsdl definition */
66     private Definition def = null;
67
68     /** verbose mode */
69     private static final boolean VERBOSE = false;
70
71     /**
72      * Internationalization
73      */

74     private static I18n i18n = I18n.getInstance(WSDLFile.class);
75
76     /**
77      * Creates a new WSDLFile object.
78      * @param cl ClassLoader to use
79      * @param name wsdl file name
80      * @throws WSDeploymentDescException when a parse error occurs
81      */

82     public WSDLFile(ClassLoader JavaDoc cl, String JavaDoc name) throws WSDeploymentDescException {
83             this(cl.getResource(name), name);
84     }
85
86     /**
87      * Creates a new WSDLFile object.
88      * @param url URL to load
89      * @param name wsdl file name
90      * @throws WSDeploymentDescException when a parse error occurs
91      */

92     public WSDLFile(URL JavaDoc url, String JavaDoc name) throws WSDeploymentDescException {
93         this.name = name;
94
95         try {
96             WSDLFactory f = WSDLFactory.newInstance();
97             WSDLReader r = f.newWSDLReader();
98
99             r.setFeature("javax.wsdl.verbose", VERBOSE); //$NON-NLS-1$
100
r.setFeature("javax.wsdl.importDocuments", true); //$NON-NLS-1$
101

102             if (url == null) {
103                 throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.notFound", name)); //$NON-NLS-1$
104
}
105
106             def = r.readWSDL(url.toExternalForm(), url.toExternalForm());
107         } catch (WSDLException e) {
108             throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.WSDLParsingError", name), e); //$NON-NLS-1$
109
}
110
111         wsdlPorts = new Vector JavaDoc();
112         fillWsdlPorts();
113     }
114
115     /**
116      * return true if the port identified by portQname is defined in WSDL ports.
117      * @param portQName the port to check.
118      * @return if the gived Qname is contained in the
119      * WSDL.definition.service.port.
120      */

121     public boolean hasPort(QName JavaDoc portQName) {
122         return hasPort(portQName.getLocalPart());
123     }
124
125     /**
126      * return true if the port identified by portName is defined in WSDL ports.
127      * @param portName the port to check.
128      * @return if the gived port is contained in the
129      * WSDL.definition.service.port.
130      */

131     public boolean hasPort(String JavaDoc portName) {
132         return getPort(portName) != null;
133     }
134
135     /**
136      * return true if the service identified by srvQName is defined in WSDL
137      * services.
138      * @param srvQName the service to check.
139      * @return true if the given Qname is contained in the
140      * WSDL.definition.service.
141      */

142     public boolean hasService(QName JavaDoc srvQName) {
143         return def.getService(srvQName) != null;
144     }
145
146     /**
147      * return true if shQName is defined in WSDL services.
148      * @param shQName a soap header Qname that could be defined in the WSDL.
149      * @return true if SOAP Header has been found in WSDL Definition.
150      */

151     public boolean hasSOAPHeader(QName JavaDoc shQName) {
152         Map JavaDoc msgs = def.getMessages();
153
154         for (Iterator JavaDoc m = msgs.values().iterator(); m.hasNext();) {
155             Message msg = (Message) m.next();
156
157             if (msg.getQName().getNamespaceURI() == shQName.getNamespaceURI()) {
158                 Part p = msg.getPart(shQName.getLocalPart());
159
160                 if (p != null) {
161                     return true;
162                 }
163             }
164         }
165
166         return false;
167     }
168
169     /**
170      * return true if all WSDL ports are defined in portList.
171      * @param portList the ports List to check.
172      * @return true if all WSDL ports are defined in
173      * WSDL.definition.service.port.
174      */

175     public boolean hasPortsIncludedIn(List JavaDoc portList) {
176         return portList.containsAll(wsdlPorts);
177     }
178
179     /**
180      * return true if the port identified by portQname use a SOAP binding.
181      * @param portQName the port to check.
182      * @return if the port identified by portQname use a SOAP binding. return
183      * false if the portQname is not defined in the WSDL file.
184      */

185     public boolean hasSOAPBinding(QName JavaDoc portQName) {
186         boolean isSoapBinding = false;
187         Port port = getPort(portQName.getLocalPart());
188
189         // get Port
190
if (port != null) {
191             List JavaDoc ee = port.getBinding().getExtensibilityElements();
192             Iterator JavaDoc eeIt = ee.iterator();
193
194             while (eeIt.hasNext() && !isSoapBinding) {
195                 // verify that it uses a soap binding
196
Object JavaDoc elem = eeIt.next();
197
198                 if (elem != null) {
199                     isSoapBinding = elem instanceof SOAPBinding;
200                 }
201             }
202         }
203
204         return isSoapBinding;
205     }
206
207     /**
208      * return WSDL definition, null if it's undefined.
209      * @return WSDL definition.
210      */

211     public Definition getDefinition() {
212         return def;
213     }
214
215     /**
216      * return the number of services defined inside the wsdl.
217      * @return the number of services defined inside the wsdl.
218      */

219     public int getNbServices() {
220         return def.getServices().size();
221     }
222
223     /**
224      * return the QName of the first service, null if no service is defined.
225      * @return the QName of the first service, null if no service is defined.
226      */

227     public QName JavaDoc getServiceQname() {
228         Map JavaDoc srvs = def.getServices();
229         Iterator JavaDoc svcIt = srvs.values().iterator();
230         QName JavaDoc res = null;
231
232         if (svcIt.hasNext()) {
233             Service svc = (Service) svcIt.next();
234             res = svc.getQName();
235         }
236
237         return res;
238     }
239
240     /**
241      * return the given port location, null if the port is undefined.
242      * @param portQName the port QName identifying the port searched.
243      * @return portQname location.
244      * @throws WSDeploymentDescException when port is not found
245      */

246     public URL JavaDoc getLocation(QName JavaDoc portQName) throws WSDeploymentDescException {
247         Port port = getPort(portQName.getLocalPart());
248
249         // get portQName port
250
if (port != null) {
251             List JavaDoc ee = port.getExtensibilityElements();
252             Iterator JavaDoc eeIt = ee.iterator();
253
254             while (eeIt.hasNext()) {
255                 // find the soap address element
256
Object JavaDoc elem = eeIt.next();
257
258                 if ((elem != null) && elem instanceof SOAPAddress) {
259                     try {
260                         return new URL JavaDoc(((SOAPAddress) elem).getLocationURI());
261                     } catch (MalformedURLException JavaDoc e) {
262                         throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.MalformedPortLocation", portQName)); //$NON-NLS-1$
263
}
264                 }
265             }
266         }
267
268         return null;
269     }
270
271     /**
272      * set the given port location if it exists.
273      * @param portQName the port to set.
274      * @param loc the port location.
275      */

276     public void setLocation(QName JavaDoc portQName, URL JavaDoc loc) {
277         Port port = getPort(portQName.getLocalPart());
278
279         if (port != null) {
280             List JavaDoc ee = port.getExtensibilityElements();
281             Iterator JavaDoc eeIt = ee.iterator();
282
283             while (eeIt.hasNext()) {
284                 // find the soap address element
285
Object JavaDoc elem = eeIt.next();
286
287                 if ((elem != null) && elem instanceof SOAPAddress) {
288                     ((SOAPAddress) elem).setLocationURI(loc.toString());
289                 }
290             }
291         }
292     }
293
294     /**
295      * Init the wsdlPorts list.
296      */

297     private void fillWsdlPorts() {
298         Map JavaDoc svcs = def.getServices();
299         Iterator JavaDoc svcsIt = svcs.values().iterator();
300
301         while (svcsIt.hasNext()) {
302             Service svc = (Service) svcsIt.next();
303
304             if (svc != null) {
305                 for (Iterator JavaDoc j = svc.getPorts().values().iterator(); j.hasNext();) {
306                     Port p = (Port) j.next();
307                     wsdlPorts.add(new QName JavaDoc(def.getTargetNamespace(), p.getName()));
308                 }
309             }
310         }
311     }
312
313     /**
314      * Return the Port identified by portName in the WSDL, return null if it's
315      * not defined inside the wsdl.
316      * @param portName the port name.
317      * @return The port object identified by the name portName
318      */

319     private Port getPort(String JavaDoc portName) {
320         Map JavaDoc svcs = def.getServices();
321         Iterator JavaDoc svcsIt = svcs.values().iterator();
322
323         while (svcsIt.hasNext()) {
324             Service svc = (Service) svcsIt.next();
325
326             if (svc != null) {
327                 Port port = svc.getPort(portName);
328
329                 // get the port identified by portQname
330
return port;
331             }
332         }
333
334         return null;
335     }
336
337     /**
338      * Return wsdl file name
339      * @return wsdl file name
340      */

341     public String JavaDoc getName() {
342         return name;
343     }
344
345     /**
346      * @return Returns a String representation of this WSDLFile.
347      */

348     public String JavaDoc toString() {
349         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
350
351         sb.append("\n" + getClass().getName()); //$NON-NLS-1$
352
sb.append("\ngetName()=" + getName()); //$NON-NLS-1$
353

354         StringWriter JavaDoc sw = new StringWriter JavaDoc();
355         // Write Definition
356
try {
357             WSDLFactory factory = WSDLFactory.newInstance();
358             WSDLWriter writer = factory.newWSDLWriter();
359
360             writer.writeWSDL(def, sw);
361
362         } catch (WSDLException e) {
363             sb.append(getI18n().getMessage("WSDLFile.writeDefError")); //$NON-NLS-1$
364
}
365
366         sb.append(sw.getBuffer().toString());
367
368         return sb.toString();
369     }
370
371     /**
372      * Return true if the 2 objects seems equals. Because the equals() method
373      * doesn't exist on Definition, it's hard to known if 2 Definition are
374      * equals in value. So we just test lists lengths. Use it ONLY in test/debug
375      * case. !!!
376      * @param other the object to compare.
377      * @return true if the 2 objects seems equals.
378      */

379     public boolean equals(Object JavaDoc other) {
380         if (other == null) {
381             return false;
382         }
383
384         if (!(other instanceof WSDLFile)) {
385             return false;
386         }
387
388         WSDLFile ref = (WSDLFile) other;
389         Definition odef = ref.getDefinition();
390
391         if (def.getServices().size() != odef.getServices().size()) {
392             return false;
393         }
394
395         if (def.getPortTypes().size() != odef.getPortTypes().size()) {
396             return false;
397         }
398
399         if (def.getMessages().size() != odef.getMessages().size()) {
400             return false;
401         }
402
403         if (def.getBindings().size() != odef.getBindings().size()) {
404             return false;
405         }
406
407         // After all theses tests, the 2 objects are equals in value
408
return true;
409     }
410
411     /**
412      * @return Returns the i18n.
413      */

414     protected static I18n getI18n() {
415         return i18n;
416     }
417 }
Popular Tags