RegistryWSDLHandler.java

00001 
00027 package org.objectweb.jonas.ws.handler;
00028 
00029 import java.net.MalformedURLException;
00030 import java.net.PasswordAuthentication;
00031 import java.net.URL;
00032 import java.util.ArrayList;
00033 import java.util.Collection;
00034 import java.util.HashSet;
00035 import java.util.Iterator;
00036 import java.util.List;
00037 import java.util.Properties;
00038 import java.util.Set;
00039 
00040 import javax.wsdl.extensions.ExtensibilityElement;
00041 import javax.wsdl.extensions.soap.SOAPAddress;
00042 import javax.xml.registry.BulkResponse;
00043 import javax.xml.registry.BusinessLifeCycleManager;
00044 import javax.xml.registry.BusinessQueryManager;
00045 import javax.xml.registry.Connection;
00046 import javax.xml.registry.ConnectionFactory;
00047 import javax.xml.registry.JAXRException;
00048 import javax.xml.registry.LifeCycleManager;
00049 import javax.xml.registry.RegistryException;
00050 import javax.xml.registry.RegistryService;
00051 import javax.xml.registry.infomodel.Classification;
00052 import javax.xml.registry.infomodel.ClassificationScheme;
00053 import javax.xml.registry.infomodel.Concept;
00054 import javax.xml.registry.infomodel.ExternalLink;
00055 import javax.xml.registry.infomodel.InternationalString;
00056 import javax.xml.registry.infomodel.Key;
00057 import javax.xml.registry.infomodel.Organization;
00058 import javax.xml.registry.infomodel.PersonName;
00059 import javax.xml.registry.infomodel.Service;
00060 import javax.xml.registry.infomodel.ServiceBinding;
00061 import javax.xml.registry.infomodel.SpecificationLink;
00062 import javax.xml.registry.infomodel.User;
00063 
00064 import org.w3c.dom.Element;
00065 
00066 import org.objectweb.jonas_lib.I18n;
00067 
00068 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
00069 
00070 import org.objectweb.jonas.common.Log;
00071 import org.objectweb.jonas.ws.WSServiceException;
00072 
00073 import org.objectweb.util.monolog.api.BasicLevel;
00074 import org.objectweb.util.monolog.api.Logger;
00075 
00092 public class RegistryWSDLHandler implements WSDLHandler {
00093 
00097     private static final String PUBLISH_URL_PROP
00098         = "javax.xml.registry.lifeCycleManagerURL";
00099 
00103     private static final String INQUIRY_URL_PROP
00104         = "javax.xml.registry.queryManagerURL";
00105 
00109     private static final String USERNAME
00110         = "jonas.service.publish.uddi.username";
00111 
00115     private static final String PASSWORD
00116         = "jonas.service.publish.uddi.password";
00117 
00121     private static final String ORGANIZATION_NAME
00122         = "jonas.service.publish.uddi.organization.name";
00123 
00127     private static final String ORGANIZATION_DESC
00128         = "jonas.service.publish.uddi.organization.desc";
00129 
00133     private static final String PERSON_NAME
00134         = "jonas.service.publish.uddi.organization.person_name";
00135 
00136 
00138     private ConnectionFactory connFactory;
00139 
00141     private PasswordAuthentication passAuth;
00142 
00144     private Properties props;
00145 
00147     private static Logger logger = Log.getLogger(Log.JONAS_PUBLISH_PREFIX);
00148 
00150     private static I18n i18n = I18n.getInstance(RegistryWSDLHandler.class);
00151 
00159     public RegistryWSDLHandler(Properties props) throws WSServiceException {
00160 
00161         this.props = props;
00162 
00163         //Check minimal properties
00164         String publishURL = props.getProperty(PUBLISH_URL_PROP);
00165         String queryURL = props.getProperty(INQUIRY_URL_PROP);
00166         String username = props.getProperty(USERNAME);
00167         String password = props.getProperty(PASSWORD);
00168 
00169         // Check if publishURL is an URL
00170         try {
00171             new URL(publishURL);
00172         } catch (MalformedURLException mue) {
00173             String err = i18n.getMessage("RegistryWSDLHandler.constr.mue", publishURL);
00174             logger.log(BasicLevel.ERROR, err);
00175             throw new WSServiceException(err, mue);
00176         }
00177 
00178         try {
00179             new URL(queryURL);
00180         } catch (MalformedURLException mue) {
00181             String err = i18n.getMessage("RegistryWSDLHandler.constr.mue", queryURL);
00182             logger.log(BasicLevel.ERROR, err);
00183             throw new WSServiceException(err, mue);
00184         }
00185 
00186         try {
00187             // Create the ConnectionFactory
00188             connFactory = ConnectionFactory.newInstance();
00189             connFactory.setProperties(props);
00190         } catch (JAXRException jaxre) {
00191             String err = i18n.getMessage("RegistryWSDLHandler.constr.jaxrException");
00192             logger.log(BasicLevel.ERROR, err);
00193             throw new WSServiceException(err, jaxre);
00194         }
00195 
00196         // Create the Authentication
00197         if (username != null && password != null) {
00198             passAuth = new PasswordAuthentication(username, password.toCharArray());
00199         } else {
00200             String err = i18n.getMessage("RegistryWSDLHandler.constr.noCreds");
00201             logger.log(BasicLevel.ERROR, err);
00202             throw new WSServiceException(err);
00203         }
00204 
00205         // Check organization props
00206         String name = props.getProperty(ORGANIZATION_NAME);
00207         String person = props.getProperty(PERSON_NAME);
00208         if ((name == null) || (person == null)) {
00209             String err = i18n.getMessage("RegistryWSDLHandler.constr.missingProp");
00210             logger.log(BasicLevel.ERROR, err);
00211             throw new WSServiceException(err);
00212         }
00213     }
00214 
00220     public void publish(ServiceDesc sd) throws WSServiceException {
00221 
00222         try {
00223             javax.wsdl.Definition def = sd.getWSDL().getDefinition();
00224 
00225             // Create the connection
00226             Connection connection = connFactory.createConnection();
00227 
00228             // Add Credentials
00229             Set creds = new HashSet();
00230             creds.add(passAuth);
00231             connection.setCredentials(creds);
00232 
00233             // Get the registry Service
00234             RegistryService rs = connection.getRegistryService();
00235 
00236             // get the Query API Manager
00237             BusinessQueryManager bqm = rs.getBusinessQueryManager();
00238 
00239             // get the Publish API Manager
00240             BusinessLifeCycleManager blcm = rs.getBusinessLifeCycleManager();
00241 
00242             // Reference a unique InternationalString
00243             InternationalString is = null;
00244 
00245             // Create the Organization with properties
00246             Organization org = blcm.createOrganization(props.getProperty(ORGANIZATION_NAME));
00247             String orgDesc = props.getProperty(ORGANIZATION_DESC);
00248             if (orgDesc != null) {
00249                 is = blcm.createInternationalString(orgDesc);
00250                 org.setDescription(is);
00251             }
00252             User contact = blcm.createUser();
00253             PersonName pName = blcm.createPersonName(props.getProperty(PERSON_NAME));
00254             contact.setPersonName(pName);
00255             org.setPrimaryContact(contact);
00256 
00257             Collection services = new ArrayList();
00258             // Create n Service (1 for each service)
00259             for (Iterator servicesIter = def.getServices().values().iterator();
00260                  servicesIter.hasNext();) {
00261 
00262                 javax.wsdl.Service s = (javax.wsdl.Service) servicesIter.next();
00263                 Service service = blcm.createService(s.getQName().getLocalPart());
00264 
00265                 Element desc = s.getDocumentationElement();
00266                 if (desc != null) {
00267                     String description = getDescription(desc);
00268                     is = blcm.createInternationalString(description);
00269                     service.setDescription(is);
00270                 }
00271 
00272                 // Create n ServiceBinding (1 for each port)
00273                 Collection serviceBindings = new ArrayList();
00274                 for (Iterator portsIter = s.getPorts().values().iterator();
00275                      portsIter.hasNext();) {
00276 
00277                     javax.wsdl.Port port = (javax.wsdl.Port) portsIter.next();
00278 
00279                     ServiceBinding binding = blcm.createServiceBinding();
00280 
00281                     Element pDesc = port.getDocumentationElement();
00282                     String pDescription = port.getName();
00283                     if (pDesc != null) {
00284                         pDescription += " " + getDescription(pDesc);
00285                     }
00286                     is = blcm.createInternationalString(pDescription);
00287                     binding.setDescription(is);
00288 
00289                     // we do not check URL validity because webapps
00290                     // are not deployed at this point.
00291                     binding.setValidateURI(false);
00292                     String url = getEndpoint(port);
00293                     binding.setAccessURI(url);
00294 
00295                     // Create the Concep for WSDL
00296                     Concept concept = blcm.createConcept(null, port.getName() + " Concept", "");
00297                     is = blcm.createInternationalString(pDescription + " Concept");
00298                     concept.setDescription(is);
00299 
00300                     ExternalLink extlink = blcm.createExternalLink("file://",
00301                                                                    "WSDL");
00302                     extlink.setValidateURI(false);
00303                     // !!! Axis specific !!!
00304                     extlink.setExternalURI(url + "?JWSDL");
00305 
00306                     concept.addExternalLink(extlink);
00307 
00308                     // The Concept is a WSDL Document
00309                     ClassificationScheme uddiOrgTypes =
00310                         bqm.findClassificationSchemeByName(null, "uddi-org:types");
00311                     Classification wsdlSpecClass = blcm.createClassification(uddiOrgTypes,
00312                                                                              "wsdlSpec",
00313                                                                              "wsdlSpec");
00314                     concept.addClassification(wsdlSpecClass);
00315 
00316                     // save into registry and get key
00317                     Collection concepts = new ArrayList();
00318                     concepts.add(concept);
00319                     BulkResponse br = blcm.saveConcepts(concepts);
00320                     String key = getKey(br);
00321 
00322                     // i18n RegistryWSDLHandler.publish.conceptKey
00323                     String msg = i18n.getMessage("RegistryWSDLHandler.publish.conceptKey",
00324                                                  port.getName(),
00325                                                  key);
00326                     logger.log(BasicLevel.INFO, msg);
00327 
00328                     // get the concept from registry
00329                     Concept c = (Concept) bqm.getRegistryObject(key, LifeCycleManager.CONCEPT);
00330 
00331                     SpecificationLink specLink = blcm.createSpecificationLink();
00332                     specLink.setSpecificationObject(c);
00333                     binding.addSpecificationLink(specLink);
00334 
00335                     serviceBindings.add(binding);
00336                 }
00337                 service.addServiceBindings(serviceBindings);
00338                 services.add(service);
00339             }
00340             // Save into the Organisation
00341             org.addServices(services);
00342 
00343             Collection orgs = new ArrayList();
00344             orgs.add(org);
00345 
00346             BulkResponse br = blcm.saveOrganizations(orgs);
00347             String key = getKey(br);
00348 
00349             // i18n RegistryWSDLHandler.publish.organisationKey
00350             String msg = i18n.getMessage("RegistryWSDLHandler.publish.organisationKey",
00351                                          org,
00352                                          key);
00353             logger.log(BasicLevel.INFO, msg);
00354 
00355 
00356         } catch (JAXRException jaxre) {
00357             String err = i18n.getMessage("RegistryWSDLHandler.publish.jaxrException");
00358             logger.log(BasicLevel.ERROR, err);
00359             throw new WSServiceException(err, jaxre);
00360         }
00361     }
00362 
00370     private String getEndpoint(javax.wsdl.Port port) {
00371         String url = null;
00372         List ee = port.getExtensibilityElements();
00373         for (Iterator ext = ee.iterator(); ext.hasNext();) {
00374             ExtensibilityElement elem = (ExtensibilityElement) ext.next();
00375             if (elem instanceof SOAPAddress) {
00376                 SOAPAddress soap = (SOAPAddress) elem;
00377                 url = soap.getLocationURI();
00378             }
00379         }
00380         return url;
00381     }
00382 
00387     private String getDescription(Element e) {
00388         return e.getFirstChild().getNodeValue();
00389     }
00390 
00402     private String getKey(BulkResponse br)
00403         throws WSServiceException, JAXRException {
00404         String keyStr = null;
00405         Collection exceptions = br.getExceptions();
00406         Key key = null;
00407         if (exceptions == null) {
00408             // save OK
00409             Collection keys = br.getCollection();
00410             Iterator keyIter = keys.iterator();
00411             if (keyIter.hasNext()) {
00412                 // we have 1 key
00413                 key = (Key) keyIter.next();
00414                 keyStr = key.getId();
00415             } else {
00416                 // no key
00417                 // i18n RegistryWSDLHandler.getKey.noKeyReturned
00418                 String err = i18n.getMessage("RegistryWSDLHandler.getKey.noKeyReturned");
00419                 logger.log(BasicLevel.ERROR, err);
00420                 throw new WSServiceException(err);
00421             }
00422         } else {
00423 
00424             // Exceptions in Server Side
00425 
00426             // i18n RegistryWSDLHandler.getKey.serverSideExceptions
00427             String err = i18n.getMessage("RegistryWSDLHandler.getKey.serverSideExceptions");
00428 
00429             // Get Server Side Cause
00430             Iterator exIter = exceptions.iterator();
00431             Exception cause = null;
00432             if (exIter.hasNext()) {
00433                 // we have 1 Exception
00434                 cause = (RegistryException) exIter.next();
00435             }
00436             logger.log(BasicLevel.ERROR, err);
00437             if (cause == null) {
00438                 throw new WSServiceException(err);
00439             } else {
00440                 throw new WSServiceException(err, cause);
00441             }
00442         }
00443 
00444         return keyStr;
00445     }
00446 }

Generated on Tue Feb 15 15:05:36 2005 for JOnAS by  doxygen 1.3.9.1