View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import org.apache.commons.cli.*;
4   import org.ogf.saga.error.*;
5   import org.ogf.saga.namespace.NSEntry;
6   import org.ogf.saga.namespace.NSFactory;
7   import org.ogf.saga.permissions.Permission;
8   import org.ogf.saga.session.Session;
9   import org.ogf.saga.session.SessionFactory;
10  import org.ogf.saga.url.URL;
11  import org.ogf.saga.url.URLFactory;
12  
13  /* ***************************************************
14   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
15   * ***             http://cc.in2p3.fr/             ***
16   * ***************************************************
17   * File:   NamespaceChmod
18   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
19   * Date:   22 mai 2010
20   * ***************************************************
21   * Description:                                      */
22  /**
23   *
24   */
25  public class NamespaceChmod extends AbstractCommand {
26      private static final int ARG_MODE = 0;
27      private static final int ARG_URL = 1;
28      private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
29  
30      private static final int UNIX_READ = 4;
31      private static final int UNIX_WRITE = 2;
32      private static final int UNIX_EXEC = 1;
33  
34      public NamespaceChmod() {
35          super("jsaga-chmod", new String[]{"mode", "URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
36      }
37  
38      public static void main(String[] args) throws Exception {
39          NamespaceChmod command = new NamespaceChmod();
40          CommandLine line = command.parse(args);
41          if (line.hasOption(OPT_HELP))
42          {
43              command.printHelpAndExit(null);
44          }
45          else
46          {
47              // get arguments
48              int mode = Integer.parseInt(command.m_nonOptionValues[ARG_MODE]);
49              URL url = URLFactory.createURL(command.m_nonOptionValues[ARG_URL]);
50  
51              // execute command
52              Session session = SessionFactory.createSession(true);
53              NSEntry entry = NSFactory.createNSEntry(session, url);
54              setUnixPermissions(entry, "user-"+entry.getOwner(), mode/10/10);
55              setUnixPermissions(entry, "group-"+entry.getGroup(), mode/10%10);
56              setUnixPermissions(entry, "*", mode%10%10);
57              entry.close();
58              System.exit(0);
59          }
60      }
61  
62      protected Options createOptions() {
63          Options opt = new Options();
64          opt.addOption(OptionBuilder.withDescription("Display this help and exit")
65                  .withLongOpt(LONGOPT_HELP)
66                  .create(OPT_HELP));
67          return opt;
68      }
69  
70      private static void setUnixPermissions(NSEntry entry, String id, int unixPerms) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
71          // convert to SAGA permissions
72          int allowed = Permission.NONE.getValue();
73          int denied = Permission.NONE.getValue();
74          if ((unixPerms & UNIX_READ) > 0) {
75              allowed = Permission.READ.or(allowed);
76          } else {
77              denied = Permission.READ.or(denied);
78          }
79          if ((unixPerms & UNIX_WRITE) > 0) {
80              allowed = Permission.WRITE.or(allowed);
81          } else {
82              denied = Permission.WRITE.or(denied);
83          }
84          if ((unixPerms & UNIX_EXEC) > 0) {
85              allowed = Permission.EXEC.or(allowed);
86          } else {
87              denied = Permission.EXEC.or(denied);
88          }
89  
90          // set permissions
91          if (allowed > Permission.NONE.getValue()) {
92              entry.permissionsAllow(id, allowed);
93          }
94          if (denied > Permission.NONE.getValue()) {
95              entry.permissionsDeny(id, allowed);
96          }
97      }
98  }