001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.hibernate.cache;
023:
024: import java.util.Properties;
025:
026: import javax.management.MBeanServer;
027: import javax.management.ObjectName;
028: import javax.transaction.TransactionManager;
029:
030: import org.hibernate.cache.Cache;
031: import org.hibernate.cache.CacheException;
032: import org.hibernate.cache.CacheProvider;
033: import org.jboss.cache.TreeCache;
034: import org.jboss.cache.TreeCacheMBean;
035: import org.jboss.logging.Logger;
036: import org.jboss.mx.util.MBeanProxyExt;
037: import org.jboss.mx.util.MBeanServerLocator;
038: import org.jboss.tm.TransactionManagerLocator;
039:
040: /**
041: * A Hibernate CacheProvider implementation which knows how to
042: * obtain a deployed JBossCache via its JMX ObjectName.
043: *
044: * @version <tt>$Revision: 57193 $</tt>
045: * @author <a href="mailto:steve@hibernate.org">Steve Ebersole</a>
046: */
047: public class DeployedTreeCacheProvider implements CacheProvider {
048: private static final Logger log = Logger
049: .getLogger(DeployedTreeCacheProvider.class);
050:
051: public static final String OBJECT_NAME_PROP = "hibernate.treecache.objectName";
052: public static final String DEFAULT_OBJECT_NAME = "jboss.cache:service=HibernateTreeCache";
053:
054: private TreeCache deployedTreeCache;
055:
056: public void start(Properties properties) throws CacheException {
057: // Determine the TreeCache MBean ObjectName.
058: String configObjectName = properties.getProperty(
059: OBJECT_NAME_PROP, DEFAULT_OBJECT_NAME);
060: ObjectName objectName;
061: try {
062: objectName = new ObjectName(configObjectName);
063: } catch (Throwable t) {
064: throw new CacheException("Malformed TreeCache ObjectName");
065: }
066:
067: TreeCacheMBean mbean;
068: try {
069: MBeanServer server = MBeanServerLocator.locateJBoss();
070: mbean = (TreeCacheMBean) MBeanProxyExt.create(
071: TreeCacheMBean.class, objectName, server);
072: } catch (Throwable t) {
073: log.warn(
074: "Unable to locate TreeCache MBean under object name ["
075: + configObjectName + "]", t);
076: throw new CacheException(
077: "Unable to locate TreeCache MBean under object name ["
078: + configObjectName + "]");
079: }
080:
081: deployedTreeCache = mbean.getInstance();
082: }
083:
084: public void stop() {
085: deployedTreeCache = null;
086: }
087:
088: public boolean isMinimalPutsEnabledByDefault() {
089: return true;
090: }
091:
092: /**
093: * Called by Hibernate in order to build the given named cache "region".
094: *
095: * @param name The cache "region" name.
096: * @param properties The configuration properties.
097: * @return The constructed Cache wrapper around the jndi-deployed TreeCache.
098: * @throws CacheException Generally indicates a problem locating the TreeCache.
099: */
100: public Cache buildCache(String name, Properties properties)
101: throws CacheException {
102: TransactionManager tm = TransactionManagerLocator.getInstance()
103: .locate();
104: return new org.hibernate.cache.TreeCache(deployedTreeCache,
105: name, tm);
106: }
107:
108: public long nextTimestamp() {
109: return System.currentTimeMillis() / 100;
110: }
111: }
|