1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.ui.script.commandline;
12
13 import org.astrogrid.community.common.exception.CommunityIdentifierException;
14 import org.astrogrid.community.common.exception.CommunitySecurityException;
15 import org.astrogrid.community.common.exception.CommunityServiceException;
16 import org.astrogrid.community.resolver.exception.CommunityResolverException;
17 import org.astrogrid.registry.RegistryException;
18 import org.astrogrid.store.Ivorn;
19 import org.astrogrid.ui.script.AbstractAstrogridApplication;
20
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.HelpFormatter;
23 import org.apache.commons.cli.Option;
24 import org.apache.commons.cli.OptionGroup;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.Parser;
27 import org.apache.commons.cli.PosixParser;
28
29 import java.net.URISyntaxException;
30 import java.util.StringTokenizer;
31
32 /*** Base class for all groovy commandline scripts.
33 * sets up infrastructure for parsing of commandline options, logging into astrogrid, and accessing system objects.
34 * @author Noel Winstanley nw@jb.man.ac.uk 13-Jan-2005
35 *
36 */
37 public abstract class CommandlineApplication extends AbstractAstrogridApplication {
38
39 /*** to be implemented by extension script - contains main body of script.
40 * all member variables will be initialized at the point this method is called
41 * @throws Exception
42 */
43 public abstract void doIt() throws Exception;
44
45 /*** 'main' execution method - parses commandline, logs in, and hten executes {@link CommandlineApplication#doIt()},
46 * trapping and reporting any exceptions
47 * @param args
48 */
49 public final void run(String[] args) {
50 try {
51 line = parser.parse(o,args);
52 login();
53 doIt();
54 } catch (Exception e) {
55 System.out.println("An Error occurred :" + e.getClass().getName());
56 System.out.println(e.getMessage());
57 displayHelp();
58 System.exit(-1);
59 }
60 }
61 /*** display standard help on commandline options. should be overridden by extenders to provide more information */
62 public void displayHelp() {
63 (new HelpFormatter()).printHelp("",o);
64 }
65
66 /*** Construct a new CommandlineApplication
67 * sets up the options objects, registers the standard -u, -p, -c and -h options.
68 */
69 public CommandlineApplication() {
70 super();
71 o.addOption("u","user",true,"username (optional)")
72 .addOption("p","password",true,"password (optional)")
73 .addOption("c","community",true,"community (optional)")
74 .addOption("h","help",false,"Display this help.");
75 }
76
77 /*** set of commandline options that are acceptable */
78 public Options o = new Options();
79 /*** a group of mutually exclusive options */
80 public OptionGroup og = new OptionGroup();
81 /*** the commandline parser that will be used */
82 public Parser parser = new PosixParser();
83 /*** results of parsing the commandline */
84 public CommandLine line ;
85
86 /*** authenticate user in the astrogrid
87 * on success, the fields of this class that contain astogrid scripting objects are initialized.
88 * @throws CommunityResolverException
89 * @throws CommunityServiceException
90 * @throws CommunitySecurityException
91 * @throws CommunityIdentifierException
92 * @throws RegistryException
93 */
94 public final void login() throws CommunityResolverException, CommunityServiceException, CommunitySecurityException, CommunityIdentifierException, RegistryException {
95 String u=null;
96 if (line.hasOption("u")) {
97 u = line.getOptionValue("user");
98 }
99
100 String p=null;
101 if (line.hasOption("p")) {
102 p = line.getOptionValue("password");
103 }
104 String comm=null;
105 if (line.hasOption("c")) {
106 comm = line.getOptionValue("community");
107 }
108 super.login(u,comm,p);
109
110 }
111
112 /*** parse an ivorn (in full or abridged form) into an Ivorn object, and check it belongs to the current user */
113 public Ivorn mkFull(String s) throws URISyntaxException {
114 Ivorn result = _mkFull(s);
115 if (!result.getPath().equals(homeIvorn.getPath())) {
116 throw new IllegalArgumentException("Cannot access another user's myspace");
117 }
118 return result;
119 }
120
121 private Ivorn _mkFull(String s) throws URISyntaxException {
122 String u = null;
123 if (s.startsWith("ivo://")) {
124 return astrogrid.getObjectBuilder().newIvorn(s);
125 }
126 StringTokenizer tok = new StringTokenizer(s,"/");
127 if (s.startsWith("#/")) {
128 tok.nextElement();
129 u = tok.nextToken();
130 return astrogrid.getObjectBuilder().newIvorn(account.getCommunity(),u,s.substring(1));
131 } else if (s.startsWith( "#")) {
132 u = tok.nextToken().substring(1);
133 return astrogrid.getObjectBuilder().newIvorn(account.getCommunity(),u,"/" + s.substring(1));
134 } else {
135 throw new IllegalArgumentException("Didn't recognize " + s);
136 }
137 }
138
139
140 }
141
142
143
144
145
146
147
148
149