01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.jems.hibernate;
23:
24: import org.apache.log4j.Logger;
25: import org.hibernate.cache.Cache;
26: import org.hibernate.cache.CacheException;
27: import org.hibernate.cache.CacheProvider;
28: import org.hibernate.cache.OptimisticTreeCache;
29: import org.hibernate.cache.TreeCache;
30: import org.jboss.mx.util.MBeanProxy;
31: import org.jboss.mx.util.MBeanServerLocator;
32:
33: import javax.management.MBeanServer;
34: import javax.management.ObjectName;
35: import java.util.Properties;
36:
37: /**
38: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
39: * @version $Revision: 9317 $
40: */
41: public class JMXTreeCacheProvider implements CacheProvider {
42:
43: /** Our logger. */
44: private static final Logger log = Logger
45: .getLogger(JMXTreeCacheProvider.class);
46:
47: public boolean isMinimalPutsEnabledByDefault() {
48: return true;
49: }
50:
51: public Cache buildCache(String regionName, Properties properties)
52: throws CacheException {
53: try {
54: String objectNameAsString = properties
55: .getProperty("hibernate.cache.object_name");
56: log.debug("Uses tree cache provider with object name "
57: + objectNameAsString);
58:
59: //
60: ObjectName providerName = ObjectName
61: .getInstance(objectNameAsString);
62:
63: //
64: MBeanServer server = MBeanServerLocator.locateJBoss();
65: TreeCacheProvider provider = (TreeCacheProvider) MBeanProxy
66: .get(TreeCacheProvider.class, providerName, server);
67: org.jboss.cache.TreeCache cache = provider.getTreeCache();
68:
69: //Select the proper type of hibernate cache to be used
70: Cache hibernateCache = null;
71: if (cache.getNodeLockingScheme().equalsIgnoreCase(
72: "OPTIMISTIC")) {
73: log.debug("Selecting Optimistic Cache");
74: hibernateCache = new OptimisticTreeCache(cache,
75: regionName);
76: } else {
77: log.debug("Selecting regular Tree Cache");
78: hibernateCache = new TreeCache(cache, regionName, cache
79: .getTransactionManager());
80: }
81:
82: return hibernateCache;
83: } catch (Exception e) {
84: throw new CacheException(e);
85: }
86: }
87:
88: public long nextTimestamp() {
89: return System.currentTimeMillis() / 100;
90: }
91:
92: public void start(Properties properties) throws CacheException {
93: }
94:
95: public void stop() {
96: }
97: }
|