KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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