View Javadoc

1   /*$Id: CommandlineApplication.java,v 1.1 2005/01/14 00:51:19 nw Exp $
2    * Created on 13-Jan-2005
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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 { // make this the full form of myspace reference.
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();// skip junk
129            u = tok.nextToken();
130            return astrogrid.getObjectBuilder().newIvorn(account.getCommunity(),u,s.substring(1));
131       } else  if (s.startsWith( "#")) { // error tolerant, of not qute correct syntax
132            u = tok.nextToken().substring(1);
133            return astrogrid.getObjectBuilder().newIvorn(account.getCommunity(),u,"/" + s.substring(1));
134       } else { // assume to be something else, and pass thru
135         throw new IllegalArgumentException("Didn't recognize " + s);
136       }
137    }
138 
139     
140 }
141 
142 
143 /* 
144 $Log: CommandlineApplication.java,v $
145 Revision 1.1  2005/01/14 00:51:19  nw
146 introduced java baseclass for all scripting apps -
147 captures the common code and hides some of the dirty washing.
148  
149 */