KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

49   public ShowBackend(VirtualDatabaseAdmin module)
50   {
51     super(module);
52   }
53
54   /**
55    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
56    */

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

167   public String JavaDoc getCommandName()
168   {
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 JavaDoc getCommandParameters()
177   {
178     return ConsoleTranslate.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 JavaDoc getCommandDescription()
185   {
186     return ConsoleTranslate.get("admin.command.show.backend.description"); //$NON-NLS-1$
187
}
188
189 }
190
Popular Tags