KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > cache > result > ResultCacheRule


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

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

41 public class ResultCacheRule implements XmlComponent
42 {
43   Trace logger = Trace.getLogger(ResultCacheRule.class
44                                    .getName());
45   private Pattern JavaDoc 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    */

60   public ResultCacheRule(String JavaDoc queryString, boolean caseSensitive,
61       boolean applyToSkeleton, long timestampResolution)
62   {
63     this.queryString = queryString;
64     queryPattern = Pattern.compile(queryString);
65     this.isCaseSensitive = caseSensitive;
66     this.applyToSkeleton = applyToSkeleton;
67     this.timestampResolution = timestampResolution;
68   }
69
70   /**
71    * Get the query pattern
72    *
73    * @return the queryPattern for this <code>ResultCacheRule</code>
74    */

75   public Pattern JavaDoc getQueryPattern()
76   {
77     return this.queryPattern;
78   }
79
80   /**
81    * Get the cache behavior
82    *
83    * @return the <code>CacheBehavior</code> for this
84    * <code>ResultCacheRule</code>
85    */

86   public CacheBehavior getCacheBehavior()
87   {
88     return behavior;
89   }
90
91   /**
92    * Set the cache behavior
93    *
94    * @param behavior behavior for this rule
95    */

96   public void setCacheBehavior(CacheBehavior behavior)
97   {
98     this.behavior = behavior;
99   }
100
101   /**
102    * Retrieve the timestamp resolution of this scheduler
103    *
104    * @return timestampResolution
105    */

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

115   public CacheBehavior matches(AbstractRequest request)
116   {
117     if (queryPattern.matcher(request.getSqlOrTemplate()).matches())
118       return behavior;
119     else
120       return null;
121   }
122
123   /**
124    * @see org.continuent.sequoia.common.xml.XmlComponent#getXml()
125    */

126   public String JavaDoc getXml()
127   {
128     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
129     info.append("<" + DatabasesXmlTags.ELT_ResultCacheRule + " "
130         + DatabasesXmlTags.ATT_queryPattern + "=\"" + queryString + "\" "
131         + DatabasesXmlTags.ATT_caseSensitive + "=\"" + isCaseSensitive + "\" "
132         + DatabasesXmlTags.ATT_applyToSkeleton + "=\"" + applyToSkeleton
133         + "\" " + DatabasesXmlTags.ATT_timestampResolution + "=\""
134         + timestampResolution / 1000 + "\" >");
135     info.append(behavior.getXml());
136     info.append("</" + DatabasesXmlTags.ELT_ResultCacheRule + ">");
137     return info.toString();
138   }
139
140 }
Popular Tags