Publishing Data to a Registry

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Chapter 10.  JAXR Implementations


An organization publishes the information about itself to a registry. This enables other organizations to query the registry, get information about the companies whose information is published in the registry, and engage in business with them.

To understand how to publish data to a registry, you will write an application called MyOrganizationPublisher.java. This application will publish the data about a fictitious company called The Great Car Parts Company.

The first requirement to publish or modify information to a registry is to obtain a valid user ID and password from the registry. The JWSDP Registry Server provides the default user ID/password pair of testuser/testuser. You will be using this user ID/password pair in your applications. After you have the user ID and password, you need to do the following in your application to publish data to a registry:

  1. Create a connection with the registry.

  2. Using the user ID and password, set the credentials with the registry.

  3. Create an Organization object and populate it with Name, Description, Key, PrimaryContact, and a collection of Classification, Service, and ServiceBindings objects.

  4. Save the organization to the registry.

Now let's get down to actually developing the MyOrganizationPublisher.java application. In the application, you will do the following:

  • Import the necessary classes.

  • Create a method called connect that creates a connection with the registry.

  • Create a method called publishdata that publishes data to the registry.

Creating the MyOrganizationPublisher.java Application

To create the MyOrganizationPublisher.java application, select a text editor and create an empty text file called MyOrganizationPublisher.java. In the file, enter the following lines of code to declare the MyOrganizationPublisher application:

public class MyOrganizationPublisher { public static void main(String[] args) {     } } 

Importing the Required Packages and Classes

Next you will import the required classes. You need to enter the following packages and classes:

  • javax.xml.registry.* The classes of the javax.xml.registry package are required to create a connection to the registry, and to obtain the interface that enables you to access the information stored in the JAXR information model.

  • javax.xml.registry.infomodel.* The classes of the javax.xml.registry.infomodel package enable you to access the elements of a registry. In using the classes of this package, you model the information that is to be published in the registry.

  • java.net.* The classes of the java.net package enable you to connect to network devices.

  • java.security.* The classes of the java.security package enable you to build the password authentication mechanism.

  • java.util.* The java.util package provides a number of utility classes such as Collection and Iterator. As you will see later, most of the JAXR methods take Collection objects as parameters or return Collection objects. Therefore, the classes of the java.util package are required to create the parameters and store the return values from the JAXR methods.

To import the classes, enter the following lines of code shown in bold:

