AMGA APIs usage
(Java, C++)
Antonio Calanducci
[email protected]
INFN Catania
EGEE NA3 Training & Dissemination
II Corso di formazione INFN su aspetti pratici
dell’integrazione di applicazioni in Grid
ICTP, Trieste 01-12 Dicembre 2008
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
Contents
• Java APIs usage, examples and
references
• C++ APIs usage, examples and references
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
Java APIs requirements
• Java APIs are “pure” Java: can be used to any platform
(Linux, Windows, Mac OS X)
• Requirements:
– AMGA APIs jar: glite-amga-api-java-v1.30.jar
 Download @ http://amga.web.cern.ch/amga/downloads/glite-amgaapi-java-v1.30.jar
– A Bouncy Castle Provider jar compatible with your Java SDK
release (bcprov-jdk1x-yyy.jar)
 Download @ http://www.bouncycastle.org/latest_releases.html
– Java SDK >= 1.5
 Download @: http://java.sun.com/javase/downloads/
– Javadoc API documentation @:
 http://amga.web.cern.ch/amga/java/index.html
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
Java API usage
• Java APIs can be used in two ways:
– Sending the commands directly to the server using the low-level
API:
 arda.md.javaclient.MDServerConnection
 It does not understand the semantics of the commands and does not
parse server response into suitable structures
 But better control on the connection to the server (you can easily abort
a query for example)
– High-level interface (that in turn uses the low level one) exposed by
 arda.md.javaclient.MDClient class
 Provide suitable structures to handle server response
 Not all the commands are implemented (use direct connection instead)
