    001    /**
    002     *      jline - Java console input library
    003     *      Copyright (c) 2002, 2003, 2004, 2005, Marc Prud'hommeaux <mwp1@cornell.edu>
    004     *      All rights reserved.
    005     *
    006     *      Redistribution and use in source and binary forms, with or
    007     *      without modification, are permitted provided that the following
    008     *      conditions are met:
    009     *
    010     *      Redistributions of source code must retain the above copyright
    011     *      notice, this list of conditions and the following disclaimer.
    012     *
    013     *      Redistributions in binary form must reproduce the above copyright
    014     *      notice, this list of conditions and the following disclaimer
    015     *      in the documentation and/or other materials provided with
    016     *      the distribution.
    017     *
    018     *      Neither the name of JLine nor the names of its contributors
    019     *      may be used to endorse or promote products derived from this
    020     *      software without specific prior written permission.
    021     *
    022     *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    023     *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
    024     *      BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    025     *      AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
    026     *      EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
    027     *      FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    028     *      OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    029     *      PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    030     *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    031     *      AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    032     *      LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    033     *      IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    034     *      OF THE POSSIBILITY OF SUCH DAMAGE.
    035     */
    036    package jline;
    037
    038    import java.io.*;
    039    import java.util.*;
    040
    041    /**
    042     *      <p>
    043     *      A pass-through application that sets the system input stream to a
    044     *      {@link ConsoleReader} and invokes the specified main method.
    045     *      </p>
    046     *
    047     *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
    048     */
    049    public class ConsoleRunner
    050    {
    051            public static final String property = "jline.history";
    052
    053            public static void main (final String[] args)
    054                    throws Exception
    055            {
    056                    String historyFileName = null;
    057
    058                    List argList = new ArrayList (Arrays.asList (args));
    059                    if (argList.size () == 0)
    060                    {
    061                            usage ();
    062                            return;
    063                    }
    064
    065                    historyFileName = System.getProperty(ConsoleRunner.property, null);
    066
    067                    // invoke the main() method
    068                    String mainClass = (String)argList.remove (0);
    069
    070                    // setup the inpout stream
    071                    ConsoleReader reader = new ConsoleReader ();
    072                    if (historyFileName != null)
    073                    {
    074                            reader.setHistory (new History (new File (
    075                                            System.getProperty ("user.home"), ".jline-" + mainClass
    076                                            + "." + historyFileName + ".history")));
    077                    }
    078                    else
    079                    {
    080                            reader.setHistory (new History (new File (
    081                                    System.getProperty ("user.home"), ".jline-" + mainClass
    082                                            + ".history")));
    083                    }
    084
    085                    String completors = System.getProperty (ConsoleRunner.class.getName ()
    086                            + ".completors", "");
    087                    List completorList = new ArrayList ();
    088                    for (StringTokenizer tok = new StringTokenizer (completors, ",");
    089                            tok.hasMoreTokens (); )
    090                    {
    091                            completorList.add ((Completor)Class.forName (tok.nextToken ())
    092                                    .newInstance ());
    093                    }
    094
    095                    if (completorList.size () > 0)
    096                            reader.addCompletor (new ArgumentCompletor (completorList));
    097
    098                    ConsoleReaderInputStream.setIn (reader);
    099                    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                            // just in case this main method is called from another program
    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