001    /*
002     * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
003     *
004     * This software is distributable under the BSD license. See the terms of the
005     * BSD license in the documentation provided with this software.
006     */
007    package jline;
008    
009    import java.io.*;
010    import java.util.*;
011    
012    /**
013     *  <p>
014     *  A pass-through application that sets the system input stream to a
015     *  {@link ConsoleReader} and invokes the specified main method.
016     *  </p>
017     *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
018     */
019    public class ConsoleRunner {
020        private static ConsoleReader reader;
021    
022        public static ConsoleReader getReader() { return reader; }
023    
024        public static final String property = "jline.history";
025    
026        public static void main(final String[] args) throws Exception {
027            String historyFileName = null;
028    
029            List argList = new ArrayList(Arrays.asList(args));
030    
031            if (argList.size() == 0) {
032                usage();
033    
034                return;
035            }
036    
037            historyFileName = System.getProperty(ConsoleRunner.property, null);
038    
039            // invoke the main() method
040            String mainClass = (String) argList.remove(0);
041    
042            // setup the inpout stream
043            reader = new ConsoleReader();
044    
045            if (historyFileName != null) {
046                reader.setHistory(new History (new File
047                    (System.getProperty("user.home"),
048                        ".jline-" + mainClass
049                            + "." + historyFileName + ".history")));
050            } else {
051                reader.setHistory(new History(new File
052                    (System.getProperty("user.home"),
053                        ".jline-" + mainClass + ".history")));
054            }
055    
056            String completors = System.getProperty
057                (ConsoleRunner.class.getName() + ".completors", "");
058            List completorList = new ArrayList();
059    
060            for (StringTokenizer tok = new StringTokenizer(completors, ",");
061                tok.hasMoreTokens();) {
062                completorList.add
063                    ((Completor) Class.forName(tok.nextToken()).newInstance());
064            }
065    
066            if (completorList.size() > 0) {
067                reader.addCompletor(new ArgumentCompletor(completorList));
068            }
069    
070            ConsoleReaderInputStream.setIn(reader);
071    
072            try {
073                Class.forName(mainClass).
074                    getMethod("main", new Class[] { String[].class }).
075                    invoke(null, new Object[] { argList.toArray(new String[0]) });
076            } finally {
077                // just in case this main method is called from another program
078                ConsoleReaderInputStream.restoreIn();
079            }
080        }
081    
082        private static void usage() {
083            System.out.println("Usage: \n   java " + "[-Djline.history='name'] "
084                + ConsoleRunner.class.getName()
085                + " <target class name> [args]"
086                + "\n\nThe -Djline.history option will avoid history"
087                + "\nmangling when running ConsoleRunner on the same application."
088                + "\n\nargs will be passed directly to the target class name.");
089        }
090    }