FileDocCategorySizeDatePackage
ExtendedClassify.javaAPI DocExample7284Mon Dec 02 08:42:16 GMT 2002ora.jwsnut.chapter7.jaxr

ExtendedClassify.java

package ora.jwsnut.chapter7.jaxr;

import java.net.PasswordAuthentication;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import javax.security.auth.x500.X500PrivateCredential;
import javax.xml.registry.BulkResponse;
import javax.xml.registry.BusinessLifeCycleManager;
import javax.xml.registry.BusinessQueryManager;
import javax.xml.registry.Connection;
import javax.xml.registry.ConnectionFactory;
import javax.xml.registry.FindQualifier;
import javax.xml.registry.JAXRException;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.Classification;
import javax.xml.registry.infomodel.ClassificationScheme;
import javax.xml.registry.infomodel.Concept;
import javax.xml.registry.infomodel.Organization;
import ora.jwsnut.chapter7.util.Utils;

public class ExtendedClassify {

    // The BusinessQueryManager
    private static BusinessQueryManager bqm;
    
    // The BusinessLifeCycleManager
    private static BusinessLifeCycleManager blcm;
    
    public static void main(String[] args) {
        
        boolean install = false;
        boolean isUddi = false;
        HashSet credentials = new HashSet();
        String authMethod = null;
        
        // Validate arguments
        if (args.length == 0) {
            usage();
        }
        String registryType = args[0];
        int index = -1;
     
        if (registryType.equals("-uddi")) {
            if (args.length != 5) {
                usage();
            }
            
            // UDDI registry uses username/password for authentication
            isUddi = true;
            index = 3;
            authMethod = "UDDI_GET_AUTHTOKEN";
            String user = args[1];
            String password = args[2];
            PasswordAuthentication auth = 
                        new PasswordAuthentication(user, password.toCharArray());
            credentials.add(auth);            
        } else if (registryType.equals("-ebxml")) {
            if (args.length != 7) {
                usage();
            }
            
            // ebXML registry requires a certificate
            index = 5;
            authMethod = "CLIENT_CERTIFICATE";
            String keyStoreName = args[1];
            String keyStoreAlias = args[2];
            String keyStorePwd = args[3];
		String keyPwd = args[4];
            try {
                X500PrivateCredential cred = Utils.getCredentials(keyStoreName, keyStoreAlias, 
                                                                  keyPwd, keyStorePwd);
                credentials.add(cred);
            } catch (Throwable t) {
                System.out.println("Failed when getting certificate");
                t.printStackTrace(System.out);
                System.exit(1);
            }
        } else {
            usage();
        }
        
        // Process the common arguments
        String queryURL = args[index];
        String lifecycleURL = args[index + 1];
        
        try {
            // Get the ConnectionFactory
            Properties props = new Properties();
            props.put("javax.xml.registry.queryManagerURL", queryURL);
            props.put("javax.xml.registry.lifeCycleManagerURL", lifecycleURL);
            props.put("javax.xml.registry.security.authenticationMethod", authMethod);
            
            ConnectionFactory cf = ConnectionFactory.newInstance();
            cf.setProperties(props);
            
            // Get and initialize the connection
            Connection conn = cf.createConnection();
            conn.setCredentials(credentials);
            conn.setSynchronous(true);
            
            // Get the RegistryService and the managers
            RegistryService registry = conn.getRegistryService();
            bqm = registry.getBusinessQueryManager();            
            blcm = registry.getBusinessLifeCycleManager();

            // Run the example code
            doExtendedClassificationSearch();
            
            // Close connection
            conn.close();           
            
        } catch (Throwable t) {
            System.out.println(t);
            t.printStackTrace(System.out);
            System.exit(1);
        }    
        
        System.exit(0);
    }
   
    private static void doExtendedClassificationSearch() throws JAXRException {
        ClassificationScheme naics = bqm.findClassificationSchemeByName(null, "%naics%");
        if (naics == null) {
            naics = bqm.findClassificationSchemeByName(null, "%NAICS%");
            if (naics == null) {
                System.out.println("COULD NOT FIND NAICS CLASSIFICATION SCHEME.");
                System.exit(1);
            }
        }
        
        // Create internal classification using findConceptByPath()
        ArrayList classifications = new ArrayList();
        String path = "/" + naics.getKey().getId() + "/51/511/5111";
        Concept publisherConcept = bqm.findConceptByPath(path); 
        System.out.println("Base classification: " + publisherConcept.getName().getValue());
        Classification bookPublishers = blcm.createClassification(publisherConcept);
        classifications.add(bookPublishers);
        
        // Find concepts for all dependents and include the
        // corresponding classifications
        Collection concepts = publisherConcept.getDescendantConcepts();
        Iterator conceptIter = concepts.iterator();
        while (conceptIter.hasNext()) {
            Concept concept = (Concept)conceptIter.next();
            System.out.println("Adding child: " + concept.getName().getValue());
            classifications.add(blcm.createClassification(concept));
        }

        // Perform the search
        ArrayList findQualifiers = new ArrayList();
        findQualifiers.add(FindQualifier.OR_LIKE_KEYS);
        BulkResponse res = bqm.findOrganizations(findQualifiers, null, classifications,
                                                 null, null, null);

        // Process results (if any)
        Collection coll = res.getCollection();
        System.out.println("Extended classification search found " + coll.size());
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Organization org = (Organization)iter.next();
            System.out.println("\t" + org.getName().getValue());
        }

        // Process exceptions (if any)
        Collection exceptions = res.getExceptions();
        if (exceptions != null && !exceptions.isEmpty()){
            System.out.println("Error while searching");
            iter = exceptions.iterator();
            while (iter.hasNext()) {
                ((Exception)iter.next()).printStackTrace(System.out);
            }
        }
    }    
    
    public static void usage() {
        System.out.println("Usage:\tExtendedClassify -uddi username password queryURL updateURL");
        System.out.println("Usage:\tExtendedClassify -ebxml keystore alias storepwd keypwd queryURL updateURL");        
        System.exit(1);
    }
}