1 package org.hibernate.cache; 3 4 import java.util.Properties ; 5 6 import javax.naming.Context ; 7 import javax.naming.InitialContext ; 8 import javax.naming.NamingException ; 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 22 public abstract class AbstractJndiBoundCacheProvider implements CacheProvider { 23 24 private static final Log log = LogFactory.getLog( AbstractJndiBoundCacheProvider.class ); 25 private Object cache; 26 27 protected void prepare(Properties properties) { 28 } 30 31 protected void release() { 32 } 34 35 41 public final void start(Properties properties) throws CacheException { 42 String 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 54 public final void stop() { 55 release(); 56 cache = null; 57 } 58 59 private Object locateCache(String jndiNamespace, Properties jndiProperties) { 60 61 Context ctx = null; 62 try { 63 ctx = new InitialContext ( jndiProperties ); 64 return ctx.lookup( jndiNamespace ); 65 } 66 catch (NamingException ne) { 67 String 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 ne ) { 77 log.info( "Unable to release initial context", ne ); 78 } 79 } 80 } 81 } 82 83 public Object getCache() { 84 return cache; 85 } 86 } 87 | Popular Tags |