View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.cmdl;
21  
22  import org.apache.log4j.Logger;
23  import org.xnap.XNap;
24  import org.xnap.plugin.*;
25  
26  public class CommandLine implements Runnable {
27  
28      //--- Data Field(s) ---
29  
30      private static Logger logger = Logger.getLogger(CommandLine.class);
31      private static CommandLine singleton = null;
32  
33  	private ReadlineConsole console = new ReadlineConsole();
34      
35      //--- Constructor(s) ---
36      
37      public CommandLine()
38      {
39      }
40  
41  	//--- Method(s) ---
42  
43      /***
44       * Returns the instance of CommandLine.
45  	 *
46  	 * @return null, if the command line was not started
47       */
48      public static CommandLine getInstance()
49      {
50  		return singleton;
51      }
52      
53      public void run() 
54      {		
55  		init();
56  
57  		while (true) {
58  			String cmd = console.readln("XNap> ");
59  			if (cmd == null) {
60  				break;
61  			}
62  
63  			try {
64  				if (!Executer.parse(cmd, console)) {
65  					console.println("unknown command (h for help)");
66  				}
67  			}
68  			catch (Exception e) {
69  				logger.debug("exception during execution: " + cmd, e);
70  				console.println("error: " + e.getLocalizedMessage());
71  			}
72  		}
73      }
74  
75      /***
76       * Starts the command line.
77       */
78      public void init()
79      {
80  		Executer.add(new ExitCmd());
81  
82  		String msg
83              = "XNap " + PluginManager.getCoreVersion()
84  			+ " (http://xnap.sourceforge.net).\n\n"
85  			+ "XNap comes with ABSOLUTELY NO WARRANTY; for details "
86  			+ "type 'readme'.\n"
87  			+ "This is free software, and you are welcome to redistribute it\n"
88  			+ "under certain conditions; type 'license' for details.\n\n"
89  			+ "Ready (h for help, q to exit)\n";
90  		console.println(msg);
91      }
92  
93  	public void start()
94  	{
95  		Thread t = new Thread(this, "CommandLine");
96  		t.start();
97  	}
98  
99      public void stop()
100     {
101   		console.saveSettings();
102     }
103 
104     private class ExitCmd extends AbstractCommand
105     {
106 		public ExitCmd()
107 		{
108 			putValue(CMD, "exit");
109 			putValue(ALIASES, new String[] { "quit", "q" });
110 			putValue(SHORT_HELP, "Quits the application.");
111 		}
112 	
113 		public void execute(String command, Console console)
114 		{
115 			XNap.exit();
116 		}
117     }
118 
119 }