KickJava   Java API By Example, From Geeks To Geeks.

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


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):
21  */

22
23 package org.continuent.sequoia.controller.cache.result;
24
25 import java.util.Hashtable JavaDoc;
26
27 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
28 import org.continuent.sequoia.controller.cache.result.rules.EagerCaching;
29 import org.continuent.sequoia.controller.cache.result.rules.NoCaching;
30 import org.continuent.sequoia.controller.cache.result.rules.RelaxedCaching;
31
32 /**
33  * Create a cache that conforms to AbstractResultCache, that is implementation
34  * independant
35  *
36  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
37  */

38 public class ResultCacheFactory
39 {
40   /**
41    * Get an instance of the current cache implementation
42    *
43    * @param granularityValue of the parsing
44    * @param maxEntries to the cache
45    * @param pendingTimeout before pending query timeout
46    * @return <code>ResultCache</code> implementation of the
47    * <code>AbstractResultCache</code>
48    * @throws InstantiationException if parsing granularity is not valid
49    */

50   public static AbstractResultCache getCacheInstance(int granularityValue,
51       int maxEntries, int pendingTimeout) throws InstantiationException JavaDoc
52   {
53     AbstractResultCache currentRequestCache = null;
54     switch (granularityValue)
55     {
56       case CachingGranularities.TABLE :
57         currentRequestCache = new ResultCacheTable(maxEntries, pendingTimeout);
58         break;
59       case CachingGranularities.DATABASE :
60         currentRequestCache = new ResultCacheDatabase(maxEntries,
61             pendingTimeout);
62         break;
63       case CachingGranularities.COLUMN :
64         currentRequestCache = new ResultCacheColumn(maxEntries, pendingTimeout);
65         break;
66       case CachingGranularities.COLUMN_UNIQUE :
67         currentRequestCache = new ResultCacheColumnUnique(maxEntries,
68             pendingTimeout);
69         break;
70       default :
71         throw new InstantiationException JavaDoc("Invalid Granularity Value");
72     }
73     return currentRequestCache;
74   }
75
76   /**
77    * Get an instance of a cache behavior for this cache
78    *
79    * @param behaviorString representation of this cache behavior, xml tag
80    * @param options for different cache rules
81    * @return an instance of a cache behavior
82    */

83   public static CacheBehavior getCacheBehaviorInstance(String JavaDoc behaviorString,
84       Hashtable JavaDoc options)
85   {
86     if (behaviorString.equalsIgnoreCase(DatabasesXmlTags.ELT_NoCaching))
87       return new NoCaching();
88     if (behaviorString.equals(DatabasesXmlTags.ELT_EagerCaching))
89     {
90       // Timeout is in seconds: *1000
91
// 0, is no timeout, and 0x1000=0 !
92
long timeout = 1000 * Long.parseLong((String JavaDoc) options
93           .get(DatabasesXmlTags.ATT_timeout));
94       return new EagerCaching(timeout);
95     }
96     if (behaviorString.equals(DatabasesXmlTags.ELT_RelaxedCaching))
97     {
98       // Timeout is in seconds: *1000
99
long timeout = 1000 * Long.parseLong((String JavaDoc) options
100           .get(DatabasesXmlTags.ATT_timeout));
101       boolean keepIfNotDirty = new Boolean JavaDoc((String JavaDoc) options
102           .get(DatabasesXmlTags.ATT_keepIfNotDirty)).booleanValue();
103       return new RelaxedCaching(keepIfNotDirty, timeout);
104     }
105     else
106       return null;
107   }
108 }
Popular Tags