header

Using the Java Client API

The AMGA Java API is distributed in two forms. As an RPM and as a tar ball. The tar ball is provided so that the Java API can be used in other platforms other than Linux, including Windows and MacOS.

To use the Java API it is necessary to include the glite-amga-api-java.jar file in the classpath. If the Java API was installed from the RPM, then this file is typically located at <GLITE_HOME>/share/java, where <GLITE_HOME> is the base directory where the gLite software is installed (typically, /usr). If the Java API was installed directly from the tar ball availabe on the AMGA Web Site, the glite-amga-api-java.jar is located on the top level directory to where the tar ball was unpacked.

A jar can be included in the classpath in two ways: by setting the CLASSPATH environment variable of by using the -classpath option in the command line arguments when running java.

To set the classpath variable:

After setting the CLASSPATH, to run a Java program it is only necessary to do the following to run a class called, for instance, QueryMetadata:

java QueryMetadata

To specify the jar file directly on the command, one must do:

Unix:

java -classpath .:glite-amga-api-java.jar QueryMetadata

On Windows the command line is similar, except the path separator is ; instead of :.

The Javadocs for the Java API can be found here http://project-arda-dev.web.cern.ch/project-arda-dev/metadata/java.

Like the C++ API, the Java API can be used in two ways. Either through the higher-level interface exposed by the class arda.md.javaclient.MDClient or by sending the commands directly to the server using the low-level API in arda.md.javaclient.MDServerConnection. Next are the two examples given for the C++ client API rewritten using the Java API. The first uses the higher-level Java API.

import java.io.IOException;
import arda.md.javaclient.*;

public class MDJavaAPI {

  public static void main(String[] args) throws IOException {
    MDServerConnection serverConn = new MDServerConnection(
      MDServerConnectionContext.loadDefaultConfiguration());
      MDClient mdClient = new MDClient(serverConn);

      System.out.println("Listing attribues of /test");
      try {
        AttributeDef[] attrs = mdClient.listAttr("/test");
        System.out.println("Result: ");
        for (int i = 0; i < attrs.length; i++) {
          System.out.println(" >" + attrs[i].name + ":" + attrs[i].type);
        }
      } catch (CommandException e) {
        System.out.println("Error: " + e.getMessage());
      }

      System.out.println("Getting gen and events attributes of /test");
      try {
        String[] keys = {"gen", "events"};
        NamedAttributesIterator attrs = mdClient.getAttr("/test", keys);
        while (attrs.hasNext()) {
          NamedAttributes entry = attrs.next();
          System.out.println("File: " + entry.getEntryName());
          String[] keys1 = entry.getKeys();
          for (int i = 0; i < keys1.length; i++) {
            System.out.println(" >" + keys1[i] + "=" + entry.getValue(keys1[i]));
          }
        }
      } catch (CommandException e) {
        System.out.println("Error: " + e.getMessage());
      }
  }
}

The following example uses the low-level API directly:

import java.io.IOException;
import arda.md.javaclient.*;

public class DirectServerConnection {

  public static void main(String[] args) throws IOException
  {
    // Loads default configuration and connects to server
    MDServerConnection serverConn = new MDServerConnection(
    MDServerConnectionContext.loadDefaultConfiguration());
    try {
      serverConn.execute("pwd");
      while (!serverConn.eot()) {
        String row = serverConn.fetchRow();
        System.out.println(">" + row);
      }
    } catch (CommandException e) {
      System.out.println("Error executing command: " + e.getMessage());
    }
  }
}

Generated on Mon Apr 16 13:59:18 2012 for AMGA by  doxygen 1.4.7