KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > CacheFactory


1 //$Id: CacheFactory.java,v 1.5 2005/02/12 07:19:08 steveebersole Exp $
2
package org.hibernate.cache;
3
4 import java.util.Properties JavaDoc;
5
6 import org.hibernate.HibernateException;
7 import org.hibernate.MappingException;
8 import org.hibernate.cfg.Settings;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 /**
13  * @author Gavin King
14  */

15 public final class CacheFactory {
16     
17     private static final Log log = LogFactory.getLog(CacheFactory.class);
18     
19     private CacheFactory() {}
20     
21     public static final String JavaDoc READ_ONLY = "read-only";
22     public static final String JavaDoc READ_WRITE = "read-write";
23     public static final String JavaDoc NONSTRICT_READ_WRITE = "nonstrict-read-write";
24     public static final String JavaDoc TRANSACTIONAL = "transactional";
25     
26     public static CacheConcurrencyStrategy createCache(
27         final String JavaDoc concurrencyStrategy,
28         String JavaDoc regionName,
29         final boolean mutable,
30         final Settings settings,
31         final Properties JavaDoc properties)
32     throws HibernateException {
33         
34         if ( concurrencyStrategy==null || !settings.isSecondLevelCacheEnabled() ) return null; //no cache
35

36         String JavaDoc prefix = settings.getCacheRegionPrefix();
37         if ( prefix!=null ) regionName = prefix + '.' + regionName;
38         
39         if ( log.isDebugEnabled() ) log.debug("instantiating cache region: " + regionName + " usage strategy: " + concurrencyStrategy);
40         
41         final CacheConcurrencyStrategy ccs;
42         if ( concurrencyStrategy.equals(READ_ONLY) ) {
43             if (mutable) log.warn( "read-only cache configured for mutable class: " + regionName );
44             ccs = new ReadOnlyCache();
45         }
46         else if ( concurrencyStrategy.equals(READ_WRITE) ) {
47             ccs = new ReadWriteCache();
48         }
49         else if ( concurrencyStrategy.equals(NONSTRICT_READ_WRITE) ) {
50             ccs = new NonstrictReadWriteCache();
51         }
52         else if ( concurrencyStrategy.equals(TRANSACTIONAL) ) {
53             ccs = new TransactionalCache();
54         }
55         else {
56             throw new MappingException("cache usage attribute should be read-write, read-only, nonstrict-read-write or transactional");
57         }
58         
59         final Cache impl;
60         try {
61             impl = settings.getCacheProvider().buildCache(regionName, properties);
62         }
63         catch (CacheException e) {
64             throw new HibernateException( "Could not instantiate cache implementation", e );
65         }
66         ccs.setCache(impl);
67         
68         return ccs;
69     }
70         
71 }
72
Popular Tags