package org.astrogrid.ogsa.gcache;

import java.net.URL;


/**
 * Exercises an Gcache service instance by retrieving a datum stored
 * in that instance.
 */
public class FetchClientS {

  public static void main (String[] args) throws Exception {

    if (args.length != 1) {
      System.err.println("Usage: java org.astrogrid.ogsa.gcache.FetchClient "
                         + "<service-instance-URL>");
      return;
    }

    try {
      FetchClientS.fetch(args[0]);
    }
    catch (Exception e) {
      System.err.println(e);
    }
  }


  private static void fetch (String handle) throws Exception {
    System.out.println("Service instance: " + handle);
    URL url = new URL(handle);

    // Find the application port on the existing service-instance.
    GcacheGridLocator portLocator = new GcacheGridLocator();
    GcachePortType port = portLocator.getGcachePortType(url);

    // Set up authenticated access: make credentials available in
    // calls to the service.
    SecurityHelper.configureSecureCall(port);

    // Invoke the port.
    System.out.println("About to call fetch().");
    String datum = port.fetch();
    System.out.println("Fetched: " + datum);
    System.out.println("The test finished without exceptions.");
  }
}
