KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > recoverylog > RecoveryLogControl


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.controller.recoverylog;
23
24 import java.sql.SQLException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.management.NotCompliantMBeanException JavaDoc;
30 import javax.management.openmbean.CompositeData JavaDoc;
31 import javax.management.openmbean.CompositeDataSupport JavaDoc;
32 import javax.management.openmbean.CompositeType JavaDoc;
33 import javax.management.openmbean.OpenDataException JavaDoc;
34 import javax.management.openmbean.OpenType JavaDoc;
35 import javax.management.openmbean.SimpleType JavaDoc;
36 import javax.management.openmbean.TabularData JavaDoc;
37 import javax.management.openmbean.TabularDataSupport JavaDoc;
38 import javax.management.openmbean.TabularType JavaDoc;
39
40 import org.continuent.sequoia.common.i18n.Translate;
41 import org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean;
42 import org.continuent.sequoia.common.log.Trace;
43 import org.continuent.sequoia.controller.jmx.AbstractStandardMBean;
44
45 /**
46  * RecoveryLogControlMBean implemementation.<br />
47  * Used to manage a RecoveryLog.
48  *
49  * @see org.continuent.sequoia.controller.recoverylog.RecoveryLog
50  */

51 public class RecoveryLogControl extends AbstractStandardMBean
52     implements
53       RecoveryLogControlMBean
54 {
55
56   private RecoveryLog managedRecoveryLog;
57   private final Trace logger = Trace
58                                                            .getLogger("org.continuent.sequoia.controller.recoverylog"); //$NON-NLS-1$
59

60   private static final OpenType JavaDoc[] LOG_ENTRY_ITEM_TYPES = new OpenType JavaDoc[]{
61       SimpleType.STRING, SimpleType.STRING, SimpleType.STRING,
62       SimpleType.STRING, SimpleType.STRING, SimpleType.STRING,
63       SimpleType.STRING, SimpleType.STRING, SimpleType.STRING,
64       SimpleType.STRING, SimpleType.STRING };
65   private static final int ENTRIES_PER_DUMP = 100;
66
67   private TabularType JavaDoc logEntriesType;
68   private CompositeType JavaDoc logEntryType;
69
70   /**
71    * Creates a new <code>RecoveryLogControl</code> object
72    *
73    * @param recoveryLog the managed RecoveryLog
74    * @throws NotCompliantMBeanException if this mbean is not compliant
75    */

76   public RecoveryLogControl(RecoveryLog recoveryLog)
77       throws NotCompliantMBeanException JavaDoc
78   {
79     super(RecoveryLogControlMBean.class);
80     this.managedRecoveryLog = recoveryLog;
81     defineTypes();
82   }
83
84   /**
85    * Defines the OpenType used by this mbean.
86    *
87    * @see #dump(long)
88    */

89   private void defineTypes()
90   {
91     try
92     {
93       logEntryType = new CompositeType JavaDoc(
94           "LogEntry", //$NON-NLS-1$
95
Translate.get("RecoveryLogControl.LogEntryDescription"), getHeaders(), getHeaders(), //$NON-NLS-1$
96
LOG_ENTRY_ITEM_TYPES);
97       logEntriesType = new TabularType JavaDoc(
98           "LogEntries", //$NON-NLS-1$
99
Translate.get("RecoveryLogControl.LogEntriesDescription"), logEntryType, getHeaders()); //$NON-NLS-1$
100
}
101     catch (OpenDataException JavaDoc e)
102     {
103       if (logger.isWarnEnabled())
104       {
105         logger.warn(Translate.get("RecoveryLogControl.openTypeWarning"), e); //$NON-NLS-1$
106
}
107     }
108   }
109
110   /**
111    * @see org.continuent.sequoia.controller.jmx.AbstractStandardMBean#getAssociatedString()
112    */

113   public String JavaDoc getAssociatedString()
114   {
115     return "recoverylog"; //$NON-NLS-1$
116
}
117
118   /**
119    * @see org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean#getHeaders()
120    */

121   public String JavaDoc[] getHeaders()
122   {
123     return managedRecoveryLog.getColumnNames();
124   }
125
126   /**
127    * @see org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean#dump(long)
128    */

129   public TabularData JavaDoc dump(long from)
130   {
131     if (logger.isDebugEnabled())
132     {
133       logger.debug("dumping recovery log from index " + from + " ("
134           + ENTRIES_PER_DUMP + " entries per page)");
135     }
136     String JavaDoc[][] entries = managedRecoveryLog.getLogEntries(from,
137         ENTRIES_PER_DUMP);
138     TabularData JavaDoc result = toTabularData(entries);
139     return result;
140   }
141
142   /**
143    * @see org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean#getEntriesPerDump()
144    */

145   public int getEntriesPerDump()
146   {
147     return ENTRIES_PER_DUMP;
148   }
149
150   /**
151    * @see org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean#getIndexes()
152    */

153   public long[] getIndexes()
154   {
155     try
156     {
157       return managedRecoveryLog.getIndexes();
158     }
159     catch (SQLException JavaDoc e)
160     {
161       if (logger.isWarnEnabled())
162       {
163         // FIXME we should specify the VBD the managed recovery log belongs to
164
logger.warn(
165             "unable to get the min and max indexes of the recovery log", e);
166       }
167       return null;
168     }
169   }
170
171   /**
172    * @see org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean#getEntries()
173    */

174   public long getEntries()
175   {
176     try
177     {
178       return managedRecoveryLog.getNumberOfLogEntries();
179     }
180     catch (SQLException JavaDoc e)
181     {
182       if (logger.isWarnEnabled())
183       {
184         // FIXME we should specify the VBD the managed recovery log belongs to
185
logger.warn(
186             "unable to get the number of log entries in the recovery log", e);
187       }
188       return -1;
189     }
190   }
191
192   /**
193    * Helper method to convert a matrix of String to a TabularData.
194    *
195    * @param entries a matrix of String
196    * @return a TabularData
197    */

198   private TabularData JavaDoc toTabularData(String JavaDoc[][] entries)
199   {
200     TabularData JavaDoc entriesData = new TabularDataSupport JavaDoc(logEntriesType);
201     for (int i = 0; i < entries.length; i++)
202     {
203       String JavaDoc[] entry = entries[i];
204       CompositeData JavaDoc entryData = toCompositeData(entry);
205       if (entryData != null)
206       {
207         entriesData.put(entryData);
208       }
209     }
210     return entriesData;
211   }
212
213   /**
214    * Helper method to convert a array of String to a CompositeData.<br />
215    * <strong>this method assumes that all the types of the CompositeData are of
216    * SimpleType.STRING.</strong>
217    *
218    * @param entry an array of String
219    * @return a CompositeData
220    * @see SimpleType#STRING
221    */

222   private CompositeData JavaDoc toCompositeData(String JavaDoc[] entry)
223   {
224     try
225     {
226       CompositeData JavaDoc entryData = new CompositeDataSupport JavaDoc(logEntryType,
227           getHeaders(), entry);
228       return entryData;
229     }
230     catch (OpenDataException JavaDoc e)
231     {
232       if (logger.isWarnEnabled())
233       {
234         logger.warn(Translate.get(
235             "RecoveryLogControl.conversionWarning", Arrays.asList(entry)), e); //$NON-NLS-1$
236
}
237       return null;
238     }
239   }
240
241   /**
242    * @see org.continuent.sequoia.common.jmx.mbeans.RecoveryLogControlMBean#getCheckpoints()
243    */

244   public Map JavaDoc getCheckpoints()
245   {
246     try
247     {
248       return managedRecoveryLog.getCheckpoints();
249     }
250     catch (SQLException JavaDoc e)
251     {
252       if (logger.isWarnEnabled())
253       {
254         logger.warn(e.getMessage(), e);
255       }
256       return new HashMap JavaDoc();
257     }
258   }
259 }
260
Popular Tags