001    /**
002     *      jline - Java console input library
003     *      Copyright (c) 2002,2003 Marc Prud'hommeaux mwp1@cornell.edu
004     *      
005     *      This library is free software; you can redistribute it and/or
006     *      modify it under the terms of the GNU Lesser General Public
007     *      License as published by the Free Software Foundation; either
008     *      version 2.1 of the License, or (at your option) any later version.
009     *      
010     *      This library is distributed in the hope that it will be useful,
011     *      but WITHOUT ANY WARRANTY; without even the implied warranty of
012     *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     *      Lesser General Public License for more details.
014     *      
015     *      You should have received a copy of the GNU Lesser General Public
016     *      License along with this library; if not, write to the Free Software
017     *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018     */
019    package jline;
020    
021    import java.io.*;
022    import java.util.*;
023    import java.text.MessageFormat;
024    
025    
026    /**
027     *      <p>
028     *      A pass-through application that sets the system input stream to a
029     *      {@link ConsoleReader} and invokes the specified main method.
030     *      </p>
031     *
032     *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
033     */
034    public class ConsoleRunner
035    {
036            public static void main (final String[] args)
037                    throws Exception
038            {
039                    List argList = new ArrayList (Arrays.asList (args));
040                    if (argList.size () == 0)
041                    {
042                            usage ();
043                            return;
044                    }
045    
046                    // invoke the main() method
047                    String mainClass = (String)argList.remove (0);
048    
049                    // setup the inpout stream
050                    ConsoleReader reader = new ConsoleReader ();
051                    reader.setHistory (new History (new File (
052                            System.getProperty ("user.home"), ".jline-" + mainClass
053                                    + ".history")));
054    
055                    String completors = System.getProperty (ConsoleRunner.class.getName ()
056                            + ".completors", "");
057                    List completorList = new ArrayList ();
058                    for (StringTokenizer tok = new StringTokenizer (completors, ",");
059                            tok.hasMoreTokens (); )
060                    {
061                            completorList.add ((Completor)Class.forName (tok.nextToken ())
062                                    .newInstance ());
063                    }
064    
065                    if (completorList.size () > 0)
066                            reader.addCompletor (new ArgumentCompletor (completorList));
067    
068                    ConsoleReaderInputStream.setIn (reader);
069                    try
070                    {
071                            Class.forName (mainClass)
072                                    .getMethod ("main", new Class[] { String[].class})
073                                    .invoke (null, new Object[] { argList.toArray (new String[0])});
074                    }
075                    finally
076                    {
077                            // just in case this main method is called from another program
078                            ConsoleReaderInputStream.restoreIn ();
079                    }
080            }
081    
082    
083            private static void usage ()
084            {
085                    throw new IllegalArgumentException ("Usage: java "
086                            + ConsoleRunner.class.getName ()
087                            + " <target class name> [args]");
088            }
089    }
090