KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > commands > dbadmin > ShowBackend


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 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): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.text.commands.dbadmin;
26
27 import java.util.ArrayList JavaDoc;
28
29 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
30 import org.objectweb.cjdbc.common.i18n.Translate;
31 import org.objectweb.cjdbc.common.jmx.mbeans.VirtualDatabaseMBean;
32 import org.objectweb.cjdbc.common.monitor.backend.BackendStatistics;
33 import org.objectweb.cjdbc.console.text.ColorPrinter;
34 import org.objectweb.cjdbc.console.text.formatter.TableFormatter;
35 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseAdmin;
36
37 /**
38  * This class defines a ShowBackend
39  *
40  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
41  * @version 1.0
42  */

43 public class ShowBackend extends AbstractAdminCommand
44 {
45
46   /**
47    * Creates a new <code>ShowBackend.java</code> object
48    *
49    * @param module the commands is attached to
50    */

51   public ShowBackend(VirtualDatabaseAdmin module)
52   {
53     super(module);
54   }
55
56   /**
57    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#parse(java.lang.String)
58    */

59   public void parse(String JavaDoc commandText) throws Exception JavaDoc
60   {
61     if (commandText.trim().length() == 0)
62     {
63       console.printError(getUsage());
64       return;
65     }
66     
67     VirtualDatabaseMBean db = jmxClient.getVirtualDatabaseProxy(dbName, user,
68         password);
69     String JavaDoc[] backendNames;
70     if (("*").equals(commandText.trim()))
71     {
72       ArrayList JavaDoc backendNamesList = db.getAllBackendNames();
73       backendNames = (String JavaDoc[]) backendNamesList
74           .toArray(new String JavaDoc[backendNamesList.size()]);
75     }
76     else
77     {
78       String JavaDoc backendName = commandText.trim();
79       backendNames = new String JavaDoc[]{backendName};
80     }
81
82     ArrayList JavaDoc stats = new ArrayList JavaDoc();
83     for (int i = 0; i < backendNames.length; i++)
84     {
85       String JavaDoc backendName = backendNames[i];
86       BackendStatistics stat = db.getBackendStatistics(backendName);
87       if (stat == null)
88       {
89         continue;
90       }
91       stats.add(stat);
92     }
93     if (stats.size() == 0)
94     {
95       console.println(ConsoleTranslate.get("admin.command.show.backend.no.stats", commandText));
96       return;
97     }
98     String JavaDoc formattedBackends = TableFormatter.format(
99         getBackendStatisticsDescriptions(),
100         getBackendStatisticsAsStrings(stats), false);
101     console.println(formattedBackends, ColorPrinter.STATUS);
102   }
103
104   private String JavaDoc[][] getBackendStatisticsAsStrings(ArrayList JavaDoc stats)
105   {
106     String JavaDoc[][] statsStr = new String JavaDoc[stats.size()][14];
107     for (int i = 0; i < statsStr.length; i++)
108     {
109       BackendStatistics stat = (BackendStatistics)stats.get(i);
110       statsStr[i][0] = stat.getBackendName();
111       statsStr[i][1] = stat.getDriverClassName();
112       statsStr[i][2] = stat.getUrl();
113       statsStr[i][3] = Integer.toString(stat.getNumberOfActiveTransactions());
114       statsStr[i][4] = Integer.toString(stat.getNumberOfPendingRequests());
115       statsStr[i][5] = Boolean.toString(stat.isReadEnabled());
116       statsStr[i][6] = Boolean.toString(stat.isWriteEnabled());
117       statsStr[i][7] = stat.getInitializationStatus();
118       statsStr[i][8] = Boolean.toString(stat.isSchemaStatic());
119       statsStr[i][9] = Integer.toString(stat.getNumberOfConnectionManagers());
120       statsStr[i][10] = Long.toString(stat.getNumberOfTotalActiveConnections());
121       statsStr[i][11] = Integer.toString(stat.getNumberOfTotalRequests());
122       statsStr[i][12] = Integer.toString(stat.getNumberOfTotalTransactions());
123       statsStr[i][13] = stat.getLastKnownCheckpoint();
124     }
125     return statsStr;
126   }
127
128   private String JavaDoc[] getBackendStatisticsDescriptions()
129   {
130     String JavaDoc[] descriptions = new String JavaDoc[14];
131     descriptions[0] = Translate.get("console.infoviewer.backend.name");
132     descriptions[1] = Translate.get("console.infoviewer.backend.driver");
133     descriptions[2] = Translate.get("console.infoviewer.backend.url");
134     descriptions[3] = Translate
135         .get("console.infoviewer.backend.active.transactions");
136     descriptions[4] = Translate
137         .get("console.infoviewer.backend.pending.requests");
138     descriptions[5] = Translate.get("console.infoviewer.backend.read.enabled");
139     descriptions[6] = Translate.get("console.infoviewer.backend.write.enabled");
140     descriptions[7] = Translate.get("console.infoviewer.backend.init.status");
141     descriptions[8] = Translate.get("console.infoviewer.backend.static.schema");
142     descriptions[9] = Translate
143         .get("console.infoviewer.backend.connection.managers");
144     descriptions[10] = Translate
145         .get("console.infoviewer.backend.total.active.connections");
146     descriptions[11] = Translate
147         .get("console.infoviewer.backend.total.requests");
148     descriptions[12] = Translate
149         .get("console.infoviewer.backend.total.transactions");
150     descriptions[13] = Translate
151         .get("console.infoviewer.backend.lastknown.checkpoint");
152     return descriptions;
153   }
154
155   /**
156    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandName()
157    */

158   public String JavaDoc getCommandName()
159   {
160
161     return "show backend";
162   }
163
164   /**
165    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandParameters()
166    */

167   public String JavaDoc getCommandParameters()
168   {
169     return ConsoleTranslate.get("admin.command.show.backend.params");
170   }
171
172   /**
173    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandDescription()
174    */

175   public String JavaDoc getCommandDescription()
176   {
177     return ConsoleTranslate.get("admin.command.show.backend.description");
178   }
179
180 }
181
Popular Tags