1
2
3
4
5
6 package gov.bnl.gums.command;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.lang.reflect.InvocationTargetException;
12 import java.lang.reflect.Method;
13
14 import java.util.Hashtable;
15 import java.util.Iterator;
16 import java.util.Map;
17 import java.util.SortedMap;
18 import java.util.TreeMap;
19 import gov.bnl.gums.admin.*;
20
21
22 /***
23 * @author carcassi
24 */
25 public class GUMSCommandLine {
26 private static Log log = LogFactory.getLog(GUMSCommandLine.class);
27 private static Map commands = new Hashtable();
28 private static SortedMap commandDescriptions = new TreeMap();
29 public static String command = "gums";
30
31 public static void addCommand(String className, String description) {
32 String command = CommandLineToolkit.getCommandName(className);
33
34 commands.put(command, className);
35 commandDescriptions.put(command, description);
36 }
37
38 public static void clearCommands() {
39 commands = new Hashtable();
40 }
41
42 static void printHelp() {
43 System.out.println("usage: " + command + " command [command-options] ");
44 System.out.println("Commands:");
45
46 Iterator iter = commandDescriptions.keySet().iterator();
47
48 while (iter.hasNext()) {
49 String command = (String) iter.next();
50
51 System.out.println(" " + command + " - " +
52 commandDescriptions.get(command));
53 }
54
55 System.out.println("For help on any command:");
56 System.out.println(" " + command + " command --help");
57 }
58
59 /***
60 * @param args the command line arguments
61 */
62 public static void main(String[] args) {
63 if (args.length == 0) {
64 printHelp();
65 System.exit(1);
66 }
67
68 String command = args[0];
69
70 if (commands.get(command) == null) {
71 System.out.println("Unknown command '" + command + "'");
72 printHelp();
73 System.exit(-1);
74 }
75
76 String[] newArgs = new String[args.length - 1];
77
78 System.arraycopy(args, 1, newArgs, 0, newArgs.length);
79
80 int retCode = runCommand(command, newArgs);
81
82 System.exit(retCode);
83 }
84
85 static int runCommand(String command, String[] args) {
86 String className = (String) commands.get(command);
87
88 if (className == null) {
89 System.out.println("Unknown command '" + command + "'");
90 printHelp();
91
92 return -1;
93 }
94
95 try {
96 Class clazz = Class.forName(className);
97 Method main = clazz.getMethod("main",
98 new Class[] { args.getClass() });
99
100 main.invoke(null, new Object[] { args });
101
102 return 0;
103 } catch (ClassNotFoundException e) {
104 System.out.println("Internal error: command class '" + className +
105 "' not found.");
106
107 return -1;
108 } catch (NoSuchMethodException e) {
109 System.out.println("Internal error: command class '" + className +
110 "' doesn't have a main method.");
111
112 return -1;
113 } catch (IllegalAccessException e) {
114 System.out.println(
115 "Internal error: main method for command class '" + className +
116 "' is not accessible (check it it's public).");
117
118 return -1;
119 } catch (IllegalArgumentException e) {
120 System.out.println(
121 "Internal error: illegal argument for main method of command class '" +
122 className + "'.");
123
124 return -1;
125 } catch (InvocationTargetException e) {
126 System.out.println("Command terminated unexpectedly: " +
127 e.getTargetException());
128 log.error("Command terminated unexpectedly", e.getTargetException());
129
130 return -1;
131 }
132 }
133 }