/* * * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * @author: Ramesh Mandava and Edward Ort */ import javax.xml.registry.*; import javax.xml.registry.infomodel.*; import java.net.*; import java.util.*; /** * The ClientQuery uses the JAXR API class to query * organizations based on classification information * */ public class ClientQuery { Connection connection = null; RegistryService rs = null; BusinessQueryManager queryManager = null; BusinessLifeCycleManager lifeCycleManager = null; String queryURL = "http://www-3.ibm.com/services/uddi/testregistry/inquiryapi"; public String getQueryURL ( ) { return queryURL; } public ClientQuery(String queryURL) { this.queryURL = queryURL; } public ClientQuery( ) { System.out.println("Using default values\n"); System.out.println("Query URL : " + queryURL ); } public static void usage( ) { System.out.println("Usage:\n java ClientQuery \n OR \n"); System.out.println("java ClientQuery"); } /** main method which will display the capability of JAXR API */ public static void main(String[] args) { ClientQuery clientQuery; if ( args.length ==0 ) { clientQuery = new ClientQuery( ); } else if ( args.length==1 ) { clientQuery = new ClientQuery( args[0]); } else { usage(); return; } // Gets connection to the registry. If it returns a non-null value, // assumes that Connection, Registry Service, Business Query // Manager instances are available in the appropriate variables if ( clientQuery.getConnection()== null ) { System.out.println("Problems while getting the connection to Registry"); return; } System.out.println("Querying based on Classification Book Stores" ); clientQuery.query("ntis-gov:naics", "Book Stores", "451211" ); //Close connection etc clientQuery.cleanup(); } /** Cleanup : freeing resources */ public void cleanup( ) { if ( connection != null ) { try { connection.close(); } catch ( JAXRException je ) { System.err.println("Error while closing Connection: " + je ); } } } /** * Establishes a connection to a registry. * */ public Connection getConnection() { /** If connection is already made to registry, use that connection only */ if ( connection != null ) { return connection; } try { // Create a Connection to the registry // after setting standard JAXR properties such as // javax.xml.registry.queryManagerURL, ConnectionFactory factory = ConnectionFactory.newInstance(); // Define connection configuration properties Properties props = new Properties(); props.setProperty("javax.xml.registry.queryManagerURL", queryURL); props.setProperty("javax.xml.registry.factoryClass", "com.sun.xml.registry.uddi.ConnectionFactoryImpl"); factory.setProperties(props); System.out.println("Created connection to registry"); connection = factory.createConnection(); rs = connection.getRegistryService(); System.out.println("Getting Business Query manager"); queryManager = rs.getBusinessQueryManager(); System.out.println("Getting Business Lifecycle manager"); lifeCycleManager = rs.getBusinessLifeCycleManager(); return connection; } catch (Exception e) { e.printStackTrace(); if (connection != null) { try { connection.close(); } catch (JAXRException je) {} } return null; } } /** * Searches for organizations with the passed classification scheme name , key name, and key value * Displays data about them. * * @param cName the Classification Scheme * @param keyName the Key Name for the Classification * @param keyValue the Key Value for the Classification */ public void query( String cName, String keyName, String keyValue) { try { System.out.println("Query URl -> " + queryURL ); getConnection( ); // Define find qualifiers and name patterns Collection findQualifiers = new ArrayList(); findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC); ClassificationScheme classificationScheme = queryManager.findClassificationSchemeByName( cName ); Classification classification = lifeCycleManager.createClassification( classificationScheme, keyName, keyValue ); Collection classifications = new ArrayList(); classifications.add ( classification ); // Find using the Classification Info BulkResponse response = queryManager.findOrganizations(findQualifiers, null, classifications, null, null, null); Collection orgs = response.getCollection(); // Display information about the organizations found Iterator orgIter = orgs.iterator(); while (orgIter.hasNext()) { Organization org = (Organization) orgIter.next(); System.out.println("Org name: " + getName(org)); System.out.println("Org description: " + getDescription(org)); System.out.println("Org key id: " + getKey(org)); // Display primary contact information User pc = org.getPrimaryContact(); if (pc != null) { PersonName pcName = pc.getPersonName(); System.out.println(" Contact name: " + pcName.getFullName()); Collection phNums = pc.getTelephoneNumbers(pc.getType()); Iterator phIter = phNums.iterator(); while (phIter.hasNext()) { TelephoneNumber num = (TelephoneNumber) phIter.next(); System.out.println(" Phone number: " + num.getNumber()); } Collection eAddrs = pc.getEmailAddresses(); Iterator eaIter = eAddrs.iterator(); while (phIter.hasNext()) { System.out.println(" Email Address: " + (EmailAddress) eaIter.next()); } } // Display service and binding information Collection services = org.getServices(); Iterator svcIter = services.iterator(); while (svcIter.hasNext()) { Service svc = (Service) svcIter.next(); System.out.println(" Service name: " + getName(svc)); System.out.println(" Service description: " + getDescription(svc)); Collection serviceBindings = svc.getServiceBindings(); Iterator sbIter = serviceBindings.iterator(); while (sbIter.hasNext()) { ServiceBinding sb = (ServiceBinding) sbIter.next(); System.out.println(" Binding " + "Description: " + getDescription(sb)); System.out.println(" Access URI: " + sb.getAccessURI()); Collection specLinks = sb.getSpecificationLinks(); Iterator specLinkIterator = specLinks.iterator(); while ( specLinkIterator.hasNext() ) { SpecificationLink sl = (SpecificationLink)specLinkIterator.next(); RegistryObject ro = sl.getSpecificationObject(); System.out.println("Specification Object description :" + getDescription( ro) ); Collection elc = sl.getExternalLinks(); Iterator eli = elc.iterator(); while( eli.hasNext() ) { ExternalLink el = (ExternalLink)eli.next(); System.out.println("External URI : " + el.getExternalURI( ) ); } } } } // prints separators between organizations System.out.println(" ===== "); } } catch (Exception e) { e.printStackTrace(); } } /** * Returns the name value for a registry object. Returns * an empty string if the value is null. * * @param ro a RegistryObject * @return the String value */ private String getName(RegistryObject ro) throws JAXRException { try { return ro.getName().getValue(); } catch (NullPointerException npe) { return ""; } } /** * Returns the description value for a registry object. * Returns an empty string if the value is null. * * @param ro a RegistryObject * @return the String value */ private String getDescription(RegistryObject ro) throws JAXRException { try { return ro.getDescription().getValue(); } catch (NullPointerException npe) { return ""; } } /** * Returns the key id value for a registry object. * Returns an empty string if the value is null. * * @param ro a RegistryObject * @return the String value */ private String getKey(RegistryObject ro) throws JAXRException { try { return ro.getKey().getId(); } catch (NullPointerException npe) { return ""; } } }