KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > module > VirtualDatabaseAdmin


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 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): Emmanuel Cecchet.
22  * Contributor(s): Mathieu Peltier, Nicolas Modrzyk
23  */

24
25 package org.objectweb.cjdbc.console.text.module;
26
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
31 import org.objectweb.cjdbc.common.jmx.mbeans.ControllerMBean;
32 import org.objectweb.cjdbc.common.jmx.mbeans.VirtualDatabaseMBean;
33 import org.objectweb.cjdbc.console.text.Console;
34 import org.objectweb.cjdbc.console.text.commands.Help;
35 import org.objectweb.cjdbc.console.text.commands.History;
36 import org.objectweb.cjdbc.console.text.commands.Quit;
37 import org.objectweb.cjdbc.console.text.commands.dbadmin.Backup;
38 import org.objectweb.cjdbc.console.text.commands.dbadmin.CopyLogFromCheckpoint;
39 import org.objectweb.cjdbc.console.text.commands.dbadmin.DeleteLogUpToCheckpoint;
40 import org.objectweb.cjdbc.console.text.commands.dbadmin.Disable;
41 import org.objectweb.cjdbc.console.text.commands.dbadmin.DisableRead;
42 import org.objectweb.cjdbc.console.text.commands.dbadmin.Enable;
43 import org.objectweb.cjdbc.console.text.commands.dbadmin.EnableRead;
44 import org.objectweb.cjdbc.console.text.commands.dbadmin.ExpertMode;
45 import org.objectweb.cjdbc.console.text.commands.dbadmin.ForceDisable;
46 import org.objectweb.cjdbc.console.text.commands.dbadmin.ForceEnable;
47 import org.objectweb.cjdbc.console.text.commands.dbadmin.GetBackendSchema;
48 import org.objectweb.cjdbc.console.text.commands.dbadmin.GetXml;
49 import org.objectweb.cjdbc.console.text.commands.dbadmin.ListBackends;
50 import org.objectweb.cjdbc.console.text.commands.dbadmin.RemoveDump;
51 import org.objectweb.cjdbc.console.text.commands.dbadmin.Replicate;
52 import org.objectweb.cjdbc.console.text.commands.dbadmin.Restore;
53 import org.objectweb.cjdbc.console.text.commands.dbadmin.SetCheckpoint;
54 import org.objectweb.cjdbc.console.text.commands.dbadmin.ShowBackend;
55 import org.objectweb.cjdbc.console.text.commands.dbadmin.ShowControllers;
56 import org.objectweb.cjdbc.console.text.commands.dbadmin.TransferBackend;
57 import org.objectweb.cjdbc.console.text.commands.dbadmin.TransferDump;
58 import org.objectweb.cjdbc.console.text.commands.dbadmin.UpdateDumpPath;
59 import org.objectweb.cjdbc.console.text.commands.dbadmin.ViewBackupers;
60 import org.objectweb.cjdbc.console.text.commands.dbadmin.ViewCheckpointNames;
61 import org.objectweb.cjdbc.console.text.commands.dbadmin.ViewDumps;
62
63 /**
64  * This is the C-JDBC controller console virtual database administration module.
65  *
66  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
67  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
68  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
69  * @version 1.0
70  */

