KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > commands > ConsoleCommand


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): Mathieu Peltier.
23  */

24
25 package org.objectweb.cjdbc.console.text.commands;
26
27 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
28 import org.objectweb.cjdbc.console.jmx.RmiJmxClient;
29 import org.objectweb.cjdbc.console.text.Console;
30 import org.objectweb.cjdbc.console.text.ConsoleException;
31 import org.objectweb.cjdbc.console.text.module.AbstractConsoleModule;
32
33 /**
34  * This class defines a ConsoleCommand
35  *
36  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
37  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
38  * @version 1.0
39  */

40 public abstract class ConsoleCommand implements Comparable JavaDoc
41 {
42   protected Console console;
43   protected RmiJmxClient jmxClient;
44   protected AbstractConsoleModule module;
45
46   /**
47    * Creates a new <code>ConsoleCommand.java</code> object
48    *
49    * @param module module that owns this commands
50    */

51   public ConsoleCommand(AbstractConsoleModule module)
52   {
53     this.console = module.getConsole();
54     this.module = module;
55     jmxClient = console.getJmxClient();
56   }
57
58   /**
59    * @see java.lang.Comparable#compareTo(java.lang.Object)
60    */

61   public int compareTo(Object JavaDoc o)
62   {
63     if (o instanceof ConsoleCommand)
64     {
65       ConsoleCommand c = (ConsoleCommand) o;
66       return getCommandName().compareTo(c.getCommandName());
67     }
68     else
69     {
70       throw new IllegalArgumentException JavaDoc();
71     }
72   }
73
74   /**
75    * Parse the text of the command
76    *
77    * @param commandText the command text
78    * @throws Exception if connection with the mbean server is lost or command
79    * does not have the proper format
80    */

81   public abstract void parse(String JavaDoc commandText) throws Exception JavaDoc;
82
83   /**
84    * Check if the JMX connection is still valid. Otherwise reconnect.
85    *
86    * @param commandText the parameters to execute the command with
87    * @throws Exception if fails
88    */

89   public void execute(String JavaDoc commandText) throws Exception JavaDoc
90   {
91     if (!jmxClient.isValidConnection())
92     {
93       try
94       {
95         jmxClient.reconnect();
96       }
97       catch (Exception JavaDoc e)
98       {
99         throw new ConsoleException(ConsoleTranslate
100             .get("jmx.server.connection.lost"));
101       }
102     }
103     parse(commandText);
104   }
105
106   /**
107    * Get the name of the command
108    *
109    * @return <code>String</code> of the command name
110    */

111   public abstract String JavaDoc getCommandName();
112
113   /**
114    * Return a <code>String</code> description of the parameters of this
115    * command.
116    *
117    * @return <code>String</code> like &lt;driverPathName&gt;
118    */

119   public String JavaDoc getCommandParameters()
120   {
121     return "";
122   }
123
124   /**
125    * Get the description of the command
126    *
127    * @return <code>String</code> of the command description
128    */

129   public abstract String JavaDoc getCommandDescription();
130
131   /**
132    * Get the usage of the command.
133    *
134    * @return <code>String</code> of the command usage ()
135    */

136   public String JavaDoc getUsage()
137   {
138     String JavaDoc usage = ConsoleTranslate.get("command.usage", new String JavaDoc[] {getCommandName(), getCommandParameters()});
139     usage += "\n " + getCommandDescription();
140     return usage;
141   }
142 }
143
144
Popular Tags