View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import org.apache.commons.cli.*;
4   import org.ogf.saga.buffer.Buffer;
5   import org.ogf.saga.buffer.BufferFactory;
6   import org.ogf.saga.file.File;
7   import org.ogf.saga.logicalfile.LogicalFile;
8   import org.ogf.saga.namespace.*;
9   import org.ogf.saga.session.Session;
10  import org.ogf.saga.session.SessionFactory;
11  import org.ogf.saga.url.URL;
12  import org.ogf.saga.url.URLFactory;
13  
14  /* ***************************************************
15  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
16  * ***             http://cc.in2p3.fr/             ***
17  * ***************************************************
18  * File:   NamespaceCat
19  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
20  * Date:   28 sept. 2007
21  * ***************************************************
22  * Description:                                      */
23  /**
24   *
25   */
26  public class NamespaceCat extends AbstractCommand {
27      private static final int BUFFER_SIZE = 1024;
28      private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
29  
30      public NamespaceCat() {
31          super("jsaga-cat", new String[]{"Source URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
32      }
33  
34      public static void main(String[] args) throws Exception {
35          NamespaceCat command = new NamespaceCat();
36          CommandLine line = command.parse(args);
37          if (line.hasOption(OPT_HELP))
38          {
39              command.printHelpAndExit(null);
40          }
41          else
42          {
43              // get arguments
44              URL source = URLFactory.createURL(command.m_nonOptionValues[0]);
45  
46              // execute command
47              Session session = SessionFactory.createSession(true);
48              NSEntry entry = NSFactory.createNSEntry(session, source, Flags.READ.getValue());
49              try {
50                  while (entry instanceof LogicalFile) {
51                      URL effectiveSource = entry.readLink();
52                      NSEntry effectiveEntry = NSFactory.createNSEntry(session, effectiveSource, Flags.READ.getValue());
53                      entry.close();
54                      entry = effectiveEntry;
55                  }
56                  if (entry instanceof File) {
57                      File file = (File) entry;
58                      Buffer buffer = BufferFactory.createBuffer(BUFFER_SIZE);
59                      for (int len; (len=file.read(buffer)) > -1; ) {
60                          System.out.write(buffer.getData(), 0, len);
61                      }
62                      buffer.close();
63                  } else if (entry instanceof NSDirectory) {
64                      throw new Exception("Entry is a directory: "+entry.getURL());
65                  }
66              } finally {
67                  entry.close();
68              }
69              System.exit(0);
70          }
71      }
72  
73      protected Options createOptions() {
74          Options opt = new Options();
75          opt.addOption(OptionBuilder.withDescription("Display this help and exit")
76                  .withLongOpt(LONGOPT_HELP)
77                  .create(OPT_HELP));
78          return opt;
79      }
80  }