1 /**
2 * jline - Java console input library
3 * Copyright (c) 2002, 2003, 2004, 2005, Marc Prud'hommeaux <mwp1@cornell.edu>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with
16 * the distribution.
17 *
18 * Neither the name of JLine nor the names of its contributors
19 * may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 package jline;
37
38 import java.io.*;
39 import java.util.*;
40
41 /**
42 * <p>
43 * A pass-through application that sets the system input stream to a
44 * {@link ConsoleReader} and invokes the specified main method.
45 * </p>
46 *
47 * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
48 */
49 public class ConsoleRunner
50 {
51 public static final String property = "jline.history";
52
53 public static void main (final String[] args)
54 throws Exception
55 {
56 String historyFileName = null;
57
58 List argList = new ArrayList (Arrays.asList (args));
59 if (argList.size () == 0)
60 {
61 usage ();
62 return;
63 }
64
65 historyFileName = System.getProperty(ConsoleRunner.property, null);
66
67
68 String mainClass = (String)argList.remove (0);
69
70
71 ConsoleReader reader = new ConsoleReader ();
72 if (historyFileName != null)
73 {
74 reader.setHistory (new History (new File (
75 System.getProperty ("user.home"), ".jline-" + mainClass
76 + "." + historyFileName + ".history")));
77 }
78 else
79 {
80 reader.setHistory (new History (new File (
81 System.getProperty ("user.home"), ".jline-" + mainClass
82 + ".history")));
83 }
84
85 String completors = System.getProperty (ConsoleRunner.class.getName ()
86 + ".completors", "");
87 List completorList = new ArrayList ();
88 for (StringTokenizer tok = new StringTokenizer (completors, ",");
89 tok.hasMoreTokens (); )
90 {
91 completorList.add ((Completor)Class.forName (tok.nextToken ())
92 .newInstance ());
93 }
94
95 if (completorList.size () > 0)
96 reader.addCompletor (new ArgumentCompletor (completorList));
97
98 ConsoleReaderInputStream.setIn (reader);
99 try
100 {
101 Class.forName (mainClass)
102 .getMethod ("main", new Class[] { String[].class})
103 .invoke (null, new Object[] { argList.toArray (new String[0])});
104 }
105 finally
106 {
107
108 ConsoleReaderInputStream.restoreIn ();
109 }
110 }
111
112
113 private static void usage ()
114 {
115 System.out.println ("Usage: \n java "
116 + "[-Djline.history='name'] "
117 + ConsoleRunner.class.getName ()
118 + " <target class name> [args]"
119 + "\n\nThe -Djline.history option will avoid history"
120 + "\nmangling when running ConsoleRunner on the same application."
121 + "\n\nargs will be passed directly to the target class name.");
122 }
123 }
124