1 /* |
|
2 * Copyright (c) 2002-2016, the original author or authors. |
|
3 * |
|
4 * This software is distributable under the BSD license. See the terms of the |
|
5 * BSD license in the documentation provided with this software. |
|
6 * |
|
7 * http://www.opensource.org/licenses/bsd-license.php |
|
8 */ |
|
9 package jdk.internal.jline.console.internal; |
|
10 |
|
11 import jdk.internal.jline.console.ConsoleReader; |
|
12 import jdk.internal.jline.console.completer.ArgumentCompleter; |
|
13 import jdk.internal.jline.console.completer.Completer; |
|
14 import jdk.internal.jline.console.history.FileHistory; |
|
15 import jdk.internal.jline.console.history.PersistentHistory; |
|
16 import jdk.internal.jline.internal.Configuration; |
|
17 |
|
18 import java.io.File; |
|
19 import java.lang.reflect.Method; |
|
20 import java.util.ArrayList; |
|
21 import java.util.Arrays; |
|
22 import java.util.List; |
|
23 import java.util.StringTokenizer; |
|
24 |
|
25 // FIXME: Clean up API and move to jline.console.runner package |
|
26 |
|
27 /** |
|
28 * A pass-through application that sets the system input stream to a |
|
29 * {@link ConsoleReader} and invokes the specified main method. |
|
30 * |
|
31 * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a> |
|
32 * @since 2.7 |
|
33 */ |
|
34 public class ConsoleRunner |
|
35 { |
|
36 public static final String property = "jline.history"; |
|
37 |
|
38 // FIXME: This is really ugly... re-write this |
|
39 |
|
40 public static void main(final String[] args) throws Exception { |
|
41 List<String> argList = new ArrayList<String>(Arrays.asList(args)); |
|
42 if (argList.size() == 0) { |
|
43 usage(); |
|
44 return; |
|
45 } |
|
46 |
|
47 String historyFileName = System.getProperty(ConsoleRunner.property, null); |
|
48 |
|
49 String mainClass = argList.remove(0); |
|
50 ConsoleReader reader = new ConsoleReader(); |
|
51 |
|
52 if (historyFileName != null) { |
|
53 reader.setHistory(new FileHistory(new File(Configuration.getUserHome(), |
|
54 String.format(".jline-%s.%s.history", mainClass, historyFileName)))); |
|
55 } |
|
56 else { |
|
57 reader.setHistory(new FileHistory(new File(Configuration.getUserHome(), |
|
58 String.format(".jline-%s.history", mainClass)))); |
|
59 } |
|
60 |
|
61 String completors = System.getProperty(ConsoleRunner.class.getName() + ".completers", ""); |
|
62 List<Completer> completorList = new ArrayList<Completer>(); |
|
63 |
|
64 for (StringTokenizer tok = new StringTokenizer(completors, ","); tok.hasMoreTokens();) { |
|
65 @SuppressWarnings("deprecation") |
|
66 Object obj = Class.forName(tok.nextToken()).newInstance(); |
|
67 completorList.add((Completer) obj); |
|
68 } |
|
69 |
|
70 if (completorList.size() > 0) { |
|
71 reader.addCompleter(new ArgumentCompleter(completorList)); |
|
72 } |
|
73 |
|
74 ConsoleReaderInputStream.setIn(reader); |
|
75 |
|
76 try { |
|
77 Class<?> type = Class.forName(mainClass); |
|
78 Method method = type.getMethod("main", String[].class); |
|
79 String[] mainArgs = argList.toArray(new String[argList.size()]); |
|
80 method.invoke(null, (Object) mainArgs); |
|
81 } |
|
82 finally { |
|
83 // just in case this main method is called from another program |
|
84 ConsoleReaderInputStream.restoreIn(); |
|
85 if (reader.getHistory() instanceof PersistentHistory) { |
|
86 ((PersistentHistory) reader.getHistory()).flush(); |
|
87 } |
|
88 } |
|
89 } |
|
90 |
|
91 private static void usage() { |
|
92 System.out.println("Usage: \n java " + "[-Djline.history='name'] " |
|
93 + ConsoleRunner.class.getName() |
|
94 + " <target class name> [args]" |
|
95 + "\n\nThe -Djline.history option will avoid history" |
|
96 + "\nmangling when running ConsoleRunner on the same application." |
|
97 + "\n\nargs will be passed directly to the target class name."); |
|
98 } |
|
99 } |
|