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