KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > handler > RegistryWSDLHandler


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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  * Initial Developer : Sauthier Guillaume
22  * --------------------------------------------------------------------------
23  * $Id: RegistryWSDLHandler.java,v 1.8 2004/10/15 12:24:56 sauthieg Exp $
24  * --------------------------------------------------------------------------
25 */

26
27 package org.objectweb.jonas.ws.handler;
28
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.PasswordAuthentication JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.Set JavaDoc;
39
40 import javax.wsdl.extensions.ExtensibilityElement;
41 import javax.wsdl.extensions.soap.SOAPAddress;
42 import javax.xml.registry.BulkResponse JavaDoc;
43 import javax.xml.registry.BusinessLifeCycleManager JavaDoc;
44 import javax.xml.registry.BusinessQueryManager JavaDoc;
45 import javax.xml.registry.Connection JavaDoc;
46 import javax.xml.registry.ConnectionFactory JavaDoc;
47 import javax.xml.registry.JAXRException JavaDoc;
48 import javax.xml.registry.LifeCycleManager JavaDoc;
49 import javax.xml.registry.RegistryException JavaDoc;
50 import javax.xml.registry.RegistryService JavaDoc;
51 import javax.xml.registry.infomodel.Classification JavaDoc;
52 import javax.xml.registry.infomodel.ClassificationScheme JavaDoc;
53 import javax.xml.registry.infomodel.Concept JavaDoc;
54 import javax.xml.registry.infomodel.ExternalLink JavaDoc;
55 import javax.xml.registry.infomodel.InternationalString JavaDoc;
56 import javax.xml.registry.infomodel.Key JavaDoc;
57 import javax.xml.registry.infomodel.Organization JavaDoc;
58 import javax.xml.registry.infomodel.PersonName JavaDoc;
59 import javax.xml.registry.infomodel.Service JavaDoc;
60 import javax.xml.registry.infomodel.ServiceBinding JavaDoc;
61 import javax.xml.registry.infomodel.SpecificationLink JavaDoc;
62 import javax.xml.registry.infomodel.User JavaDoc;
63
64 import org.w3c.dom.Element JavaDoc;
65
66 import org.objectweb.jonas_lib.I18n;
67
68 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
69
70 import org.objectweb.jonas.common.Log;
71 import org.objectweb.jonas.ws.WSServiceException;
72
73 import org.objectweb.util.monolog.api.BasicLevel;
74 import org.objectweb.util.monolog.api.Logger;
75
76 /**
77  * Publish a given WSDL Definition into a specified WebServices registry (UDDI/ebXML).
78  * Use JAXR (only UDDI At this time).
79  * properties :<br/>
80  * - <code>jonas.service.publish.uddi.username</code> : publisher username<br/>
81  * - <code>jonas.service.publish.uddi.password</code> : publisher password<br/>
82  * - <code>javax.xml.registry.lifeCycleManagerURL</code> : url of the Publish API for a registry<br/>
83  * - <code>javax.xml.registry.queryManagerURL</code> : url of the Query API for a registry<br/>
84  * - <code>jonas.service.publish.uddi.organization.name</code> : The organisation name<br/>
85  * - <code>jonas.service.publish.uddi.organization.desc</code> : The organization description (optionnal)<br/>
86  * - <code>jonas.service.publish.uddi.organization.person_name</code> : Primary Contact for the organization<br/>
87  * The property file is given 'as is' to the ConnectionFactory, If you want to add some JAXR specific
88  * properties, add them in this property file.
89  *
90  * @author Guillaume Sauthier
91  */

