1
2
3
4
5
6
7 package jline;
8
9 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, null);
34
35
36 String mainClass = (String) argList.remove(0);
37
38
39 ConsoleReader reader = new ConsoleReader();
40
41 if (historyFileName != null) {
42 reader.setHistory(new History (new File
43 (System.getProperty("user.home"),
44 ".jline-" + mainClass
45 + "." + historyFileName + ".history")));
46 } else {
47 reader.setHistory(new History(new File
48 (System.getProperty("user.home"),
49 ".jline-" + mainClass + ".history")));
50 }
51
52 String completors = System.getProperty
53 (ConsoleRunner.class.getName() + ".completors", "");
54 List completorList = new ArrayList();
55
56 for (StringTokenizer tok = new StringTokenizer(completors, ",");
57 tok.hasMoreTokens();) {
58 completorList.add
59 ((Completor) Class.forName(tok.nextToken()).newInstance());
60 }
61
62 if (completorList.size() > 0) {
63 reader.addCompletor(new ArgumentCompletor(completorList));
64 }
65
66 ConsoleReaderInputStream.setIn(reader);
67
68 try {
69 Class.forName(mainClass).
70 getMethod("main", new Class[] { String[].class }).
71 invoke(null, new Object[] { argList.toArray(new String[0]) });
72 } finally {
73
74 ConsoleReaderInputStream.restoreIn();
75 }
76 }
77
78 private static void usage() {
79 System.out.println("Usage: \n java " + "[-Djline.history='name'] "
80 + ConsoleRunner.class.getName()
81 + " <target class name> [args]"
82 + "\n\nThe -Djline.history option will avoid history"
83 + "\nmangling when running ConsoleRunner on the same application."
84 + "\n\nargs will be passed directly to the target class name.");
85 }
86 }