Querying Data from 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


So far in the chapter, you've learned how to save information about an organization into a registry. In this section, you will learn to write an application that queries a registry for information about organizations.

The application you will write will be named MyRegistryQuery.java. It will take a string as the parameter and then search the registry for all organizations that have the specified string in their names.

Similar to the MyOrganizationPublisher application, you will import the necessary classes, create a connect method to connect to the registry, and create a runQuery method to do the actual querying on the registry.

Creating the MyRegistryQuery.java Application

To create the MyRegistryQuery application, select a text editor and create an empty text file called MyRegistryQuery.java. Enter the following lines of code in the file to import the classes, and declare the MyRegistryQuery application:

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

Note that you do not need to have a user ID and password to run a query against a registry. Therefore, unlike the MyOrganizationPublisher application, there is no need to import the java.security package in the MyRegistryQuery application.

Writing the Plumbing Code

Now you will write the necessary plumbing code for the application. In the MyRegistryQuery 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.

  • Because the application needs a string as the parameter, you need to specify code to check it. In case the parameter is not specified, the program should exit.

  • A default constructor for the MyRegistryQuery class. You will use this constructor to instantiate the MyRegistryQuery class and make calls to the connect and runQuery methods from within the application.

  • The connect method, which will have the required code to make a connection to the registry server.

  • The runQuery method, which will have the required code to run the query on the registry.

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

