So far you have learned how to publish data to a registry and how to query a registry for information. You will now learn how to delete data from a registry. You can only delete data that you have submitted. A registry does not allow the deletion of information about organizations that are published by other users. The BusinessLifeCycleManager interface provides several delete methods with which you can remove information about an organization. For example, the deleteService method enables you to delete services associated with an organization. You will write an application called MyDataDeleter.java that will take as a command-line argument a string containing the key generated by the registry when the organization was saved. The application will delete the organization that matches the key value sent as the command-line parameter. Similar to the MyOrganizationPublisher application, you will import the necessary classes, create a connect method to connect to the registry, and a delete method to do the actual deletion of data on the registry. Creating the MyDataDeleter.java Application To create the MyDataDeleter application, select a text editor and create an empty text file called MyDataDeleter.java. In the file, enter the following lines of code to import the classes and declare the MyDataDeleter application: import javax.xml.registry.*; import javax.xml.registry.infomodel.*; import java.net.*; import java.security.*; import java.util.*; public class MyDataDeleter { Writing the Plumbing Code Next you will write the necessary plumbing code for the application. In the MyDataDeleter application, you need to define the following: -
A null Connection object. This will be used to hold the reference to the Connection object obtained from the instance of the ConnectionFactory class. -
Because the application needs a string as the parameter, you need to specify code to check it. In case the parameter is not specified, the program should exit. -
A default constructor for the MyDataDeleter class. You will be using the constructor to create an instance of the MyDataDeleter class and make calls to the connect and delete methods from within the application. -
The connect method, which will have the required code to make a connection to the registry server. -
The delete method, which will have the required code to delete data from the registry. To complete these tasks, enter the lines of code shown here in bold: public class MyDataDeleter { Connection connection = null; public static void main(String[] args) { if (args.length < 1) { System.out.println("Usage: runclient MyDataDeleter keyString"); System.exit(1); } String keyStr = new String(args[0]); System.out.println("Key string is " + keyStr); MyDataDeleter mdd = new MyDataDeleter(); mdd.connect(); mdd.delete(keyStr); } Connecting to the Registry Next you will write the code that enables the application to connect to the registry. The code for connecting to the registry (the connect method) is the same as the code that you wrote for MyOrganizationPublisher. For your convenience it is shown again in Listing 10.4. Listing 10.4 The connect Methodpublic void connect() { String queryURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String publishURL = "http://localhost:8080/registry-server/RegistryServerServlet"; String httpProxyHost = ""; String httpProxyPort = ""; String httpsProxyHost = ""; String httpsProxyPort = ""; 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(); } } Now let's write the code for the delete method. Deleting Data from the Registry The delete method, which contains the code for deleting data from the registry, will take as a parameter a string that contains the key for the organization that needs to be deleted. To do this, you need to do the following: -
Get a reference to the RegistryService object. -
From the RegistryService object, obtain instances of the BusinessLifeCycleManager and BusinessQueryManager objects. The BusinessLifeCycleManager object enables the creation of the Organization object and its constituent objects that are submitted to the registry. The BusinessQueryManager enables you to run queries on the registry service. -
Create a PasswordAuthentication object with the default user ID/password combination of testuser/testuser. Using the PasswordAuthentication object, set the credentials with the registry. -
Using the createKey method of the BusinessLifeCycleManager interface, create a key from the specified string. -
Using the deleteOrganizations method of the BusinessLifeCycleManager interface, delete the organization that has the specified key value. -
Verify that the organization has been successfully deleted. To get the instances of the interfaces, enter the following lines of code: public void delete(String keystring) { RegistryService rsvrc = null; BusinessLifeCycleManager blcmgr = null; javax.xml.registry.infomodel.Key key = null; String username = "testuser"; String password = "testuser"; try { rsvrc = connection.getRegistryService(); blcmgr = rsvrc.getBusinessLifeCycleManager(); System.out.println("Got registry service and business life cycle manager"); Next you need to set the credentials with the registry. To do so, enter the lines of code shown here in bold: System.out.println("Got registry service and business 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"); Now you will create the key from the string specified in the command-line argument. Once the key is obtained, you'll invoke the deleteOrganization method to delete the organization with the specified key. To do so, enter the lines of code shown here in bold: System.out.println("Security credentials established"); key = blcmgr.createKey(keystring); String id = key.getId(); System.out.println("Id of the organization to be deleted :" + id); Collection keys = new ArrayList(); keys.add(key); BulkResponse response = blcmgr.deleteOrganizations(keys); Finally, you need to verify that the deletion was successful. To do this, you'll check whether any exception was thrown when the deleteOrganizations was called. If no exception was thrown, the deletion was successful. To verify the deletion, enter the following lines of code shown here in bold: BulkResponse response = blcmgr.deleteOrganizations(keys); Collection exceptions = response.getExceptions(); if (exceptions == null) { System.out.println("Organization deleted"); } else { Iterator excIter = exceptions.iterator(); Exception exception = null; while (excIter.hasNext()) { exception = (Exception) excIter.next(); System.err.println("Exception on delete: " + exception.toString()); } } } catch (Exception e) { e.printStackTrace(); } finally { // At end, close connection to registry if (connection != null) { try { connection.close(); } catch (JAXRException je) {} } } } } NOTE The code discussed here is available in the MyJAXRExample folder. You can now compile and run the MyDataDeleter application. To compile the application, enter the following command at the command prompt: javac -classpath .;d:\jwsdp-1_0\common\lib\jaxr-ri.jar; d:\jwsdp-1_0\common\lib\jaxr-api. jar;d:\jwsdp-1_0\common\lib\jaxrpc-ri.jar;d:\jwsdp-1_0\common\lib\jaxrpc-api.jar; d:\ jwsdp-1_0\common\lib\activation.jar;d:\jwsdp-1_0\common\lib\dom4j.jar;d:\jwsdp-1_0\common\ lib\jaxm-api.jar; d:\jwsdp-1_0\common\lib\jaxm-client.jar;d:\jwsdp-1_0\common\lib\log4j. jar; d:\jwsdp-1_0\common\lib\mail.jar;d:\jwsdp-1_0\common\lib\xalan.jar; d:\jwsdp-1_0\ common\lib\xerces.jar;d:\jwsdp-1_0\common\lib\jaxp-api.jar -d . MyDataDeleter.java NOTE To build the client, you can also use the buildclient.bat batch file available in the MyJAXRExample folder. The syntax to run the buildclient.bat file is as follows: buildclient MyDataDeleter.java Currently the registry server has data for only one company, The Great Car Parts Company, so you will run the application and delete the record for it. To run the application, enter the following at the command prompt (the key value might be different in your case. Enter the key value that you got while saving the organization information): java -DuseSOAP=true -classpath .;d:\jwsdp-1_0\common\endorsed\xalan.jar; d:\jwsdp-1_0\ common\endorsed\dom.jar;d:\jwsdp-1_0\common\endorsed\sax.jar; d:\jwsdp-1_0\common\ endorsed\xercesImpl.jar;d:\jwsdp-1_0\common\endorsed\xsltc.jar;d:\jwsdp-1_0\common\lib\ jaxr-ri.jar;d:\jwsdp-1_0\common\lib\jaxr-api.jar;d:\jwsdp-1_0\common\lib\jaxrpc-ri.jar;d:\ jwsdp-1_0\common\lib\jaxrpc-api.jar;d:\jwsdp-1_0\common\lib\activation.jar;d:\jwsdp-1_0\ common\lib\dom4j.jar;d:\jwsdp-1_0\common\lib\jaxm-api.jar;d:\jwsdp-1_0\common\lib\ jaxm-client.jar;d:\jwsdp-1_0\common\lib\log4j.jar;d:\jwsdp-1_0\common\lib\mail.jar;d:\ jwsdp-1_0\common\lib\xalan.jar;d:\jwsdp-1_0\common\lib\xerces.jar;d:\jwsdp-1_0\common\lib\ jaxp-api.jar;d:\jwsdp-1_0\common\lib\commons-logging.jar;d:\jwsdp-1_0\common\lib\soap.jar; d:\jwsdp-1_0\common\lib\castor-0.9.3.9-xml.jar; MyDataDeleter eeb543f8-5dee-b543-8407-30aeb62c40bd NOTE To run the application, you can also use the buildclient.bat batch file available in the MyJAXRExample folder. The syntax to run the runMyDataDeleter.bat file is as follows: runMyDataDeleter MyDataDeleter eeb543f8-5dee-b543-8407-30aeb62c40bd The output should be similar to the following: Key string is eeb543f8-5dee-b543-8407-30aeb62c40bd Created connection to registry Got registry service and business life cycle manager Security credentials established Id of the organization to be deleted :eeb543f8-5dee-b543-8407-30aeb62c40bd Organization deleted |