92 public class RegistryWSDLHandler implements WSDLHandler {
93
94     /**
95      * Standard Publish URL
96      */

97     private static final String JavaDoc PUBLISH_URL_PROP
98         = "javax.xml.registry.lifeCycleManagerURL";
99
100     /**
101      * Standard Inquiry URL
102      */

103     private static final String JavaDoc INQUIRY_URL_PROP
104         = "javax.xml.registry.queryManagerURL";
105
106     /**
107      * JOnAS Username
108      */

109     private static final String JavaDoc USERNAME
110         = "jonas.service.publish.uddi.username";
111
112     /**
113      * JOnAS Password
114      */

115     private static final String JavaDoc PASSWORD
116         = "jonas.service.publish.uddi.password";
117
118     /**
119      * JOnAS Organization name
120      */

121     private static final String JavaDoc ORGANIZATION_NAME
122         = "jonas.service.publish.uddi.organization.name";
123
124     /**
125      * JOnAS Organization description
126      */

127     private static final String JavaDoc ORGANIZATION_DESC
128         = "jonas.service.publish.uddi.organization.desc";
129
130     /**
131      * JOnAS Person under wich WSDL will be published
132      */

133     private static final String JavaDoc PERSON_NAME
134         = "jonas.service.publish.uddi.organization.person_name";
135
136
137     /** JAXR ConnectionFactory to use */
138     private ConnectionFactory JavaDoc connFactory;
139
140     /** username/password pair */
141     private PasswordAuthentication JavaDoc passAuth;
142
143     /** list of properties */
144     private Properties JavaDoc props;
145
146     /** Publication Logger (special file storing keys) */
147     private static Logger logger = Log.getLogger(Log.JONAS_PUBLISH_PREFIX);
148
149     /** Internationalisation tool */
150     private static I18n i18n = I18n.getInstance(RegistryWSDLHandler.class);
151
152     /**
153      * Constructor : creates a RegistryWSDLHandler object.
154      *
155      * @param props uddi.properties file
156      *
157      * @throws WSServiceException When some properties are missing
158      */

159     public RegistryWSDLHandler(Properties JavaDoc props) throws WSServiceException {
160
161         this.props = props;
162
163         //Check minimal properties
164
String JavaDoc publishURL = props.getProperty(PUBLISH_URL_PROP);
165         String JavaDoc queryURL = props.getProperty(INQUIRY_URL_PROP);
166         String JavaDoc username = props.getProperty(USERNAME);
167         String JavaDoc password = props.getProperty(PASSWORD);
168
169         // Check if publishURL is an URL
170
try {
171             new URL JavaDoc(publishURL);
172         } catch (MalformedURLException JavaDoc mue) {
173             String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.constr.mue", publishURL);
174             logger.log(BasicLevel.ERROR, err);
175             throw new WSServiceException(err, mue);
176         }
177
178         try {
179             new URL JavaDoc(queryURL);
180         } catch (MalformedURLException JavaDoc mue) {
181             String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.constr.mue", queryURL);
182             logger.log(BasicLevel.ERROR, err);
183             throw new WSServiceException(err, mue);
184         }
185
186         try {
187             // Create the ConnectionFactory
188
connFactory = ConnectionFactory.newInstance();
189             connFactory.setProperties(props);
190         } catch (JAXRException JavaDoc jaxre) {
191             String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.constr.jaxrException");
192             logger.log(BasicLevel.ERROR, err);
193             throw new WSServiceException(err, jaxre);
194         }
195
196         // Create the Authentication
197
if (username != null && password != null) {
198             passAuth = new PasswordAuthentication JavaDoc(username, password.toCharArray());
199         } else {
200             String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.constr.noCreds");
201             logger.log(BasicLevel.ERROR, err);
202             throw new WSServiceException(err);
203         }
204
205         // Check organization props
206
String JavaDoc name = props.getProperty(ORGANIZATION_NAME);
207         String JavaDoc person = props.getProperty(PERSON_NAME);
208         if ((name == null) || (person == null)) {
209             String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.constr.missingProp");
210             logger.log(BasicLevel.ERROR, err);
211             throw new WSServiceException(err);
212         }
213     }
214
215     /**
216      * Publish the WSDL into a Registry.
217      * @param sd the Service containing the WSDL file to publish.
218      * @throws WSServiceException When cannot access to publish API
219      */

220     public void publish(ServiceDesc sd) throws WSServiceException {
221
222         try {
223             javax.wsdl.Definition def = sd.getWSDL().getDefinition();
224
225             // Create the connection
226
Connection JavaDoc connection = connFactory.createConnection();
227
228             // Add Credentials
229
Set JavaDoc creds = new HashSet JavaDoc();
230             creds.add(passAuth);
231             connection.setCredentials(creds);
232
233             // Get the registry Service
234
RegistryService JavaDoc rs = connection.getRegistryService();
235
236             // get the Query API Manager
237
BusinessQueryManager JavaDoc bqm = rs.getBusinessQueryManager();
238
239             // get the Publish API Manager
240
BusinessLifeCycleManager JavaDoc blcm = rs.getBusinessLifeCycleManager();
241
242             // Reference a unique InternationalString
243
InternationalString JavaDoc is = null;
244
245             // Create the Organization with properties
246
Organization JavaDoc org = blcm.createOrganization(props.getProperty(ORGANIZATION_NAME));
247             String JavaDoc orgDesc = props.getProperty(ORGANIZATION_DESC);
248             if (orgDesc != null) {
249                 is = blcm.createInternationalString(orgDesc);
250                 org.setDescription(is);
251             }
252             User JavaDoc contact = blcm.createUser();
253             PersonName JavaDoc pName = blcm.createPersonName(props.getProperty(PERSON_NAME));
254             contact.setPersonName(pName);
255             org.setPrimaryContact(contact);
256
257             Collection JavaDoc services = new ArrayList JavaDoc();
258             // Create n Service (1 for each service)
259
for (Iterator JavaDoc servicesIter = def.getServices().values().iterator();
260                  servicesIter.hasNext();) {
261
262                 javax.wsdl.Service s = (javax.wsdl.Service) servicesIter.next();
263                 Service JavaDoc service = blcm.createService(s.getQName().getLocalPart());
264
265                 Element desc = s.getDocumentationElement();
266                 if (desc != null) {
267                     String JavaDoc description = getDescription(desc);
268                     is = blcm.createInternationalString(description);
269                     service.setDescription(is);
270                 }
271
272                 // Create n ServiceBinding (1 for each port)
273
Collection JavaDoc serviceBindings = new ArrayList JavaDoc();
274                 for (Iterator JavaDoc portsIter = s.getPorts().values().iterator();
275                      portsIter.hasNext();) {
276
277                     javax.wsdl.Port port = (javax.wsdl.Port) portsIter.next();
278
279                     ServiceBinding JavaDoc binding = blcm.createServiceBinding();
280
281                     Element pDesc = port.getDocumentationElement();
282                     String JavaDoc pDescription = port.getName();
283                     if (pDesc != null) {
284                         pDescription += " " + getDescription(pDesc);
285                     }
286                     is = blcm.createInternationalString(pDescription);
287                     binding.setDescription(is);
288
289                     // we do not check URL validity because webapps
290
// are not deployed at this point.
291
binding.setValidateURI(false);
292                     String JavaDoc url = getEndpoint(port);
293                     binding.setAccessURI(url);
294
295                     // Create the Concep for WSDL
296
Concept JavaDoc concept = blcm.createConcept(null, port.getName() + " Concept", "");
297                     is = blcm.createInternationalString(pDescription + " Concept");
298                     concept.setDescription(is);
299
300                     ExternalLink JavaDoc extlink = blcm.createExternalLink("file://",
301                                                                    "WSDL");
302                     extlink.setValidateURI(false);
303                     // !!! Axis specific !!!
304
extlink.setExternalURI(url + "?JWSDL");
305
306                     concept.addExternalLink(extlink);
307
308                     // The Concept is a WSDL Document
309
ClassificationScheme JavaDoc uddiOrgTypes =
310                         bqm.findClassificationSchemeByName(null, "uddi-org:types");
311                     Classification JavaDoc wsdlSpecClass = blcm.createClassification(uddiOrgTypes,
312                                                                              "wsdlSpec",
313                                                                              "wsdlSpec");
314                     concept.addClassification(wsdlSpecClass);
315
316                     // save into registry and get key
317
Collection JavaDoc concepts = new ArrayList JavaDoc();
318                     concepts.add(concept);
319                     BulkResponse JavaDoc br = blcm.saveConcepts(concepts);
320                     String JavaDoc key = getKey(br);
321
322                     // i18n RegistryWSDLHandler.publish.conceptKey
323
String JavaDoc msg = i18n.getMessage("RegistryWSDLHandler.publish.conceptKey",
324                                                  port.getName(),
325                                                  key);
326                     logger.log(BasicLevel.INFO, msg);
327
328                     // get the concept from registry
329
Concept JavaDoc c = (Concept JavaDoc) bqm.getRegistryObject(key, LifeCycleManager.CONCEPT);
330
331                     SpecificationLink JavaDoc specLink = blcm.createSpecificationLink();
332                     specLink.setSpecificationObject(c);
333                     binding.addSpecificationLink(specLink);
334
335                     serviceBindings.add(binding);
336                 }
337                 service.addServiceBindings(serviceBindings);
338                 services.add(service);
339             }
340             // Save into the Organisation
341
org.addServices(services);
342
343             Collection JavaDoc orgs = new ArrayList JavaDoc();
344             orgs.add(org);
345
346             BulkResponse JavaDoc br = blcm.saveOrganizations(orgs);
347             String JavaDoc key = getKey(br);
348
349             // i18n RegistryWSDLHandler.publish.organisationKey
350
String JavaDoc msg = i18n.getMessage("RegistryWSDLHandler.publish.organisationKey",
351                                          org,
352                                          key);
353             logger.log(BasicLevel.INFO, msg);
354
355
356         } catch (JAXRException JavaDoc jaxre) {
357             String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.publish.jaxrException");
358             logger.log(BasicLevel.ERROR, err);
359             throw new WSServiceException(err, jaxre);
360         }
361     }
362
363     /**
364      * Return the <code>soap:location</code> of the Port.
365      *
366      * @param port the <code>wsdl:port</code> we want the endpoint URL.
367      *
368      * @return the endpoint URL.
369      */

370     private String JavaDoc getEndpoint(javax.wsdl.Port port) {
371         String JavaDoc url = null;
372         List JavaDoc ee = port.getExtensibilityElements();
373         for (Iterator JavaDoc ext = ee.iterator(); ext.hasNext();) {
374             ExtensibilityElement elem = (ExtensibilityElement) ext.next();
375             if (elem instanceof SOAPAddress) {
376                 SOAPAddress soap = (SOAPAddress) elem;
377                 url = soap.getLocationURI();
378             }
379         }
380         return url;
381     }
382
383     /**
384      * @param e Element containing Description
385      * @return Returns the desciption of the given Element
386      */

387     private String JavaDoc getDescription(Element e) {
388         return e.getFirstChild().getNodeValue();
389     }
390
391     /**
392      * Return the Key contained in the Registry response.
393      *
394      * @param br the Registry response
395      *
396      * @return the key contained in the response
397      *
398      * @throws WSServiceException when no key returned or response
399      * wraps server side exceptions.
400      * @throws JAXRException If no Key was returned by the server
401      */

402     private String JavaDoc getKey(BulkResponse JavaDoc br)
403         throws WSServiceException, JAXRException JavaDoc {
404         String JavaDoc keyStr = null;
405         Collection JavaDoc exceptions = br.getExceptions();
406         Key JavaDoc key = null;
407         if (exceptions == null) {
408             // save OK
409
Collection JavaDoc keys = br.getCollection();
410             Iterator JavaDoc keyIter = keys.iterator();
411             if (keyIter.hasNext()) {
412                 // we have 1 key
413
key = (Key JavaDoc) keyIter.next();
414                 keyStr = key.getId();
415             } else {
416                 // no key
417
// i18n RegistryWSDLHandler.getKey.noKeyReturned
418
String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.getKey.noKeyReturned");
419                 logger.log(BasicLevel.ERROR, err);
420                 throw new WSServiceException(err);
421             }
422         } else {
423
424             // Exceptions in Server Side
425

426             // i18n RegistryWSDLHandler.getKey.serverSideExceptions
427
String JavaDoc err = i18n.getMessage("RegistryWSDLHandler.getKey.serverSideExceptions");
428
429             // Get Server Side Cause
430
Iterator JavaDoc exIter = exceptions.iterator();
431             Exception JavaDoc cause = null;
432             if (exIter.hasNext()) {
433                 // we have 1 Exception
434
cause = (RegistryException JavaDoc) exIter.next();
435             }
436             logger.log(BasicLevel.ERROR, err);
437             if (cause == null) {
438                 throw new WSServiceException(err);
439             } else {
440                 throw new WSServiceException(err, cause);
441             }
442         }
443
444         return keyStr;
445     }
446 }
447
Popular Tags