KickJava   Java API By Example, From Geeks To Geeks.

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


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): Jeff Mesnil.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.console.text.commands.dbadmin;
23
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.management.openmbean.CompositeData JavaDoc;
34 import javax.management.openmbean.TabularData JavaDoc;
35
36 import org.continuent.sequoia.common.i18n.ConsoleTranslate;
37 import org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean;
38 import org.continuent.sequoia.console.text.formatter.TableFormatter;
39 import org.continuent.sequoia.console.text.module.VirtualDatabaseAdmin;
40
41 /**
42  * This class defines the command used to dump the recovery log.
43  */

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

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

59   public void parse(String JavaDoc commandText) throws Exception JavaDoc
60   {
61     if ("indexes".equals(commandText.trim())) //$NON-NLS-1$
62
{
63       printIndexes();
64       return;
65     }
66     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(commandText.trim());
67
68     long min = 0;
69     if (tokenizer.hasMoreTokens())
70     {
71       String JavaDoc minStr = tokenizer.nextToken();
72       try
73       {
74         min = Long.parseLong(minStr);
75       }
76       catch (NumberFormatException JavaDoc e)
77       {
78         console.printError(getUsage());
79         return;
80       }
81     }
82
83     RecoveryLogControlMBean recoveryLog = jmxClient.getRecoveryLog(dbName,
84         user, password);
85
86     long max;
87     String JavaDoc maxStr;
88     if (tokenizer.hasMoreTokens())
89     {
90       maxStr = tokenizer.nextToken();
91       try
92       {
93         max = Long.parseLong(maxStr);
94       }
95       catch (NumberFormatException JavaDoc e)
96       {
97         console.printError(getUsage());
98         return;
99       }
100     }
101     else
102     {
103       max = recoveryLog.getIndexes()[1];
104     }
105
106     if (min < 0 || max < 0)
107     {
108       console.printError("Negative indexes are not allowed");
109       return;
110     }
111
112     String JavaDoc[] headers = recoveryLog.getHeaders();
113     // first headers is used to sort the entries
114
final String JavaDoc idKey = headers[0];
115     TabularData JavaDoc logEntries = recoveryLog.dump(min);
116
117     if (logEntries.isEmpty())
118     {
119       console.printInfo(ConsoleTranslate.get("DumpRecoveryLog.empty")); //$NON-NLS-1$
120
return;
121     }
122     long lastIndex = 0;
123     while (!logEntries.isEmpty())
124     {
125       List JavaDoc entries = sortEntriesById(logEntries, idKey);
126       lastIndex = getIndexOfLastEntry(entries, idKey);
127       if (lastIndex > max)
128       {
129         removeAfterIndex(entries, max, idKey);
130       }
131       String JavaDoc[][] entriesStr = from(entries, headers);
132       console.println(TableFormatter.format(headers, entriesStr, true));
133
134       if (lastIndex > max)
135       {
136         break;
137       }
138       logEntries = recoveryLog.dump(lastIndex + 1);
139     }
140   }
141   
142   /*
143    * Remove all entries whose indexes are greater than the index parameter
144    */

145   private void removeAfterIndex(List JavaDoc entries, long index, String JavaDoc idKey)
146   {
147     Iterator JavaDoc iter = entries.iterator();
148     while (iter.hasNext())
149     {
150       CompositeData JavaDoc data = (CompositeData JavaDoc) iter.next();
151       String JavaDoc idStr = (String JavaDoc) data.get(idKey);
152       long id = Long.parseLong(idStr);
153       if (id > index)
154       {
155         iter.remove();
156       }
157     }
158   }
159
160   private void printIndexes()
161   {
162     try
163     {
164       RecoveryLogControlMBean recoveryLog = jmxClient.getRecoveryLog(dbName,
165           user, password);
166       long[] indexes = recoveryLog.getIndexes();
167       String JavaDoc minIndex = (indexes != null)
168           ? Long.toString(indexes[0])
169           : ConsoleTranslate.get("DumpRecoveryLog.notAvailable"); //$NON-NLS-1$
170
String JavaDoc maxIndex = (indexes != null)
171           ? Long.toString(indexes[1])
172           : ConsoleTranslate.get("DumpRecoveryLog.notAvailable"); //$NON-NLS-1$
173
console.println(ConsoleTranslate
174           .get("DumpRecoveryLog.minIndex", minIndex)); //$NON-NLS-1$
175
console.println(ConsoleTranslate
176           .get("DumpRecoveryLog.maxIndex", maxIndex)); //$NON-NLS-1$
177
console.println(ConsoleTranslate.get(
178           "DumpRecoveryLog.numberOfEntries", recoveryLog.getEntries())); //$NON-NLS-1$
179
}
180     catch (IOException JavaDoc e)
181     {
182       console.printError(e.getMessage());
183     }
184   }
185
186   /**
187    * Sorth the log entries by their ids.
188    *
189    * @param logEntries a TabularData corresponding to the log entries
190    * @param idKey the key to use to retrieve the index of the log entries
191    * @return a List&lt;CompositeData&gt; where the composite data are sorted by
192    * the specified <code>idKey</code>
193    */

