1 package fr.in2p3.jsaga.command;
2
3 import fr.in2p3.jsaga.impl.namespace.AbstractNSEntryImpl;
4 import org.apache.commons.cli.*;
5 import org.ogf.saga.error.IncorrectStateException;
6 import org.ogf.saga.namespace.NSEntry;
7 import org.ogf.saga.namespace.NSFactory;
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
15
16
17
18
19
20
21
22
23
24
25 public class NamespaceTest extends AbstractCommand {
26 private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
27 private static final String OPT_EXISTS = "e", LONGOPT_EXISTS = "exist";
28 private static final String OPT_ISFILE = "f", LONGOPT_ISFILE = "file";
29 private static final String OPT_ISDIRECTORY = "d", LONGOPT_ISDIRECTORY = "directory";
30 private static final String OPT_ISLINK = "L", LONGOPT_ISLINK = "link";
31
32 private static final int FLAGS_BYPASSEXIST = 4096;
33
34 public NamespaceTest() {
35 super("jsaga-test", new String[]{"URL"}, new String[]{OPT_HELP, LONGOPT_HELP});
36 }
37
38 public static void main(String[] args) throws Exception {
39 NamespaceTest command = new NamespaceTest();
40 CommandLine line = command.parse(args);
41 if (line.hasOption(OPT_HELP))
42 {
43 command.printHelpAndExit(null);
44 }
45 else
46 {
47
48 URL url = URLFactory.createURL(command.m_nonOptionValues[0]);
49
50
51 Session session = SessionFactory.createSession(true);
52 NSEntry entry = NSFactory.createNSEntry(session, url, FLAGS_BYPASSEXIST);
53 boolean success;
54 if (line.hasOption(OPT_EXISTS)) {
55 success = ((AbstractNSEntryImpl)entry).exists();
56 } else if (line.hasOption(OPT_ISFILE)) {
57 try {
58 success = entry.isEntry();
59 } catch(IncorrectStateException e) {
60 success = false;
61 }
62 } else if (line.hasOption(OPT_ISDIRECTORY)) {
63 try {
64 success = entry.isDir();
65 } catch(IncorrectStateException e) {
66 success = false;
67 }
68 } else if (line.hasOption(OPT_ISLINK)) {
69 try {
70 success = entry.isLink();
71 } catch(IncorrectStateException e) {
72 success = false;
73 }
74 } else {
75 throw new Exception("Unexpected exception");
76 }
77 entry.close();
78
79
80 System.exit(success ? 0 : 1);
81 }
82 }
83
84 protected Options createOptions() {
85 Options opt = new Options();
86 opt.addOption(OptionBuilder.withDescription("Display this help and exit")
87 .withLongOpt(LONGOPT_HELP)
88 .create(OPT_HELP));
89
90 OptionGroup group = new OptionGroup();
91 group.addOption(OptionBuilder.withDescription("File exists")
92 .withLongOpt(LONGOPT_EXISTS)
93 .create(OPT_EXISTS));
94 group.addOption(OptionBuilder.withDescription("File exists and is a regular file")
95 .withLongOpt(LONGOPT_ISFILE)
96 .create(OPT_ISFILE));
97 group.addOption(OptionBuilder.withDescription("File exists and is a directory")
98 .withLongOpt(LONGOPT_ISDIRECTORY)
99 .create(OPT_ISDIRECTORY));
100 group.addOption(OptionBuilder.withDescription("File exists and is a symbolic link")
101 .withLongOpt(LONGOPT_ISLINK)
102 .create(OPT_ISLINK));
103 group.setRequired(true);
104 opt.addOptionGroup(group);
105
106 return opt;
107 }
108 }