001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Emmanuel Cecchet.
021: * Contributor(s): Mathieu Peltier, Nicolas Modrzyk
022: */package org.continuent.sequoia.console.text.module;
023:
024: import java.util.HashSet;
025: import java.util.Set;
026:
027: import org.continuent.sequoia.common.i18n.ConsoleTranslate;
028: import org.continuent.sequoia.common.jmx.mbeans.ControllerMBean;
029: import org.continuent.sequoia.common.jmx.mbeans.VirtualDatabaseMBean;
030: import org.continuent.sequoia.console.text.Console;
031: import org.continuent.sequoia.console.text.ConsoleException;
032:
033: /**
034: * This is the Sequoia controller console virtual database administration
035: * module.
036: *
037: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
038: * @author <a href="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
039: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
040: * @version 1.0
041: */
042: public class VirtualDatabaseAdmin extends AbstractConsoleModule {
043: private String virtualDbName, login, password;
044:
045: /**
046: * Returns the login value.
047: *
048: * @return Returns the login.
049: */
050: public String getLogin() {
051: return login;
052: }
053:
054: /**
055: * Returns the password value.
056: *
057: * @return Returns the password.
058: */
059: public String getPassword() {
060: return password;
061: }
062:
063: /**
064: * Returns the virtualDbName value.
065: *
066: * @return Returns the virtualDbName.
067: */
068: public String getVirtualDbName() {
069: return virtualDbName;
070: }
071:
072: /**
073: * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#login(java.lang.String[])
074: */
075: public void login(String[] params) throws ConsoleException {
076: // In case a login has failed before
077: quit = false;
078: String vdbName = params[0];
079: console.getConsoleReader().addCompletor(this .getCompletor());
080: console.getConsoleReader().removeCompletor(
081: console.getControllerModule().getCompletor());
082:
083: if (vdbName == null || vdbName.trim().equals("")) {
084: vdbName = console.readLine(ConsoleTranslate
085: .get("admin.login.dbname"));
086: if (vdbName == null)
087: return;
088: }
089:
090: login = console.readLine(ConsoleTranslate
091: .get("admin.login.user"));
092: if (login == null)
093: return;
094:
095: password = console.readPassword(ConsoleTranslate
096: .get("admin.login.password"));
097: if (password == null)
098: return;
099:
100: try {
101: ControllerMBean mbean = console.getJmxClient()
102: .getControllerProxy();
103: if (!mbean.getVirtualDatabaseNames().contains(vdbName)) {
104: throw new ConsoleException(ConsoleTranslate.get(
105: "module.database.invalid", vdbName));
106: }
107: VirtualDatabaseMBean vdb = console.getJmxClient()
108: .getVirtualDatabaseProxy(vdbName, login, password);
109: if (!vdb.checkAdminAuthentication(login, password)) {
110: throw new ConsoleException(ConsoleTranslate.get(
111: "module.database.login.fail", login));
112: }
113: } catch (ConsoleException e) {
114: quit();
115: throw e;
116: } catch (Exception e) {
117: quit();
118: throw new ConsoleException(e);
119: }
120:
121: virtualDbName = vdbName;
122: // Reload commands because target has changed
123: loadCommands();
124: console.printInfo(ConsoleTranslate.get("admin.login.ready",
125: virtualDbName));
126: }
127:
128: /**
129: * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#getDescriptionString()
130: */
131: public String getDescriptionString() {
132: return "VirtualDatabase Administration";
133: }
134:
135: /**
136: * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#getPromptString()
137: */
138: public String getPromptString() {
139: return virtualDbName + "(" + login + ")";
140: }
141:
142: /**
143: * Create a <code>Set</code> of expert commands. This <code>Set</code> of
144: * commands can be added/removed dynamically to the list of admin commands
145: * with the methods {@link #addExpertCommands()} and
146: * {@link #removeExpertCommands()}
147: *
148: * @return the set of expert commands
149: */
150: private Set expertCommandsSet() {
151: Set expertCmds = new HashSet();
152: String expertCommandsList = loadCommandsFromProperties("admin.expert");
153: String[] expertCommands = parseCommands(expertCommandsList);
154: addCommands(expertCommands, expertCmds);
155: return expertCmds;
156: }
157:
158: /**
159: * Create a <code>Set</code> of debug commands. This <code>Set</code> of
160: * commands can be added/removed dynamically to the list of admin commands
161: * with the methods {@link #addDebugCommands()} and
162: * {@link #removeDebugCommands()}
163: *
164: * @return the set of expert commands
165: */
166: private Set debugCommandsSet() {
167: Set debugCmds = new HashSet();
168: String debugCommandsList = loadCommandsFromProperties("admin.debug");
169: String[] debugCommands = parseCommands(debugCommandsList);
170: addCommands(debugCommands, debugCmds);
171: return debugCmds;
172: }
173:
174: /**
175: * Add the expert commands to the list of admin commands.
176: */
177: public void addExpertCommands() {
178: commands.addAll(expertCommandsSet());
179: // reload the completor or the newly added
180: // commands won't be taken into account
181: reloadCompletor();
182: }
183:
184: /**
185: * Revmoe the expert commands from the list of admin commands.
186: */
187: public void removeExpertCommands() {
188: commands.removeAll(expertCommandsSet());
189: // reload the completor or the removed
190: // commands will still be taken into account
191: reloadCompletor();
192: }
193:
194: /**
195: * Add the debug commands to the list of admin commands.
196: */
197: public void addDebugCommands() {
198: commands.addAll(debugCommandsSet());
199: // reload the completor or the newly added
200: // commands won't be taken into account
201: reloadCompletor();
202: }
203:
204: /**
205: * Revmoe the debug commands from the list of admin commands.
206: */
207: public void removeDebugCommands() {
208: commands.removeAll(debugCommandsSet());
209: // reload the completor or the removed
210: // commands will still be taken into account
211: reloadCompletor();
212: }
213:
214: /**
215: * Creates a new <code>VirtualDatabaseAdmin</code> instance.
216: *
217: * @param console console console
218: */
219: public VirtualDatabaseAdmin(Console console) {
220: super (console);
221: }
222:
223: /**
224: * @see org.continuent.sequoia.console.text.module.AbstractConsoleModule#getModuleID()
225: */
226: protected String getModuleID() {
227: return "admin"; //$NON-NLS-1$
228: }
229: }
|