71 public class VirtualDatabaseAdmin extends AbstractConsoleModule
72 {
73   private String JavaDoc virtualDbName, login, password;
74   
75   /**
76    * Returns the login value.
77    *
78    * @return Returns the login.
79    */

80   public String JavaDoc getLogin()
81   {
82     return login;
83   }
84
85   /**
86    * Returns the password value.
87    *
88    * @return Returns the password.
89    */

90   public String JavaDoc getPassword()
91   {
92     return password;
93   }
94
95   /**
96    * Returns the virtualDbName value.
97    *
98    * @return Returns the virtualDbName.
99    */

100   public String JavaDoc getVirtualDbName()
101   {
102     return virtualDbName;
103   }
104
105   /**
106    * @see org.objectweb.cjdbc.console.text.module.AbstractConsoleModule#login(java.lang.String[])
107    */

108   public void login(String JavaDoc[] params)
109   {
110     // In case a login has failed before
111
quit = false;
112     String JavaDoc vdbName = params[0];
113     try
114     {
115       console.getConsoleReader().addCompletor(this.getCompletor());
116       console.getConsoleReader().removeCompletor(
117           console.getControllerModule().getCompletor());
118       
119       if (vdbName == null || vdbName.trim().equals(""))
120       {
121         vdbName = console.readLine(ConsoleTranslate.get("admin.login.dbname"));
122         if (vdbName == null)
123           return;
124       }
125
126       login = console.readLine(ConsoleTranslate.get("admin.login.user"));
127       if (login == null)
128         return;
129
130       password = console.readPassword(ConsoleTranslate
131           .get("admin.login.password"));
132       if (password == null)
133         return;
134
135       try
136       {
137         ControllerMBean mbean = console.getJmxClient().getControllerProxy();
138         if (!mbean.hasVirtualDatabase(vdbName))
139         {
140           console.printError(ConsoleTranslate.get("module.database.invalid",
141               vdbName));
142           quit();
143           return;
144         }
145       }
146       catch (Exception JavaDoc e)
147       {
148         e.printStackTrace();
149         // does not exists: quit
150
console.printError(ConsoleTranslate.get("module.database.invalid",
151             vdbName), e);
152         quit();
153       }
154       try
155       {
156         VirtualDatabaseMBean vdb = console.getJmxClient()
157             .getVirtualDatabaseProxy(vdbName, login, password);
158         if (!vdb.checkAdminAuthentication(login, password))
159         {
160           console.printError(ConsoleTranslate.get("module.database.login.fail",
161               login));
162           quit();
163         }
164       }
165       catch (Exception JavaDoc e)
166       {
167         console.printError(ConsoleTranslate.get("module.database.login.fail",
168             login));
169         quit();
170       }
171       this.virtualDbName = vdbName;
172       if (quit)
173         return;
174
175       // Reload commands because target has changed
176
loadCommands();
177       console.println(ConsoleTranslate.get("admin.login.ready", virtualDbName));
178     }
179     catch (Exception JavaDoc e)
180     {
181       console.printError(e.getMessage(), e);
182       quit();
183     }
184
185   }
186
187   /**
188    * @see org.objectweb.cjdbc.console.text.module.AbstractConsoleModule#getDescriptionString()
189    */

190   public String JavaDoc getDescriptionString()
191   {
192     return "VirtualDatabase Administration";
193   }
194
195   /**
196    * @see org.objectweb.cjdbc.console.text.module.AbstractConsoleModule#getPromptString()
197    */

198   public String JavaDoc getPromptString()
199   {
200     return virtualDbName + "(" + login + ")";
201   }
202
203   /**
204    * @see org.objectweb.cjdbc.console.text.module.AbstractConsoleModule#loadCommands()
205    */

206   protected void loadCommands()
207   {
208     commands.clear();
209     commands.add(new Help(this));
210     commands.add(new History(this));
211     commands.add(new Quit(this));
212     commands.add(new Backup(this));
213     commands.add(new Disable(this));
214     commands.add(new Enable(this));
215     commands.add(new ExpertMode(this));
216     commands.add(new GetXml(this));
217     commands.add(new ListBackends(this));
218     commands.add(new RemoveDump(this));
219     commands.add(new Restore(this));
220     commands.add(new ShowBackend(this));
221     commands.add(new ShowControllers(this));
222     commands.add(new TransferDump(this));
223     commands.add(new ViewBackupers(this));
224     commands.add(new ViewDumps(this));
225   }
226
227   /**
228    * Create a <code>Set</code> of expert commands.
229    * This <code>Set</code> of commands can be added/removed dynamically
230    * to the list of admin commands with the methods {@link addExpertCommands()}
231    * and {@link removeExpertCommands()}
232    * @return
233    */

234   private Set JavaDoc expertCommandsSet()
235   {
236     Set JavaDoc expertCmds = new HashSet JavaDoc();
237     expertCmds.add(new CopyLogFromCheckpoint(this));
238     expertCmds.add(new DeleteLogUpToCheckpoint(this));
239     expertCmds.add(new DisableRead(this));
240     expertCmds.add(new EnableRead(this));
241     expertCmds.add(new ForceEnable(this));
242     expertCmds.add(new ForceDisable(this));
243     expertCmds.add(new GetBackendSchema(this));
244     expertCmds.add(new Replicate(this));
245     expertCmds.add(new SetCheckpoint(this));
246     expertCmds.add(new TransferBackend(this));
247     expertCmds.add(new UpdateDumpPath(this));
248     expertCmds.add(new ViewCheckpointNames(this));
249     return expertCmds;
250   }
251   
252   /**
253    * Add the expert commands to the list of admin commands.
254    *
255    */

256   public void addExpertCommands()
257   {
258     commands.addAll(expertCommandsSet());
259     // reload the completor or the newly added
260
// commands won't be taken into account
261
reloadCompletor();
262   }
263
264   /**
265    * Revmoe the expert commands from the list of admin commands.
266    *
267    */

268   public void removeExpertCommands()
269   {
270     commands.removeAll(expertCommandsSet());
271     // reload the completor or the removed
272
// commands will still be taken into account
273
reloadCompletor();
274   }
275
276   /**
277    * Creates a new <code>VirtualDatabaseAdmin</code> instance.
278    *
279    * @param console console console
280    */

281   public VirtualDatabaseAdmin(Console console)
282   {
283     super(console);
284   }
285 }
Popular Tags