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