1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.dependencyfinder.cli; |
34 |
| |
35 |
| import java.io.*; |
36 |
| import java.util.*; |
37 |
| |
38 |
| import com.jeantessier.commandline.*; |
39 |
| import com.jeantessier.commandline.Printer; |
40 |
| import com.jeantessier.commandline.TextPrinter; |
41 |
| import com.jeantessier.dependencyfinder.*; |
42 |
| import com.jeantessier.dependency.*; |
43 |
| import org.apache.log4j.*; |
44 |
| |
45 |
| public abstract class Command { |
46 |
| public static final String DEFAULT_LOGFILE = "System.out"; |
47 |
| public static final String DEFAULT_INCLUDES = "//"; |
48 |
| |
49 |
| private CommandLine commandLine; |
50 |
| private CommandLineUsage commandLineUsage; |
51 |
| |
52 |
| private Date startTime; |
53 |
| private VerboseListener verboseListener; |
54 |
| private PrintWriter out; |
55 |
| |
56 |
16
| public String getName() {
|
57 |
16
| return getClass().getSimpleName();
|
58 |
| } |
59 |
| |
60 |
139
| private void resetCommandLine() {
|
61 |
139
| commandLine = new CommandLine(getParameterStrategy());
|
62 |
139
| populateCommandLineSwitches();
|
63 |
| } |
64 |
| |
65 |
132
| protected ParameterStrategy getParameterStrategy() {
|
66 |
132
| return new CollectingParameterStrategy();
|
67 |
| } |
68 |
| |
69 |
4640
| protected CommandLine getCommandLine() {
|
70 |
4640
| if (commandLine == null) {
|
71 |
16
| resetCommandLine();
|
72 |
| } |
73 |
| |
74 |
4640
| return commandLine;
|
75 |
| } |
76 |
| |
77 |
16
| public CommandLineUsage getCommandLineUsage() {
|
78 |
16
| if (commandLineUsage == null) {
|
79 |
16
| commandLineUsage = new CommandLineUsage(getName());
|
80 |
16
| getCommandLine().accept(commandLineUsage);
|
81 |
| } |
82 |
| |
83 |
16
| return commandLineUsage;
|
84 |
| } |
85 |
| |
86 |
0
| protected VerboseListener getVerboseListener() {
|
87 |
0
| return verboseListener;
|
88 |
| } |
89 |
| |
90 |
2
| public void run(String[] args) throws Exception {
|
91 |
2
| if (validateCommandLine(args, System.err)) {
|
92 |
2
| process();
|
93 |
| } else { |
94 |
0
| System.exit(1);
|
95 |
| } |
96 |
| } |
97 |
| |
98 |
139
| protected void populateCommandLineSwitches() {
|
99 |
139
| getCommandLine().addToggleSwitch("echo");
|
100 |
139
| getCommandLine().addToggleSwitch("help");
|
101 |
139
| getCommandLine().addSingleValueSwitch("out");
|
102 |
139
| getCommandLine().addToggleSwitch("time");
|
103 |
139
| getCommandLine().addOptionalValueSwitch("verbose", DEFAULT_LOGFILE);
|
104 |
139
| getCommandLine().addToggleSwitch("version");
|
105 |
| } |
106 |
| |
107 |
91
| protected void populateCommandLineSwitchesForXMLOutput(String defaultEncoding, String defaultDTDPrefix, String defaultIndentText) {
|
108 |
91
| getCommandLine().addSingleValueSwitch("encoding", defaultEncoding);
|
109 |
91
| getCommandLine().addSingleValueSwitch("dtd-prefix", defaultDTDPrefix);
|
110 |
91
| getCommandLine().addSingleValueSwitch("indent-text", defaultIndentText);
|
111 |
| } |
112 |
| |
113 |
123
| protected Collection<CommandLineException> parseCommandLine(String[] args) {
|
114 |
123
| resetCommandLine();
|
115 |
123
| return getCommandLine().parse(args);
|
116 |
| } |
117 |
| |
118 |
96
| protected boolean validateCommandLine(String[] args, PrintStream out) {
|
119 |
96
| boolean result = true;
|
120 |
| |
121 |
96
| Collection<CommandLineException> exceptions = parseCommandLine(args);
|
122 |
| |
123 |
96
| if (getCommandLine().getToggleSwitch("version")) {
|
124 |
16
| showVersion(out);
|
125 |
16
| result = false;
|
126 |
| } |
127 |
| |
128 |
96
| if (getCommandLine().getToggleSwitch("help")) {
|
129 |
16
| showError(out);
|
130 |
16
| result = false;
|
131 |
| } |
132 |
| |
133 |
96
| if (getCommandLine().getToggleSwitch("echo")) {
|
134 |
26
| echo(out);
|
135 |
26
| result = false;
|
136 |
| } |
137 |
| |
138 |
96
| if (result) {
|
139 |
38
| for (CommandLineException exception : exceptions) {
|
140 |
19
| result = false;
|
141 |
19
| Logger.getLogger(getClass()).error(exception);
|
142 |
| } |
143 |
| } |
144 |
| |
145 |
96
| return result;
|
146 |
| } |
147 |
| |
148 |
41
| protected Collection<CommandLineException> validateCommandLineForScoping() {
|
149 |
41
| Collection<CommandLineException> exceptions = new ArrayList<CommandLineException>();
|
150 |
| |
151 |
41
| if (hasScopeRegularExpressionSwitches() && hasScopeListSwitches()) {
|
152 |
1
| exceptions.add(new CommandLineException("You can use switches for regular expressions or lists for scope, but not at the same time"));
|
153 |
| } |
154 |
| |
155 |
41
| return exceptions;
|
156 |
| } |
157 |
| |
158 |
40
| protected Collection<CommandLineException> validateCommandLineForFiltering() {
|
159 |
40
| Collection<CommandLineException> exceptions = new ArrayList<CommandLineException>();
|
160 |
| |
161 |
40
| if (hasFilterRegularExpressionSwitches() && hasFilterListSwitches()) {
|
162 |
2
| exceptions.add(new CommandLineException("You can use switches for regular expressions or lists for filter, but not at the same time"));
|
163 |
| } |
164 |
| |
165 |
40
| return exceptions;
|
166 |
| } |
167 |
| |
168 |
2
| private void process() throws Exception {
|
169 |
2
| startProcessing();
|
170 |
2
| doProcessing();
|
171 |
2
| stopProcessing();
|
172 |
| } |
173 |
| |
174 |
2
| private void startProcessing() throws IOException {
|
175 |
2
| startVerboseListener();
|
176 |
| |
177 |
2
| startTimer();
|
178 |
| } |
179 |
| |
180 |
| protected abstract void doProcessing() throws Exception; |
181 |
| |
182 |
2
| private void stopProcessing() throws IOException {
|
183 |
2
| stopTimer();
|
184 |
2
| stopOutput();
|
185 |
2
| stopVerboseListener();
|
186 |
| } |
187 |
| |
188 |
2
| private void startVerboseListener() throws IOException {
|
189 |
2
| verboseListener = new VerboseListener();
|
190 |
2
| if (commandLine.isPresent("verbose")) {
|
191 |
0
| if (DEFAULT_LOGFILE.equals(commandLine.getOptionalSwitch("verbose"))) {
|
192 |
0
| verboseListener.setWriter(new OutputStreamWriter(System.out));
|
193 |
| } else { |
194 |
0
| verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose")));
|
195 |
| } |
196 |
| } |
197 |
| } |
198 |
| |
199 |
2
| private void stopVerboseListener() {
|
200 |
2
| verboseListener.close();
|
201 |
| } |
202 |
| |
203 |
2
| private void startTimer() {
|
204 |
2
| startTime = new Date();
|
205 |
| } |
206 |
| |
207 |
2
| private void stopTimer() {
|
208 |
2
| if (commandLine.getToggleSwitch("time")) {
|
209 |
0
| Date end = new Date();
|
210 |
0
| System.err.println(getClass().getName() + ": " + ((end.getTime() - (double) startTime.getTime()) / 1000) + " secs.");
|
211 |
| } |
212 |
| } |
213 |
| |
214 |
1
| private void startOutput() throws IOException {
|
215 |
1
| if (getCommandLine().isPresent("out")) {
|
216 |
1
| out = new PrintWriter(new FileWriter(getCommandLine().getSingleSwitch("out")));
|
217 |
| } else { |
218 |
0
| out = new PrintWriter(new OutputStreamWriter(System.out));
|
219 |
| } |
220 |
| } |
221 |
| |
222 |
2
| private void stopOutput() throws IOException {
|
223 |
2
| if (out != null) {
|
224 |
1
| out.close();
|
225 |
| } |
226 |
| } |
227 |
| |
228 |
0
| protected void echo() {
|
229 |
0
| echo(System.err);
|
230 |
| } |
231 |
| |
232 |
26
| protected void echo(PrintStream out) {
|
233 |
26
| Printer printer = new TextPrinter(getClass().getSimpleName());
|
234 |
26
| getCommandLine().accept(printer);
|
235 |
26
| out.println(printer);
|
236 |
| } |
237 |
| |
238 |
0
| protected void showError() {
|
239 |
0
| showError(System.err);
|
240 |
| } |
241 |
| |
242 |
16
| protected void showError(PrintStream out) {
|
243 |
16
| out.println(getCommandLineUsage());
|
244 |
16
| showSpecificUsage(out);
|
245 |
| } |
246 |
| |
247 |
0
| protected void showError(String msg) {
|
248 |
0
| showError(System.err, msg);
|
249 |
| } |
250 |
| |
251 |
0
| protected void showError(PrintStream out, String msg) {
|
252 |
0
| out.println(msg);
|
253 |
0
| showError(out);
|
254 |
| } |
255 |
| |
256 |
| protected abstract void showSpecificUsage(PrintStream out); |
257 |
| |
258 |
0
| protected void showVersion() {
|
259 |
0
| showVersion(System.err);
|
260 |
| } |
261 |
| |
262 |
16
| protected void showVersion(PrintStream out) {
|
263 |
16
| Version version = new Version();
|
264 |
| |
265 |
16
| out.print(version.getImplementationTitle());
|
266 |
16
| out.print(" ");
|
267 |
16
| out.print(version.getImplementationVersion());
|
268 |
16
| out.print(" (c) ");
|
269 |
16
| out.print(version.getCopyrightDate());
|
270 |
16
| out.print(" ");
|
271 |
16
| out.print(version.getCopyrightHolder());
|
272 |
16
| out.println();
|
273 |
| |
274 |
16
| out.print(version.getImplementationURL());
|
275 |
16
| out.println();
|
276 |
| |
277 |
16
| out.print("Compiled on ");
|
278 |
16
| out.print(version.getImplementationDate());
|
279 |
16
| out.println();
|
280 |
| } |
281 |
| |
282 |
37
| protected void populateCommandLineSwitchesForScoping() {
|
283 |
37
| populateRegularExpressionCommandLineSwitches("scope", true, DEFAULT_INCLUDES);
|
284 |
37
| populateListCommandLineSwitches("scope");
|
285 |
| } |
286 |
| |
287 |
43
| protected void populateCommandLineSwitchesForFiltering() {
|
288 |
43
| populateRegularExpressionCommandLineSwitches("filter", true, DEFAULT_INCLUDES);
|
289 |
43
| populateListCommandLineSwitches("filter");
|
290 |
| } |
291 |
| |
292 |
14
| protected void populateCommandLineSwitchesForStartCondition() {
|
293 |
14
| populateRegularExpressionCommandLineSwitches("start", false, DEFAULT_INCLUDES);
|
294 |
14
| populateListCommandLineSwitches("start");
|
295 |
| } |
296 |
| |
297 |
7
| protected void populateCommandLineSwitchesForStopCondition() {
|
298 |
7
| populateRegularExpressionCommandLineSwitches("stop", false, null);
|
299 |
7
| populateListCommandLineSwitches("stop");
|
300 |
| } |
301 |
| |
302 |
101
| protected void populateRegularExpressionCommandLineSwitches(String name, boolean addToggles, String defaultIncludes) {
|
303 |
101
| if (defaultIncludes != null) {
|
304 |
94
| getCommandLine().addMultipleValuesSwitch(name + "-includes", defaultIncludes);
|
305 |
| } else { |
306 |
7
| getCommandLine().addMultipleValuesSwitch(name + "-includes");
|
307 |
| } |
308 |
101
| getCommandLine().addMultipleValuesSwitch(name + "-excludes");
|
309 |
101
| getCommandLine().addMultipleValuesSwitch("package-" + name + "-includes");
|
310 |
101
| getCommandLine().addMultipleValuesSwitch("package-" + name + "-excludes");
|
311 |
101
| getCommandLine().addMultipleValuesSwitch("class-" + name + "-includes");
|
312 |
101
| getCommandLine().addMultipleValuesSwitch("class-" + name + "-excludes");
|
313 |
101
| getCommandLine().addMultipleValuesSwitch("feature-" + name + "-includes");
|
314 |
101
| getCommandLine().addMultipleValuesSwitch("feature-" + name + "-excludes");
|
315 |
| |
316 |
101
| if (addToggles) {
|
317 |
80
| getCommandLine().addToggleSwitch("package-" + name);
|
318 |
80
| getCommandLine().addToggleSwitch("class-" + name);
|
319 |
80
| getCommandLine().addToggleSwitch("feature-" + name);
|
320 |
| } |
321 |
| } |
322 |
| |
323 |
101
| protected void populateListCommandLineSwitches(String name) {
|
324 |
101
| getCommandLine().addMultipleValuesSwitch(name + "-includes-list");
|
325 |
101
| getCommandLine().addMultipleValuesSwitch(name + "-excludes-list");
|
326 |
| } |
327 |
| |
328 |
0
| protected SelectionCriteria getScopeCriteria() {
|
329 |
0
| return getSelectionCriteria("scope", new ComprehensiveSelectionCriteria());
|
330 |
| } |
331 |
| |
332 |
0
| protected SelectionCriteria getFilterCriteria() {
|
333 |
0
| return getSelectionCriteria("filter", new ComprehensiveSelectionCriteria());
|
334 |
| } |
335 |
| |
336 |
0
| protected SelectionCriteria getStartCriteria() {
|
337 |
0
| return getSelectionCriteria("start", new ComprehensiveSelectionCriteria());
|
338 |
| } |
339 |
| |
340 |
0
| protected SelectionCriteria getStopCriteria() {
|
341 |
0
| return getSelectionCriteria("stop", new NullSelectionCriteria());
|
342 |
| } |
343 |
| |
344 |
0
| protected SelectionCriteria getSelectionCriteria(String name, SelectionCriteria defaultSelectionCriteria) {
|
345 |
0
| SelectionCriteria result = defaultSelectionCriteria;
|
346 |
| |
347 |
0
| if (hasRegularExpressionSwitches(name)) {
|
348 |
0
| RegularExpressionSelectionCriteria regularExpressionFilterCriteria = new RegularExpressionSelectionCriteria();
|
349 |
| |
350 |
0
| if (getCommandLine().isPresent("package-" + name) || getCommandLine().isPresent("class-" + name) || getCommandLine().isPresent("feature-" + name)) {
|
351 |
0
| regularExpressionFilterCriteria.setMatchingPackages(getCommandLine().getToggleSwitch("package-" + name));
|
352 |
0
| regularExpressionFilterCriteria.setMatchingClasses(getCommandLine().getToggleSwitch("class-" + name));
|
353 |
0
| regularExpressionFilterCriteria.setMatchingFeatures(getCommandLine().getToggleSwitch("feature-" + name));
|
354 |
| } |
355 |
| |
356 |
0
| if (getCommandLine().isPresent(name + "-includes") || (!getCommandLine().isPresent("package-" + name + "-includes") && !getCommandLine().isPresent("class-" + name + "-includes") && !getCommandLine().isPresent("feature-" + name + "-includes"))) {
|
357 |
| |
358 |
0
| regularExpressionFilterCriteria.setGlobalIncludes(getCommandLine().getMultipleSwitch(name + "-includes"));
|
359 |
| } |
360 |
0
| regularExpressionFilterCriteria.setGlobalExcludes(getCommandLine().getMultipleSwitch(name + "-excludes"));
|
361 |
0
| regularExpressionFilterCriteria.setPackageIncludes(getCommandLine().getMultipleSwitch("package-" + name + "-includes"));
|
362 |
0
| regularExpressionFilterCriteria.setPackageExcludes(getCommandLine().getMultipleSwitch("package-" + name + "-excludes"));
|
363 |
0
| regularExpressionFilterCriteria.setClassIncludes(getCommandLine().getMultipleSwitch("class-" + name + "-includes"));
|
364 |
0
| regularExpressionFilterCriteria.setClassExcludes(getCommandLine().getMultipleSwitch("class-" + name + "-excludes"));
|
365 |
0
| regularExpressionFilterCriteria.setFeatureIncludes(getCommandLine().getMultipleSwitch("feature-" + name + "-includes"));
|
366 |
0
| regularExpressionFilterCriteria.setFeatureExcludes(getCommandLine().getMultipleSwitch("feature-" + name + "-excludes"));
|
367 |
| |
368 |
0
| result = regularExpressionFilterCriteria;
|
369 |
0
| } else if (hasListSwitches(name)) {
|
370 |
0
| result = createCollectionSelectionCriteria(getCommandLine().getMultipleSwitch(name + "-includes-list"), getCommandLine().getMultipleSwitch(name + "-excludes-list"));
|
371 |
| } |
372 |
| |
373 |
0
| return result;
|
374 |
| } |
375 |
| |
376 |
41
| protected boolean hasScopeRegularExpressionSwitches() {
|
377 |
41
| return hasRegularExpressionSwitches("scope");
|
378 |
| } |
379 |
| |
380 |
40
| protected boolean hasFilterRegularExpressionSwitches() {
|
381 |
40
| return hasRegularExpressionSwitches("filter");
|
382 |
| } |
383 |
| |
384 |
81
| protected boolean hasRegularExpressionSwitches(String name) {
|
385 |
81
| Collection<String> switches = getCommandLine().getPresentSwitches();
|
386 |
| |
387 |
81
| return
|
388 |
| switches.contains(name + "-includes") || |
389 |
| switches.contains(name + "-excludes") || |
390 |
| switches.contains("package-" + name) || |
391 |
| switches.contains("package-" + name + "-includes") || |
392 |
| switches.contains("package-" + name + "-excludes") || |
393 |
| switches.contains("class-" + name) || |
394 |
| switches.contains("class-" + name + "-includes") || |
395 |
| switches.contains("class-" + name + "-excludes") || |
396 |
| switches.contains("feature-" + name) || |
397 |
| switches.contains("feature-" + name + "-includes") || |
398 |
| switches.contains("feature-" + name + "-excludes"); |
399 |
| } |
400 |
| |
401 |
10
| protected boolean hasScopeListSwitches() {
|
402 |
10
| return hasListSwitches("scope");
|
403 |
| } |
404 |
| |
405 |
11
| protected boolean hasFilterListSwitches() {
|
406 |
11
| return hasListSwitches("filter");
|
407 |
| } |
408 |
| |
409 |
21
| protected boolean hasListSwitches(String name) {
|
410 |
21
| Collection<String> switches = getCommandLine().getPresentSwitches();
|
411 |
| |
412 |
21
| return
|
413 |
| switches.contains(name + "-includes-list") || |
414 |
| switches.contains(name + "-excludes-list"); |
415 |
| } |
416 |
| |
417 |
0
| protected CollectionSelectionCriteria createCollectionSelectionCriteria(Collection<String> includes, Collection<String> excludes) {
|
418 |
0
| return new CollectionSelectionCriteria(loadCollection(includes), loadCollection(excludes));
|
419 |
| } |
420 |
| |
421 |
0
| private Collection<String> loadCollection(Collection<String> filenames) {
|
422 |
0
| Collection<String> result = null;
|
423 |
| |
424 |
0
| if (!filenames.isEmpty()) {
|
425 |
0
| result = new HashSet<String>();
|
426 |
| |
427 |
0
| for (String filename : filenames) {
|
428 |
0
| BufferedReader reader = null;
|
429 |
0
| try {
|
430 |
0
| reader = new BufferedReader(new FileReader(filename));
|
431 |
| |
432 |
0
| String line;
|
433 |
0
| while ((line = reader.readLine()) != null) {
|
434 |
0
| result.add(line);
|
435 |
| } |
436 |
| } catch (IOException ex) { |
437 |
0
| Logger.getLogger(getClass()).error("Couldn't read file " + filename, ex);
|
438 |
| } finally { |
439 |
0
| try {
|
440 |
0
| if (reader != null) {
|
441 |
0
| reader.close();
|
442 |
| } |
443 |
| } catch (IOException ex) { |
444 |
0
| Logger.getLogger(getClass()).error("Couldn't close file " + filename, ex);
|
445 |
| } |
446 |
| } |
447 |
| } |
448 |
| } |
449 |
| |
450 |
0
| return result;
|
451 |
| } |
452 |
| |
453 |
1
| protected PrintWriter getOut() throws IOException {
|
454 |
1
| if (out == null) {
|
455 |
1
| startOutput();
|
456 |
| } |
457 |
| |
458 |
1
| return out;
|
459 |
| } |
460 |
| |
461 |
0
| protected void setOut(PrintWriter out) {
|
462 |
0
| this.out = out;
|
463 |
| } |
464 |
| } |