next up previous contents
Next: C++ Application Program Interface Up: Java Application Program Interface Previous: Example   Contents

Example Program

The Java program below shows example uses of the 4 methods copyAndRegisterFile, replicateFile, listReplicas, and getBestFile. After setting up the configuration and creating a ReplicaManager instance, a file stored on the local file system is copied to Grid storage and registered in the catalogs with a LFN (copyAndRegisterFile). The file is then replicated to Grid storage on another site (replicateFile) and the RM is asked for a list of the replicas of the LFN now available (listReplicas).

Then the RM is asked for a copy of the file to be used by a locally running job (getBestFile). If the file is not available on the local SE, a replica will be created there.

/**
 * EDGReplicaManagerExample.java
 *
 * Copyright (c) 2002-2003 CERN, on behalf of the EU DataGrid.
 * For license conditions see LICENSE file or
 * http://www.edg.org/license.html
 *
 */

import java.net.URI;
import java.net.URISyntaxException;

import java.util.Iterator;
import java.util.Set;

import org.edg.data.reptor.ReplicaManager;
import org.edg.data.reptor.ReplicaManagerImpl;
import org.edg.data.reptor.ReplicaManagerException;
import org.edg.data.reptor.Configuration;

public class EDGReplicaManagerExample {


    public EDGReplicaManagerExample() {
    }

    /**
     * Set up the configuration and the ReplicaManager instance.
     */
    public void setUp() throws Exception {
        try {
            m_config = new Configuration("/opt/edg/etc/edg-replica-manager \
                           /edg-replica-manager.conf", "wpsix");
            m_replicaManager = new ReplicaManagerImpl(m_config);
        } catch(Exception e) {
            System.out.println("Could not create ReplicaManagerImpl object "
                                + e.getMessage());
	    throw new Exception(e);
        }
    }

    /**
     * Here we copy and register a file to the grid, replicate it
     * to another site, list the replicas of the file, and then
     * call getBestFile to obtain a local copy of the file.
     */
    public void startReplication() throws Exception {
    
        // prepare source (local file), first SE destination and LFN
        URI source = new URI("file:/tmp/higgs0123");
        URI destination = 
               new URI("srm://ced-rc0.datagrid.cnr.it/flatfiles/SE00/tutor/higgs0123");
        URI lfn = new URI("lfn:myHiggs0123");

        // copy the file into the Grid
        try {
            m_replicaManager.copyAndRegisterFile(source, destination,
                                                 lfn, null, 0);
        } catch(ReptorException e1) {
            System.out.println("copyAndRegister failure " + e1.getMessage());
        }

        // replicate to a second SE and let the RM decide 
        // about the distination dir
        destination = new URI (null, "hepbf2.ph.qmul.ac.uk", null, null);
        try {
            m_replicaManager.replicateFile(lfn, destination, "gsiftp", 1);
        } catch(ReptorException e2) {
            System.out.println("replicateFile failure " + e2.getMessage());
        }

        // list the replicas - there should be two now
        System.out.println("List replicas...");
        Set replicas = null;
        try {
            replicas = m_replicaManager.listReplicas(lfn); 

            Iterator it = replicas.iterator();
            while(it.hasNext()) {
                URI replica = (URI) it.next();
                System.out.println(replica.toString());
            }
        } catch(ReptorException e3) {
            System.out.println("listReplicas failure " + e3.getMessage());
        }

        // Now we want a copy of the file locally to use for a job.
        // We call getBestFile and it will replicate to the local SE
        // if the file is not already there.
        try {
            System.out.println("Calling getBestFile..."); 

            // if the destination is null, the local (default) SE is used
            URI localFile = m_replicaManager.getBestFile(lfn, null, "gsiftp", 
                                                         null, 0);
        } catch(ReptorException e4) {
             System.out.println("getBestFile failure " + e4.getMessage());
        }
    }


    /*
     * Main Program
     */
    public static void main(String[] args) {

        System.out.println("EDG Replica Manager: example use of JavaAPI");

        EDGReplicaManagerExample example = new EDGReplicaManagerExample();
        try {
            example.setUp();
            example.startReplication();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
 
    /*
     * member variables
     */
    private static ReplicaManager m_replicaManager;
    private static Configuration m_config;

}



2005-02-04