001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): ______________________________________.
021: */package org.continuent.sequoia.controller.monitoring;
022:
023: import java.util.regex.Pattern;
024:
025: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
026: import org.continuent.sequoia.controller.requests.AbstractRequest;
027:
028: /**
029: * This class implements a SQL monitoring rule.
030: *
031: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
032: * @version 1.0
033: */
034: public class SQLMonitoringRule {
035: private Pattern queryPattern;
036: private boolean isCaseSentive;
037: private boolean applyToSkeleton;
038: private boolean monitoring;
039:
040: /**
041: * Creates a new SQL Monitoring rule
042: *
043: * @param queryPattern the query pattern to match
044: * @param isCaseSentive true if matching is case sensitive
045: * @param applyToSkeleton true if matching applies to the query skeleton
046: * @param monitoring true if the request must be monitored
047: */
048: public SQLMonitoringRule(String queryPattern,
049: boolean isCaseSentive, boolean applyToSkeleton,
050: boolean monitoring) {
051: this .isCaseSentive = isCaseSentive;
052: if (isCaseSentive)
053: this .queryPattern = Pattern.compile(queryPattern);
054: else
055: this .queryPattern = Pattern.compile(queryPattern,
056: Pattern.CASE_INSENSITIVE);
057: this .applyToSkeleton = applyToSkeleton;
058: this .monitoring = monitoring;
059: }
060:
061: /**
062: * If matching is case sensitive or not
063: *
064: * @return true if the matching is case sensitive
065: */
066: public boolean isCaseSentive() {
067: return isCaseSentive;
068: }
069:
070: /**
071: * If monitoring is activated or not.
072: *
073: * @return true if monitoring is activated for this pattern
074: */
075: public boolean isMonitoring() {
076: return monitoring;
077: }
078:
079: /**
080: * Get query pattern
081: *
082: * @return the query pattern
083: */
084: public String getQueryPattern() {
085: return queryPattern.toString();
086: }
087:
088: /**
089: * Set the matching case sensitiveness
090: *
091: * @param b true if matching is case sensitive
092: */
093: public void setCaseSentive(boolean b) {
094: isCaseSentive = b;
095: }
096:
097: /**
098: * Set the monitoring on or off
099: *
100: * @param b true if monitoring must be activated for this rule
101: */
102: public void setMonitoring(boolean b) {
103: monitoring = b;
104: }
105:
106: /**
107: * Sets the query pattern
108: *
109: * @param queryPattern the queryPattern
110: */
111: public void setQueryPattern(String queryPattern) {
112: this .queryPattern = Pattern.compile(queryPattern);
113: }
114:
115: /**
116: * If the pattern apply to the skeleton ot the instanciated query.
117: *
118: * @return true if the pattern apply to the query skeleton
119: */
120: public boolean isApplyToSkeleton() {
121: return applyToSkeleton;
122: }
123:
124: /**
125: * Set to true if the pattern apply to the query skeleton
126: *
127: * @param b true if the pattern apply to the query skeleton
128: */
129: public void setApplyToSkeleton(boolean b) {
130: applyToSkeleton = b;
131: }
132:
133: /**
134: * Returns true if the given query matches the pattern of this rule. This
135: * function applies the applytoSkeleton rule.
136: *
137: * @param request the query
138: * @return the SQL that matches the rule or null if it does not match
139: */
140: public String matches(AbstractRequest request) {
141: if (queryPattern.matcher(request.getSqlOrTemplate()).matches())
142: return request.getSqlOrTemplate();
143: else
144: return null;
145: }
146:
147: /**
148: * @see org.continuent.sequoia.common.xml.XmlComponent#getXml()
149: */
150: public String getXml() {
151: String info = "<" + DatabasesXmlTags.ELT_SQLMonitoringRule
152: + " " + DatabasesXmlTags.ATT_queryPattern + "=\""
153: + getQueryPattern() + "\" "
154: + DatabasesXmlTags.ATT_caseSensitive + "=\""
155: + isCaseSentive() + "\" "
156: + DatabasesXmlTags.ATT_applyToSkeleton + "=\""
157: + isApplyToSkeleton() + "\" "
158: + DatabasesXmlTags.ATT_monitoring + "=\"";
159: if (isMonitoring())
160: info += "on";
161: else
162: info += "off";
163: info += "\"/>";
164: return info;
165: }
166:
167: }
|