KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: EhCacheProvider.java,v 1.6 2005/06/28 18:35:10 steveebersole Exp $
2
/* ====================================================================
3  * The Apache Software License, Version 1.1
4  *
5  * Copyright (c) 2003 - 2004 Greg Luck. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Greg Luck
22  * (http://sourceforge.net/users/gregluck) and contributors.
23  * See http://sourceforge.net/project/memberlist.php?group_id=93232
24  * for a list of contributors"
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "EHCache" must not be used to endorse or promote products
29  * derived from this software without prior written permission. For written
30  * permission, please contact Greg Luck (gregluck at users.sourceforge.net).
31  *
32  * 5. Products derived from this software may not be called "EHCache"
33  * nor may "EHCache" appear in their names without prior written
34  * permission of Greg Luck.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL GREG LUCK OR OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by contributors
51  * individuals on behalf of the EHCache project. For more
52  * information on EHCache, please see <http://ehcache.sourceforge.net/>.
53  *
54  */

55 package org.hibernate.cache;
56
57 import net.sf.ehcache.CacheManager;
58
59 import java.util.Properties JavaDoc;
60
61 import org.apache.commons.logging.Log;
62 import org.apache.commons.logging.LogFactory;
63
64 /**
65  * Cache Provider plugin for Hibernate
66  *
67  * Use <code>hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider</code>
68  * in Hibernate 3.x or later
69  *
70  * Taken from EhCache 0.9 distribution
71  * @author Greg Luck
72  * @author Emmanuel Bernard
73  */

74 public class EhCacheProvider implements CacheProvider {
75
76     private static final Log log = LogFactory.getLog(EhCacheProvider.class);
77
78     // CacheManager.create() actually returns a singleton reference, which is causing
79
// problems with users attempting to use multiple SessionFactories all using the
80
// EhCacheProvider in the same classloader. The work-around is to use simple reference
81
// counting here...
82
private static int referenceCount = 0;
83
84     private CacheManager manager;
85
86     /**
87      * Builds a Cache.
88      * <p>
89      * Even though this method provides properties, they are not used.
90      * Properties for EHCache are specified in the ehcache.xml file.
91      * Configuration will be read from ehcache.xml for a cache declaration
92      * where the name attribute matches the name parameter in this builder.
93      *
94      * @param name the name of the cache. Must match a cache configured in ehcache.xml
95      * @param properties not used
96      * @return a newly built cache will be built and initialised
97      * @throws CacheException inter alia, if a cache of the same name already exists
98      */

99     public Cache buildCache(String JavaDoc name, Properties JavaDoc properties) throws CacheException {
100         try {
101             net.sf.ehcache.Cache cache = manager.getCache(name);
102             if (cache == null) {
103                 log.warn("Could not find configuration [" + name + "]; using defaults.");
104                 manager.addCache(name);
105                 cache = manager.getCache(name);
106                 log.debug("started EHCache region: " + name);
107             }
108             return new EhCache(cache);
109         }
110         catch (net.sf.ehcache.CacheException e) {
111             throw new CacheException(e);
112         }
113     }
114
115     /**
116      * Returns the next timestamp.
117      */

118     public long nextTimestamp() {
119         return Timestamper.next();
120     }
121
122     /**
123      * Callback to perform any necessary initialization of the underlying cache implementation
124      * during SessionFactory construction.
125      *
126      * @param properties current configuration settings.
127      */

128     public void start(Properties JavaDoc properties) throws CacheException {
129         try {
130             manager = CacheManager.create();
131             referenceCount++;
132         }
133         catch (net.sf.ehcache.CacheException e) {
134             throw new CacheException(e);
135         }
136     }
137
138     /**
139      * Callback to perform any necessary cleanup of the underlying cache implementation
140      * during SessionFactory.close().
141      */

142     public void stop() {
143         if ( manager != null ) {
144             if ( --referenceCount == 0 ) {
145                 manager.shutdown();
146             }
147             manager = null;
148         }
149     }
150
151     public boolean isMinimalPutsEnabledByDefault() {
152         return false;
153     }
154
155 }
156
Popular Tags