KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > cache > result > ResultCacheRule


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): Nicolas Modrzyk
22  * Contributor(s): Emmanuel Cecchet.
23  */

24
25 package org.objectweb.cjdbc.controller.cache.result;
26
27 import org.apache.regexp.RE;
28 import org.apache.regexp.RESyntaxException;
29 import org.objectweb.cjdbc.common.log.Trace;
30 import org.objectweb.cjdbc.common.sql.AbstractRequest;
31 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
32 import org.objectweb.cjdbc.common.xml.XmlComponent;
33
34 /**
35  * This is the to define cache rules in the cache. A <code>ResultCacheRule</code>
36  * is defined by a queryPattern, set to 'default' if default rule, and a <code>CacheBehavior</code>.
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk</a>
39  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
40  * @version 1.0
41  */

42 public class ResultCacheRule implements XmlComponent
43 {
44   Trace logger = Trace.getLogger(ResultCacheRule.class.getName());
45   private RE queryPattern;
46   private String JavaDoc queryString;
47   private boolean isCaseSensitive;
48   private boolean applyToSkeleton;
49   private long timestampResolution;
50   private CacheBehavior behavior;
51
52   /**
53    * Creates a new <code>ResultCacheRule</code>
54    *
55    * @param queryString for this rule
56    * @param caseSensitive true if matching is case sensitive
57    * @param applyToSkeleton true if rule apply to query skeleton
58    * @param timestampResolution timestamp resolution for NOW() macro
59    * @throws RESyntaxException if the query pattern is invalid
60    */

61   public ResultCacheRule(
62     String JavaDoc queryString,
63     boolean caseSensitive,
64     boolean applyToSkeleton,
65     long timestampResolution)
66     throws RESyntaxException
67   {
68     this.queryString = queryString;
69     queryPattern = new RE(queryString);
70     this.isCaseSensitive = caseSensitive;
71     this.applyToSkeleton = applyToSkeleton;
72     this.timestampResolution = timestampResolution;
73   }
74
75   /**
76    * Get the query pattern
77    *
78    * @return the queryPattern for this <code>ResultCacheRule</code>
79    */

80   public RE getQueryPattern()
81   {
82     return this.queryPattern;
83   }
84
85   /**
86    * Get the cache behavior
87    *
88    * @return the <code>CacheBehavior</code> for this <code>ResultCacheRule</code>
89    */

90   public CacheBehavior getCacheBehavior()
91   {
92     return behavior;
93   }
94
95   /**
96    * Set the cache behavior
97    *
98    * @param behavior behavior for this rule
99    */

100   public void setCacheBehavior(CacheBehavior behavior)
101   {
102     this.behavior = behavior;
103   }
104
105   /**
106    * Retrieve the timestamp resolution of this scheduler
107    *
108    * @return timestampResolution
109    */

110   public long getTimestampResolution()
111   {
112     return this.timestampResolution;
113   }
114
115   /**
116    * @param request we may want to add to the cache
117    * @return the behavior to get the entry
118    */

119   public CacheBehavior matches(AbstractRequest request)
120   {
121     if (queryPattern.match(request.getSQL()))
122     {
123       return behavior;
124     }
125     else
126       return null;
127   }
128
129   /**
130    * @see org.objectweb.cjdbc.common.xml.XmlComponent#getXml()
131    */

132   public String JavaDoc getXml()
133   {
134     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
135     info.append(
136       "<"
137         + DatabasesXmlTags.ELT_ResultCacheRule
138         + " "
139         + DatabasesXmlTags.ATT_queryPattern
140         + "=\""
141         + queryString
142         + "\" "
143         + DatabasesXmlTags.ATT_caseSensitive
144         + "=\""
145         + isCaseSensitive
146         + "\" "
147         + DatabasesXmlTags.ATT_applyToSkeleton
148         + "=\""
149         + applyToSkeleton
150         + "\" "
151         + DatabasesXmlTags.ATT_timestampResolution
152         + "=\""
153         + timestampResolution / 1000
154         + "\" >");
155     info.append(behavior.getXml());
156     info.append("</" + DatabasesXmlTags.ELT_ResultCacheRule + ">");
157     return info.toString();
158   }
159
160 }
Popular Tags