View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import org.apache.commons.cli.*;
4   import org.ogf.saga.error.NotImplementedException;
5   import org.ogf.saga.error.BadParameterException;
6   import org.ogf.saga.file.File;
7   import org.ogf.saga.namespace.NSEntry;
8   import org.ogf.saga.namespace.NSFactory;
9   import org.ogf.saga.permissions.Permission;
10  import org.ogf.saga.session.Session;
11  import org.ogf.saga.session.SessionFactory;
12  import org.ogf.saga.url.URL;
13  import org.ogf.saga.url.URLFactory;
14  
15  import java.util.Date;
16  
17  /* ***************************************************
18  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
19  * ***             http://cc.in2p3.fr/             ***
20  * ***************************************************
21  * File:   NamespaceStat
22  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
23  * Date:   14 oct. 2008
24  * ***************************************************
25  * Description:                                      */
26  /**
27   *
28   */
29  public class NamespaceStat extends AbstractCommand {
30      private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
31      private static final String OPT_NAME = "n", LONGOPT_NAME = "name";
32      private static final String OPT_TYPE = "t", LONGOPT_TYPE = "type";
33      private static final String OPT_SIZE = "s", LONGOPT_SIZE = "size";
34      private static final String OPT_PERM = "p", LONGOPT_PERM = "perm";
35      private static final String OPT_OWNER = "o", LONGOPT_OWNER = "owner";
36      private static final String OPT_GROUP = "g", LONGOPT_GROUP = "group";
37      private static final String OPT_DATE = "d", LONGOPT_DATE = "date";
38  
39      public NamespaceStat() {
40          super("jsaga-stat", new String[]{"URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
41      }
42  
43      public static void main(String[] args) throws Exception {
44          NamespaceStat command = new NamespaceStat();
45          CommandLine line = command.parse(args);
46          if (line.hasOption(OPT_HELP))
47          {
48              command.printHelpAndExit(null);
49          }
50          else
51          {
52              // get arguments
53              URL url = URLFactory.createURL(command.m_nonOptionValues[0]);
54  
55              // execute command
56              Session session = SessionFactory.createSession(true);
57              NSEntry entry = NSFactory.createNSEntry(session, url);
58              if (line.hasOption(OPT_NAME)) {
59                  System.out.println(entry.getName());
60              } else if (line.hasOption(OPT_TYPE)) {
61                  System.out.println(getType(entry));
62              } else if (line.hasOption(OPT_SIZE)) {
63                  System.out.println(getSize(entry));
64              } else if (line.hasOption(OPT_PERM)) {
65                  System.out.println(getPerm(entry));
66              } else if (line.hasOption(OPT_OWNER)) {
67                  System.out.println(getOwner(entry));
68              } else if (line.hasOption(OPT_GROUP)) {
69                  System.out.println(getGroup(entry));
70              } else if (line.hasOption(OPT_DATE)) {
71                  System.out.println(getMTime(entry));
72              } else {
73                  System.out.println("  File: "+entry.getName());
74                  System.out.println("  Type: "+getType(entry));
75                  System.out.println("  Size: "+getSize(entry));
76                  System.out.println("  Perm: "+getPerm(entry));
77                  System.out.println(" Owner: "+getOwner(entry));
78                  System.out.println(" Group: "+getGroup(entry));
79                  System.out.println("Modify: "+ getMTime(entry));
80              }
81              entry.close();
82              System.exit(0);
83          }
84      }
85  
86      private static String getType(NSEntry entry) throws Exception {
87          if (entry.isEntry()) {
88              return "file";
89          } else if (entry.isDir()) {
90              return "directory";
91          } else if (entry.isLink()) {
92              return "link";
93          } else {
94              return "unknown";
95          }
96      }
97  
98      private static long getSize(NSEntry entry) throws Exception {
99          if (entry instanceof File) {
100             return ((File) entry).getSize();
101         } else {
102             return 0;
103         }
104     }
105 
106     private static String getPerm(NSEntry entry) throws Exception {
107         try {
108             StringBuffer perms = new StringBuffer();
109             perms.append(entry.permissionsCheck(null, Permission.QUERY.getValue()) ? "q" : "-");
110             perms.append(entry.permissionsCheck(null, Permission.READ.getValue()) ? "r" : "-");
111             perms.append(entry.permissionsCheck(null, Permission.WRITE.getValue()) ? "w" : "-");
112             perms.append(entry.permissionsCheck(null, Permission.EXEC.getValue()) ? "x" : "-");
113             perms.append(entry.permissionsCheck(null, Permission.OWNER.getValue()) ? "o" : "-");
114             return perms.toString();
115         } catch(BadParameterException e) {
116             return "?";
117         } catch(NotImplementedException e) {
118             return "?";
119         }
120     }
121 
122     private static String getOwner(NSEntry entry) throws Exception {
123         try {
124             return entry.getOwner();
125         } catch(NotImplementedException e) {
126             return "?";
127         }
128     }
129 
130     private static String getGroup(NSEntry entry) throws Exception {
131         try {
132             return entry.getGroup();
133         } catch(NotImplementedException e) {
134             return "?";
135         }
136     }
137 
138     private static String getMTime(NSEntry entry) throws Exception {
139         try {
140             return new Date(entry.getMTime()).toString();
141         } catch(NotImplementedException e) {
142             return "?";
143         }
144     }
145 
146     protected Options createOptions() {
147         Options opt = new Options();
148         opt.addOption(OptionBuilder.withDescription("Display this help and exit")
149                 .withLongOpt(LONGOPT_HELP)
150                 .create(OPT_HELP));
151 
152         OptionGroup group = new OptionGroup();
153         group.addOption(OptionBuilder.withDescription("Entry name")
154                 .withLongOpt(LONGOPT_NAME)
155                 .create(OPT_NAME));
156         group.addOption(OptionBuilder.withDescription("Entry type (file | directory | link)")
157                 .withLongOpt(LONGOPT_TYPE)
158                 .create(OPT_TYPE));
159         group.addOption(OptionBuilder.withDescription("Entry size (or 0 if entry is not a file)")
160                 .withLongOpt(LONGOPT_SIZE)
161                 .create(OPT_SIZE));
162         group.addOption(OptionBuilder.withDescription("Entry permissions")
163                 .withLongOpt(LONGOPT_PERM)
164                 .create(OPT_PERM));
165         group.addOption(OptionBuilder.withDescription("Entry owner")
166                 .withLongOpt(LONGOPT_OWNER)
167                 .create(OPT_OWNER));
168         group.addOption(OptionBuilder.withDescription("Entry group")
169                 .withLongOpt(LONGOPT_GROUP)
170                 .create(OPT_GROUP));
171         group.addOption(OptionBuilder.withDescription("Entry last modification date")
172                 .withLongOpt(LONGOPT_DATE)
173                 .create(OPT_DATE));
174         group.setRequired(false);
175         opt.addOptionGroup(group);
176 
177         return opt;
178     }
179 }