• A higher level OO framework has been developed that
implement several OO Design Patterns (not covered here).
More info@: http://amga.web.cern.ch/amga/api_java13.html
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
Low level API:
MDServerConnection
import java.io.IOException;
import arda.md.javaclient.*;
public class DirectAMGA {
public static void main(String[] args) throws IOException
{
// Loads default configuration from mdclientjava.config 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());
}
}
}
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
High level API:
arda.md.javaclient.MDClient #1
import arda.md.javaclient.*;
import java.util.Iterator;
public class MDJavaAPI {
public static String TEST_DIR = "/gilda/tony/dir1";
public static void main(String[] args) throws Exception {
/*Alternative configuration, using mdjavaclient.config file:
MDServerConnectionContext mdConContext =
MDServerConnectionContext.loadDefaultConfiguration());
*/
//setting MDServerConnectionContext
MDServerConnectionContext mdConContext = new MDServerConnectionContext();
mdConContext.setHost("amga.grid.box");
mdConContext.setPort(8833);
mdConContext.setCurrentDir("/");
mdConContext.setGroupMask("rwx");
mdConContext.setPermissionMask("rwx");
mdConContext.setLogin("NULL");
mdConContext.setUseSSL(true);
mdConContext.setAuthMode(MDServerConnectionContext.AUTH_GRIDPROXY);
mdConContext.setGridProxyFile("/tmp/x509up_u503");
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
High level API:
arda.md.javaclient.MDClient #2
MDServerConnection serverConn = new MDServerConnection(mdConContext);
MDClient mdClient = new MDClient(serverConn);
System.out.println("Listing attribues of " + TEST_DIR);
try {
AttributeDefList attrs = mdClient.listAttr(TEST_DIR);
Iterator<AttributeDef> it = attrs.iterator();
System.out.println("Result: ");
while(it.hasNext())
{
AttributeDef att = it.next();
System.out.println(" >" + att.getName() + ":" + att.getType());
}
} catch(CommandException e) {
System.out.println("Error: " + e.getMessage());
}
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
High level API:
arda.md.javaclient.MDClient #3
System.out.println("Getting comment and id attributes of " + TEST_DIR);
try {
String[] keys = {"remark", "id"};
AttributeSetList col = mdClient.getAttr(TEST_DIR, keys);
Iterator<AttributeSet> attrs = col.iterator();
while(attrs.hasNext()) {
AttributeSet set = attrs.next();
System.out.println("File: " + set.getEntry());
String[] keys1 = set.getKeys();
for(int i = 0; i < keys1.length; i++) {
System.out.println(" >" + keys1[i] + "=" +
set.getValue(keys1[i]));
}
}
} catch(CommandException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
Compile and run the examples
• Set up the CLASSPATH properly:
– export CLASSPATH=$HOME/glite-amga-api-java-v1.30.jar:/opt/
d-cache/srm/lib/security/bcprov-jdk15-133.jar:.
• Set up properly mdjavaclient.config (if you use
loadDefaultConfiguration()):
–
–
–
–
–
–
Host = amga.grid.box
Port = 8833
Login = NULL
AuthMode = GridProxy
GridProxyFile=/tmp/x509up_u501
UseSSL = 1
• Compile and run the code:
– javac MDJavaAPI.java
– java MDJavaAPI
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
mdjavacli and mdjavaclient
• Java versions of the mdcli/mdclient CLI tools are
available and distributed with the previous Java AMGA
release.
– Download them at:
 http://amga.web.cern.ch/amga/downloads/ardamd-apijava-1.2.6RC1.tar.gz
– Set up properly the mdjavaclient.config (as in the previous slide)
– Use the wrapper bash script (mdjavacli.sh and mdjavaclient.sh)
to set up automatically the CLASSPATH and launch the main
classes
– AMGA Java API homepage:
 http://amga.web.cern.ch/amga/api_java13.html
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
C++ APIs requirements
• C++ API are available on the same platformd AMGA CLI
RPMs are available (currently SLC 3/4)
• To build and run you have to be sure the following are
installed in you system:
[desktop18] /opt/glite/lib > ls
-rw-r--r-- 1 root root 776204
-rwxr-xr-x 1 root root
864
lrwxrwxrwx 1 root root
20
libMDClient.so.0.0.0
lrwxrwxrwx 1 root root
20
-> libMDClient.so.0.0.0
-rwxr-xr-x 1 root root 1867114
0.0.0
-la /opt/glite/lib/libMDClient.*
19 nov 20:49 /opt/glite/lib/libMDClient.a
19 nov 20:49 /opt/glite/lib/libMDClient.la
19 nov 21:45 /opt/glite/lib/libMDClient.so ->
19 nov 21:45 /opt/glite/lib/libMDClient.so.0
19 nov 20:49 /opt/glite/lib/libMDClient.so.
• Header files are here:
/opt/glite/include/AMGAService.h
/opt/glite/include/MDClient.h
/opt/glite/include/md_api.h
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
C++ available APIs
• As Java APIs, they can be used in two ways:
– a low level API that sends direct commands to the server
 using MDClient.h
 Documentation available at:
• http://project-arda-dev.web.cern.ch/project-arda-dev/metadata/
md__api_8cc.html
– a high level API that parse and provides suitable structures to
contain results
 using md_api.h
 Documentation available at:
• http://project-arda-dev.web.cern.ch/project-arda-dev/metadata/
classMDClient.html
• C++ API homepage:
– http://amga.web.cern.ch/amga/api_cpp.html
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
MDClient.h usage
#include <MDClient.h>
#include <iostream>
int main (int argc, char *argv[])
{
int res;
MDClient client;
// client.setDebug(true);
if(client.connectToServer()){
std::cout << client.getError() << std::endl;
return 5;
}
std::string command="pwd";
if( ( res=client.execute(command)) ){
std::cout << " ERROR: execute failed"
<< "
(" << res << "): "
<< client.getError() << std::endl;
return res;
}
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
MDClient.h usage
while(!client.eot()) {
std::string row;
if(res=client.fetchRow(row)){
std::cout << "Error fetching: " << res << std::endl;
return res;
}
std::cout << row << std::endl;
}
return 0;
}
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
md_api.h usage
#include "md_api.h"
#include <iostream>
int main (int argc, char *argv[])
{
int res;
std::cout << "Listing attributes of /gilda/tony/dir1\n";
std::list< std::string > attrList;
std::list< std::string > types;
if( (res=listAttr("/gilda/tony/dir1", attrList, types)) == 0){
std::cout << " Result:" << std::endl;
std::list< std::string >::iterator I=attrList.begin();
while(I != attrList.end())
std::cout << " >" << (*I++) << "<" << std::endl;
} else {
std::cout << " Error: " << res << std::endl;
}
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
md_api.h usage #2
std::cout << "Getting id and comment attributes of /gilda/tony/dir1\n";
AttributeList attributeList(2);
std::list< std::string > attributes;
attributes.push_back("id");
attributes.push_back("comment");
if( (res=getAttr("/gilda/tony/dir1/", attributes, attributeList)) == 0){
std::cout << " Result:" << std::endl;
while(!attributeList.lastRow()){
std::vector< std::string > attrs;
std::string filename;
attributeList.getRow(filename, attrs);
std::cout << "File: >" << filename << "<" << std::endl;
for(size_t i=0; i< attrs.size(); i++)
std::cout << " >" << attrs[i] << "<" << std::endl;
std::cout << std::endl;
}
} else {
std::cout << " Error: " << res << std::endl;
}
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
Compile and run the example
• Include header file and library directories at compile
time:
– g++ -m32 -I/opt/glite/include/ -L/opt/glite/lib -lMDClient
direct.cpp -o direct
• Set up properly LD_LIBRARY_PATH to find
libMDClient.*
– export LD_LIBRARY_PATH=/opt/glite/lib:
$LD_LIBRARY_PATH
• You don’t need to set any special AMGA configuration
file. It tries the ones of mdcli/mdclient in the following
order:
– $PWD/mdclient.config
– $HOME/.mdclient.config
– /etc/mdclient.config
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/
Questions…
www.ccr.infn.it
II Corso integrazione applicazioni Grid @ ICTP, 1-12 Dec 2008
http://grid.infn.it/