package com.sun.eportal; /* * * 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.*; import java.io.*; /** * 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"; String queryURL = "http://localhost:8080/registry-server/RegistryServerServlet"; // String queryURL = "http://uddi.rte.microsoft.com/inquire"; /* String httpProxyHost = "YOUR_HTTP_PROXYHOST"; String httpProxyPort = "YOUR_HTTP_PROXYPORT"; String httpsProxyHost = "YOUR_HTTPS_PROXYHOST"; String httpsProxyPort = "YOUR_HTTPS_PROXYPORT"; */ String httpProxyHost = ""; String httpProxyPort = ""; String httpsProxyHost = ""; String httpsProxyPort = ""; String classificationScheme="ntis-gov:naics"; String keyName ="Book Stores"; String keyValue = "451211"; public void setClassificationScheme( String cs ) { this.classificationScheme = cs; } public String getClassificationScheme( ) { return classificationScheme ; } public void setKeyName( String kn ) { this.keyName = kn; } public void setKeyValue( String kv ) { this.keyValue = kv; } public String getKeyName( ) { return keyName ; } public String getKeyValue( ) { return keyValue ; } public String getQueryURL ( ) { return queryURL; } public void setQueryURL ( String qURL ) { queryURL = qURL; } public ClientQuery(String queryURL) { this.queryURL = queryURL; } public ClientQuery( String cscheme, String keyName, String keyValue) { this.classificationScheme = cscheme; this.keyName = keyName; this.keyValue = keyValue; } 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) { try { ClientQuery clientQuery; System.out.println("args.length -> " + args.length ); for ( int i=0; i " + args[i] ); } if ( args.length ==0 ) { clientQuery = new ClientQuery( ); } else if ( args.length==1 ) { clientQuery = new ClientQuery( args[0]); } else if ( args.length==3 ) { clientQuery = new ClientQuery ( args[0], args[1], args[2] ); } else if ( args.length == 4 ) { clientQuery = new ClientQuery ( args[0], args[1] + " " + args[2], args[3] ); } 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" ); Vector results = clientQuery.query(clientQuery.getClassificationScheme(), clientQuery.getKeyName(), clientQuery.getKeyValue()); //clientQuery.query("ntis-gov:naics", "Book Stores", "451211" ); //clientQuery.query("ntis-gov:naics", "Pension Funds", "52511" ); //Now store information in a file named query-results.xml FileOutputStream fout = new FileOutputStream( "query-results.xml"); // fout.write ( " \n".getBytes() ); fout.write(" \n".getBytes() ); fout.write ( "\n".getBytes() ); for ( int i=0; i\n".getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( spi.getProviderName().getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( spi.getProviderDescription().getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( spi.getServiceName().getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( spi.getServiceDescription().getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( spi.getEndpoint().getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( spi.getCommunicationType().getBytes() ); fout.write ( "\n".getBytes() ); fout.write ( "\n".getBytes() ); } fout.write ( "\n".getBytes() ); fout.close(); System.out.println("Now closing connection"); //Close connection etc clientQuery.cleanup(); } catch ( Exception e ) { System.out.println("Exception in ClientQuery main : " + e ); } } /** 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 ) { System.err.println("Connection already exist : Using that -> " + connection ); return connection; } try { // Create a Connection to the registry // after setting standard JAXR properties such as // javax.xml.registry.queryManagerURL, ConnectionFactory factory = ConnectionFactory.newInstance(); System.err.println("Connection Factory => " + factory ); // 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"); props.setProperty("javax.xml.registry.http.proxyHost", httpProxyHost ); props.setProperty("javax.xml.registry.http.proxyPort", httpProxyPort ); props.setProperty("javax.xml.registry.https.proxyHost", httpProxyHost ); props.setProperty("javax.xml.registry.https.proxyPort", httpProxyPort ); factory.setProperties(props); System.out.println("Trying to Create connection to registry"); Properties myProps = factory.getProperties( ); for ( Enumeration e = myProps.propertyNames(); e.hasMoreElements(); ) { String key= (String) e.nextElement(); String value = (String)myProps.get(key); System.out.println("\t" + key + ":" + value ); } connection = factory.createConnection(); System.out.println("connection value : " + connection ); 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) { System.err.println("Exception in getConnection : " + 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 Vector query( String cName, String keyName, String keyValue) { Vector queryResultVector = new Vector(); try { System.out.println("Query URl -> " + queryURL ); if ( getConnection( ) == null ) { System.err.println("ERROR: Problem in getting connection to Registry"); return null; } // Define find qualifiers and name patterns Collection findQualifiers = new ArrayList(); findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC); ClassificationScheme classificationScheme = queryManager.findClassificationSchemeByName(findQualifiers, cName ); Classification classification = lifeCycleManager.createClassification( classificationScheme, keyName, keyValue ); Collection classifications = new ArrayList(); classifications.add ( classification ); /* Collection elinks = new ArrayList(); ExternalLink elink = lifeCycleManager.createExternalLink( "http://www.verticalorganization.com/books/books.wsdl" , "WSDL document describing the book ordering service interface" ); elinks.add( elink ); */ // 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()) { //Now create ServiceProviderInfo for each organization Organization org = (Organization) orgIter.next(); String orgName = getName( org ); String orgDescription = getDescription( org ); System.out.println("Org name: " + orgName); System.out.println("Org description: " + orgDescription ); System.out.println("Org key id: " + getKey(org)); // Display service and binding information Collection services = org.getServices(); Iterator svcIter = services.iterator(); while (svcIter.hasNext()) { Service svc = (Service) svcIter.next(); String serviceKey = svc.getKey().getId(); System.out.println("Service Key => " + serviceKey ); // svc = (Service)queryManager.getRegistryObject( serviceKey); String serviceName = getName(svc); String serviceDescription = getDescription(svc); System.out.println(" Service name: " + serviceName ); System.out.println(" Service description: " + serviceDescription); Collection serviceBindings = svc.getServiceBindings(); Iterator sbIter = serviceBindings.iterator(); int count=0; // Working around the bug in registry server: // This got introduced because of some other bug fix //while (sbIter.hasNext()) { if (sbIter.hasNext()) { count++; ServiceBinding sb = (ServiceBinding) sbIter.next(); String bindingDescription = getDescription (sb ); String accessURI = sb.getAccessURI( ); System.out.println(" Binding " + "Description: " + bindingDescription ); System.out.println(" Access URI: " + accessURI ); //Now controlling by whether binding description contains JAXRPC or JAXM if ( ( bindingDescription.indexOf( "JAXRPC-FCS" ) >= 0 ) || ( bindingDescription.indexOf("JAXM-FCS") >= 0 ) ) { System.out.println("Hurray !! Our Own Service Endpoint Found. Now populate our list" ); ServiceProviderInfo spi = new ServiceProviderInfo ( orgName, orgDescription ); spi.setServiceName ( serviceName ); spi.setServiceDescription ( serviceDescription ); spi.setEndpoint ( accessURI ); if ( bindingDescription.indexOf("JAXRPC" ) >= 0 ) { spi.setCommunicationType("JAXRPC" ); } else { spi.setCommunicationType("JAXM" ); } queryResultVector.addElement( spi ); System.out.println("queryyyyyyyyyyyy"+queryResultVector.size()); } else { System.out.println("Warning : This is not a conforming Service Provider" ); } Collection specLinks = sb.getSpecificationLinks(); Iterator specLinkIterator = specLinks.iterator(); while ( specLinkIterator.hasNext() ) { SpecificationLink sl = (SpecificationLink)specLinkIterator.next(); System.err.println("Specification link key -> " + sl.getKey() ); RegistryObject ro = sl.getSpecificationObject(); System.out.println("Specification Object description :" + getDescription( ro) ); Collection elc = ro.getExternalLinks(); System.err.println("External Links collection : " + elc ); Iterator eli = elc.iterator(); while( eli.hasNext() ) { ExternalLink el = (ExternalLink)eli.next(); System.out.println("External URI : " + el.getExternalURI( ) ); } } } System.out.println(" Service Binding Count -> " + count ); } } } catch (Exception e) { e.printStackTrace(); } finally { return queryResultVector; } } /** * 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 ""; } } }