KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > monitoring > SQLMonitoring


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): Emmanuel Cecchet.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.controller.monitoring;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.objectweb.cjdbc.common.log.Trace;
33 import org.objectweb.cjdbc.common.sql.AbstractRequest;
34 import org.objectweb.cjdbc.common.util.Stats;
35 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
36
37 /**
38  * This class implements a SQL monitoring module.
39  *
40  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
41  * @version 1.0
42  */

43 public class SQLMonitoring extends Monitoring
44 {
45   private Hashtable JavaDoc statList; // SQL query -> Stat
46
private ArrayList JavaDoc ruleList;
47   private boolean defaultRule;
48
49   private static Trace logger = null;
50
51   /**
52    * Create a SQLMonitoring object.
53    *
54    * @param vdbName name of the virtual database to be used by the logger
55    */

56   public SQLMonitoring(String JavaDoc vdbName)
57   {
58     statList = new Hashtable JavaDoc();
59     ruleList = new ArrayList JavaDoc();
60     logger = Trace.getLogger("org.objectweb.cjdbc.controller.monitoring."
61         + vdbName);
62   }
63
64   /**
65    * @see org.objectweb.cjdbc.controller.monitoring.Monitoring#cleanStats()
66    */

67   public void cleanStats()
68   {
69     statList.clear();
70   }
71   
72   /**
73    * Log the time elapsed to execute the given request.
74    *
75    * @param request the request executed
76    * @param time time elapsed to execute this request
77    */

78   public final void logRequestTime(AbstractRequest request, long time)
79   {
80     Stats stat = getStatForRequest(request);
81     if (stat == null)
82       return;
83     stat.incrementCount();
84     stat.updateTime(time);
85     if (logger.isDebugEnabled())
86       logger.debug(time + " " + stat.getName());
87   }
88
89   /**
90    * Log an error for the given request.
91    *
92    * @param request the request that failed to execute
93    */

94   public final void logError(AbstractRequest request)
95   {
96     Stats stat = getStatForRequest(request);
97     if (stat == null)
98       return;
99     stat.incrementError();
100     if (logger.isDebugEnabled())
101       logger.debug("ERROR " + stat.getName());
102   }
103
104   /**
105    * Log a cache hit for the given request.
106    *
107    * @param request the request that failed to execute
108    */

109   public final void logCacheHit(AbstractRequest request)
110   {
111     Stats stat = getStatForRequest(request);
112     if (stat == null)
113       return;
114     stat.incrementCacheHit();
115     if (logger.isDebugEnabled())
116       logger.debug("Cache hit " + stat.getName());
117   }
118
119   /**
120    * Reset the stats associated to a request.
121    *
122    * @param request the request to reset
123    */

124   public final void resetRequestStat(AbstractRequest request)
125   {
126     Stats stat = getStatForRequest(request);
127     if (stat == null)
128       return;
129     stat.reset();
130   }
131
132   /**
133    * Retrieve the stat corresponding to a request and create it if it does not
134    * exist.
135    *
136    * @param request the request to look for
137    * @return corresponding stat or null if a rule does not authorize this
138    * request to be monitored
139    */

140   public final Stats getStatForRequest(AbstractRequest request)
141   {
142     String JavaDoc sql = monitorRequestRule(request);
143     if (sql == null)
144       return null;
145
146     // Note that the Hashtable is synchronized
147
Stats stat = (Stats) statList.get(sql);
148     if (stat == null)
149     { // No entry for this query, create a new Stats entry
150
stat = new Stats(sql);
151       statList.put(sql, stat);
152     }
153     return stat;
154   }
155
156   /**
157    * Return all stats information in the form of a String
158    *
159    * @return stats information
160    */

161   public String JavaDoc[][] getAllStatsInformation()
162   {
163     Collection JavaDoc values = statList.values();
164     String JavaDoc[][] result = new String JavaDoc[values.size()][];
165     int i = 0;
166     for (Iterator JavaDoc iter = values.iterator(); iter.hasNext(); i++)
167     {
168       Stats stat = (Stats) iter.next();
169       result[i] = stat.toStringTable();
170     }
171     return result;
172   }
173
174   /**
175    * Dump all stats using the current logger (INFO level).
176    */

177   public void dumpAllStatsInformation()
178   {
179     if (logger.isInfoEnabled())
180     {
181       for (Iterator JavaDoc iter = statList.values().iterator(); iter.hasNext();)
182       {
183         Stats stat = (Stats) iter.next();
184         logger.info(stat.singleLineDisplay());
185       }
186     }
187   }
188
189   /*
190    * Rules Management
191    */

192
193   /**
194    * Get the default monitoring rule
195    *
196    * @return true if default is monitoring enabled
197    */

198   public boolean getDefaultRule()
199   {
200     return defaultRule;
201   }
202
203   /**
204    * Defines the default rule
205    *
206    * @param monitoring true if on, false is off
207    */

208   public void setDefaultRule(boolean monitoring)
209   {
210     this.defaultRule = monitoring;
211   }
212
213   /**
214    * Add a rule to the list.
215    *
216    * @param rule the rule to add
217    */

218   public void addRule(SQLMonitoringRule rule)
219   {
220     this.ruleList.add(rule);
221   }
222
223   /**
224    * Check the rule list to check of this request should be monitored or not.
225    *
226    * @param request the query to look for
227    * @return the SQL query to monitor or null if monitoring is off for this
228    * request
229    */

230   private String JavaDoc monitorRequestRule(AbstractRequest request)
231   {
232     for (int i = 0; i < ruleList.size(); i++)
233     {
234       SQLMonitoringRule rule = (SQLMonitoringRule) ruleList.get(i);
235       String JavaDoc sql = rule.matches(request);
236       if (sql != null)
237       { // This rule matches
238
if (rule.isMonitoring())
239           return sql;
240         else
241           return null;
242       }
243     }
244
245     // No rule matched, use the default rule
246
if (defaultRule)
247     {
248       if (request.getSqlSkeleton() == null)
249         return request.getSQL();
250       else
251         return request.getSqlSkeleton();
252     }
253     else
254       return null;
255   }
256
257   /**
258    * @return Returns the ruleList.
259    */

260   public ArrayList JavaDoc getRuleList()
261   {
262     return ruleList;
263   }
264
265   /**
266    * @see org.objectweb.cjdbc.common.xml.XmlComponent#getXml()
267    */

268   public String JavaDoc getXmlImpl()
269   {
270     String JavaDoc info = "<" + DatabasesXmlTags.ELT_SQLMonitoring + " "
271         + DatabasesXmlTags.ATT_defaultMonitoring + "=\"";
272     String JavaDoc defaultMonitoring = getDefaultRule()?"on":"off";
273     info += defaultMonitoring;
274     info += "\">";
275     for (int i = 0; i < ruleList.size(); i++)
276     {
277       SQLMonitoringRule rule = (SQLMonitoringRule) ruleList.get(i);
278       info += rule.getXml();
279     }
280     info += "</" + DatabasesXmlTags.ELT_SQLMonitoring + ">";
281     return info;
282   }
283
284 }
Popular Tags