Creating Applications That Register Supplier Information to the Registry Since there are four suppliers, you will create the following four applications: -
BestDealsonAccessoriesRegistryPublisher.java This application registers the information about The Best Deals on Accessories Inc. into the JWSDP Registry Server. -
CoolAccessoriesRegistryPublisher.java This application registers the information about Cool Accessories Inc. into the JWSDP Registry Server. -
TheGreatPartsSupplierRegistryPublisher.java This application registers the information about The Great Parts Supplier Inc. into the JWSDP Registry Server. -
WeSupplyPartsRegistryPublisher.java This application registers the information about We Supply Parts Inc. into the JWSDP Registry Server. These applications are similar to each other. The only difference between them is the actual data that they register with the registry. Writing the BestDealsonAccessoriesRegistryPublisher.java Application The code for BestDealsonAccessoriesRegistryPublisher.java uses the interfaces and classes described in the JAXR specification to publish information about itself in the registry. The important thing to notice is the access URI for the Web service. The servlet will be accessing this URI for executing the Web service. NOTE The code presented here is very similar to the MyOrganizationPublisher.java application that you developed in Chapter 11, "Working with JAX-RPC." To understand the code presented in BestDealsonAccessoriesRegistryPublisher.java in greater detail, refer to the description of the MyOrganizationPublisher.java application. To create the BestDealsonAccessoriesRegistryPublisher.java application, open a text editor of your choice and save an empty text file by that name. Then enter the lines of code shown in Listing 12.1. Listing 12.1 Code for BestDealsonAccessoriesRegistryPublisher.javaimport javax.xml.registry.*; import javax.xml.registry.infomodel.*; import java.net.*; import java.security.*; import java.util.*; public class BestDealsonAccessoriesRegistryPublisher { Connection connection = null; public BestDealsonAccessoriesRegistryPublisher () {} public static void main(String[] args) { BestDealsonAccessoriesRegistryPublisher bdarp = new BestDealsonAccessoriesRegistryPublisher (); bdarp.connect(); bdarp.publishdata(); } public void connect() { String queryURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String publishURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String httpProxyHost = ""; String httpProxyPort = "8080"; String httpsProxyHost = ""; String httpsProxyPort = "8080"; Properties props = new Properties(); props.setProperty("javax.xml.registry.queryManagerURL", queryURL); props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishURL); props.setProperty("com.sun.xml.registry.http.proxyHost", httpProxyHost); props.setProperty("com.sun.xml.registry.http.proxyPort", httpProxyPort); props.setProperty("com.sun.xml.registry.https.proxyHost", httpsProxyHost); props.setProperty("com.sun.xml.registry.https.proxyPort", httpsProxyPort); try { ConnectionFactory factory = ConnectionFactory.newInstance(); factory.setProperties(props); connection = factory.createConnection(); System.out.println("Created connection to registry"); } catch (Exception e) { e.printStackTrace(); } } public void publishdata() { String username = "testuser"; String password = "testuser"; RegistryService rsrvc = null; BusinessLifeCycleManager bsnslfcmgr = null; BusinessQueryManager bqrymgr = null; try { rsrvc = connection.getRegistryService(); bsnslfcmgr = rsrvc.getBusinessLifeCycleManager(); bqrymgr = rsrvc.getBusinessQueryManager(); System.out.println("Got registry service, query " + "manager, and life cycle manager"); // Get authorization from the registry PasswordAuthentication passwdAuth = new PasswordAuthentication(username, password.toCharArray()); Set creds = new HashSet(); creds.add(passwdAuth); connection.setCredentials(creds); System.out.println("Security credentials established"); // Create organization, specify name and description Organization orgn = bsnslfcmgr.createOrganization("Best Deals on Accessories Inc."); InternationalString descn = bsnslfcmgr.createInternationalString ("Suppliers of high quality car accessories"); orgn.setDescription(descn); System.out.println("Description about the organization added"); // Enter the Contact Person Name User primaryContact = bsnslfcmgr.createUser(); PersonName pName = bsnslfcmgr.createPersonName("Stephen"); primaryContact.setPersonName(pName); // Enter primary contact phone number TelephoneNumber telNo = bsnslfcmgr.createTelephoneNumber(); telNo.setNumber("(800) 555-55555"); Collection phoneNums = new ArrayList(); phoneNums.add(telNo); primaryContact.setTelephoneNumbers(phoneNums); // Enter primary contact email address EmailAddress emailAdd = bsnslfcmgr.createEmailAddress ("stephen@bestdealsonaccessories.com"); Collection emailAddresses = new ArrayList(); emailAddresses.add(emailAdd); primaryContact.setEmailAddresses(emailAddresses); // Set primary contact for organization orgn.setPrimaryContact(primaryContact); System.out.println("Primary contact details added"); // Set classification scheme to NAICS ClassificationScheme cScheme = bqrymgr.findClassificationSchemeByName (null, "ntis-gov:naics"); // Create and add classification Classification classification = (Classification) bsnslfcmgr.createClassification(cScheme, "Car Parts and Accessories", "123456"); Collection classifications = new ArrayList(); classifications.add(classification); orgn.addClassifications(classifications); System.out.println("Classification added"); // Create services and service Collection services = new ArrayList(); Service service = bsnslfcmgr.createService("CarPartRequest"); InternationalString desc = bsnslfcmgr.createInternationalString("Order Car Parts"); service.setDescription(desc); // Create service bindings Collection serviceBindings = new ArrayList(); ServiceBinding binding = bsnslfcmgr.createServiceBinding(); descn = bsnslfcmgr.createInternationalString ("Binding for CarPartRequest service"); binding.setDescription(descn); // Turn off address validation binding.setValidateURI(false); binding.setAccessURI ("http://localhost:8080/BestDealsonAccessories/jaxrpc/ CarPartRequestIF"); serviceBindings.add(binding); // Add service bindings to service service.addServiceBindings(serviceBindings); // Add service to services, then add services to organization services.add(service); orgn.addServices(services); System.out.println("Services added"); // Add organization and submit to registry // Retrieve key if successful Collection orgs = new ArrayList(); orgs.add(orgn); //Iterator for keys or exception Iterator itr; BulkResponse response = bsnslfcmgr.saveOrganizations(orgs); Collection exceptions = response.getExceptions(); if (exceptions == null) { System.out.println("Organization saved"); Collection keys = response.getCollection(); itr = keys.iterator(); if (itr.hasNext()) { javax.xml.registry.infomodel.Key orgKey = (javax.xml.registry.infomodel.Key) itr.next(); String id = orgKey.getId(); System.out.println("Organization key is " + id); } } else { itr = exceptions.iterator(); Exception exception = null; while (itr.hasNext()) { exception = (Exception) itr.next(); System.err.println("Failed to save because: " + exception.toString()); } } } catch (Exception e) { e.printStackTrace(); } finally { // At end, close connection to registry if (connection != null) { try { connection.close(); } catch (JAXRException je) {} } } } } Similar to the BestDealsonAccessoriesRegistryPublisher.java application, create the other three applications by entering the code in Listings 12.2, 12.3, and 12.4. Listing 12.2 Code for CoolAccessoriesRegistryPublisher.javaimport javax.xml.registry.*; import javax.xml.registry.infomodel.*; import java.net.*; import java.security.*; import java.util.*; public class CoolAccessoriesRegistryPublisher { Connection connection = null; public CoolAccessoriesRegistryPublisher() {} public static void main(String[] args) { CoolAccessoriesRegistryPublisher carp = new CoolAccessoriesRegistryPublisher(); carp.connect(); carp.publishdata(); } public void connect() { String queryURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String publishURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String httpProxyHost = ""; String httpProxyPort = "8080"; String httpsProxyHost = ""; String httpsProxyPort = "8080"; Properties props = new Properties(); props.setProperty("javax.xml.registry.queryManagerURL", queryURL); props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishURL); props.setProperty("com.sun.xml.registry.http.proxyHost", httpProxyHost); props.setProperty("com.sun.xml.registry.http.proxyPort", httpProxyPort); props.setProperty("com.sun.xml.registry.https.proxyHost", httpsProxyHost); props.setProperty("com.sun.xml.registry.https.proxyPort", httpsProxyPort); try { ConnectionFactory factory = ConnectionFactory.newInstance(); factory.setProperties(props); connection = factory.createConnection(); System.out.println("Created connection to registry"); } catch (Exception e) { e.printStackTrace(); } } public void publishdata() { String username = "testuser"; String password = "testuser"; RegistryService rsrvc = null; BusinessLifeCycleManager bsnslfcmgr = null; BusinessQueryManager bqrymgr = null; try { rsrvc = connection.getRegistryService(); bsnslfcmgr = rsrvc.getBusinessLifeCycleManager(); bqrymgr = rsrvc.getBusinessQueryManager(); System.out.println("Got registry service, query " + "manager, and life cycle manager"); // Get authorization from the registry PasswordAuthentication passwdAuth = new PasswordAuthentication(username, password.toCharArray()); Set creds = new HashSet(); creds.add(passwdAuth); connection.setCredentials(creds); System.out.println("Security credentials established"); // Create organization, specify name and description Organization orgn = bsnslfcmgr.createOrganization("Cool Accessories Inc."); InternationalString descn = bsnslfcmgr.createInternationalString ("Suppliers of high quality car accessories"); orgn.setDescription(descn); System.out.println("Description about the organization added"); // Enter the Contact Person Name User primaryContact = bsnslfcmgr.createUser(); PersonName pName = bsnslfcmgr.createPersonName("James"); primaryContact.setPersonName(pName); // Enter primary contact phone number TelephoneNumber telNo = bsnslfcmgr.createTelephoneNumber(); telNo.setNumber("(800) 555-4444"); Collection phoneNums = new ArrayList(); phoneNums.add(telNo); primaryContact.setTelephoneNumbers(phoneNums); // Enter primary contact email address EmailAddress emailAdd = bsnslfcmgr.createEmailAddress("james@coolaccessories.com"); Collection emailAddresses = new ArrayList(); emailAddresses.add(emailAdd); primaryContact.setEmailAddresses(emailAddresses); // Set primary contact for organization orgn.setPrimaryContact(primaryContact); System.out.println("Primary contact details added"); // Set classification scheme to NAICS ClassificationScheme cScheme = bqrymgr.findClassificationSchemeByName(null, "ntis-gov:naics"); // Create and add classification Classification classification = (Classification) bsnslfcmgr.createClassification(cScheme, "Car Parts and Accessories", "123456"); Collection classifications = new ArrayList(); classifications.add(classification); orgn.addClassifications(classifications); System.out.println("Classification added"); // Create services and service Collection services = new ArrayList(); Service service = bsnslfcmgr.createService("CarPartRequest"); InternationalString desc = bsnslfcmgr.createInternationalString("Order Car Parts"); service.setDescription(desc); // Create service bindings Collection serviceBindings = new ArrayList(); ServiceBinding binding = bsnslfcmgr.createServiceBinding(); descn = bsnslfcmgr.createInternationalString ("Binding for CarPartRequest service"); binding.setDescription(descn); // Turn off address validation binding.setValidateURI(false); binding.setAccessURI ("http://localhost:8080/coolaccessories/jaxrpc/"); serviceBindings.add(binding); // Add service bindings to service service.addServiceBindings(serviceBindings); // Add service to services, then add services to organization services.add(service); orgn.addServices(services); System.out.println("Services added"); // Add organization and submit to registry // Retrieve key if successful Collection orgs = new ArrayList(); orgs.add(orgn); //Iterator for keys or exception Iterator itr; BulkResponse response = bsnslfcmgr.saveOrganizations(orgs); Collection exceptions = response.getExceptions(); if (exceptions == null) { System.out.println("Organization saved"); Collection keys = response.getCollection(); itr = keys.iterator(); if (itr.hasNext()) { javax.xml.registry.infomodel.Key orgKey = (javax.xml.registry.infomodel.Key) itr.next(); String id = orgKey.getId(); System.out.println("Organization key is " + id); } } else { itr = exceptions.iterator(); Exception exception = null; while (itr.hasNext()) { exception = (Exception) itr.next(); System.err.println("Failed to save because: " + exception.toString()); } } } catch (Exception e) { e.printStackTrace(); } finally { // At end, close connection to registry if (connection != null) { try { connection.close(); } catch (JAXRException je) {} } } } } Listing 12.3 Code for TheGreatPartsSupplierRegistryPublisher.javaimport javax.xml.registry.*; import javax.xml.registry.infomodel.*; import java.net.*; import java.security.*; import java.util.*; public class TheGreatPartsSupplierRegistryPublisher { Connection connection = null; public TheGreatPartsSupplierRegistryPublisher() {} public static void main(String[] args) { TheGreatPartsSupplierRegistryPublisher gpsrp = new TheGreatPartsSupplierRegistryPublisher (); gpsrp.connect(); gpsrp.publishdata(); } public void connect() { String queryURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String publishURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String httpProxyHost = ""; String httpProxyPort = "8080"; String httpsProxyHost = ""; String httpsProxyPort = "8080"; Properties props = new Properties(); props.setProperty("javax.xml.registry.queryManagerURL", queryURL); props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishURL); props.setProperty("com.sun.xml.registry.http.proxyHost", httpProxyHost); props.setProperty("com.sun.xml.registry.http.proxyPort", httpProxyPort); props.setProperty("com.sun.xml.registry.https.proxyHost", httpsProxyHost); props.setProperty("com.sun.xml.registry.https.proxyPort", httpsProxyPort); try { ConnectionFactory factory = ConnectionFactory.newInstance(); factory.setProperties(props); connection = factory.createConnection(); System.out.println("Created connection to registry"); } catch (Exception e) { e.printStackTrace(); } } public void publishdata() { String username = "testuser"; String password = "testuser"; RegistryService rsrvc = null; BusinessLifeCycleManager bsnslfcmgr = null; BusinessQueryManager bqrymgr = null; try { rsrvc = connection.getRegistryService(); bsnslfcmgr = rsrvc.getBusinessLifeCycleManager(); bqrymgr = rsrvc.getBusinessQueryManager(); System.out.println("Got registry service, query " + "manager, and life cycle manager"); // Get authorization from the registry PasswordAuthentication passwdAuth = new PasswordAuthentication(username, password.toCharArray()); Set creds = new HashSet(); creds.add(passwdAuth); connection.setCredentials(creds); System.out.println("Security credentials established"); // Create organization, specify name and description Organization orgn = bsnslfcmgr.createOrganization("The Great Car Parts Company"); InternationalString descn = bsnslfcmgr.createInternationalString ("Suppliers of high quality car accessories"); orgn.setDescription(descn); System.out.println("Description about the organization added"); // Enter the Contact Person Name User primaryContact = bsnslfcmgr.createUser(); PersonName pName = bsnslfcmgr.createPersonName("David"); primaryContact.setPersonName(pName); // Enter primary contact phone number TelephoneNumber telNo = bsnslfcmgr.createTelephoneNumber(); telNo.setNumber("(800) 555-3333"); Collection phoneNums = new ArrayList(); phoneNums.add(telNo); primaryContact.setTelephoneNumbers(phoneNums); // Enter primary contact email address EmailAddress emailAdd = bsnslfcmgr.createEmailAddress("david@greatpartssupplier.com"); Collection emailAddresses = new ArrayList(); emailAddresses.add(emailAdd); primaryContact.setEmailAddresses(emailAddresses); // Set primary contact for organization orgn.setPrimaryContact(primaryContact); System.out.println("Primary contact details added"); // Set classification scheme to NAICS ClassificationScheme cScheme = bqrymgr.findClassificationSchemeByName(null, "ntis-gov:naics"); // Create and add classification Classification classification = (Classification) bsnslfcmgr.createClassification(cScheme, "Car Parts and Accessories", "123456"); Collection classifications = new ArrayList(); classifications.add(classification); orgn.addClassifications(classifications); System.out.println("Classification added"); // Create services and service Collection services = new ArrayList(); Service service = bsnslfcmgr.createService("CarPartRequest"); InternationalString desc = bsnslfcmgr.createInternationalString("Order Car Parts"); service.setDescription(desc); // Create service bindings Collection serviceBindings = new ArrayList(); ServiceBinding binding = bsnslfcmgr.createServiceBinding(); descn = bsnslfcmgr.createInternationalString ("Binding for CarPartRequest service"); binding.setDescription(descn); // Turn off address validation binding.setValidateURI(false); binding.setAccessURI ("http://localhost:8080/greatcarpartsupplier/jaxrpc/ CarPartrequestIF"); serviceBindings.add(binding); // Add service bindings to service service.addServiceBindings(serviceBindings); // Add service to services, then add services to organization services.add(service); orgn.addServices(services); System.out.println("Services added"); // Add organization and submit to registry // Retrieve key if successful Collection orgs = new ArrayList(); orgs.add(orgn); //Iterator for keys or exception Iterator itr; BulkResponse response = bsnslfcmgr.saveOrganizations(orgs); Collection exceptions = response.getExceptions(); if (exceptions == null) { System.out.println("Organization saved"); Collection keys = response.getCollection(); itr = keys.iterator(); if (itr.hasNext()) { javax.xml.registry.infomodel.Key orgKey = (javax.xml.registry.infomodel.Key) itr.next(); String id = orgKey.getId(); System.out.println("Organization key is " + id); } } else { itr = exceptions.iterator(); Exception exception = null; while (itr.hasNext()) { exception = (Exception) itr.next(); System.err.println("Failed to save because: " + exception.toString()); } } } catch (Exception e) { e.printStackTrace(); } finally { // At end, close connection to registry if (connection != null) { try { connection.close(); } catch (JAXRException je) {} } } } } Listing 12.4 Code for WeSupplyPartsRegistryPublisher.javaimport javax.xml.registry.*; import javax.xml.registry.infomodel.*; import java.net.*; import java.security.*; import java.util.*; public class WeSupplyPartsRegistryPublisher { Connection connection = null; public WeSupplyPartsRegistryPublisher() {} public static void main(String[] args) { WeSupplyPartsRegistryPublisher wsprp = new WeSupplyPartsRegistryPublisher(); wsprp.connect(); wsprp.publishdata(); } public void connect() { String queryURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String publishURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String httpProxyHost = ""; String httpProxyPort = "8080"; String httpsProxyHost = ""; String httpsProxyPort = "8080"; Properties props = new Properties(); props.setProperty("javax.xml.registry.queryManagerURL", queryURL); props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishURL); props.setProperty("com.sun.xml.registry.http.proxyHost", httpProxyHost); props.setProperty("com.sun.xml.registry.http.proxyPort", httpProxyPort); props.setProperty("com.sun.xml.registry.https.proxyHost", httpsProxyHost); props.setProperty("com.sun.xml.registry.https.proxyPort", httpsProxyPort); try { ConnectionFactory factory = ConnectionFactory.newInstance(); factory.setProperties(props); connection = factory.createConnection(); System.out.println("Created connection to registry"); } catch (Exception e) { e.printStackTrace(); } } public void publishdata() { String username = "testuser"; String password = "testuser"; RegistryService rsrvc = null; BusinessLifeCycleManager bsnslfcmgr = null; BusinessQueryManager bqrymgr = null; try { rsrvc = connection.getRegistryService(); bsnslfcmgr = rsrvc.getBusinessLifeCycleManager(); bqrymgr = rsrvc.getBusinessQueryManager(); System.out.println("Got registry service, query " + "manager, and life cycle manager"); // Get authorization from the registry PasswordAuthentication passwdAuth = new PasswordAuthentication(username, password.toCharArray()); Set creds = new HashSet(); creds.add(passwdAuth); connection.setCredentials(creds); System.out.println("Security credentials established"); // Create organization, specify name and description Organization orgn = bsnslfcmgr.createOrganization("We Supply Parts Inc."); InternationalString descn = bsnslfcmgr.createInternationalString ("Suppliers of high quality car accessories"); orgn.setDescription(descn); System.out.println("Description about the organization added"); // Enter the Contact Person Name User primaryContact = bsnslfcmgr.createUser(); PersonName pName = bsnslfcmgr.createPersonName("John"); primaryContact.setPersonName(pName); // Enter primary contact phone number TelephoneNumber telNo = bsnslfcmgr.createTelephoneNumber(); telNo.setNumber("(800) 555-1111"); Collection phoneNums = new ArrayList(); phoneNums.add(telNo); primaryContact.setTelephoneNumbers(phoneNums); // Enter primary contact email address EmailAddress emailAdd = bsnslfcmgr.createEmailAddress("jack@wesupplyparts.com"); Collection emailAddresses = new ArrayList(); emailAddresses.add(emailAdd); primaryContact.setEmailAddresses(emailAddresses); // Set primary contact for organization orgn.setPrimaryContact(primaryContact); System.out.println("Primary contact details added"); // Set classification scheme to NAICS ClassificationScheme cScheme = bqrymgr.findClassificationSchemeByName(null, "ntis-gov:naics"); // Create and add classification Classification classification = (Classification) bsnslfcmgr.createClassification(cScheme, "Car Parts and Accessories", "123456"); Collection classifications = new ArrayList(); classifications.add(classification); orgn.addClassifications(classifications); System.out.println("Classification added"); // Create services and service Collection services = new ArrayList(); Service service = bsnslfcmgr.createService("CarPartRequest"); InternationalString desc = bsnslfcmgr.createInternationalString("Order Car Parts"); service.setDescription(desc); // Create service bindings Collection serviceBindings = new ArrayList(); ServiceBinding binding = bsnslfcmgr.createServiceBinding(); descn = bsnslfcmgr.createInternationalString("Binding for CarPartRequest service"); binding.setDescription(descn); // Turn off address validation binding.setValidateURI(false); binding.setAccessURI("http://localhost:8080/wesupplyparts/jaxrpc/ CarPartRequestIF"); serviceBindings.add(binding); // Add service bindings to service service.addServiceBindings(serviceBindings); // Add service to services, then add services to organization services.add(service); orgn.addServices(services); System.out.println("Services added"); // Add organization and submit to registry // Retrieve key if successful Collection orgs = new ArrayList(); orgs.add(orgn); //Iterator for keys or exception Iterator itr; BulkResponse response = bsnslfcmgr.saveOrganizations(orgs); Collection exceptions = response.getExceptions(); if (exceptions == null) { System.out.println("Organization saved"); Collection keys = response.getCollection(); itr = keys.iterator(); if (itr.hasNext()) { javax.xml.registry.infomodel.Key orgKey = (javax.xml.registry.infomodel.Key) itr.next(); String id = orgKey.getId(); System.out.println("Organization key is " + id); } } else { itr = exceptions.iterator(); Exception exception = null; while (itr.hasNext()) { exception = (Exception) itr.next(); System.err.println("Failed to save because: " + exception.toString()); } } } catch (Exception e) { e.printStackTrace(); } finally { // At end, close connection to registry if (connection != null) { try { connection.close(); } catch (JAXRException je) {} } } } } Next you'll develop the Web services for the four suppliers. |