1 24 25 package org.objectweb.cjdbc.controller.monitoring; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 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 43 public class SQLMonitoring extends Monitoring 44 { 45 private Hashtable statList; private ArrayList ruleList; 47 private boolean defaultRule; 48 49 private static Trace logger = null; 50 51 56 public SQLMonitoring(String vdbName) 57 { 58 statList = new Hashtable (); 59 ruleList = new ArrayList (); 60 logger = Trace.getLogger("org.objectweb.cjdbc.controller.monitoring." 61 + vdbName); 62 } 63 64 67 public void cleanStats() 68 { 69 statList.clear(); 70 } 71 72 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 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 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 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 140 public final Stats getStatForRequest(AbstractRequest request) 141 { 142 String sql = monitorRequestRule(request); 143 if (sql == null) 144 return null; 145 146 Stats stat = (Stats) statList.get(sql); 148 if (stat == null) 149 { stat = new Stats(sql); 151 statList.put(sql, stat); 152 } 153 return stat; 154 } 155 156 161 public String [][] getAllStatsInformation() 162 { 163 Collection values = statList.values(); 164 String [][] result = new String [values.size()][]; 165 int i = 0; 166 for (Iterator 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 177 public void dumpAllStatsInformation() 178 { 179 if (logger.isInfoEnabled()) 180 { 181 for (Iterator iter = statList.values().iterator(); iter.hasNext();) 182 { 183 Stats stat = (Stats) iter.next(); 184 logger.info(stat.singleLineDisplay()); 185 } 186 } 187 } 188 189 192 193 198 public boolean getDefaultRule() 199 { 200 return defaultRule; 201 } 202 203 208 public void setDefaultRule(boolean monitoring) 209 { 210 this.defaultRule = monitoring; 211 } 212 213 218 public void addRule(SQLMonitoringRule rule) 219 { 220 this.ruleList.add(rule); 221 } 222 223 230 private String monitorRequestRule(AbstractRequest request) 231 { 232 for (int i = 0; i < ruleList.size(); i++) 233 { 234 SQLMonitoringRule rule = (SQLMonitoringRule) ruleList.get(i); 235 String sql = rule.matches(request); 236 if (sql != null) 237 { if (rule.isMonitoring()) 239 return sql; 240 else 241 return null; 242 } 243 } 244 245 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 260 public ArrayList getRuleList() 261 { 262 return ruleList; 263 } 264 265 268 public String getXmlImpl() 269 { 270 String info = "<" + DatabasesXmlTags.ELT_SQLMonitoring + " " 271 + DatabasesXmlTags.ATT_defaultMonitoring + "=\""; 272 String 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 |