~ubuntu-branches/ubuntu/trusty/nordugrid-arc/trusty-proposed

« back to all changes in this revision

Viewing changes to java/examples/DTRGenerator.java

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2013-05-08 13:48:03 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20130508134803-mrhc5w4d5y7ubyj4
Tags: 3.0.1-1
3.0.1 Release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// The nordugrid-arc-java package is required. To compile and run this example:
 
3
// 
 
4
// export CLASSPATH=/usr/lib64/java/arc.jar:.
 
5
// export LD_LIBRARY_PATH=/usr/lib64/java
 
6
// javac DTRGenerator.java
 
7
// java DTRGenerator /bin/ls /tmp/dtrtest
 
8
// 
 
9
// The PATHs above may vary depending on ARC install location and system
 
10
// architecture. 
 
11
 
 
12
import nordugrid.arc.*; // For the sake of brevity in this example import everything from arc
 
13
 
 
14
// Implementation of DTR Generator.
 
15
// Cannot inherit from DTRCallback as it is a pure virtual class and swig does not
 
16
// create a default constructor, so extend Scheduler which inherits from DTRCallback.
 
17
class DTRGenerator extends Scheduler {
 
18
    private Logger logger;
 
19
    private LogDestination logdest;
 
20
    private SimpleCondition cond;
 
21
 
 
22
    // Create a new Generator and set up logging to stdout
 
23
    public DTRGenerator() {
 
24
        logger = new Logger(Logger.getRootLogger(), "Generator");
 
25
        logdest = new LogStream_ostream(arc.getStdout());
 
26
        Logger.getRootLogger().addDestination(logdest);
 
27
        Logger.getRootLogger().setThreshold(LogLevel.DEBUG);
 
28
        cond = new SimpleCondition();
 
29
    }
 
30
 
 
31
    // Implementation of callback from DTRCallback
 
32
    public void receiveDTR(DTRPointer dtr) {
 
33
        // root logger is disabled in Scheduler thread so need to add it here
 
34
        Logger.getRootLogger().addDestination(logdest);
 
35
        logger.msg(LogLevel.INFO, "Received DTR " + dtr.get_id() + " in state " + dtr.get_status().str());
 
36
        Logger.getRootLogger().removeDestinations();
 
37
        cond.signal();
 
38
    }
 
39
 
 
40
    // Run the transfer and wait for the callback on completion
 
41
    private void run(final String source, final String dest) {
 
42
        // Set log level for DTR (must be done before starting Scheduler)
 
43
        DTR.setLOG_LEVEL(LogLevel.DEBUG);
 
44
 
 
45
        // Start Scheduler thread
 
46
        Scheduler scheduler = new Scheduler();
 
47
        scheduler.start();
 
48
 
 
49
        // UserConfig contains information such as the location of credentials
 
50
        UserConfig cfg = new UserConfig();
 
51
 
 
52
        // The ID can be used to group DTRs together
 
53
        String id = "1234";
 
54
 
 
55
        // Logger for DTRs
 
56
        DTRLogger dtrlog = arc.createDTRLogger(Logger.getRootLogger(), "DTR");
 
57
        dtrlog.addDestination(logdest);
 
58
 
 
59
        // Use current user's uid for the transfer
 
60
        User user = new User();
 
61
        // Create a DTR
 
62
        DTRPointer dtr = arc.createDTRPtr(source, dest, cfg, id, user.get_uid(), dtrlog);
 
63
        logger.msg(LogLevel.INFO, "Created DTR "+ dtr.get_id());
 
64
 
 
65
        // Register this callback in order to receive completed DTRs
 
66
        dtr.registerCallback(this, StagingProcesses.GENERATOR);
 
67
        // This line must be here in order to pass the DTR to the Scheduler
 
68
        dtr.registerCallback(scheduler, StagingProcesses.SCHEDULER);
 
69
 
 
70
        // Push the DTR to the Scheduler
 
71
        DTR.push(dtr, StagingProcesses.SCHEDULER);
 
72
 
 
73
        // Wait until callback is called
 
74
        // Note: SimpleCondition.wait() is renamed to _wait() as wait() is a java.lang.Object method
 
75
        cond._wait();
 
76
 
 
77
        // DTR is finished, so stop Scheduler
 
78
        scheduler.stop();
 
79
    }
 
80
 
 
81
    public static void main(String[] args) {
 
82
        if (args.length != 2) {
 
83
            System.out.println("Usage: java DTRGenerator source destination");
 
84
            return;
 
85
        }
 
86
        DTRGenerator gen = new DTRGenerator();
 
87
        gen.run(args[0], args[1]);
 
88
    }
 
89
}