001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Nicolas Modrzyk
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.console.text.commands.dbadmin;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import org.continuent.sequoia.common.i18n.ConsoleTranslate;
027: import org.continuent.sequoia.common.i18n.Translate;
028: import org.continuent.sequoia.common.jmx.mbeans.VirtualDatabaseMBean;
029: import org.continuent.sequoia.common.jmx.monitoring.backend.BackendStatistics;
030: import org.continuent.sequoia.console.text.formatter.TableFormatter;
031: import org.continuent.sequoia.console.text.module.VirtualDatabaseAdmin;
032:
033: /**
034: * This class defines a ShowBackend
035: *
036: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
037: * @version 1.0
038: */
039: public class ShowBackend extends AbstractAdminCommand {
040:
041: /**
042: * Creates a new <code>ShowBackend.java</code> object
043: *
044: * @param module the commands is attached to
045: */
046: public ShowBackend(VirtualDatabaseAdmin module) {
047: super (module);
048: }
049:
050: /**
051: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
052: */
053: public void parse(String commandText) throws Exception {
054: if (commandText.trim().length() == 0) {
055: console.printError(getUsage());
056: return;
057: }
058:
059: VirtualDatabaseMBean db = jmxClient.getVirtualDatabaseProxy(
060: dbName, user, password);
061: String[] backendNames;
062: if (("*").equals(commandText.trim())) //$NON-NLS-1$
063: {
064: List backendNamesList = db.getAllBackendNames();
065: backendNames = (String[]) backendNamesList
066: .toArray(new String[backendNamesList.size()]);
067: } else {
068: String backendName = commandText.trim();
069: backendNames = new String[] { backendName };
070: }
071:
072: ArrayList stats = new ArrayList();
073: if (backendNames.length == 0) {
074: console.printInfo(ConsoleTranslate
075: .get("admin.command.show.backend.no.backends")); //$NON-NLS-1$
076: return;
077: }
078: for (int i = 0; i < backendNames.length; i++) {
079: String backendName = backendNames[i];
080: BackendStatistics stat = db
081: .getBackendStatistics(backendName);
082: if (stat == null) {
083: continue;
084: }
085: stats.add(stat);
086: }
087: if (stats.size() == 0) {
088: console
089: .printInfo(ConsoleTranslate
090: .get(
091: "admin.command.show.backend.no.stats", commandText)); //$NON-NLS-1$
092: return;
093: }
094: String formattedBackends = TableFormatter.format(
095: getBackendStatisticsDescriptions(),
096: getBackendStatisticsAsStrings(stats), false);
097: console.println(formattedBackends);
098: }
099:
100: private String[][] getBackendStatisticsAsStrings(ArrayList stats) {
101: String[][] statsStr = new String[stats.size()][15];
102: for (int i = 0; i < statsStr.length; i++) {
103: BackendStatistics stat = (BackendStatistics) stats.get(i);
104: statsStr[i][0] = stat.getBackendName();
105: statsStr[i][1] = stat.getDriverClassName();
106: statsStr[i][2] = stat.getUrl();
107: statsStr[i][3] = Integer.toString(stat
108: .getNumberOfActiveTransactions());
109: statsStr[i][4] = Integer.toString(stat
110: .getNumberOfPendingRequests());
111: statsStr[i][5] = Boolean.toString(stat.isReadEnabled());
112: statsStr[i][6] = Boolean.toString(stat.isWriteEnabled());
113: statsStr[i][7] = stat.getInitializationStatus();
114: statsStr[i][8] = Boolean.toString(stat.isSchemaStatic());
115: statsStr[i][9] = Integer.toString(stat
116: .getNumberOfConnectionManagers());
117: statsStr[i][10] = Long.toString(stat
118: .getNumberOfTotalActiveConnections());
119: statsStr[i][11] = Integer.toString(stat
120: .getNumberOfPersistentConnections());
121: statsStr[i][12] = Integer.toString(stat
122: .getNumberOfTotalRequests());
123: statsStr[i][13] = Integer.toString(stat
124: .getNumberOfTotalTransactions());
125: statsStr[i][14] = stat.getLastKnownCheckpoint();
126: }
127: return statsStr;
128: }
129:
130: private String[] getBackendStatisticsDescriptions() {
131: String[] descriptions = new String[15];
132: descriptions[0] = Translate
133: .get("console.infoviewer.backend.name"); //$NON-NLS-1$
134: descriptions[1] = Translate
135: .get("console.infoviewer.backend.driver"); //$NON-NLS-1$
136: descriptions[2] = Translate
137: .get("console.infoviewer.backend.url"); //$NON-NLS-1$
138: descriptions[3] = Translate
139: .get("console.infoviewer.backend.active.transactions"); //$NON-NLS-1$
140: descriptions[4] = Translate
141: .get("console.infoviewer.backend.pending.requests"); //$NON-NLS-1$
142: descriptions[5] = Translate
143: .get("console.infoviewer.backend.read.enabled"); //$NON-NLS-1$
144: descriptions[6] = Translate
145: .get("console.infoviewer.backend.write.enabled"); //$NON-NLS-1$
146: descriptions[7] = Translate
147: .get("console.infoviewer.backend.init.status"); //$NON-NLS-1$
148: descriptions[8] = Translate
149: .get("console.infoviewer.backend.static.schema"); //$NON-NLS-1$
150: descriptions[9] = Translate
151: .get("console.infoviewer.backend.connection.managers"); //$NON-NLS-1$
152: descriptions[10] = Translate
153: .get("console.infoviewer.backend.total.active.connections"); //$NON-NLS-1$
154: descriptions[11] = Translate
155: .get("console.infoviewer.backend.persistent.connections"); //$NON-NLS-1$
156: descriptions[12] = Translate
157: .get("console.infoviewer.backend.total.requests"); //$NON-NLS-1$
158: descriptions[13] = Translate
159: .get("console.infoviewer.backend.total.transactions"); //$NON-NLS-1$
160: descriptions[14] = Translate
161: .get("console.infoviewer.backend.lastknown.checkpoint"); //$NON-NLS-1$
162: return descriptions;
163: }
164:
165: /**
166: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
167: */
168: public String getCommandName() {
169:
170: return "show backend"; //$NON-NLS-1$
171: }
172:
173: /**
174: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
175: */
176: public String getCommandParameters() {
177: return ConsoleTranslate
178: .get("admin.command.show.backend.params"); //$NON-NLS-1$
179: }
180:
181: /**
182: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
183: */
184: public String getCommandDescription() {
185: return ConsoleTranslate
186: .get("admin.command.show.backend.description"); //$NON-NLS-1$
187: }
188:
189: }
|