KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: AbstractJndiBoundCacheProvider.java,v 1.2 2005/03/16 06:01:15 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.util.Properties JavaDoc;
5
6 import javax.naming.Context JavaDoc;
7 import javax.naming.InitialContext JavaDoc;
8 import javax.naming.NamingException JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.hibernate.cfg.Environment;
13 import org.hibernate.util.NamingHelper;
14 import org.hibernate.util.StringHelper;
15
16 /**
17  * Support for CacheProvider implementations which are backed by caches bound
18  * into JNDI namespace.
19  *
20  * @author Steve Ebersole
21  */

22 public abstract class AbstractJndiBoundCacheProvider implements CacheProvider {
23
24     private static final Log log = LogFactory.getLog( AbstractJndiBoundCacheProvider.class );
25     private Object JavaDoc cache;
26
27     protected void prepare(Properties JavaDoc properties) {
28         // Do nothing; subclasses may override.
29
}
30
31     protected void release() {
32         // Do nothing; subclasses may override.
33
}
34
35     /**
36      * Callback to perform any necessary initialization of the underlying cache implementation during SessionFactory
37      * construction.
38      *
39      * @param properties current configuration settings.
40      */

41     public final void start(Properties JavaDoc properties) throws CacheException {
42         String JavaDoc jndiNamespace = properties.getProperty( Environment.CACHE_NAMESPACE );
43         if ( StringHelper.isEmpty( jndiNamespace ) ) {
44             throw new CacheException( "No JNDI namespace specified for cache" );
45         }
46         cache = locateCache( jndiNamespace, NamingHelper.getJndiProperties( properties ) );
47         prepare( properties );
48     }
49
50     /**
51      * Callback to perform any necessary cleanup of the underlying cache
52      * implementation during SessionFactory.close().
53      */

54     public final void stop() {
55         release();
56         cache = null;
57     }
58
59     private Object JavaDoc locateCache(String JavaDoc jndiNamespace, Properties JavaDoc jndiProperties) {
60
61         Context JavaDoc ctx = null;
62         try {
63             ctx = new InitialContext JavaDoc( jndiProperties );
64             return ctx.lookup( jndiNamespace );
65         }
66         catch (NamingException JavaDoc ne) {
67             String JavaDoc msg = "Unable to retreive Cache from JNDI [" + jndiNamespace + "]";
68             log.info( msg, ne );
69             throw new CacheException( msg );
70         }
71         finally {
72             if ( ctx != null ) {
73                 try {
74                     ctx.close();
75                 }
76                 catch( NamingException JavaDoc ne ) {
77                     log.info( "Unable to release initial context", ne );
78                 }
79             }
80         }
81     }
82     
83     public Object JavaDoc getCache() {
84         return cache;
85     }
86 }
87
Popular Tags