1 26 27 package org.objectweb.jonas.ws.handler; 28 29 import java.net.MalformedURLException ; 30 import java.net.PasswordAuthentication ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Properties ; 38 import java.util.Set ; 39 40 import javax.wsdl.extensions.ExtensibilityElement; 41 import javax.wsdl.extensions.soap.SOAPAddress; 42 import javax.xml.registry.BulkResponse ; 43 import javax.xml.registry.BusinessLifeCycleManager ; 44 import javax.xml.registry.BusinessQueryManager ; 45 import javax.xml.registry.Connection ; 46 import javax.xml.registry.ConnectionFactory ; 47 import javax.xml.registry.JAXRException ; 48 import javax.xml.registry.LifeCycleManager ; 49 import javax.xml.registry.RegistryException ; 50 import javax.xml.registry.RegistryService ; 51 import javax.xml.registry.infomodel.Classification ; 52 import javax.xml.registry.infomodel.ClassificationScheme ; 53 import javax.xml.registry.infomodel.Concept ; 54 import javax.xml.registry.infomodel.ExternalLink ; 55 import javax.xml.registry.infomodel.InternationalString ; 56 import javax.xml.registry.infomodel.Key ; 57 import javax.xml.registry.infomodel.Organization ; 58 import javax.xml.registry.infomodel.PersonName ; 59 import javax.xml.registry.infomodel.Service ; 60 import javax.xml.registry.infomodel.ServiceBinding ; 61 import javax.xml.registry.infomodel.SpecificationLink ; 62 import javax.xml.registry.infomodel.User ; 63 64 import org.w3c.dom.Element ; 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 92 public class RegistryWSDLHandler implements WSDLHandler { 93 94 97 private static final String PUBLISH_URL_PROP 98 = "javax.xml.registry.lifeCycleManagerURL"; 99 100 103 private static final String INQUIRY_URL_PROP 104 = "javax.xml.registry.queryManagerURL"; 105 106 109 private static final String USERNAME 110 = "jonas.service.publish.uddi.username"; 111 112 115 private static final String PASSWORD 116 = "jonas.service.publish.uddi.password"; 117 118 121 private static final String ORGANIZATION_NAME 122 = "jonas.service.publish.uddi.organization.name"; 123 124 127 private static final String ORGANIZATION_DESC 128 = "jonas.service.publish.uddi.organization.desc"; 129 130 133 private static final String PERSON_NAME 134 = "jonas.service.publish.uddi.organization.person_name"; 135 136 137 138 private ConnectionFactory connFactory; 139 140 141 private PasswordAuthentication passAuth; 142 143 144 private Properties props; 145 146 147 private static Logger logger = Log.getLogger(Log.JONAS_PUBLISH_PREFIX); 148 149 150 private static I18n i18n = I18n.getInstance(RegistryWSDLHandler.class); 151 152 159 public RegistryWSDLHandler(Properties props) throws WSServiceException { 160 161 this.props = props; 162 163 String publishURL = props.getProperty(PUBLISH_URL_PROP); 165 String queryURL = props.getProperty(INQUIRY_URL_PROP); 166 String username = props.getProperty(USERNAME); 167 String password = props.getProperty(PASSWORD); 168 169 try { 171 new URL (publishURL); 172 } catch (MalformedURLException mue) { 173 String 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 (queryURL); 180 } catch (MalformedURLException mue) { 181 String err = i18n.getMessage("RegistryWSDLHandler.constr.mue", queryURL); 182 logger.log(BasicLevel.ERROR, err); 183 throw new WSServiceException(err, mue); 184 } 185 186 try { 187 connFactory = ConnectionFactory.newInstance(); 189 connFactory.setProperties(props); 190 } catch (JAXRException jaxre) { 191 String err = i18n.getMessage("RegistryWSDLHandler.constr.jaxrException"); 192 logger.log(BasicLevel.ERROR, err); 193 throw new WSServiceException(err, jaxre); 194 } 195 196 if (username != null && password != null) { 198 passAuth = new PasswordAuthentication (username, password.toCharArray()); 199 } else { 200 String err = i18n.getMessage("RegistryWSDLHandler.constr.noCreds"); 201 logger.log(BasicLevel.ERROR, err); 202 throw new WSServiceException(err); 203 } 204 205 String name = props.getProperty(ORGANIZATION_NAME); 207 String person = props.getProperty(PERSON_NAME); 208 if ((name == null) || (person == null)) { 209 String err = i18n.getMessage("RegistryWSDLHandler.constr.missingProp"); 210 logger.log(BasicLevel.ERROR, err); 211 throw new WSServiceException(err); 212 } 213 } 214 215 220 public void publish(ServiceDesc sd) throws WSServiceException { 221 222 try { 223 javax.wsdl.Definition def = sd.getWSDL().getDefinition(); 224 225 Connection connection = connFactory.createConnection(); 227 228 Set creds = new HashSet (); 230 creds.add(passAuth); 231 connection.setCredentials(creds); 232 233 RegistryService rs = connection.getRegistryService(); 235 236 BusinessQueryManager bqm = rs.getBusinessQueryManager(); 238 239 BusinessLifeCycleManager blcm = rs.getBusinessLifeCycleManager(); 241 242 InternationalString is = null; 244 245 Organization org = blcm.createOrganization(props.getProperty(ORGANIZATION_NAME)); 247 String orgDesc = props.getProperty(ORGANIZATION_DESC); 248 if (orgDesc != null) { 249 is = blcm.createInternationalString(orgDesc); 250 org.setDescription(is); 251 } 252 User contact = blcm.createUser(); 253 PersonName pName = blcm.createPersonName(props.getProperty(PERSON_NAME)); 254 contact.setPersonName(pName); 255 org.setPrimaryContact(contact); 256 257 Collection services = new ArrayList (); 258 for (Iterator servicesIter = def.getServices().values().iterator(); 260 servicesIter.hasNext();) { 261 262 javax.wsdl.Service s = (javax.wsdl.Service) servicesIter.next(); 263 Service service = blcm.createService(s.getQName().getLocalPart()); 264 265 Element desc = s.getDocumentationElement(); 266 if (desc != null) { 267 String description = getDescription(desc); 268 is = blcm.createInternationalString(description); 269 service.setDescription(is); 270 } 271 272 Collection serviceBindings = new ArrayList (); 274 for (Iterator portsIter = s.getPorts().values().iterator(); 275 portsIter.hasNext();) { 276 277 javax.wsdl.Port port = (javax.wsdl.Port) portsIter.next(); 278 279 ServiceBinding binding = blcm.createServiceBinding(); 280 281 Element pDesc = port.getDocumentationElement(); 282 String pDescription = port.getName(); 283 if (pDesc != null) { 284 pDescription += " " + getDescription(pDesc); 285 } 286 is = blcm.createInternationalString(pDescription); 287 binding.setDescription(is); 288 289 binding.setValidateURI(false); 292 String url = getEndpoint(port); 293 binding.setAccessURI(url); 294 295 Concept concept = blcm.createConcept(null, port.getName() + " Concept", ""); 297 is = blcm.createInternationalString(pDescription + " Concept"); 298 concept.setDescription(is); 299 300 ExternalLink extlink = blcm.createExternalLink("file://", 301 "WSDL"); 302 extlink.setValidateURI(false); 303 extlink.setExternalURI(url + "?JWSDL"); 305 306 concept.addExternalLink(extlink); 307 308 ClassificationScheme uddiOrgTypes = 310 bqm.findClassificationSchemeByName(null, "uddi-org:types"); 311 Classification wsdlSpecClass = blcm.createClassification(uddiOrgTypes, 312 "wsdlSpec", 313 "wsdlSpec"); 314 concept.addClassification(wsdlSpecClass); 315 316 Collection concepts = new ArrayList (); 318 concepts.add(concept); 319 BulkResponse br = blcm.saveConcepts(concepts); 320 String key = getKey(br); 321 322 String msg = i18n.getMessage("RegistryWSDLHandler.publish.conceptKey", 324 port.getName(), 325 key); 326 logger.log(BasicLevel.INFO, msg); 327 328 Concept c = (Concept ) bqm.getRegistryObject(key, LifeCycleManager.CONCEPT); 330 331 SpecificationLink 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 org.addServices(services); 342 343 Collection orgs = new ArrayList (); 344 orgs.add(org); 345 346 BulkResponse br = blcm.saveOrganizations(orgs); 347 String key = getKey(br); 348 349 String msg = i18n.getMessage("RegistryWSDLHandler.publish.organisationKey", 351 org, 352 key); 353 logger.log(BasicLevel.INFO, msg); 354 355 356 } catch (JAXRException jaxre) { 357 String err = i18n.getMessage("RegistryWSDLHandler.publish.jaxrException"); 358 logger.log(BasicLevel.ERROR, err); 359 throw new WSServiceException(err, jaxre); 360 } 361 } 362 363 370 private String getEndpoint(javax.wsdl.Port port) { 371 String url = null; 372 List ee = port.getExtensibilityElements(); 373 for (Iterator 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 387 private String getDescription(Element e) { 388 return e.getFirstChild().getNodeValue(); 389 } 390 391 402 private String getKey(BulkResponse br) 403 throws WSServiceException, JAXRException { 404 String keyStr = null; 405 Collection exceptions = br.getExceptions(); 406 Key key = null; 407 if (exceptions == null) { 408 Collection keys = br.getCollection(); 410 Iterator keyIter = keys.iterator(); 411 if (keyIter.hasNext()) { 412 key = (Key ) keyIter.next(); 414 keyStr = key.getId(); 415 } else { 416 String err = i18n.getMessage("RegistryWSDLHandler.getKey.noKeyReturned"); 419 logger.log(BasicLevel.ERROR, err); 420 throw new WSServiceException(err); 421 } 422 } else { 423 424 426 String err = i18n.getMessage("RegistryWSDLHandler.getKey.serverSideExceptions"); 428 429 Iterator exIter = exceptions.iterator(); 431 Exception cause = null; 432 if (exIter.hasNext()) { 433 cause = (RegistryException ) 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 |