1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.cli; |
19 | |
|
20 | |
import org.apache.any23.plugin.Any23PluginManager; |
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.lang.annotation.ElementType; |
25 | |
import java.lang.annotation.Retention; |
26 | |
import java.lang.annotation.RetentionPolicy; |
27 | |
import java.lang.annotation.Target; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
@ToolRunner.Skip |
37 | 0 | public class ToolRunner { |
38 | |
|
39 | 0 | public static final File HOME_PLUGIN_DIR = new File( |
40 | |
new File(System.getProperty("user.home")), |
41 | |
".any23/plugins" |
42 | |
); |
43 | |
|
44 | 0 | private static final String USAGE = String.format( |
45 | |
"Usage: %s <utility> [options...]", |
46 | |
ToolRunner.class.getSimpleName() |
47 | |
); |
48 | |
|
49 | |
public static void main(String[] args) throws IOException { |
50 | |
|
51 | 0 | final Class<Tool>[] tools = getToolsInClasspath(); |
52 | |
try { |
53 | 0 | if (args.length < 1) { |
54 | 0 | usage(null, tools); |
55 | |
} |
56 | |
|
57 | 0 | final String toolName = args[0]; |
58 | 0 | Class<Tool> targetTool = null; |
59 | 0 | for(Class<Tool> tool : tools) { |
60 | 0 | if(tool.getSimpleName().equals(toolName)) { |
61 | 0 | targetTool = tool; |
62 | 0 | break; |
63 | |
} |
64 | |
} |
65 | 0 | if(targetTool == null) { |
66 | 0 | usage( String.format("[%s] is not a valid tool name.", toolName), tools); |
67 | 0 | throw new IllegalStateException(); |
68 | |
} |
69 | |
|
70 | 0 | String[] mainArgs = new String[args.length - 1]; |
71 | 0 | System.arraycopy(args, 1, mainArgs, 0, mainArgs.length); |
72 | 0 | final Tool targetToolInstance = targetTool.newInstance(); |
73 | 0 | targetToolInstance.run(mainArgs); |
74 | 0 | } catch (Throwable e) { |
75 | 0 | e.printStackTrace(); |
76 | 0 | Throwable cause = e.getCause(); |
77 | 0 | if(cause != null) cause.printStackTrace(); |
78 | 0 | usage(e.toString(), null); |
79 | 0 | } |
80 | 0 | } |
81 | |
|
82 | |
public static Class<Tool>[] getToolsInClasspath() throws IOException { |
83 | 0 | final Any23PluginManager pluginManager = Any23PluginManager.getInstance(); |
84 | 0 | if(HOME_PLUGIN_DIR.exists()) { |
85 | 0 | pluginManager.loadJARDir(HOME_PLUGIN_DIR); |
86 | |
} |
87 | 0 | return pluginManager.getTools(); |
88 | |
} |
89 | |
|
90 | |
private static String padLeft(String s, int n) { |
91 | 0 | return String.format("%1$#" + n + "s", s); |
92 | |
} |
93 | |
|
94 | |
private static String getUtilitiesMessage(Class<Tool>[] toolClasses) { |
95 | 0 | StringBuffer sb = new StringBuffer(); |
96 | 0 | sb.append(" where <utility> is one of:\n"); |
97 | |
Description description; |
98 | |
String utilityName; |
99 | |
int padding; |
100 | 0 | for (Class<Tool> toolClass : toolClasses) { |
101 | 0 | utilityName = toolClass.getSimpleName(); |
102 | 0 | sb.append("\t").append(utilityName); |
103 | 0 | description = toolClass.getAnnotation(Description.class); |
104 | 0 | padding = 100 - utilityName.length(); |
105 | 0 | if (description != null) { |
106 | 0 | sb.append( padLeft( description.value(), padding >= 0 ? padding : 0) ); |
107 | |
} |
108 | 0 | sb.append('\n'); |
109 | |
} |
110 | 0 | return sb.toString(); |
111 | |
} |
112 | |
|
113 | |
private static void usage(String msg, Class<Tool>[] utilities) { |
114 | 0 | if(msg != null) { |
115 | 0 | System.err.println("*** ERROR: " + msg); |
116 | 0 | System.err.println(); |
117 | |
} |
118 | 0 | System.err.println(USAGE); |
119 | 0 | if(utilities != null) { |
120 | 0 | System.err.println( getUtilitiesMessage(utilities) ); |
121 | |
} |
122 | 0 | System.exit(1); |
123 | 0 | } |
124 | |
|
125 | |
@Retention(RetentionPolicy.RUNTIME) |
126 | |
@Target(ElementType.TYPE) |
127 | |
public @interface Skip {} |
128 | |
|
129 | 0 | @Retention(RetentionPolicy.RUNTIME) |
130 | |
@Target(ElementType.TYPE) |
131 | |
public @interface Description { String value(); } |
132 | |
|
133 | |
} |
134 | |
|