001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Gilles Rayrat.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.console.text.commands.dbadmin;
021:
022: import java.util.StringTokenizer;
023:
024: import org.continuent.sequoia.common.i18n.ConsoleTranslate;
025: import org.continuent.sequoia.common.jmx.mbeans.DatabaseBackendMBean;
026: import org.continuent.sequoia.console.text.module.VirtualDatabaseAdmin;
027:
028: /**
029: * This class defines the command used to dump the backend schema.
030: */
031: public class DumpBackendSchema extends AbstractAdminCommand {
032:
033: /**
034: * Creates a new <code>DumpBackendSchema</code> object
035: *
036: * @param module the commands is attached to
037: */
038: public DumpBackendSchema(VirtualDatabaseAdmin module) {
039: super (module);
040: }
041:
042: /**
043: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
044: */
045: public void parse(String commandText) throws Exception {
046: // args: <backend name> [table name] [/columns] [/locks]
047:
048: StringTokenizer st = new StringTokenizer(commandText.trim());
049: String backendName;
050: String tableName = null;
051: boolean dumpColumns = false;
052: boolean dumpLocks = false;
053: if (st.countTokens() == 0 || st.countTokens() > 4) {
054: console.printError(getUsage());
055: return;
056: }
057:
058: backendName = st.nextToken();
059: while (st.hasMoreTokens()) {
060: String token = st.nextToken();
061:
062: if ("/columns".equals(token))
063: dumpColumns = true;
064: else if ("/locks".equals(token))
065: dumpLocks = true;
066: else
067: tableName = token;
068: }
069:
070: DatabaseBackendMBean dbMbean = jmxClient
071: .getDatabaseBackendProxy(dbName, backendName, user,
072: password);
073:
074: if (tableName == null) {
075: String[] names = dbMbean.getTablesNames();
076: for (int i = 0; i < names.length; i++) {
077: printTable(names[i], dumpColumns, dumpLocks, dbMbean);
078: }
079: } else {
080: printTable(tableName, dumpColumns, dumpLocks, dbMbean);
081: }
082: }
083:
084: private void printTable(String tableName, boolean dumpColumns,
085: boolean dumpLocks, DatabaseBackendMBean dbMbean) {
086: console.println(tableName);
087: if (dumpColumns)
088: printColumnNames(dbMbean, tableName);
089: if (dumpLocks)
090: printLocks(dbMbean, tableName);
091: }
092:
093: private void printLocks(DatabaseBackendMBean dbMbean,
094: String tableName) {
095: console.println(dbMbean.getLockInfo(tableName));
096: }
097:
098: private void printColumnNames(DatabaseBackendMBean dbMbean,
099: String tableName) {
100: String[] columns = dbMbean.getColumnsNames(tableName);
101:
102: if (columns == null) {
103: console.printError(ConsoleTranslate.get(
104: "DumpBackendSchema.noSuchTable", tableName));
105: return;
106: }
107:
108: for (int j = 0; j < columns.length; j++)
109: console.println(" " + columns[j]);
110: }
111:
112: /**
113: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
114: */
115: public String getCommandName() {
116:
117: return "dump backend schema"; //$NON-NLS-1$
118: }
119:
120: /**
121: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
122: */
123: public String getCommandParameters() {
124: return ConsoleTranslate.get("DumpBackendSchema.params"); //$NON-NLS-1$
125: }
126:
127: /**
128: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
129: */
130: public String getCommandDescription() {
131: return ConsoleTranslate.get("DumpBackendSchema.description"); //$NON-NLS-1$
132: }
133: }
|