194   private static List JavaDoc sortEntriesById(TabularData JavaDoc logEntries, final String JavaDoc idKey)
195   {
196     List JavaDoc entries = new ArrayList JavaDoc(logEntries.values());
197     Collections.sort(entries, new Comparator JavaDoc()
198     {
199
200       public int compare(Object JavaDoc o1, Object JavaDoc o2)
201       {
202         CompositeData JavaDoc entry1 = (CompositeData JavaDoc) o1;
203         CompositeData JavaDoc entry2 = (CompositeData JavaDoc) o2;
204
205         String JavaDoc idStr1 = (String JavaDoc) entry1.get(idKey);
206         String JavaDoc idStr2 = (String JavaDoc) entry2.get(idKey);
207
208         int id1 = Integer.parseInt(idStr1);
209         int id2 = Integer.parseInt(idStr2);
210         return id1 - id2;
211       }
212     });
213     return entries;
214   }
215
216   /**
217    * Returns the index of the last entry in the <code>entries</code> List
218    *
219    * @param entries a List&lt;CompositeData&gt;
220    * @param idKey the key corresponding to the id of the index
221    * @return the index of the last entry in the <code>entries</code> List
222    */

223   private static int getIndexOfLastEntry(List JavaDoc entries, String JavaDoc idKey)
224   {
225     CompositeData JavaDoc data = (CompositeData JavaDoc) entries.get(entries.size() - 1);
226     String JavaDoc idStr = (String JavaDoc) data.get(idKey);
227     return Integer.parseInt(idStr);
228   }
229
230   /**
231    * Convert a Collection&lt;CompositeData&gt; to a matrix of String.
232    *
233    * @param collection a Collection&lt;CompositeData&gt;
234    * @param headers the headers corresponding to the items to retrieve for each
235    * CompositeDate
236    * @return a matrix of String
237    */

238   private static String JavaDoc[][] from(Collection JavaDoc collection, String JavaDoc[] headers)
239   {
240     String JavaDoc[][] entries = new String JavaDoc[collection.size()][];
241     Iterator JavaDoc iter = collection.iterator();
242     int i = 0;
243     while (iter.hasNext())
244     {
245       CompositeData JavaDoc logEntry = (CompositeData JavaDoc) iter.next();
246       Object JavaDoc[] logEntryItems = logEntry.getAll(headers);
247       String JavaDoc[] entry = new String JavaDoc[logEntryItems.length];
248       for (int j = 0; j < entry.length; j++)
249       {
250         entry[j] = logEntryItems[j].toString();
251       }
252       entries[i] = entry;
253       i++;
254     }
255     return entries;
256   }
257
258   /**
259    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
260    */

261   public String JavaDoc getCommandName()
262   {
263
264     return "dump recoverylog"; //$NON-NLS-1$
265
}
266
267   /**
268    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
269    */

270   public String JavaDoc getCommandParameters()
271   {
272     return "([indexes] | (<min>* <max>*))"; //$NON-NLS-1$
273
}
274
275   /**
276    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
277    */

278   public String JavaDoc getCommandDescription()
279   {
280     return ConsoleTranslate.get("DumpRecoveryLog.description"); //$NON-NLS-1$
281
}
282 }
283
Popular Tags