//(c) International Business Machines Corporation, 2002 - 2004. //(c) University of Edinburgh 2002 - 2004. //See OGSA-DAI-Licence.txt for licencing information. package uk.org.ogsadai.client; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import org.apache.axis.message.MessageElement; import org.w3c.dom.Document; import org.w3c.dom.Element; import uk.org.ogsadai.common.XMLUtilities; import uk.org.ogsadai.common.exception.common.CommonSystemException; import uk.org.ogsadai.common.exception.common.CommonUserException; import uk.org.ogsadai.dataresource.DataResourceManagerProperties; import uk.org.ogsadai.dataresource.DataResourceProperties; import uk.org.ogsadai.service.wsi.DataServiceLocator; import uk.org.ogsadai.service.wsi.DataServicePortType; import uk.org.ogsadai.service.wsi.wsrp.GetMultipleResourceProperties; import uk.org.ogsadai.service.wsi.wsrp.GetResourceProperties; import uk.org.ogsadai.service.wsi.wsrp._GetMultipleResourcePropertiesResponse; import uk.org.ogsadai.service.wsi.wsrp._GetResourcePropertyResponse; import uk.org.ogsadai.xml.wsi.ExtensibilityType; /** * A simple client which demonstrates the use of the web-services interface * with the OGSA-DAI WS-I Data Service. * * @author The OGSA-DAI Project Team */ public class WSIExampleClient { private static String HANDLE = "http://localhost:8090/ogsadai-wsi/services/ogsadai/DataService/0"; private static String QUERY_STRING = "show tables"; /** * Execute the demonstration. */ public static void main(String[] args) throws Throwable { /* for (int i = 0; i < args.length; i++) { System.out.println("ARGS[" + i + "] = " + args[i]); } */ if (args.length != 2 && args.length != 4) { System.err.println("Wrong arguments!!!"); System.err.println("USAGE: java uk.org.ogsadai.client.WSIExampleClient [-s ] -q "); System.exit(0); } else if (args.length == 2 && args[0].equals("-q")) { QUERY_STRING = args[1]; } else if (args.length == 4) { if (args[0].equals("-s")) { HANDLE = args[1]; QUERY_STRING = args[3]; } else if (args[0].equals("-q")) { HANDLE = args[3]; QUERY_STRING = args[1]; } } else { System.err.println("Wrong arguments!!!"); System.err.println("USAGE: java uk.org.ogsadai.client.WSIExampleClient [-s ] -q "); System.exit(0); } (new WSIExampleClient()).run(); } /** * Presents a typical use-case. * * @throws Throwable if there is any kind of error */ public void run() throws Throwable { DataServiceLocator locator = new DataServiceLocator(); locator.setDataServicePortEndpointAddress(HANDLE); locator.setGetResourcePropertyPortEndpointAddress(HANDLE); locator.setGetMultipleResourcePropertyPortEndpointAddress(HANDLE); /* System.out.println("Get the product info and database schema about resource with handle " + HANDLE); getProductInfo(locator); getDatabaseSchema(locator); System.out.println("Get the activity type and perform document schema"); getActivityTypes(locator); getPerformDocSchema(locator); */ System.out.println("Perform a simple SQL query"); performDatabaseQuery(locator); System.out.println("Get the request status for the query"); getRequestStatus(locator); } /** * Obtain and display an XML document describing the status of the * current request processed by the Data Service on the given resource. * * @param locator the service locator */ public void getRequestStatus(DataServiceLocator locator) throws ServiceException, RemoteException, CommonSystemException { QName query = DataResourceManagerProperties.REQUEST_STATUS; String status = executeMetadataQuery(locator, query); System.out.println("Request status: " + status); } /** * Obtain and display the schema of perform documents accepted by the * resource with the given ID. * * @param locator the service locator */ public void getPerformDocSchema(DataServiceLocator locator) throws ServiceException, RemoteException, CommonSystemException { QName query = DataResourceProperties.PERFORM_DOCUMENT_SCHEMA; String schema = executeMetadataQuery(locator, query); System.out.println("Perform document schema: " + schema); } /** * Obtain and display an XML document describing the types of activities * recognised by the resource with the given ID. * * @param locator the service locator * @param resourceID the ID of the resource */ public void getActivityTypes(DataServiceLocator locator) throws ServiceException, RemoteException, CommonSystemException { QName query = DataResourceProperties.ACTIVITY_TYPES; String types = executeMetadataQuery(locator, query); System.out.println("Activity types: " + types); } /** * Obtain and display an XML document describing some details about * the nature of the database underlying the resource with the given * ID. * * @param locator the service locator * @param resourceID the ID of the resource */ public void getProductInfo(DataServiceLocator locator) throws ServiceException, RemoteException, CommonSystemException { QName query = DataResourceProperties.PRODUCT_INFO; String info = executeMetadataQuery(locator, query); System.out.println("Product info: " + info); } /** * Obtain and display the schema of the database underlying the * resource with the given ID. * * @param locator the service locator * @param resourceID the ID of the resource */ public void getDatabaseSchema(DataServiceLocator locator) throws ServiceException, RemoteException, CommonSystemException { QName query = DataResourceProperties.DATABASE_SCHEMA; String schema = executeMetadataQuery(locator, query); System.out.println("Schema: " + schema); } /** * Submit a perform document for a typical query to the resource with * the given ID, and display the resulting result set. This uses * the Data Service's perform operation. * * @param locator the service locator * @param resourceID the ID of the resource */ public void performDatabaseQuery(DataServiceLocator locator) throws ServiceException, RemoteException, CommonSystemException, CommonUserException { String query = "\n" + "" + " " + " " + QUERY_STRING + "" + " " + " " + ""; Document queryDoc = XMLUtilities.xmlStringToDOM(query, false); ExtensibilityType ext = documentToExtensibilityType(queryDoc); // Obtain the local interface for the remote service DataServicePortType service = locator.getDataServicePort(); // Submit the perform document and receive the response ExtensibilityType resp = service.perform(ext); // Extract the result set from the response Element result = extractElementFromExtensibilityType(resp); String resultString = XMLUtilities.xmlDOMToString(result); System.out.println("Result: " + resultString); } /** * Obtain multiple items of metadata from the Data Service relating to * the resource with the given ID. This uses the Data Service's * GetMultipleResourceProperties operation, which is * from the WS-ResourceProperties specification. * * @param locator the service locator * @param resourceID the ID of the resource * @param query an array of names of metadata items to obtain * @return an array containing the values of the metadata */ private String[] executeMultipleMetadataQuery(DataServiceLocator locator, QName[] query) throws ServiceException, RemoteException, CommonSystemException { GetMultipleResourceProperties wsrpMultiService = locator.getGetMultipleResourcePropertyPort(); _GetMultipleResourcePropertiesResponse response = wsrpMultiService.getMultipleResourceProperties(query); Element[] results = extractElementFromGetMultipleResourcePropertiesResponse(response); String[] resultString = new String[results.length]; for (int i=0; iGetResourceProperty operation, which is * from the WS-ResourceProperties specification. * * @param locator the service locator * @param resourceID the ID of the resource * @param query the name of the metadata item to obtain * @return the values of the metadata */ public String executeMetadataQuery(DataServiceLocator locator, QName query) throws ServiceException, RemoteException, CommonSystemException { GetResourceProperties wsrpService = locator.getGetResourcePropertyPort(); _GetResourcePropertyResponse schemaResp = wsrpService.getResourceProperty(query); Element schema = extractElementFromGetResourcePropertyResponse(schemaResp); String schemaString = XMLUtilities.xmlDOMToString(schema); return schemaString; } /** * Convert a Document object into an * ExtensibilityType object. * * @param document the object to convert * @return the converted object */ private static ExtensibilityType documentToExtensibilityType(Document document) { MessageElement[] elements = new MessageElement[1]; elements[0] = new MessageElement((Element) document.getFirstChild()); ExtensibilityType ext = new ExtensibilityType(); ext.set_any(elements); return ext; } /** * Extract each element of the contents of the given response parameter * and return an array containing the Element object * representing each, respectively. * * @param resp the response parameter * @return the array of elements */ private static Element[] extractElementFromGetMultipleResourcePropertiesResponse(_GetMultipleResourcePropertiesResponse resp) { MessageElement[] respElements = resp.get_any(); try { Element[] elements = new Element[respElements.length]; for (int i=0; iElement object * representing it. * * @param resp the response parameter * @return the element */ private static Element extractElementFromGetResourcePropertyResponse(_GetResourcePropertyResponse resp) { MessageElement[] respElements = resp.get_any(); try { return respElements[0].getAsDOM(); } catch (Exception e) { throw new IllegalArgumentException("First element did not contain a valid XML document"); } } /** * Extract the first (and probably only) element from the given * ExtensibilityType object and return the * Element object representing it. * * @param ext the ExtensibilityType object * @return the element */ private static Element extractElementFromExtensibilityType(ExtensibilityType ext) { MessageElement[] respElements = ext.get_any(); try { return respElements[0].getAsDOM(); } catch (Exception e) { throw new IllegalArgumentException("First element did not contain a valid XML document"); } } }