KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > monitoring > SQLMonitoringRule


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): ______________________________________.
21  */

22
23 package org.continuent.sequoia.controller.monitoring;
24
25 import java.util.regex.Pattern JavaDoc;
26
27 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
28 import org.continuent.sequoia.controller.requests.AbstractRequest;
29
30 /**
31  * This class implements a SQL monitoring rule.
32  *
33  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
34  * @version 1.0
35  */

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

51   public SQLMonitoringRule(String JavaDoc queryPattern, boolean isCaseSentive,
52       boolean applyToSkeleton, boolean monitoring)
53   {
54     this.isCaseSentive = isCaseSentive;
55     if (isCaseSentive)
56       this.queryPattern = Pattern.compile(queryPattern);
57     else
58       this.queryPattern = Pattern.compile(queryPattern,
59           Pattern.CASE_INSENSITIVE);
60     this.applyToSkeleton = applyToSkeleton;
61     this.monitoring = monitoring;
62   }
63
64   /**
65    * If matching is case sensitive or not
66    *
67    * @return true if the matching is case sensitive
68    */

69   public boolean isCaseSentive()
70   {
71     return isCaseSentive;
72   }
73
74   /**
75    * If monitoring is activated or not.
76    *
77    * @return true if monitoring is activated for this pattern
78    */

79   public boolean isMonitoring()
80   {
81     return monitoring;
82   }
83
84   /**
85    * Get query pattern
86    *
87    * @return the query pattern
88    */

89   public String JavaDoc getQueryPattern()
90   {
91     return queryPattern.toString();
92   }
93
94   /**
95    * Set the matching case sensitiveness
96    *
97    * @param b true if matching is case sensitive
98    */

99   public void setCaseSentive(boolean b)
100   {
101     isCaseSentive = b;
102   }
103
104   /**
105    * Set the monitoring on or off
106    *
107    * @param b true if monitoring must be activated for this rule
108    */

109   public void setMonitoring(boolean b)
110   {
111     monitoring = b;
112   }
113
114   /**
115    * Sets the query pattern
116    *
117    * @param queryPattern the queryPattern
118    */

119   public void setQueryPattern(String JavaDoc queryPattern)
120   {
121     this.queryPattern = Pattern.compile(queryPattern);
122   }
123
124   /**
125    * If the pattern apply to the skeleton ot the instanciated query.
126    *
127    * @return true if the pattern apply to the query skeleton
128    */

129   public boolean isApplyToSkeleton()
130   {
131     return applyToSkeleton;
132   }
133
134   /**
135    * Set to true if the pattern apply to the query skeleton
136    *
137    * @param b true if the pattern apply to the query skeleton
138    */

139   public void setApplyToSkeleton(boolean b)
140   {
141     applyToSkeleton = b;
142   }
143
144   /**
145    * Returns true if the given query matches the pattern of this rule. This
146    * function applies the applytoSkeleton rule.
147    *
148    * @param request the query
149    * @return the SQL that matches the rule or null if it does not match
150    */

151   public String JavaDoc matches(AbstractRequest request)
152   {
153     if (queryPattern.matcher(request.getSqlOrTemplate()).matches())
154       return request.getSqlOrTemplate();
155     else
156       return null;
157   }
158
159   /**
160    * @see org.continuent.sequoia.common.xml.XmlComponent#getXml()
161    */

162   public String JavaDoc getXml()
163   {
164     String JavaDoc info = "<" + DatabasesXmlTags.ELT_SQLMonitoringRule + " "
165         + DatabasesXmlTags.ATT_queryPattern + "=\"" + getQueryPattern() + "\" "
166         + DatabasesXmlTags.ATT_caseSensitive + "=\"" + isCaseSentive() + "\" "
167         + DatabasesXmlTags.ATT_applyToSkeleton + "=\"" + isApplyToSkeleton()
168         + "\" " + DatabasesXmlTags.ATT_monitoring + "=\"";
169     if (isMonitoring())
170       info += "on";
171     else
172       info += "off";
173     info += "\"/>";
174     return info;
175   }
176
177 }
178
Popular Tags