package com.corej2eepatterns.servicelocator;
// importy
public class ServiceLocator {
    . . . 

  // zwraca obiekt Connection dla rejestru UDDI
  public Connection getRegistryConnection(String registryURL)
  {
    javax.xml.registry.Connection
      registryConnection = null;
    try {
      if (cache.containsKey(registryURL)) {
        registryConnection =
          (Connection) cache.get(registryURL);
      } else {
        // tworzy obiekt Connection do rejestru 
        // po ustawieniu standardowych waciwoci JAXR takich jak
        // javax.xml.registry.queryManagerURL
  
        javax.xml.registry.ConnectionFactory factory =
          ConnectionFactory.newInstance();

        // Definuje waciwoci konfiguracji poczenia
        Properties props = new Properties();
        props.setProperty(
          "javax.xml.registry.queryManagerURL", 
          registryURL);
        props.setProperty(
          "javax.xml.registry.factoryClass",
       "com.sun.xml.registry.uddi.ConnectionFactoryImpl");
        factory.setProperties(props);

        registryConnection = factory.createConnection();
        cache.put(registryURL, registryConnection);
      }
    } catch (Exception e) {
      throw new ServiceLocatorException(e);
    } catch (JAXRException je) {
      throw new ServiceLocatorException(je);
    }
    return registryConnection;
  }

  // Przeszukuje Registry w poszukiwaniu Service URI Endpoint,
  // uywajc tModelKey z opublikowanej usugi.
  // registryURL - to adres URL dostawcy UDDI
  // tModelKey   - to teksty wyszukiwania specyficzny dla
  //     opublikowanych elementw biznesowych
  public String getServiceAccessURI(
    String registryURL, String tModelKey) {

    javax.xml.registry.Connection regCon = null;
    javax.xml.registry.RegistryService regSvc = null;
    javax.xml.registry.BusinessQueryManager
      queryManager = null;
    javax.xml.registry.BusinessLifeCycleManager
      lifeCycleManager = null;
    String serviceAccessURI = null;
    try {
      if (cache.containsKey(tModelKey)) {
        serviceAccessURI = (String) cache.get(tModelKey);
      } else {
        regCon = getRegistryConnection(registryURL);
        regSvc = regCon.getRegistryService();
        queryManager = regSvc.getBusinessQueryManager();
        javax.xml.registry.infomodel.RegistryObject
          regob = businessQueryManager.
            getRegistryObject(tModelKey,
              BusinessLifeCycleManager.CONCEPT);
        Collection coll = new ArrayList();
        coll.add(regob);

        // znajdowanie organizacji 
        BulkResponse results =
          businessQueryManager.findOrganizations(
            null, null, null, coll, null, null);

        Collection co = results.getCollection();
        Iterator it = co.iterator();

        while (it.hasNext()) {
          Organization org = (Organization)it.next();
          String orgName = org.getName().getValue();
          Collection cc = org.getServices();
          for (Iterator iterator=cc.iterator(); 
            iterator.hasNext();) {
            Service service = (Service) iterator.next();
            Collection cb = service.getServiceBindings();
            for (Iterator ito = cb.iterator();
              ito.hasNext();) {                  
              ServiceBinding serviceBinding = 
                (ServiceBinding) ito.next();
              if (serviceBinding != null ||
                serviceBinding.getAccessURI() != null) {
                  serviceBinding.getAccessURI();
                  serviceAccessURI =
                    serviceBinding.getAccessURI();
                  cache.put(tModelKey,
                    serviceBinding.getAccessURI());
              }
            }
          }
        }
      }
    } catch (JAXRException e) {
      throw new ServiceLocatorException(e);
    }

    return serviceAccessURI;
  }

  . . .
}
