/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.web.tomcat.service.session.distributedcache.impl;
import java.io.File;
import javax.management.MBeanServer;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import org.jboss.cache.Cache;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapper;
import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapperMBean;
import org.jboss.cache.transaction.BatchModeTransactionManagerLookup;
import org.jboss.web.tomcat.service.session.distributedcache.impl.jbc.FieldBasedJBossCacheService;
import org.jboss.web.tomcat.service.session.distributedcache.impl.jbc.JBossCacheService;
import org.jboss.web.tomcat.service.session.distributedcache.spi.ClusteringNotSupportedException;
import org.jboss.web.tomcat.service.session.distributedcache.spi.DistributedCacheManager;
import org.jboss.web.tomcat.service.session.distributedcache.spi.FieldBasedDistributedCacheManager;
import org.jboss.web.tomcat.service.session.distributedcache.spi.TomcatClusterConfig;
import org.jboss.web.tomcat.service.session.distributedcache.spi.TomcatClusterDistributedCacheManagerFactory;
/**
* @author Brian Stansberry
*
*/
public class DistributedCacheManagerFactoryImpl implements TomcatClusterDistributedCacheManagerFactory
{
public static final String DEFAULT_CLUSTER_NAME = "Tomcat-Cluster";
/** TreeCache's isolation level */
public static final String DEFAULT_ISOLATION_LEVEL = "REPEATABLE_READ";
/** TreeCache's cache mode */
public static final String DEFAULT_CACHE_MODE = "REPL_ASYNC";
/** TreeCache's lock aquisition timeout */
public static final long DEFAULT_LOCK_TIMEOUT = 15000;
/** TransactionManagerLookup implementation that the TreeCache should use. */
public static final String DEFAULT_TM_LOOKUP =
BatchModeTransactionManagerLookup.class.getName();
private TomcatClusterConfig tomcatConfig;
private MBeanServer mserver;
private boolean pojoCacheLocal = false;
private PojoCache pojoCache;
private Cache plainCache;
public DistributedCacheManager getDistributedCacheManager(String cacheConfigName)
throws ClusteringNotSupportedException
{
return plainCache == null? new JBossCacheService(cacheConfigName) : new JBossCacheService(plainCache);
}
public FieldBasedDistributedCacheManager getFieldBasedDistributedCacheManager(String cacheConfigName)
throws ClusteringNotSupportedException
{
return pojoCache == null? new FieldBasedJBossCacheService(cacheConfigName) : new FieldBasedJBossCacheService(pojoCache);
}
public TomcatClusterConfig getTomcatClusterConfig()
{
return tomcatConfig;
}
public void setTomcatClusterConfig(TomcatClusterConfig clusterConfig)
{
this.tomcatConfig = clusterConfig;
}
public void start() throws Exception
{
if (tomcatConfig != null)
{
initializePojoCache();
}
}
public void stop() throws Exception
{
if (pojoCache != null)
{
pojoCache.stop();
pojoCache.destroy();
if (pojoCacheLocal && mserver != null && tomcatConfig.getCacheObjectName() != null)
{
mserver.unregisterMBean(new ObjectName(tomcatConfig.getCacheObjectName()));
}
}
}
public PojoCache getPojoCache()
{
return pojoCache;
}
/**
* Hook for test fixtures to inject a PojoCache, which if present will
* be used to create the DistributedCacheManager in preference to any
* passed <code>cacheConfigName</code>.
*/
public void setPojoCache(PojoCache pojoCache)
{
this.pojoCache = pojoCache;
this.plainCache = pojoCache.getCache();
}
public Cache getPlainCache()
{
return plainCache;
}
/**
* Hook for test fixtures to inject a Cache, which if present will
* be used to create the DistributedCacheManager in preference to any
* passed <code>cacheConfigName</code>.
*/
public void setPlainCache(Cache plainCache)
{
this.plainCache = plainCache;
this.pojoCache = null;
}
/**
* Convenience method for test fixtures to clear any injected cache.
*/
public void clearCaches()
{
this.plainCache = null;
this.pojoCache = null;
}
/**
* Gets our PojCache, either from a local reference or the JMX
* server. If one is not found, creates and configures it.
*/
private void initializePojoCache() throws Exception
{
if (pojoCache == null) {
PojoCacheJmxWrapperMBean pcWrapper = null;
MBeanServer server = tomcatConfig.getMBeanServer();
String cfgName = tomcatConfig.getCacheObjectName();
ObjectName objName = cfgName == null ? null : new ObjectName(tomcatConfig.getCacheObjectName());
if (server != null && objName != null && server.isRegistered(objName))
{
// Get a proxy to the existing TreeCache
pcWrapper = ((PojoCacheJmxWrapperMBean)
MBeanServerInvocationHandler.newProxyInstance(server, objName, PojoCacheJmxWrapperMBean.class, false));
}
else
{
// See if there is an XML descriptor file to configure the cache
File configFile = tomcatConfig.getCacheConfigFile();
String clusterName = tomcatConfig.getClusterName();
if (configFile != null)
{
pcWrapper = new PojoCacheJmxWrapper(PojoCacheFactory.createCache(configFile.getAbsolutePath(), false));
Configuration config = pojoCache.getCache().getConfiguration();
if (clusterName != null)
{
// Override the XML config with the name provided in
// server.xml. Method setClusterName is specified in the
// Cluster interface, otherwise we would not do this
config.setClusterName(clusterName);
}
}
else
{
// User did not try to configure the cache.
// Configure it using defaults. Only exception
// is the clusterName, which user can specify in server.xml.
Configuration config = new Configuration();
String channelName = (clusterName == null) ? DEFAULT_CLUSTER_NAME
: clusterName;
config.setClusterName(channelName);
config.setIsolationLevel(DEFAULT_ISOLATION_LEVEL);
config.setCacheMode(DEFAULT_CACHE_MODE);
config.setLockAcquisitionTimeout(DEFAULT_LOCK_TIMEOUT);
config.setTransactionManagerLookupClass(DEFAULT_TM_LOOKUP);
pcWrapper = new PojoCacheJmxWrapper(PojoCacheFactory.createCache(config, false));
}
if (server != null && objName != null)
{
server.registerMBean(pcWrapper, objName);
}
pojoCacheLocal = true;
}
setPojoCache(pcWrapper.getPojoCache());
}
}
}