public class MyRegistryQuery {     Connection connection = null;     public static void main(String[] args) {         if (args.length < 1) {             System.out.println("Usage: runclient " +                 "MyRegistryQuery string");             System.exit(1);         }         String queryString = new String(args[0]);         System.out.println("Query string is " + queryString);         MyRegistryQuery mrq = new MyRegistryQuery();        mrq.connect();     mrq.runQuery(queryString);     } 

Connecting to the Registry

Next you will write the code that enables the application to connect to the registry. The code for connecting to the registry (the connect method) is very similar to the one that you wrote for MyOrganizationPublisher, and is shown in Listing 10.2. The only difference is that because you will only be querying the registry, there is no need to define the system properties for HTTPS connections. These properties need to be defined only when you access the registry for publishing data or for the modification of data.

Listing 10.2 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 = "";     Properties props = new Properties();     props.setProperty("javax.xml.registry.queryManagerURL",      queryURL);     props.setProperty("com.sun.xml.registry.http.proxyHost",     httpProxyHost);    props.setProperty("com.sun.xml.registry.http.proxyPort",      httpProxyPort);       try {             // Create the connection,             ConnectionFactory factory =                 ConnectionFactory.newInstance();             factory.setProperties(props);             connection = factory.createConnection();             System.out.println("Connection created to the registry");         } catch (Exception e) {             e.printStackTrace();         }     } 

Now that the connection to the registry server has been made, let's write the code for the runQuery method that takes care of querying the registry.

Querying the Registry

A JAXR client queries the registry to get information about the organizations that have published data in the registry. The BusinessQueryManager interface provides a number of find methods with which an application can get different types of information from the registry. For example, the findOrganizations method returns a list of organizations that meet the criteria specified in the search string, such as the name of the organization. Similarly, the findServices method returns the set of services published by an organization.

In the MyRegistryQuery application, a string is passed as a command-line parameter. The application then searches for the organizations that have the specified string appearing in their names.

To build this query, you need to do the following:

  1. Get a reference to the RegistryService object.

  2. From the RegistryService object, obtain the instance of and BusinessQueryManager object. The BusinessQueryManager enables you to run queries on the registry service.

  3. Use one of the constants of the FindQualifier interface to determine the sorting and pattern-matching criteria. For example, you can determine whether the searched data should be sorted by name in an ascending or descending order.

  4. Use the string supplied from the command line as the name pattern against which to match.

  5. Call the findOrganization method with the combination of FindQualifiers and name patterns as parameters.

  6. Iterate over the BulkResponse object received as a result of the findOrganization method and display the organization data.

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

public void runQuery(String qString) {         RegistryService rsvrc = null;         BusinessQueryManager bsnsqrymgr = null;         try {             // Get registry service and query manager             rsvrc = connection.getRegistryService();             bsnsqrymgr = rsvrc.getBusinessQueryManager();             System.out.println("Got registry service and " +                 "query manager"); 

Next you need to specify the FindQualifier constants and the name pattern. The findOrganiation method takes these as the parameters to query the registry. However, the findOrganization method takes these as Collection objects. So, you need to first convert the FindQualifier and name pattern to Collection objects. To do so, enter the code shown here in bold:

System.out.println("Got registry service and " +                 "query manager"); // Specify the find qualifiers and name patterns             Collection findQualifiers = new ArrayList();             findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);             Collection namePatterns = new ArrayList();             namePatterns.add("%" + qString + "%"); 

Note that the query string is wrapped in a pair of % characters. This ensures that the query returns all those organizations that have the specified query string anywhere in their name.

Next you need to invoke the findOrganizations method. This method returns a BulkResponse object. To invoke the findOrganizations method, enter the following lines of code:

namePatterns.add("%" + qString + "%");        // Find organizations that match the name pattern containing the query string             BulkResponse response =                 bsnsqrymgr.findOrganizations(findQualifiers,                     namePatterns, null, null, null, null); 

The BulkResponse object contains a collection of objects. You need to get the collection of objects from the BulkResponse object. You need to iterate over the Collection object to get the organization details. To do so, enter the lines of code as shown in Listing 10.3.

Listing 10.3 Iterating Over the Collection to Get the Organization Details
Collection orgs = response.getCollection();             // Display information about the organizations found             Iterator iter = orgs.iterator();             if (!(iter.hasNext())) {          System.out.println("There are no organizations which match the supplied  graphics/ccc.gifpattern");             } else while (iter.hasNext()) {                 Organization orgn = (Organization) iter.next();                 //System.out.println("Organization name: " + getName(orgn));                 System.out.println("Organization name: " + orgn. getName().getValue());                 System.out.println("Organization description: " +                    orgn.getDescription().getValue());                 System.out.println("Organization key id: " + orgn. getKey().getId());                 // Display contact information such as contact person's name, phone  graphics/ccc.gifnumber etc                 User prmrycont = orgn.getPrimaryContact();                 if (prmrycont != null) {                     PersonName prmrycontName = prmrycont.getPersonName();                     System.out.println(" Contact name: " +                         prmrycontName.getFullName());                     Collection phoneNums =                         prmrycont.getTelephoneNumbers(null);                    Iterator phoneiter = phoneNums.iterator();          while (phoneiter.hasNext()) {                         TelephoneNumber number =                             (TelephoneNumber) phoneiter.next();                         System.out.println("  Phone number: " +                             number.getNumber());                     }                     Collection emailadd = prmrycont.getEmailAddresses();                     Iterator emailiter = emailadd.iterator();                     while (emailiter.hasNext()) {                         EmailAddress eAd =                             (EmailAddress) emailiter.next();                         System.out.println("  Email Address: " +                             eAd.getAddress());                     }                 }                 // Display service and binding information                 Collection services = orgn.getServices();         Iterator srvciter = services.iterator();                 while (srvciter.hasNext()) {                     Service srvc = (Service) srvciter.next();                     System.out.println(" Service name: " +                         srvc.getName().getValue());                     System.out.println(" Service description: " +                         srvc.getDescription().getValue());                     Collection serviceBindings =                         srvc.getServiceBindings();                     Iterator srvcbndgiter = serviceBindings.iterator();                     while (srvcbndgiter.hasNext()) {                         ServiceBinding sb =                   (ServiceBinding) srvcbndgiter.next();                         System.out.println("  Binding " +                             "Description: " +                             sb.getDescription().getValue());                         System.out.println("  Access URI: " +                             sb.getAccessURI());                     }                 }                 // Print spacer between organizations                 System.out.println(" --- ");             }         } 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 in this section is available in the MyJAXRExample folder.


You can now compile and run the MyRegistryQuery application. To compile the application, enter 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\ graphics/ccc.gifcommon\lib\jaxm-api.jar; d:\jwsdp-1_0\common\lib\jaxm-client.jar;d:\jwsdp-1_0\common\lib\ graphics/ccc.giflog4j.jar; d:\jwsdp-1_0\common\lib\mail.jar;d:\jwsdp-1_0\common\lib\xalan.jar; d:\ graphics/ccc.gifjwsdp-1_0\common\lib\xerces.jar;d:\jwsdp-1_0\common\lib\jaxp-api.jar -d . MyRegistryQuery.java 

NOTE

To build the client, 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 MyRegistryQuery.java 

Currently the registry server has data for only one company: The Great Car Parts Company. So, we will run the application and search for companies that have the string Car in their names.

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.jar; graphics/ccc.gifd:\jwsdp-1_0\common\lib\castor-0.9.3.9-xml.jar; MyRegistryQuery Car 

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:

runMyRegistryQuery MyORegistryQuery Car 

The output should be similar to the following:

Query string is Car Connection created to the registry Got registry service and query manager Organization name: The Great Car Parts Company Organization description: Suppliers of high quality car accessories Organization key id: eeb543f8-5dee-b543-8407-30aeb62c40bd  Contact name: Jack   Phone number: (800) 555-1111   Email Address: jack@greatcarpartcompany.com  Service name: CarPartRequest  Service description: Order Car Parts   Binding Description: Binding for CarPartRequest service   Access URI: http://greatcarpartcompany.com:8080/CarPartRequest/  --- 

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/ch10lev1sec3]

 
 


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