KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > hibernate > cache > DeployedTreeCacheProvider


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.hibernate.cache;
8
9 import java.util.Properties JavaDoc;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13 import javax.transaction.TransactionManager JavaDoc;
14
15 import org.hibernate.cache.Cache;
16 import org.hibernate.cache.CacheException;
17 import org.hibernate.cache.CacheProvider;
18 import org.jboss.cache.jmx.CacheJmxWrapperMBean;
19 import org.jboss.mx.util.MBeanProxyExt;
20 import org.jboss.mx.util.MBeanServerLocator;
21 import org.jboss.tm.TransactionManagerLocator;
22
23 /**
24  * A Hibernate CacheProvider implementation which knows how to
25  * obtain a deployed JBossCache via its JMX ObjectName.
26  *
27  * @version <tt>$Revision: 58543 $</tt>
28  * @author <a HREF="mailto:steve@hibernate.org">Steve Ebersole</a>
29  */

30 public class DeployedTreeCacheProvider implements CacheProvider
31 {
32    public static final String JavaDoc OBJECT_NAME_PROP = "hibernate.treecache.objectName";
33    public static final String JavaDoc DEFAULT_OBJECT_NAME = "jboss.cache:service=HibernateTreeCache";
34
35    private org.jboss.cache.Cache deployedTreeCache;
36
37    public void start(Properties JavaDoc properties) throws CacheException
38    {
39       // Determine the TreeCache MBean ObjectName.
40
String JavaDoc configObjectName = properties.getProperty( OBJECT_NAME_PROP, DEFAULT_OBJECT_NAME );
41       ObjectName JavaDoc objectName;
42       try
43       {
44          objectName = new ObjectName JavaDoc( configObjectName );
45       }
46       catch( Throwable JavaDoc t )
47       {
48          throw new CacheException( "Malformed TreeCache ObjectName");
49       }
50
51       CacheJmxWrapperMBean mbean;
52       try
53       {
54          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
55          mbean = (CacheJmxWrapperMBean) MBeanProxyExt.create(CacheJmxWrapperMBean.class, objectName, server);
56          deployedTreeCache = mbean.getCache();
57       }
58       catch( Throwable JavaDoc t )
59       {
60          throw new CacheException( "Unable to locate TreeCache MBean under object name [" + configObjectName + "]" );
61       }
62    }
63
64    public void stop()
65    {
66       deployedTreeCache = null;
67    }
68
69    public boolean isMinimalPutsEnabledByDefault()
70    {
71       return true;
72    }
73
74    /**
75     * Called by Hibernate in order to build the given named cache "region".
76     *
77     * @param name The cache "region" name.
78     * @param properties The configuration properties.
79     * @return The constructed Cache wrapper around the jndi-deployed TreeCache.
80     * @throws CacheException Generally indicates a problem locating the TreeCache.
81     */

82    public Cache buildCache(String JavaDoc name, Properties JavaDoc properties) throws CacheException
83    {
84       TransactionManager JavaDoc tm = TransactionManagerLocator.getInstance().locate();
85       return new JBCCache( deployedTreeCache, name, tm );
86    }
87
88    public long nextTimestamp()
89    {
90         return System.currentTimeMillis() / 100;
91    }
92 }
93
Popular Tags