KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.objectweb.cjdbc.controller.monitoring;
25
26 import org.apache.regexp.RE;
27 import org.apache.regexp.RESyntaxException;
28 import org.objectweb.cjdbc.common.sql.AbstractRequest;
29 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
30
31 /**
32  * This class implements a SQL monitoring rule.
33  *
34  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
35  * @version 1.0
36  */

37 public class SQLMonitoringRule
38 {
39   private RE queryPattern;
40   private boolean isCaseSentive;
41   private boolean applyToSkeleton;
42   private boolean monitoring;
43
44   /**
45    * Creates a new SQL Monitoring rule
46    *
47    * @param queryPattern the query pattern to match
48    * @param isCaseSentive true if matching is case sensitive
49    * @param applyToSkeleton true if matching applies to the query skeleton
50    * @param monitoring true if the request must be monitored
51    */

52   public SQLMonitoringRule(
53     String JavaDoc queryPattern,
54     boolean isCaseSentive,
55     boolean applyToSkeleton,
56     boolean monitoring)
57   {
58     try
59     {
60       this.queryPattern = new RE(queryPattern);
61     }
62     catch (RESyntaxException e)
63     {
64       throw new RuntimeException JavaDoc(
65         "Invalid regexp in SQL Monitoring rule (" + e + ")");
66     }
67     this.isCaseSentive = isCaseSentive;
68     if (isCaseSentive)
69       this.queryPattern.setMatchFlags(RE.MATCH_NORMAL);
70     else
71       this.queryPattern.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
72     this.applyToSkeleton = applyToSkeleton;
73     this.monitoring = monitoring;
74   }
75
76   /**
77    * If matching is case sensitive or not
78    *
79    * @return true if the matching is case sensitive
80    */

81   public boolean isCaseSentive()
82   {
83     return isCaseSentive;
84   }
85
86   /**
87    * If monitoring is activated or not.
88    *
89    * @return true if monitoring is activated for this pattern
90    */

91   public boolean isMonitoring()
92   {
93     return monitoring;
94   }
95
96   /**
97    * Get query pattern
98    *
99    * @return the query pattern
100    */

101   public String JavaDoc getQueryPattern()
102   {
103     return queryPattern.toString();
104   }
105
106   /**
107    * Set the matching case sensitiveness
108    *
109    * @param b true if matching is case sensitive
110    */

111   public void setCaseSentive(boolean b)
112   {
113     isCaseSentive = b;
114   }
115
116   /**
117    * Set the monitoring on or off
118    *
119    * @param b true if monitoring must be activated for this rule
120    */

121   public void setMonitoring(boolean b)
122   {
123     monitoring = b;
124   }
125
126   /**
127    * Sets the query pattern
128    *
129    * @param queryPattern the queryPattern
130    */

131   public void setQueryPattern(String JavaDoc queryPattern)
132   {
133     try
134     {
135       this.queryPattern = new RE(queryPattern);
136     }
137     catch (RESyntaxException e)
138     {
139       throw new RuntimeException JavaDoc(
140         "Invalid regexp in SQL Monitoring rule (" + e + ")");
141     }
142   }
143
144   /**
145    * If the pattern apply to the skeleton ot the instanciated query.
146    *
147    * @return true if the pattern apply to the query skeleton
148    */

149   public boolean isApplyToSkeleton()
150   {
151     return applyToSkeleton;
152   }
153
154   /**
155    * Set to true if the pattern apply to the query skeleton
156    *
157    * @param b true if the pattern apply to the query skeleton
158    */

159   public void setApplyToSkeleton(boolean b)
160   {
161     applyToSkeleton = b;
162   }
163
164   /**
165    * Returns true if the given query matches the pattern of this rule. This
166    * function applies the applytoSkeleton rule.
167    *
168    * @param request the query
169    * @return the SQL that matches the rule or null if it does not match
170    */

171   public String JavaDoc matches(AbstractRequest request)
172   {
173     if (applyToSkeleton)
174     {
175       String JavaDoc skel = request.getSqlSkeleton();
176       if (skel == null)
177         return null;
178       else
179       {
180         if (queryPattern.match(skel))
181           return skel;
182         else
183           return null;
184       }
185     }
186     else
187     {
188       if (queryPattern.match(request.getSQL()))
189         return request.getSQL();
190       else
191         return null;
192     }
193   }
194
195   /**
196    * @see org.objectweb.cjdbc.common.xml.XmlComponent#getXml()
197    */

198   public String JavaDoc getXml()
199   {
200     String JavaDoc info =
201       "<"
202         + DatabasesXmlTags.ELT_SQLMonitoringRule
203         + " "
204         + DatabasesXmlTags.ATT_queryPattern
205         + "=\""
206         + getQueryPattern()
207         + "\" "
208         + DatabasesXmlTags.ATT_caseSensitive
209         + "=\""
210         + isCaseSentive()
211         + "\" "
212         + DatabasesXmlTags.ATT_applyToSkeleton
213         + "=\""
214         + isApplyToSkeleton()
215         + "\" "
216         + DatabasesXmlTags.ATT_monitoring
217         + "=\"";
218     if (isMonitoring())
219       info += "on";
220     else
221       info += "off";
222     info += "\"/>";
223     return info;
224   }
225
226 }
227
Popular Tags