6.1.2.4.
Example Code
Up one level
Overview
Full documentation for creating an OGSA-DAI client is available in the OGSA-DAI documentation. This page aims to provide a simple example to get started with the OGSA-DAI client library.
The GRIA OGSA-DAI service has several endpoints. The most important ones are the service itself (OgsaDaiServiceI2) for use with the GRIA API and OgsaDaiSubscription endpoint that presents itself as an OGSA-DAI service.
Before trying this example remember to follow the instructions on configuring an OGSA-DAI client.
import java.sql.ResultSet; import uk.ac.soton.itinnovation.grid.client.ogsadai.OgsaDaiSSLHelper; import uk.org.ogsadai.client.toolkit.GenericServiceFetcher; import uk.org.ogsadai.client.toolkit.ResourceID; import uk.org.ogsadai.client.toolkit.Response; import uk.org.ogsadai.client.toolkit.activity.ActivityRequest; import uk.org.ogsadai.client.toolkit.activity.sql.SQLQuery; import uk.org.ogsadai.client.toolkit.activity.sql.WebRowSet; import uk.org.ogsadai.client.toolkit.properties.DAIVersion; import uk.org.ogsadai.client.toolkit.service.DataService; public class Test { public static void main(String[] args) { try { // Loads the keystore specified in the crypto.properties file // This only needs to be done once on application startup. OgsaDaiSSLHelper.setupKeystore(); // This is the URL of the GRIA OGSA-DAI service String handle = "https://www.changeme.com:8443/gria-ogsadai-service/services/OgsaDaiSubscription"; DataService service = GenericServiceFetcher.getInstance().getDataService(handle, null); // Fetch the OGSA-DAI version DAIVersion version = service.getVersion(); System.out.println("Version: " + version.getMajorVersion() + "." + version.getMinorVersion()); // Get a list of data resources we can access // This will return our database subscriptions // (those for which we have the "owner" or "client" role) ResourceID[] resources = service.getResourceIDs(); for (int i=0; i<resources.length; i++) { System.out.println(resources[i].getName()); } if (resources.length < 1) return; // In this example we shall perform a query on the first subscription that is returned service.setResourceID(resources[0]); // Create a query to list the tables in the database SQLQuery query = new SQLQuery("show tables"); WebRowSet rowset = new WebRowSet(query.getOutput()); ActivityRequest request = new ActivityRequest(); request.add(query); request.add(rowset); // Perform the request Response response = service.perform(request); // Get and display the results ResultSet result = rowset.getResultSet(); while (result.next()) { String tableName = result.getString(1); System.out.println(tableName); } } catch (Throwable e) { e.printStackTrace(); } } }
