Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

AbstractConsoleModule.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): Mathieu Peltier.
00023  */
00024 
00025 package org.objectweb.cjdbc.console.text.module;
00026 
00027 import java.util.ArrayList;
00028 import java.util.Hashtable;
00029 import java.util.Iterator;
00030 import java.util.LinkedList;
00031 import java.util.NoSuchElementException;
00032 import java.util.StringTokenizer;
00033 import java.util.TreeSet;
00034 
00035 import jline.SimpleCompletor;
00036 
00037 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
00038 import org.objectweb.cjdbc.console.text.Console;
00039 import org.objectweb.cjdbc.console.text.commands.ConsoleCommand;
00040 import org.objectweb.cjdbc.console.text.commands.Help;
00041 import org.objectweb.cjdbc.console.text.commands.History;
00042 import org.objectweb.cjdbc.console.text.commands.Quit;
00043 
00044 /**
00045  * This class defines a AbstractConsoleModule
00046  * 
00047  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00048  * @author <a href="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
00049  * @version 1.0
00050  */
00051 public abstract class AbstractConsoleModule
00052 {
00053   Console                  console;
00054   TreeSet                  commands;
00055   boolean                  quit;
00056   LinkedList               history;
00057   private static final int HISTORY_MAX = 10;
00058 
00059   /**
00060    * Creates a new <code>AbstractConsoleModule.java</code> object
00061    * 
00062    * @param console to refer from
00063    */
00064   public AbstractConsoleModule(Console console)
00065   {
00066     this.console = console;
00067     this.commands = new TreeSet();
00068     this.history = new LinkedList();
00069     commands.add(new Help(this));
00070     commands.add(new History(this));
00071     commands.add(new Quit(this));
00072     console.println(ConsoleTranslate.get("module.loading",
00073         getDescriptionString()));
00074     this.loadCommands();
00075     this.loadCompletor();
00076   }
00077 
00078   /**
00079    * Loads the commands for this module
00080    */
00081   protected abstract void loadCommands();
00082 
00083   protected void loadCompletor()
00084   {
00085     int size = commands.size();
00086     if (size <= 0)
00087       return;
00088     Iterator iter = commands.iterator();
00089     ArrayList list = new ArrayList(size);
00090     while (iter.hasNext())
00091     {
00092       list.add(((ConsoleCommand) iter.next()).getCommandName());
00093     }
00094     console.getConsoleReader().addCompletor(
00095         new SimpleCompletor((String[]) list.toArray(new String[size])));
00096   }
00097 
00098   /**
00099    * Text description of this module
00100    * 
00101    * @return <code>String</code> description to display
00102    */
00103   public abstract String getDescriptionString();
00104 
00105   /**
00106    * Display help for this module
00107    */
00108   public void help()
00109   {
00110     console.println(ConsoleTranslate.get("module.commands.available",
00111         getDescriptionString()));
00112     ConsoleCommand command;
00113     Iterator it = commands.iterator();
00114     while (it.hasNext())
00115     {
00116       command = (ConsoleCommand) it.next();
00117       console.printInfo(command.getCommandName() + " "
00118           + command.getCommandParameters());
00119       console.println("   " + command.getCommandDescription());
00120     }
00121   }
00122 
00123   /**
00124    * Quit this module
00125    */
00126   public void quit()
00127   {
00128     this.quit = true;
00129   }
00130 
00131   /**
00132    * Get all the commands for this module
00133    * 
00134    * @return <code>TreeSet</code> of commands (commandName|commandObject)
00135    */
00136   public TreeSet getCommands()
00137   {
00138     return commands;
00139   }
00140 
00141   /**
00142    * Get the prompt string for this module
00143    * 
00144    * @return <code>String</code> to place before prompt
00145    */
00146   public abstract String getPromptString();
00147 
00148   /**
00149    * Handle a serie of commands
00150    */
00151   public void handlePrompt()
00152   {
00153     if (quit)
00154     {
00155       console.printError(ConsoleTranslate.get("module.quitting",
00156           getDescriptionString()));
00157       return;
00158     }
00159 
00160     Hashtable hashCommands = getHashCommands();
00161 
00162     //login();
00163     quit = false;
00164     while (!quit)
00165     {
00166       try
00167       {
00168         String commandLine = console.readLine(getPromptString());
00169         if (commandLine == null)
00170         {
00171           quit = true;
00172           break;
00173         }
00174         if (commandLine.equals(""))
00175           continue;
00176         else
00177           manageHistory(commandLine);
00178 
00179         handleCommandLine(commandLine, hashCommands);
00180 
00181       }
00182       catch (Exception e)
00183       {
00184         console.printError(ConsoleTranslate.get("module.command.got.error",
00185             new Object[]{e.getClass(), e.getMessage()}), e);
00186       }
00187     }
00188   }
00189 
00190   /**
00191    * Get the list of commands as strings for this module
00192    * 
00193    * @return <code>Hashtable</code> list of <code>String</code> objects
00194    */
00195   public final Hashtable getHashCommands()
00196   {
00197     Hashtable hashCommands = new Hashtable();
00198     ConsoleCommand consoleCommand;
00199     Iterator it = commands.iterator();
00200     while (it.hasNext())
00201     {
00202       consoleCommand = (ConsoleCommand) it.next();
00203       hashCommands.put(consoleCommand.getCommandName(), consoleCommand);
00204     }
00205     return hashCommands;
00206   }
00207 
00208   /**
00209    * Handle module command
00210    * 
00211    * @param commandLine the command line to handle
00212    * @param hashCommands the list of commands available for this module
00213    * @throws Exception if fails
00214    */
00215   public final void handleCommandLine(String commandLine, Hashtable hashCommands)
00216       throws Exception
00217   {
00218     ConsoleCommand consoleCommand;
00219     StringTokenizer st = new StringTokenizer(commandLine);
00220     if (!st.hasMoreTokens())
00221     {
00222       console.printError(ConsoleTranslate.get("module.command.not.supported",
00223           ""));
00224       return;
00225     }
00226     String command = st.nextToken();
00227     consoleCommand = null; // reinit
00228     try
00229     {
00230       consoleCommand = (ConsoleCommand) hashCommands.get(command);
00231     }
00232     catch (NoSuchElementException e)
00233     {
00234       consoleCommand = null;
00235     }
00236     if (consoleCommand == null)
00237     {
00238       console.printError(ConsoleTranslate.get("module.command.not.supported",
00239           command));
00240     }
00241     else
00242     {
00243       consoleCommand.execute(commandLine.substring(command.length()));
00244     }
00245   }
00246 
00247   /**
00248    * Add the command to the history. Removes the first item in the list if the
00249    * history is too large.
00250    * 
00251    * @param command taken from the command line
00252    */
00253   public final void manageHistory(String command)
00254   {
00255     history.add(command);
00256     if (history.size() > HISTORY_MAX)
00257       history.removeFirst();
00258   }
00259 
00260   /**
00261    * Handles login in this module
00262    * 
00263    * @param params parameters to use to login in this module
00264    * @throws Exception if fails
00265    */
00266   public abstract void login(String[] params) throws Exception;
00267 
00268   /**
00269    * Get access to the console
00270    * 
00271    * @return <code>Console</code> instace
00272    */
00273   public Console getConsole()
00274   {
00275     return console;
00276   }
00277 
00278   /**
00279    * Returns the history value.
00280    * 
00281    * @return Returns the history.
00282    */
00283   public LinkedList getHistory()
00284   {
00285     return history;
00286   }
00287 }

Generated on Mon Apr 11 22:01:28 2005 for C-JDBC by  doxygen 1.3.9.1