KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > commands > ConsoleCommand


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk.
20  * Contributor(s): Mathieu Peltier.
21  */

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

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

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

59   public int compareTo(Object JavaDoc o)
60   {
61     if (o instanceof ConsoleCommand)
62     {
63       ConsoleCommand c = (ConsoleCommand) o;
64       return getCommandName().compareTo(c.getCommandName());
65     }
66     else
67     {
68       throw new IllegalArgumentException JavaDoc();
69     }
70   }
71
72   /**
73    * Check that a JMX connection is established with a controller. If no
74    * connection is available, a ConsoleException is thrown indicating that the
75    * console must be connected to a controller in order to execute the command.
76    *
77    * @throws ConsoleException if no JMX connection is available
78    */

79   protected void checkJmxConnectionToController() throws ConsoleException
80   {
81     // Force a refresh of jmxClient in case we have reconnected to another
82
// controller
83
jmxClient = console.getJmxClient();
84
85     if (jmxClient == null)
86       throw new ConsoleException(ConsoleTranslate
87           .get("console.command.requires.connection")); //$NON-NLS-1$
88
}
89
90   /**
91    * Parse the text of the command
92    *
93    * @param commandText the command text
94    * @throws Exception if connection with the mbean server is lost or command
95    * does not have the proper format
96    */

97   public abstract void parse(String JavaDoc commandText) throws Exception JavaDoc;
98
99   /**
100    * Check if the JMX connection is still valid. Otherwise reconnect.
101    *
102    * @param commandText the parameters to execute the command with
103    * @throws Exception if fails
104    */

105   public void execute(String JavaDoc commandText) throws Exception JavaDoc
106   {
107     parse(commandText);
108   }
109
110   /**
111    * Get the name of the command
112    *
113    * @return <code>String</code> of the command name
114    */

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

123   public String JavaDoc getCommandParameters()
124   {
125     return ""; //$NON-NLS-1$
126
}
127
128   /**
129    * Get the description of the command
130    *
131    * @return <code>String</code> of the command description
132    */

133   public abstract String JavaDoc getCommandDescription();
134
135   /**
136    * Get the usage of the command.
137    *
138    * @return <code>String</code> of the command usage ()
139    */

140   public String JavaDoc getUsage()
141   {
142     String JavaDoc usage = ConsoleTranslate
143         .get(
144             "command.usage", new String JavaDoc[]{getCommandName(), getCommandParameters()}); //$NON-NLS-1$
145
usage += "\n " + getCommandDescription(); //$NON-NLS-1$
146
return usage;
147   }
148 }
149
Popular Tags