FileDocCategorySizeDatePackage
Classify.javaAPI DocExample10740Mon Dec 02 08:42:44 GMT 2002ora.jwsnut.chapter7.jaxr

Classify

public class Classify extends Object

Fields Summary
private static javax.xml.registry.BusinessQueryManager
bqm
private static javax.xml.registry.BusinessLifeCycleManager
blcm
Constructors Summary
Methods Summary
private static voiddoExternalClassificationSearch()

        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 external classification
        Classification bookPublishers = blcm.createClassification(naics, "Book Publishers", "51113");
        
        // Perform the search
        ArrayList classifications = new ArrayList();
        classifications.add(bookPublishers);
        BulkResponse res = bqm.findOrganizations(null, null, classifications,
                                                 null, null, null);
        
        // Process results (if any)
        Collection coll = res.getCollection();
        System.out.println("External 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);
            }
        }
    
private static voiddoInternalClassificationSearch1()

        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 getDescendentConcepts();
        Concept publisherConcept = null;
        Collection concepts = naics.getDescendantConcepts();
        Iterator conceptIter = concepts.iterator();
        while (conceptIter.hasNext()) {
            Concept concept = (Concept)conceptIter.next();
            String value = concept.getValue();
            if (value != null && value.equals("51113")) {
                publisherConcept = concept;
                break;
            }
        }
        
        if (publisherConcept == null) {
            System.out.println("ERROR: Book Publisher concept not found");
        } else {
            Classification bookPublishers = blcm.createClassification(publisherConcept);

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

            // Process results (if any)
            Collection coll = res.getCollection();
            System.out.println("Internal classification search #1 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);
                }
            }
        }
    
private static voiddoInternalClassificationSearch2()

        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()
        String path = "/" + naics.getKey().getId() + "/51/511/5111/51113";
        Concept publisherConcept = bqm.findConceptByPath(path); 
        Classification bookPublishers = blcm.createClassification(publisherConcept);
        
        // Perform the search
        ArrayList classifications = new ArrayList();
        classifications.add(bookPublishers);
        BulkResponse res = bqm.findOrganizations(null, null, classifications,
                                                 null, null, null);
        
        // Process results (if any)
        Collection coll = res.getCollection();
        System.out.println("Internal classification search #2 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 voidmain(java.lang.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
            doExternalClassificationSearch();
            doInternalClassificationSearch1();
            doInternalClassificationSearch2();
            
            // Close connection
            conn.close();           
            
        } catch (Throwable t) {
            System.out.println(t);
            t.printStackTrace(System.out);
            System.exit(1);
        }    
        
        System.exit(0);
    
public static voidusage()

        System.out.println("Usage:\tClassify -uddi username password queryURL updateURL");
        System.out.println("Usage:\tClassify -ebxml keystore alias storepwd keypwd queryURL updateURL");        
        System.exit(1);