KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > commands > dbadmin > DumpBackendSchema


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

21
22 package org.continuent.sequoia.console.text.commands.dbadmin;
23
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.continuent.sequoia.common.i18n.ConsoleTranslate;
27 import org.continuent.sequoia.common.jmx.mbeans.DatabaseBackendMBean;
28 import org.continuent.sequoia.console.text.module.VirtualDatabaseAdmin;
29
30 /**
31  * This class defines the command used to dump the backend schema.
32  */

33 public class DumpBackendSchema extends AbstractAdminCommand
34 {
35
36   /**
37    * Creates a new <code>DumpBackendSchema</code> object
38    *
39    * @param module the commands is attached to
40    */

41   public DumpBackendSchema(VirtualDatabaseAdmin module)
42   {
43     super(module);
44   }
45
46   /**
47    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
48    */

49   public void parse(String JavaDoc commandText) throws Exception JavaDoc
50   {
51     // args: <backend name> [table name] [/columns] [/locks]
52

53     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(commandText.trim());
54     String JavaDoc backendName;
55     String JavaDoc tableName = null;
56     boolean dumpColumns = false;
57     boolean dumpLocks = false;
58     if (st.countTokens() == 0 || st.countTokens() > 4)
59     {
60       console.printError(getUsage());
61       return;
62     }
63
64     backendName = st.nextToken();
65     while (st.hasMoreTokens())
66     {
67       String JavaDoc token = st.nextToken();
68
69       if ("/columns".equals(token))
70         dumpColumns = true;
71       else if ("/locks".equals(token))
72         dumpLocks = true;
73       else
74         tableName = token;
75     }
76
77     DatabaseBackendMBean dbMbean = jmxClient.getDatabaseBackendProxy(dbName,
78         backendName, user, password);
79
80     if (tableName == null)
81     {
82       String JavaDoc[] names = dbMbean.getTablesNames();
83       for (int i = 0; i < names.length; i++)
84       {
85         printTable(names[i], dumpColumns, dumpLocks, dbMbean);
86       }
87     }
88     else
89     {
90       printTable(tableName, dumpColumns, dumpLocks, dbMbean);
91     }
92   }
93
94   private void printTable(String JavaDoc tableName, boolean dumpColumns,
95       boolean dumpLocks, DatabaseBackendMBean dbMbean)
96   {
97     console.println(tableName);
98     if (dumpColumns)
99       printColumnNames(dbMbean, tableName);
100     if (dumpLocks)
101       printLocks(dbMbean, tableName);
102   }
103
104   private void printLocks(DatabaseBackendMBean dbMbean, String JavaDoc tableName)
105   {
106     console.println(dbMbean.getLockInfo(tableName));
107   }
108
109   private void printColumnNames(DatabaseBackendMBean dbMbean, String JavaDoc tableName)
110   {
111     String JavaDoc[] columns = dbMbean.getColumnsNames(tableName);
112
113     if (columns == null)
114     {
115       console.printError(ConsoleTranslate.get("DumpBackendSchema.noSuchTable",
116           tableName));
117       return;
118     }
119
120     for (int j = 0; j < columns.length; j++)
121       console.println(" " + columns[j]);
122   }
123
124   /**
125    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
126    */

127   public String JavaDoc getCommandName()
128   {
129
130     return "dump backend schema"; //$NON-NLS-1$
131
}
132
133   /**
134    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
135    */

136   public String JavaDoc getCommandParameters()
137   {
138     return ConsoleTranslate.get("DumpBackendSchema.params"); //$NON-NLS-1$
139
}
140
141   /**
142    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
143    */

144   public String JavaDoc getCommandDescription()
145   {
146     return ConsoleTranslate.get("DumpBackendSchema.description"); //$NON-NLS-1$
147
}
148 }
149
Popular Tags