Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

SQLMonitoringRule.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): ______________________________________.
00023  */
00024 package org.objectweb.cjdbc.controller.monitoring;
00025 
00026 import org.apache.regexp.RE;
00027 import org.apache.regexp.RESyntaxException;
00028 import org.objectweb.cjdbc.common.sql.AbstractRequest;
00029 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00030 
00031 /**
00032  * This class implements a SQL monitoring rule.
00033  * 
00034  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00035  * @version 1.0
00036  */
00037 public class SQLMonitoringRule
00038 {
00039   private RE queryPattern;
00040   private boolean isCaseSentive;
00041   private boolean applyToSkeleton;
00042   private boolean monitoring;
00043 
00044   /**
00045    * Creates a new SQL Monitoring rule
00046    * 
00047    * @param queryPattern the query pattern to match
00048    * @param isCaseSentive true if matching is case sensitive
00049    * @param applyToSkeleton true if matching applies to the query skeleton
00050    * @param monitoring true if the request must be monitored
00051    */
00052   public SQLMonitoringRule(
00053     String queryPattern,
00054     boolean isCaseSentive,
00055     boolean applyToSkeleton,
00056     boolean monitoring)
00057   {
00058     try
00059     {
00060       this.queryPattern = new RE(queryPattern);
00061     }
00062     catch (RESyntaxException e)
00063     {
00064       throw new RuntimeException(
00065         "Invalid regexp in SQL Monitoring rule (" + e + ")");
00066     }
00067     this.isCaseSentive = isCaseSentive;
00068     if (isCaseSentive)
00069       this.queryPattern.setMatchFlags(RE.MATCH_NORMAL);
00070     else
00071       this.queryPattern.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
00072     this.applyToSkeleton = applyToSkeleton;
00073     this.monitoring = monitoring;
00074   }
00075 
00076   /**
00077    * If matching is case sensitive or not
00078    * 
00079    * @return true if the matching is case sensitive
00080    */
00081   public boolean isCaseSentive()
00082   {
00083     return isCaseSentive;
00084   }
00085 
00086   /**
00087    * If monitoring is activated or not.
00088    * 
00089    * @return true if monitoring is activated for this pattern
00090    */
00091   public boolean isMonitoring()
00092   {
00093     return monitoring;
00094   }
00095 
00096   /**
00097    * Get query pattern
00098    * 
00099    * @return the query pattern
00100    */
00101   public String getQueryPattern()
00102   {
00103     return queryPattern.toString();
00104   }
00105 
00106   /**
00107    * Set the matching case sensitiveness
00108    * 
00109    * @param b true if matching is case sensitive
00110    */
00111   public void setCaseSentive(boolean b)
00112   {
00113     isCaseSentive = b;
00114   }
00115 
00116   /**
00117    * Set the monitoring on or off
00118    * 
00119    * @param b true if monitoring must be activated for this rule
00120    */
00121   public void setMonitoring(boolean b)
00122   {
00123     monitoring = b;
00124   }
00125 
00126   /**
00127    * Sets the query pattern
00128    * 
00129    * @param queryPattern the queryPattern
00130    */
00131   public void setQueryPattern(String queryPattern)
00132   {
00133     try
00134     {
00135       this.queryPattern = new RE(queryPattern);
00136     }
00137     catch (RESyntaxException e)
00138     {
00139       throw new RuntimeException(
00140         "Invalid regexp in SQL Monitoring rule (" + e + ")");
00141     }
00142   }
00143 
00144   /**
00145    * If the pattern apply to the skeleton ot the instanciated query.
00146    * 
00147    * @return true if the pattern apply to the query skeleton
00148    */
00149   public boolean isApplyToSkeleton()
00150   {
00151     return applyToSkeleton;
00152   }
00153 
00154   /**
00155    * Set to true if the pattern apply to the query skeleton
00156    * 
00157    * @param b true if the pattern apply to the query skeleton
00158    */
00159   public void setApplyToSkeleton(boolean b)
00160   {
00161     applyToSkeleton = b;
00162   }
00163 
00164   /**
00165    * Returns true if the given query matches the pattern of this rule. This
00166    * function applies the applytoSkeleton rule.
00167    * 
00168    * @param request the query
00169    * @return the SQL that matches the rule or null if it does not match
00170    */
00171   public String matches(AbstractRequest request)
00172   {
00173     if (applyToSkeleton)
00174     {
00175       String skel = request.getSqlSkeleton();
00176       if (skel == null)
00177         return null;
00178       else
00179       {
00180         if (queryPattern.match(skel))
00181           return skel;
00182         else
00183           return null;
00184       }
00185     }
00186     else
00187     {
00188       if (queryPattern.match(request.getSQL()))
00189         return request.getSQL();
00190       else
00191         return null;
00192     }
00193   }
00194 
00195   /**
00196    * @see org.objectweb.cjdbc.common.xml.XmlComponent#getXml()
00197    */
00198   public String getXml()
00199   {
00200     String info =
00201       "<"
00202         + DatabasesXmlTags.ELT_SQLMonitoringRule
00203         + " "
00204         + DatabasesXmlTags.ATT_queryPattern
00205         + "=\""
00206         + getQueryPattern()
00207         + "\" "
00208         + DatabasesXmlTags.ATT_caseSensitive
00209         + "=\""
00210         + isCaseSentive()
00211         + "\" "
00212         + DatabasesXmlTags.ATT_applyToSkeleton
00213         + "=\""
00214         + isApplyToSkeleton()
00215         + "\" "
00216         + DatabasesXmlTags.ATT_monitoring
00217         + "=\"";
00218     if (isMonitoring())
00219       info += "on";
00220     else
00221       info += "off";
00222     info += "\"/>";
00223     return info;
00224   }
00225 
00226 }

Generated on Mon Apr 11 22:01:35 2005 for C-JDBC by  doxygen 1.3.9.1