01: /*
02: * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
03: *
04: * This software is distributable under the BSD license. See the terms of the
05: * BSD license in the documentation provided with this software.
06: */
07: package jline;
08:
09: import java.io.*;
10: import java.util.*;
11:
12: /**
13: * <p>
14: * A pass-through application that sets the system input stream to a
15: * {@link ConsoleReader} and invokes the specified main method.
16: * </p>
17: * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
18: */
19: public class ConsoleRunner {
20: public static final String property = "jline.history";
21:
22: public static void main(final String[] args) throws Exception {
23: String historyFileName = null;
24:
25: List argList = new ArrayList(Arrays.asList(args));
26:
27: if (argList.size() == 0) {
28: usage();
29:
30: return;
31: }
32:
33: historyFileName = System.getProperty(ConsoleRunner.property,
34: null);
35:
36: // invoke the main() method
37: String mainClass = (String) argList.remove(0);
38:
39: // setup the inpout stream
40: ConsoleReader reader = new ConsoleReader();
41:
42: if (historyFileName != null) {
43: reader.setHistory(new History(new File(System
44: .getProperty("user.home"), ".jline-" + mainClass
45: + "." + historyFileName + ".history")));
46: } else {
47: reader.setHistory(new History(new File(System
48: .getProperty("user.home"), ".jline-" + mainClass
49: + ".history")));
50: }
51:
52: String completors = System.getProperty(ConsoleRunner.class
53: .getName()
54: + ".completors", "");
55: List completorList = new ArrayList();
56:
57: for (StringTokenizer tok = new StringTokenizer(completors, ","); tok
58: .hasMoreTokens();) {
59: completorList.add((Completor) Class
60: .forName(tok.nextToken()).newInstance());
61: }
62:
63: if (completorList.size() > 0) {
64: reader.addCompletor(new ArgumentCompletor(completorList));
65: }
66:
67: ConsoleReaderInputStream.setIn(reader);
68:
69: try {
70: Class.forName(mainClass).getMethod("main",
71: new Class[] { String[].class }).invoke(null,
72: new Object[] { argList.toArray(new String[0]) });
73: } finally {
74: // just in case this main method is called from another program
75: ConsoleReaderInputStream.restoreIn();
76: }
77: }
78:
79: private static void usage() {
80: System.out
81: .println("Usage: \n java "
82: + "[-Djline.history='name'] "
83: + ConsoleRunner.class.getName()
84: + " <target class name> [args]"
85: + "\n\nThe -Djline.history option will avoid history"
86: + "\nmangling when running ConsoleRunner on the same application."
87: + "\n\nargs will be passed directly to the target class name.");
88: }
89: }
|