import javax.xml.registry.*; import javax.xml.registry.infomodel.*; import java.net.*; import java.security.*; import java.util.*; public class MyOrganizationPublisher { 

Writing the Plumbing Code

Now you will write the necessary plumbing code for the application. In the MyOrganizationPublisher application, you need to define the following:

  • A null Connection object. This will be used to hold the reference to the Connection object obtained from the instance of the ConnectionFactory class.

    You will be using this to make the connect method, which will have the required code to make a connection to the registry server.

  • The publishdata method, which will have the required code to publish data to the registry.

To accomplish these tasks, enter the following lines of code shown in bold:

public class MyOrganizationPublisher {     Connection connection = null;     public static void main(String[] args) {     MyOrganizationPublisher mop = new MyOrganizationPublisher();         mop.connect();         mop.publishdata();     } 

Connecting to the Registry

With the plumbing code done, let's write the code to connect to the registry. Similar to what you have done so far for the other APIs, a connection to a registry is obtained using a connection factory. However, unlike the other APIs, you need to perform one extra step before you can create a connection to the registry service.

The extra step is to set six properties: the URLs to the publish and query registries, the HTTP proxy host and port address for accessing the query service, and the HTTPS proxy host and port address for accessing the publish service:

  • javax.xml.registry.queryManagerURL This property specifies the URL of the query manager service in the registry to which the application is connecting.

  • javax.xml.registry.lifeCycleManagerURL This property specifies the URL for the life cycle manager service in the registry. The life cycle manager service enables you to publish and manage data in the registry.

  • com.sun.xml.registry.http.proxyHost This property specifies the HTTP proxy host that will be used to connect to registries outside your enterprise's firewall. The HTTP proxy host is used when you are accessing the registry for queries.

  • com.sun.xml.registry.http.proxyPort This property specifies the HTTP proxy port to connect to registries outside your enterprise's firewall. Typically, the port value is 8080.

  • com.sun.xml.registry.https.proxyHost This property specifies the HTTPS proxy host that will be used to connect to registries outside your enterprise's firewall. The HTTPS proxy host is used when you are accessing the registry for publishing or editing data.

  • com.sun.xml.registry.https.proxyPort This property specifies the HTTPS proxy port to connect to registries outside your enterprise's firewall. Typically, the port value is 8080.

As you will be using the JWSDP Registry Server, the URLs to the publish and query registries are the same. However, the URLs for other UDDI-based registries might be different. You might recall that the UDDI specification describes two API sets for publishing and inquiring: publish APIs and inquiry APIs. Following that naming convention, the IBM and the Microsoft registries have different URLs for inquiry and publishing services. For example, the IBM registry has the following URLs for inquiry and publishing, respectively:

http://www-3.ibm.com/services/uddi/v2beta/inquiryapi

https://www-3.ibm.com/services/uddi/v2beta/protect/publishapi

Also, because the JWSDP Registry Server is within the firewall, the proxy host and port address will be empty. Here again, in case you want to access registries outside your firewall, you will need to specify the proxy host and port address.

Coming back to the code for the connect method, you need to do the following:

  1. Define two String objects to hold the URL values for the JWSDP Registry Server query and publish services. For the JWSDP registry service, the URL for both the publish and inquiry services is the same.

  2. Define four String objects for storing the proxy host and port values for the HTTP and HTTPS connections.

  3. Create a Properties object and store the properties.

  4. Set the properties on the ConnectionFactory instance.

  5. Create the connection to the registry.

To accomplish these tasks, enter the lines of code shown in Listing 10.1.

Listing 10.1 The connect Method
public void connect() {         String queryURL =               "http://localhost:8080/registry-server/RegistryServerServlet";         String publishURL =             "http://localhost:8080/registry-server/RegistryServerServlet";         String httpProxyHost = "";         String httpProxyPort = "";         String httpsProxyHost = "";         String httpsProxyPort = "";         Properties props = new Properties();         props.setProperty("javax.xml.registry.queryManagerURL",             queryUrl);         props.setProperty("javax.xml.registry.lifeCycleManagerURL",             publishUrl);         props.setProperty("com.sun.xml.registry.http.proxyHost",             httpProxyHost);         props.setProperty("com.sun.xml.registry.http.proxyPort",             httpProxyPort);         props.setProperty("com.sun.xml.registry.https.proxyHost",             httpsProxyHost);         props.setProperty("com.sun.xml.registry.https.proxyPort",             httpsProxyPort);         try {                ConnectionFactory factory =                ConnectionFactory.newInstance();             factory.setProperties(props);             connection = factory.createConnection();             System.out.println("Created connection to registry");         } catch (Exception e) {             e.printStackTrace();            }     } 

Now that the connection to the registry server has been made, let's write the code for the publishdata method, which takes care of publishing data to the registry.

Publishing Data to the Registry

Publishing data to the registry implies creating an Organization object, populating the object, submitting the object to the registry, saving the submitted data, and obtaining the organization key generated by the registry when the data is saved.

For this example, you will use the following information to populate the Organization object:

  • Company Name: The Great Car Parts Company

  • Description: Suppliers of high quality car accessories

  • Contact Person Name: Jack

  • Telephone Number: (800) 555-111

  • Contact Email Address: jack@greatcarpartcompany.com

  • Classification Scheme To Be Used: NAICS

  • Classified As: Car Parts and Accessories

  • Service: CarPartRequest

  • Service Description: Order car parts

  • Service Binding URI: http://greatcarpartcompany.com:8080/CarPartRequest/

  • Service Binding Description: Binding for CarPartRequest Service

To do all this, you need to do the following in the publishdata method:

  1. Get a reference to the RegistryService object.

  2. From the RegistryService object, obtain the instances of the BusinessLifeCycleManager and BusinessQueryManager objects. The BusinessLifeCycleManager object enables the creation of the Organization object and its constituent objects that are submitted to the registry. The BusinessQueryManager enables you to run queries on the registry service.

  3. Create a PasswordAuthentication object with the default user ID/password combination of testuser/testuser. Using the PasswordAuthentication object, set the credentials with the registry.

  4. Create an Organization object and populate it with Name, Description, Key, PrimaryContact, and a collection of Classification, Service, and ServiceBindings objects.

  5. Save the organization to the registry.

  6. Get the key generated by the registry when an organization is saved. This key is required when you want to modify registry data.

To get the instances of the interfaces, enter the following lines of code:

public void publishdata() {         String username = "testuser";         String password = "testuser";         RegistryService rsrvc = null;         BusinessLifeCycleManager bsnslfcmgr = null;         BusinessQueryManager bqrymgr = null;         try {             rsrvc = connection.getRegistryService();             bsnslfcmgr = rsrvc.getBusinessLifeCycleManager();             bqrymgr = rsrvc.getBusinessQueryManager();             System.out.println("Got registry service, query " +                 "manager, and life cycle manager"); 

Next you need to set the credentials with the registry. To do so, enter the following lines of code shown in bold:

System.out.println("Got registry service, query manager, and life cycle manager");             // Get authorization from the registry             PasswordAuthentication passwdAuth =                 new PasswordAuthentication(username,                     password.toCharArray());             Set creds = new HashSet();             creds.add(passwdAuth);             connection.setCredentials(creds);         System.out.println("Security credentials established"); 

Note that you created a Set object and populated it with the PasswordAuthentication object. This was required because the setCredentials method of the Connection object takes a Set object as its parameter.

Next you will create an Organization object and provide a description for it. To do so, enter the following lines of code shown in bold:

System.out.println("Security credentials established");             // Create organization name and description             Organization orgn =                 bsnslfcmgr.createOrganization("The Great Car Parts Company");             InternationalString descn =                 bsnslfcmgr.createInternationalString("Suppliers of high quality car  graphics/ccc.gifaccessories");             orgn.setDescription(descn);             System.out.println("Description about the organization added"); 

The next step is to create an Organization object. To do so, enter the following bold lines of code:

System.out.println("Security credentials established");             // Create organization, specify name and description             Organization orgn =                 bsnslfcmgr.createOrganization("The Great Car Parts Company");             InternationalString descn =                 bsnslfcmgr.createInternationalString("Suppliers of high quality car  graphics/ccc.gifaccessories");             orgn.setDescription(descn);        System.out.println("Description about the organization added"); 

The Organization object needs to be populated with contact details. To do so, enter the following lines of code shown in bold:

System.out.println("Security credentials established");             // Create organization, specify name and description             Organization orgn =                 bsnslfcmgr.createOrganization("The Great Car Parts Company");             InternationalString descn =                 bsnslfcmgr.createInternationalString("Suppliers of high quality car  graphics/ccc.gifaccessories");             orgn.setDescription(descn);             System.out.println("Description about the organization added");             // Enter the Contact Person Name             User primaryContact = bsnslfcmgr.createUser();             PersonName pName = bsnslfcmgr.createPersonName("Jack");             primaryContact.setPersonName(pName);             // Enter primary contact phone number             TelephoneNumber telNo = bsnslfcmgr.createTelephoneNumber();             telNo.setNumber("(800) 555-1111");       Collection phoneNums = new ArrayList();             phoneNums.add(telNo);             primaryContact.setTelephoneNumbers(phoneNums);             // Enter primary contact email address             EmailAddress emailAdd =                 bsnslfcmgr.createEmailAddress("jack@greatcarpartcompany.com");             Collection emailAddresses = new ArrayList();             emailAddresses.add(emailAdd);             primaryContact.setEmailAddresses(emailAddresses);             // Set primary contact for organization             orgn.setPrimaryContact(primaryContact);        System.out.println("Primary contact details added"); 

Now you will specify the classification scheme under which to classify the organization. In our case, it is the NAICS classification scheme. To do that, you will first query the registry to get the NAICS classification scheme, and then classify the organization within the NAICS classification scheme.

To do so, enter the lines of code shown here in bold:

System.out.println("Primary contact details added");             // Set classification scheme to NAICS             ClassificationScheme cScheme =                 bqrymgr.findClassificationSchemeByName(null, "ntis-gov:naics");             // Create and add classification             Classification classification = (Classification)                 bsnslfcmgr.createClassification(cScheme,                     "Car Parts and Accessories", "123456");             Collection classifications = new ArrayList();             classifications.add(classification);             orgn.addClassifications(classifications);         System.out.println("Classification added"); 

Note that you use the findClassificationSchemeByName method of the BusinessQueryManager interface to query the registry for NAICS classification.

Next you will specify the services that the organization provides. For each service, there will be a ServiceBinding object that describes how to access the service. This description consists of the textual description, an access URI, and an optional specification link. To do so, add the lines of code shown here in bold:

System.out.println("Classification added"); // Create services and service          Collection services = new ArrayList();             Service service =                 bsnslfcmgr.createService("CarPartRequest");             InternationalString desc =                 bsnslfcmgr.createInternationalString("Order Car Parts");             service.setDescription(desc);             // Create service bindings             Collection serviceBindings = new ArrayList();             ServiceBinding binding = bsnslfcmgr.createServiceBinding();             descn = bsnslfcmgr.createInternationalString("Binding for CarPartRequest  graphics/ccc.gifservice");             binding.setDescription(descn);             // turn off address validation             binding.setValidateURI(false);             binding.setAccessURI("http://greatcarpartcompany.com:8080/CarPartRequest/");             serviceBindings.add(binding);             // Add service bindings to service             service.addServiceBindings(serviceBindings);             // Add service to services, then add services to organization             services.add(service);             orgn.addServices(services);           System.out.println("Services added"); 

Note that JAXR attempts to check the validity of the access URI by default. Because in this case it is a hypothetical URI, you do not want JAXR to validate the address. To do so, you would use the setValidate method with the false parameter.

After you have added the services and their service bindings, you are ready to save the Organization object to the registry. To save the Organization object, you use the saveOrganizations method of the BusinessLifeCycleManager interface. The saveOrganizations method returns a BulkResponse object. The BulkResponse object contains a collection of objects. Depending upon whether the save was successful, the BulkResponse will contain the keys generated by either the registry or exception objects.

To save the Organization object and determine the status of the save operation, enter the lines of code listed in bold here:

System.out.println("Services added"); // Add organization and submit to registry             // Retrieve key if successful             Collection orgs = new ArrayList();             orgs.add(orgn);             //Iterator for keys or exception             Iterator itr;             BulkResponse response = bsnslfcmgr.saveOrganizations(orgs);             Collection exceptions = response.getExceptions();             if (exceptions == null) {                 System.out.println("Organization saved");                 Collection keys = response.getCollection();                 itr = keys.iterator();                 if (itr.hasNext()) {                     javax.xml.registry.infomodel.Key orgKey =                     (javax.xml.registry.infomodel.Key) itr.next();                 String id = orgKey.getId();              System.out.println("Organization key is " + id);             }         } else {             itr = exceptions.iterator();             Exception exception = null;             while (itr.hasNext()) {                 exception = (Exception) itr.next();                 System.err.println("Failed to save because: " +                     exception.toString());             }         }     } catch (Exception e) {         e.printStackTrace();     } finally  {         // At end, close connection to registry         if (connection != null) {             try {                 connection.close();             } catch (JAXRException je) {}         }     } } 

NOTE

The code discussed so far in this chapter is available in the MyJAXRExample folder.


You are now all set to compile and run the MyOrganizationPublisher application. To compile the application, specify the following command at the command prompt:

javac -classpath .;d:\jwsdp-1_0\common\lib\jaxr-ri.jar;d:\jwsdp-1_0\common\lib\jaxr-api. graphics/ccc.gifjar;d:\jwsdp-1_0\common\lib\jaxrpc-ri.jar;d:\jwsdp-1_0\common\lib\jaxrpc-api.jar;d:\ graphics/ccc.gifjwsdp-1_0\common\lib\activation.jar;d:\jwsdp-1_0\common\lib\dom4j.jar;d:\jwsdp-1_0\common\ graphics/ccc.giflib\jaxm-api.jar;d:\jwsdp-1_0\common\lib\jaxm-client.jar;d:\jwsdp-1_0\common\lib\log4j. graphics/ccc.gifjar;d:\jwsdp-1_0\common\lib\mail.jar;d:\jwsdp-1_0\common\lib\xalan.jar;d:\jwsdp-1_0\ graphics/ccc.gifcommon\lib\xerces.jar;d:\jwsdp-1_0\common\lib\jaxp-api.jar-d . MyOrganizationPublisher.java 

NOTE

To build the application, you can also use the buildclient.bat batch file available in the MyJAXRExample folder. The syntax to run the buildclient.bat file is as follows:

buildclient MyOrganizationPublisher.java 

To run the application, enter the following at the command prompt:

java -DuseSOAP=true -classpath .;d:\jwsdp-1_0\common\endorsed\xalan.jar; d:\jwsdp-1_0\ graphics/ccc.gifcommon\endorsed\dom.jar; d:\jwsdp-1_0\common\endorsed\sax.jar;d:\jwsdp-1_0\common\ graphics/ccc.gifendorsed\xercesImpl.jar;d:\jwsdp-1_0\common\endorsed\xsltc.jar;d:\jwsdp-1_0\common\lib\ graphics/ccc.gifjaxr-ri.jar;d:\jwsdp-1_0\common\lib\jaxr-api.jar;d:\jwsdp-1_0\common\lib\jaxrpc-ri.jar;d:\ graphics/ccc.gifjwsdp-1_0\common\lib\jaxrpc-api.jar;d:\jwsdp-1_0\common\lib\activation.jar;d:\jwsdp-1_0\ graphics/ccc.gifcommon\lib\dom4j.jar;d:\jwsdp-1_0\common\lib\jaxm-api.jar;d:\jwsdp-1_0\common\lib\ graphics/ccc.gifjaxm-client.jar;d:\jwsdp-1_0\common\lib\log4j.jar;d:\jwsdp-1_0\common\lib\mail.jar;d:\ graphics/ccc.gifjwsdp-1_0\common\lib\xalan.jar;d:\jwsdp-1_0\common\lib\xerces.jar;d:\jwsdp-1_0\common\lib\ graphics/ccc.gifjaxp-api.jar;d:\jwsdp-1_0\common\lib\commons-logging. jar;d:\jwsdp-1_0\common\lib\soap. graphics/ccc.gifjar;d:\jwsdp-1_0\common\lib\castor-0.9.3.9-xml.jar; MyOrganizationPublisher 

NOTE

To run the application, you can also use the buildclient.bat batch file available in the MyJAXRExample folder. The syntax to run the runclient.bat file is as follows:

runclient MyOrganizationPublisher 

The output of the application would be similar to the following code:

Created connection to registry Got registry service, query manager, and life cycle manager Security credentials established Description about the organization added Primary contact details added Classification added Services added Organization saved Organization key is eeb543f8-5dee-b543-8407-30aeb62c40bd Note that the organization key generated by the UDDI registry might be different. 

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/ch10lev1sec2]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net