[Ws Wiki] Update of "How do I access jUDDI from Java" by IvanKrizsan

Apache Wiki <[email protected]>
Newsgroups gmane.comp.apache.webservices.general
Message-ID <[email protected]>
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Ws Wiki" for change notification.

The following page has been changed by IvanKrizsan:
http://wiki.apache.org/ws/How_do_I_access_jUDDI_from_Java

------------------------------------------------------------------------------
  */
  package com.ivan.jaxr;
  
+ import java.net.PasswordAuthentication;
  import java.util.ArrayList;
  import java.util.Collection;
+ import java.util.HashSet;
  import java.util.Properties;
+ import java.util.Set;
  
  import javax.xml.registry.BulkResponse;
  import javax.xml.registry.BusinessLifeCycleManager;
@@ -19, +22 @@

  import javax.xml.registry.FindQualifier;
  import javax.xml.registry.JAXRException;
  import javax.xml.registry.RegistryService;
+ import javax.xml.registry.infomodel.Key;
+ import javax.xml.registry.infomodel.Organization;
  
  /**
   * This class contains JAXR examples doing the following:
   * - Connect to a UDDI registry.
-  * - Query the UDDI registry for organization(s).
+  * - Locate organizations meeting specific criteria.
+  * - Create and save a new organization.
   * 
   * Configured to use a jUDDI instance running in Tomcat on
   * localhost.
+  * Note that a publisher must have been inserted into the database
+  * and the appropriate constant below set to reflect the publisher id.
   *
   * Requires the following libraries:
   * jaxr-api.jar, jaxr-impl.jar, jaxb-api.jar, jaxb-impl.jar,
@@ -39, +47 @@

  {
      /* Constant(s): */
      private static final String PUBLICATION_MANAGER_URL =
-         "http://localhost:8080/juddi//publication";
+         "http://localhost:8080/juddi/publish";
      private static final String INQUIRY_MANAGER_URL =
          "http://localhost:8080/juddi/inquiry";
+     private static final String JUDDI_PUBLISHER_ID = "juddi";
  
      /* Instance variable(s): */
      private BusinessQueryManager mBusinessQueryMgr;
      private BusinessLifeCycleManager mBusinessLifecycleMgr;
+     private Connection mConnection;
  
-     
      /**
       * Prepares for access to the UDDI registry.
       */
@@ -64, +73 @@

          /* 
           * Set the inquiry and publication manager URLs to be used
           * by the connection factory.
+          * Also sets the authentication method, event though it is
+          * not strictly necessary.
           */
          Properties theUDDIConnectionProperties = new Properties();
          theUDDIConnectionProperties.setProperty(
              "javax.xml.registry.queryManagerURL", INQUIRY_MANAGER_URL);
          theUDDIConnectionProperties.setProperty(
              "javax.xml.registry.lifeCycleManagerURL", PUBLICATION_MANAGER_URL);
+         theUDDIConnectionProperties.setProperty(
+             "javax.xml.registry.security.authenticationMethod",
+             "UDDI_GET_AUTHTOKEN");
  
          theJAXRConnectionFactory.setProperties(theUDDIConnectionProperties);
  
          /* Finally we can retrieve a JAXR connection. */
-         Connection theUDDIConnection =
-             theJAXRConnectionFactory.createConnection();
+         mConnection = theJAXRConnectionFactory.createConnection();
  
          /* 
           * Get the RegistryService, which is used to retrieve,
           * among other things:
           * - BusinessLifeCycleManager which allows for creation and
-          *   deletion of organizations, services, associations,
-          *   classification schemes and concepts.
+          *   deletion of different kinds of objects, such as
+          *   organizations, services etc.
-          * - BusinessQueryManager which allows for searching for the
+          * - BusinessQueryManager which allows for searching among
-          *   above entity types.
+          *   different kinds of objects.
-          * - Retrieving a CapabilityProfile object from which the
+          * - A CapabilityProfile object from which the capability
-          *   capability level and JAXR version can be retrieved.
+          *   level and JAXR version can be retrieved.
           */
+         RegistryService theRegistryService = mConnection.getRegistryService();
-         RegistryService theRegistryService =
-             theUDDIConnection.getRegistryService();
  
          mBusinessQueryMgr = theRegistryService.getBusinessQueryManager();
          mBusinessLifecycleMgr =
