1/*
2 * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
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 */
7package jline;
8
9import java.io.*;
10import 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 */
19public class ConsoleRunner {
20    private static ConsoleReader reader;
21
22    public static ConsoleReader getReader() { return reader; }
23
24    public static final String property = "jline.history";
25
26    public static void main(final String[] args) throws Exception {
27        String historyFileName = null;
28
29        List argList = new ArrayList(Arrays.asList(args));
30
31        if (argList.size() == 0) {
32            usage();
33
34            return;
35        }
36
37        historyFileName = System.getProperty(ConsoleRunner.property, null);
38
39        // invoke the main() method
40        String mainClass = (String) argList.remove(0);
41
42        // setup the inpout stream
43        reader = new ConsoleReader();
44
45        if (historyFileName != null) {
46            reader.setHistory(new History (new File
47                (System.getProperty("user.home"),
48                    ".jline-" + mainClass
49                        + "." + historyFileName + ".history")));
50        } else {
51            reader.setHistory(new History(new File
52                (System.getProperty("user.home"),
53                    ".jline-" + mainClass + ".history")));
54        }
55
56        String completors = System.getProperty
57            (ConsoleRunner.class.getName() + ".completors", "");
58        List completorList = new ArrayList();
59
60        for (StringTokenizer tok = new StringTokenizer(completors, ",");
61            tok.hasMoreTokens();) {
62            completorList.add
63                ((Completor) Class.forName(tok.nextToken()).newInstance());
64        }
65
66        if (completorList.size() > 0) {
67            reader.addCompletor(new ArgumentCompletor(completorList));
68        }
69
70        ConsoleReaderInputStream.setIn(reader);
71
72        try {
73            Class.forName(mainClass).
74                getMethod("main", new Class[] { String[].class }).
75                invoke(null, new Object[] { argList.toArray(new String[0]) });
76        } finally {
77            // just in case this main method is called from another program
78            ConsoleReaderInputStream.restoreIn();
79        }
80    }
81
82    private static void usage() {
83        System.out.println("Usage: \n   java " + "[-Djline.history='name'] "
84            + ConsoleRunner.class.getName()
85            + " <target class name> [args]"
86            + "\n\nThe -Djline.history option will avoid history"
87            + "\nmangling when running ConsoleRunner on the same application."
88            + "\n\nargs will be passed directly to the target class name.");
89    }
90}
91