KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > module > VirtualDatabaseAdmin


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

23
24 package org.continuent.sequoia.console.text.module;
25
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.continuent.sequoia.common.i18n.ConsoleTranslate;
30 import org.continuent.sequoia.common.jmx.mbeans.ControllerMBean;
31 import org.continuent.sequoia.common.jmx.mbeans.VirtualDatabaseMBean;
32 import org.continuent.sequoia.console.text.Console;
33 import org.continuent.sequoia.console.text.ConsoleException;
34
35 /**
36  * This is the Sequoia controller console virtual database administration
37  * module.
38  *
39  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
40  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
41  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
42  * @version 1.0
43  */

44 public class VirtualDatabaseAdmin extends AbstractConsoleModule
45 {
46   private String JavaDoc virtualDbName, login, password;
47
48   /**
49    * Returns the login value.
50    *
51    * @return Returns the login.
52    */

53   public String JavaDoc getLogin()
54   {
55     return login;
56   }
57
58   /**
59    * Returns the password value.
60    *
61    * @return Returns the password.
62    */

63   public String JavaDoc getPassword()
64   {
65     return password;
66   }
67
68   /**
69    * Returns the virtualDbName value.
70    *
71    * @return Returns the virtualDbName.
72    */

73   public String JavaDoc getVirtualDbName()
74   {
75     return virtualDbName;
76   }
77
78   /**
79    * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#login(java.lang.String[])
80    */

81   public void login(String JavaDoc[] params) throws ConsoleException
82   {
83     // In case a login has failed before
84
quit = false;
85     String JavaDoc vdbName = params[0];
86       console.getConsoleReader().addCompletor(this.getCompletor());
87       console.getConsoleReader().removeCompletor(
88           console.getControllerModule().getCompletor());
89
90       if (vdbName == null || vdbName.trim().equals(""))
91       {
92         vdbName = console.readLine(ConsoleTranslate.get("admin.login.dbname"));
93         if (vdbName == null)
94           return;
95       }
96
97       login = console.readLine(ConsoleTranslate.get("admin.login.user"));
98       if (login == null)
99         return;
100
101       password = console.readPassword(ConsoleTranslate
102           .get("admin.login.password"));
103       if (password == null)
104         return;
105
106       try
107       {
108         ControllerMBean mbean = console.getJmxClient().getControllerProxy();
109         if (!mbean.getVirtualDatabaseNames().contains(vdbName))
110         {
111           throw new ConsoleException(ConsoleTranslate.get("module.database.invalid",
112               vdbName));
113         }
114         VirtualDatabaseMBean vdb = console.getJmxClient()
115             .getVirtualDatabaseProxy(vdbName, login, password);
116         if (!vdb.checkAdminAuthentication(login, password))
117         {
118           throw new ConsoleException(ConsoleTranslate.get("module.database.login.fail", login));
119         }
120       } catch (ConsoleException e)
121       {
122         quit();
123         throw e;
124       }
125       catch (Exception JavaDoc e)
126       {
127         quit();
128         throw new ConsoleException(e);
129       }
130
131       virtualDbName = vdbName;
132       // Reload commands because target has changed
133
loadCommands();
134       console.printInfo(ConsoleTranslate.get("admin.login.ready", virtualDbName));
135     }
136
137   /**
138    * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#getDescriptionString()
139    */

140   public String JavaDoc getDescriptionString()
141   {
142     return "VirtualDatabase Administration";
143   }
144
145   /**
146    * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#getPromptString()
147    */

148   public String JavaDoc getPromptString()
149   {
150     return virtualDbName + "(" + login + ")";
151   }
152
153   /**
154    * Create a <code>Set</code> of expert commands. This <code>Set</code> of
155    * commands can be added/removed dynamically to the list of admin commands
156    * with the methods {@link #addExpertCommands()} and
157    * {@link #removeExpertCommands()}
158    *
159    * @return the set of expert commands
160    */

161   private Set JavaDoc expertCommandsSet()
162   {
163     Set JavaDoc expertCmds = new HashSet JavaDoc();
164     String JavaDoc expertCommandsList = loadCommandsFromProperties("admin.expert");
165     String JavaDoc[] expertCommands = parseCommands(expertCommandsList);
166     addCommands(expertCommands, expertCmds);
167     return expertCmds;
168   }
169   
170   /**
171    * Create a <code>Set</code> of debug commands. This <code>Set</code> of
172    * commands can be added/removed dynamically to the list of admin commands
173    * with the methods {@link #addDebugCommands()} and
174    * {@link #removeDebugCommands()}
175    *
176    * @return the set of expert commands
177    */

178   private Set JavaDoc debugCommandsSet()
179   {
180     Set JavaDoc debugCmds = new HashSet JavaDoc();
181     String JavaDoc debugCommandsList = loadCommandsFromProperties("admin.debug");
182     String JavaDoc[] debugCommands = parseCommands(debugCommandsList);
183     addCommands(debugCommands, debugCmds);
184     return debugCmds;
185   }
186
187   /**
188    * Add the expert commands to the list of admin commands.
189    */

190   public void addExpertCommands()
191   {
192     commands.addAll(expertCommandsSet());
193     // reload the completor or the newly added
194
// commands won't be taken into account
195
reloadCompletor();
196   }
197
198   /**
199    * Revmoe the expert commands from the list of admin commands.
200    */

201   public void removeExpertCommands()
202   {
203     commands.removeAll(expertCommandsSet());
204     // reload the completor or the removed
205
// commands will still be taken into account
206
reloadCompletor();
207   }
208
209   /**
210    * Add the debug commands to the list of admin commands.
211    */

212   public void addDebugCommands()
213   {
214     commands.addAll(debugCommandsSet());
215     // reload the completor or the newly added
216
// commands won't be taken into account
217
reloadCompletor();
218   }
219
220   /**
221    * Revmoe the debug commands from the list of admin commands.
222    */

223   public void removeDebugCommands()
224   {
225     commands.removeAll(debugCommandsSet());
226     // reload the completor or the removed
227
// commands will still be taken into account
228
reloadCompletor();
229   }
230
231   /**
232    * Creates a new <code>VirtualDatabaseAdmin</code> instance.
233    *
234    * @param console console console
235    */

236   public VirtualDatabaseAdmin(Console console)
237   {
238     super(console);
239   }
240
241   /**
242    * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#getModuleID()
243    */

244   protected String JavaDoc getModuleID()
245   {
246     return "admin"; //$NON-NLS-1$
247
}
248 }
Popular Tags