@@ -105, +117 @@

          {
              JAXRExamples theInstance = new JAXRExamples();
              theInstance.init();
-             
+ 
-             theInstance.queryForOrganization();
+             theInstance.addOrganization("Bad Cars Inc");
          } catch (JAXRException theException)
          {
              theException.printStackTrace();
@@ -123, +135 @@

          /*
           * Create list of query qualifiers. In this case:
           * - Case sensitive matching of organization names.
-          * - Sort found organisations by name in descending order.
+          * - Sort found organizations by name in descending order.
           */
-         ArrayList theQueryQualifiers = new ArrayList();
+         ArrayList<String> theQueryQualifiers = new ArrayList<String>();
          theQueryQualifiers.add(FindQualifier.CASE_SENSITIVE_MATCH);
          theQueryQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
  
+         /* 
-         /* Create list of organization names to search for. */
+          * Create list of organization names to search for.
+          * The '%%' means that any name will match.
+          */
-         ArrayList theOrganizationNames = new ArrayList();
+         ArrayList<String> theOrganizationNames = new ArrayList<String>();
          theOrganizationNames.add("%%");
  
          /* Use the business query manager to query for the organization(s). */
@@ -138, +153 @@

              mBusinessQueryMgr.findOrganizations(theQueryQualifiers,
                  theOrganizationNames, null, null, null, null);
  
-         /* Retrieve list of exceptions and results from the response. */
          Collection theQueryExceptions = theQueryResponse.getExceptions();
-         Collection theOrganizations = theQueryResponse.getCollection();
+         Collection<Organization> theOrganizations = theQueryResponse.getCollection();
  
+         if (theQueryExceptions == null)
+         {
-         /* Output the result to the console. */
+             /* Output the result to the console. */
-         System.out.println("Listing organizations:");
+             System.out.println("Listing organizations:");
-         for (Object theOrganization : theOrganizations)
+             for (Organization theOrganization : theOrganizations)
+             {
+                 System.out.println("An organization: "
+                     + theOrganization.getName().getValue());
+             }
+         } else
+         {
+             System.out.println("An error occurred querying for organizations.");
-         {
+         }
-             System.out.println("An organization: " + theOrganization);
+     }
+ 
+     /**
+      * Adds an organization with the supplied name to the registry.
+      * 
+      * @param inOrgName Name of organization to add.
+      * @throws JAXRException If error occurred inserting organization
+      * into registry.
+      */
+     public void addOrganization(final String inOrgName) throws JAXRException
+     {
+         /* 
+          * Set the credentials for the connection.
+          * JAXR will help us retrieve an UDDI authorization token and
+          * include it in subsequent requests.
+          * JUDDI will, in its default configuration, authenticate users
+          * which are present in the PUBLISHER table, PUBLISHER_ID column
+          * regardless of the password supplied.
+          */
+         PasswordAuthentication thePswAuthentication =
+             new PasswordAuthentication(JUDDI_PUBLISHER_ID, "".toCharArray());
+         Set<PasswordAuthentication> theConnectionCredentials =
+             new HashSet<PasswordAuthentication>();
+         theConnectionCredentials.add(thePswAuthentication);
+         mConnection.setCredentials(theConnectionCredentials);
+ 
+         /* Create an organization object and give it a name. */
+         Organization theOrganization =
+             mBusinessLifecycleMgr.createOrganization(inOrgName);
+ 
+         /* Place all organizations to be added in a collection. */
+         Collection<Organization> theOrgCollection =
+             new ArrayList<Organization>();
+         theOrgCollection.add(theOrganization);
+ 
+         /* Save the organization(s) in the registry. */
+         BulkResponse theQueryResponse =
+             mBusinessLifecycleMgr.saveOrganizations(theOrgCollection);
+ 
+         /* 
+          * If no exceptions in the response, then the operation
+          * succeeded and there will be a key for the saved organization.
+          */
+         if (theQueryResponse.getExceptions() == null)
+         {
+             Collection<Key> theResponseCollection =
+                 theQueryResponse.getCollection();
+             Key theOrgKey = theResponseCollection.iterator().next();
+             System.out
+                 .println("Successfully created and saved an organization.");
+             System.out.println("Organization Key = " + theOrgKey.getId());
+         } else
+         {
+             System.out.println("An error occurred adding the organization: " +
+                 inOrgName);
          }
      }
  }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.