View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import org.apache.commons.cli.*;
4   import org.ogf.saga.context.Context;
5   import org.ogf.saga.error.*;
6   import org.ogf.saga.monitoring.*;
7   import org.ogf.saga.namespace.*;
8   import org.ogf.saga.session.Session;
9   import org.ogf.saga.session.SessionFactory;
10  import org.ogf.saga.task.Task;
11  import org.ogf.saga.task.TaskMode;
12  import org.ogf.saga.url.URL;
13  import org.ogf.saga.url.URLFactory;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   NamespaceCopy
20  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
21  * Date:   12 sept. 2007
22  * ***************************************************
23  * Description:                                      */
24  /**
25   *
26   */
27  public class NamespaceCopy extends AbstractCommand {
28      private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
29      private static final String OPT_NOT_OVERWRITE = "i", LONGOPT_NOT_OVERWRITE = "interactive";
30      private static final String OPT_RECURSIVE = "r", LONGOPT_RECURSIVE = "recursive";
31      private static final String OPT_PRESERVE_TIMES = "p", LONGOPT_PRESERVE_TIMES = "preserve";
32      private static final String OPT_MONITOR = "m", LONGOPT_MONITOR = "monitor";
33  
34      private static final int FLAGS_PRESERVETIMES = 8192;
35  
36      public NamespaceCopy() {
37          super("jsaga-cp", new String[]{"Source URL", "Target URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
38      }
39  
40      public static void main(String[] args) throws Exception {
41          NamespaceCopy command = new NamespaceCopy();
42          CommandLine line = command.parse(args);
43          if (line.hasOption(OPT_HELP))
44          {
45              command.printHelpAndExit(null);
46          }
47          else
48          {
49              // get arguments
50              URL source = URLFactory.createURL(command.m_nonOptionValues[0]);
51              URL target = URLFactory.createURL(command.m_nonOptionValues[1]);
52              int flags = (line.hasOption(OPT_NOT_OVERWRITE) ? Flags.NONE : Flags.OVERWRITE)
53                      .or((line.hasOption(OPT_RECURSIVE) ? Flags.RECURSIVE : Flags.NONE)
54                      .or((line.hasOption(OPT_PRESERVE_TIMES) ? FLAGS_PRESERVETIMES : Flags.NONE.getValue())));
55  
56              // execute command
57              Session session = SessionFactory.createSession(true);
58              NSEntry entry = NSFactory.createNSEntry(session, source, Flags.NONE.getValue());
59              if (line.hasOption(OPT_MONITOR)) {
60                  Task task = entry.copy(TaskMode.TASK, target, flags);
61                  try {
62                      Metric metric = task.getMetric("file.copy.progress");
63                      metric.addCallback(new Callback(){
64                          public boolean cb(Monitorable mt, Metric metric, Context ctx) throws NotImplementedException, AuthorizationFailedException {
65                              try {
66                                  String value = metric.getAttribute(Metric.VALUE);
67                                  String unit = metric.getAttribute(Metric.UNIT);
68                                  System.out.println("Progress: "+value+" "+unit);
69                              }
70                              catch (NotImplementedException e) {throw e;}
71                              catch (AuthorizationFailedException e) {throw e;}
72                              catch (Exception e) {
73                                  e.printStackTrace();
74                              }
75                              // callback must stay registered
76                              return true;
77                          }
78                      });
79                  } catch(DoesNotExistException e) {
80                      System.err.println("WARN: Monitoring is not supported for this kind of transfer");
81                  }
82                  task.run();
83                  task.waitFor();
84                  switch(task.getState()) {
85                      case DONE:
86                          System.out.println("File successfully copied !");
87                          break;
88                      default:
89                          task.rethrow();
90                          break;
91                  }
92              } else {
93                  entry.copy(target, flags);
94              }
95              entry.close();
96              System.exit(0);
97          }
98      }
99  
100     protected Options createOptions() {
101         Options opt = new Options();
102         opt.addOption(OptionBuilder.withDescription("Display this help and exit")
103                 .withLongOpt(LONGOPT_HELP)
104                 .create(OPT_HELP));
105         opt.addOption(OptionBuilder.withDescription("Do not overwrite target")
106                 .isRequired(false)
107                 .withLongOpt(LONGOPT_NOT_OVERWRITE)
108                 .create(OPT_NOT_OVERWRITE));
109         opt.addOption(OptionBuilder.withDescription("Copy recursively")
110                 .isRequired(false)
111                 .withLongOpt(LONGOPT_RECURSIVE)
112                 .create(OPT_RECURSIVE));
113         opt.addOption(OptionBuilder.withDescription("Preserve times")
114                 .isRequired(false)
115                 .withLongOpt(LONGOPT_PRESERVE_TIMES)
116                 .create(OPT_PRESERVE_TIMES));
117         opt.addOption(OptionBuilder.withDescription("Monitor transfer")
118                 .isRequired(false)
119                 .withLongOpt(LONGOPT_MONITOR)
120                 .create(OPT_MONITOR));
121         return opt;
122     }
123 }