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.ejb3.entity;
023:
024: import java.util.Properties;
025:
026: import javax.management.ObjectName;
027:
028: import org.hibernate.cache.Cache;
029: import org.hibernate.cache.CacheException;
030: import org.hibernate.cache.CacheProvider;
031: import org.jboss.cache.TreeCache;
032: import org.jboss.cache.TreeCacheMBean;
033: import org.jboss.ejb3.tx.TxUtil;
034: import org.jboss.logging.Logger;
035: import org.jboss.mx.util.MBeanProxyExt;
036: import org.jboss.mx.util.MBeanServerLocator;
037:
038: /**
039: * Support for integration as a 2nd level cache with an already existing
040: * JBoss Cache (TreeCache) instance. The ObjectName of the cache is
041: * provided via the <code>hibernate.treecache.mbean.object_name</code>
042: * configuration property.
043: * <p/>
044: * This class supports both optimistic and pessimistic locking, providing
045: * instances of <code>org.hibernate.cache.OptimisticCache</code> if the
046: * underlying JBoss Cache is configured for optimistic locking.
047: *
048: * @author Gavin King
049: * @author Brian Stansberry
050: */
051: public class TreeCacheProviderHook extends
052: org.hibernate.cache.TreeCacheProvider implements CacheProvider {
053: /**
054: * Name of the Hibernate configuration property used to provide
055: * the ObjectName of the JBoss Cache instance.
056: */
057: public static final String HIBERNATE_CACHE_OBJECT_NAME_PROPERTY = "hibernate.treecache.mbean.object_name";
058:
059: /**
060: * Default ObjectName for the JBoss Cache instance that will be used
061: * if {@link HIBERNATE_CACHE_OBJECT_NAME_PROPERTY} is not provided.
062: */
063: public static final String DEFAULT_MBEAN_OBJECT_NAME = "jboss.cache:service=EJB3EntityTreeCache";
064:
065: protected Logger log = Logger.getLogger(getClass());
066:
067: private org.jboss.cache.TreeCache cache;
068: private boolean optimistic;
069:
070: /**
071: * Construct and configure the Cache representation of a named cache region.
072: *
073: * @param regionName the name of the cache region
074: * @param properties configuration settings
075: * @return The Cache representation of the named cache region. If the
076: * underlying JBoss Cache is configured for optimistic locking,
077: * the returned object will also implement org.hibernate.cache.OptimisticCache.
078: * @throws org.hibernate.cache.CacheException
079: * Indicates an error building the cache region.
080: */
081: public Cache buildCache(String regionName, Properties properties)
082: throws CacheException {
083: String regionPrefix = properties
084: .getProperty("hibernate.cache.region_prefix");
085:
086: if (optimistic) {
087: return new OptimisticJBCCache(cache, regionName,
088: regionPrefix);
089: } else {
090: return new JBCCache(cache, regionName, regionPrefix, TxUtil
091: .getTransactionManager());
092: }
093: }
094:
095: public boolean isMinimalPutsEnabledByDefault() {
096: return true;
097: }
098:
099: public long nextTimestamp() {
100: return System.currentTimeMillis() / 100;
101: }
102:
103: /**
104: * Find the underlying JBoss Cache TreeCache instance.
105: *
106: * @param properties All current config settings.
107: * If {@link #HIBERNATE_CACHE_OBJECT_NAME_PROPERTY} is provided,
108: * the value will be the expected name of the cache; otherwise
109: * {@link #DEFAULT_MBEAN_OBJECT_NAME} will be used.
110: * @throws org.hibernate.cache.CacheException
111: * Indicates a problem preparing cache for use.
112: */
113: public void start(Properties properties) {
114: try {
115: String cacheName = (String) properties
116: .get(HIBERNATE_CACHE_OBJECT_NAME_PROPERTY);
117: if (cacheName == null) {
118: cacheName = DEFAULT_MBEAN_OBJECT_NAME;
119: }
120: ObjectName mbeanObjectName = new ObjectName(cacheName);
121: TreeCacheMBean mbean = (TreeCacheMBean) MBeanProxyExt
122: .create(TreeCacheMBean.class, mbeanObjectName,
123: MBeanServerLocator.locateJBoss());
124: cache = mbean.getInstance();
125: if ("OPTIMISTIC".equals(cache.getNodeLockingScheme())) {
126: optimistic = true;
127: log
128: .debug("JBoss Cache is configured for optimistic locking; "
129: + "provided Cache implementations will also implement OptimisticCache");
130: }
131: } catch (Exception e) {
132: throw new CacheException(e);
133: }
134: }
135:
136: public void stop() {
137: }
138:
139: public boolean isOptimistic() {
140: return optimistic;
141: }
142:
143: public TreeCache getUnderlyingCache() {
144: return cache;
145: }
146:
147: }
|