📄 multiplexingcacheinstancemanager.java
字号:
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Middleware LLC. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program 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 distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */package org.hibernate.cache.jbc2.builder;import java.util.Properties;import javax.transaction.TransactionManager;import org.jboss.cache.Cache;import org.jboss.cache.CacheManager;import org.jboss.cache.CacheManagerImpl;import org.jboss.cache.CacheStatus;import org.jboss.cache.config.Configuration;import org.jgroups.ChannelFactory;import org.jgroups.JChannelFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.hibernate.cache.CacheException;import org.hibernate.cache.jbc2.CacheInstanceManager;import org.hibernate.cache.jbc2.util.CacheHelper;import org.hibernate.cfg.Settings;import org.hibernate.transaction.TransactionManagerLookup;import org.hibernate.util.PropertiesHelper;/** * Allows building separate {@link Cache} instances for each type of region, * with the expectation that a single multiplexed JGroups resource (i.e. a * multiplexed channel or a shared transport channel) will be shared between * the caches.<p/> * * @author Steve Ebersole * @author Brian Stansberry */public class MultiplexingCacheInstanceManager implements CacheInstanceManager { private static final Logger log = LoggerFactory.getLogger(MultiplexingCacheInstanceManager.class); /** * Classpath or filesystem resource containing JBoss Cache * configurations the factory should use. * * @see #DEF_CACHE_FACTORY_RESOURCE */ public static final String CACHE_FACTORY_RESOURCE_PROP = "hibernate.cache.region.jbc2.configs"; /** * Classpath or filesystem resource containing JGroups protocol * stack configurations the <code>org.jgroups.ChannelFactory</code> * should use. * * @see #DEF_JGROUPS_RESOURCE */ public static final String CHANNEL_FACTORY_RESOURCE_PROP = "hibernate.cache.region.jbc2.jgroups.stacks"; /** * Name of the configuration that should be used for entity caches. * * @see #DEF_ENTITY_RESOURCE */ public static final String ENTITY_CACHE_RESOURCE_PROP = "hibernate.cache.region.jbc2.cfg.entity"; /** * Name of the configuration that should be used for collection caches. * No default value, as by default we try to use the same JBoss Cache * instance we use for entity caching. * * @see #ENTITY_CACHE_RESOURCE_PROP * @see #DEF_ENTITY_RESOURCE */ public static final String COLLECTION_CACHE_RESOURCE_PROP = "hibernate.cache.region.jbc2.cfg.collection"; /** * Name of the configuration that should be used for timestamp caches. * * @see #DEF_TS_RESOURCE */ public static final String TIMESTAMP_CACHE_RESOURCE_PROP = "hibernate.cache.region.jbc2.cfg.ts"; /** * Name of the configuration that should be used for query caches. * * @see #DEF_QUERY_RESOURCE */ public static final String QUERY_CACHE_RESOURCE_PROP = "hibernate.cache.region.jbc2.cfg.query"; /** * Default value for {@link #CACHE_FACTORY_RESOURCE_PROP}. Specifies * the "jbc2-configs.xml" file in this package. */ public static final String DEF_CACHE_FACTORY_RESOURCE = "org/hibernate/cache/jbc2/builder/jbc2-configs.xml"; /** * Default value for {@link #CHANNEL_FACTORY_RESOURCE_PROP}. Specifies * the "jgroups-stacks.xml" file in this package. */ public static final String DEF_JGROUPS_RESOURCE = "org/hibernate/cache/jbc2/builder/jgroups-stacks.xml"; /** * Default value for {@link #ENTITY_CACHE_RESOURCE_PROP}. */ public static final String DEF_ENTITY_RESOURCE = "optimistic-entity"; /** * Default value for {@link #TIMESTAMP_CACHE_RESOURCE_PROP}. */ public static final String DEF_TS_RESOURCE = "timestamps-cache"; /** * Default value for {@link #ENTITY_CACHE_RESOURCE_PROP}. */ public static final String DEF_QUERY_RESOURCE = "local-query"; /** Cache for entities */ private Cache jbcEntityCache; /** Cache for collections */ private Cache jbcCollectionCache; /** Cache for timestamps */ private Cache jbcTsCache; /** Cache for queries */ private Cache jbcQueryCache; /** Name of config used for entities. */ private String entityConfig = null; /** Name of config used for collections. */ private String collectionConfig = null; /** Name of config used for queries. */ private String queryConfig = null; /** Name of config used for timestamps. */ private String tsConfig = null; /** Our cache factory */ private CacheManager jbcFactory; /** Our channel factory */ private ChannelFactory channelFactory; /** * Did we create the factory ourself and thus can assume we are not * sharing it (and the caches) with other users? */ private boolean selfCreatedFactory; /** * Create a new MultiplexingCacheInstanceManager. */ public MultiplexingCacheInstanceManager() { } /** * Create a new MultiplexingCacheInstanceManager using the provided {@link Cache}s. * <p/> * If this constructor is used, the {@link #start(Settings, Properties)} * method will make no attempt to create a cache factory or obtain caches * from it. Only the <code>Cache</code>s passed as arguments to this * constructor will be available. * * @param jbcEntityCache The entity cache * @param jbcCollectionCache the collection cache * @param jbcTsCache The timestamps cache * @param jbcQueryCache The query cache */ public MultiplexingCacheInstanceManager( Cache jbcEntityCache, Cache jbcCollectionCache, Cache jbcTsCache, Cache jbcQueryCache) { this.jbcEntityCache = jbcEntityCache; this.jbcCollectionCache = jbcCollectionCache; this.jbcTsCache = jbcTsCache; this.jbcQueryCache = jbcQueryCache; } /** * Getter for property 'cacheFactory'. * @see #setCacheFactory * * @return Value for property 'cacheFactory'. */ public CacheManager getCacheFactory() { return jbcFactory; } /** * Setter for property 'cacheFactory'. * @see #getCacheFactory * * @param factory Value to set for property 'cacheFactory'. */ public void setCacheFactory(CacheManager factory) { this.jbcFactory = factory; } /** * Getter for property 'channelFactory'. * @see #setChannelFactory * * @return Value for property 'channelFactory'. */ public ChannelFactory getChannelFactory() { return channelFactory; } /** * Setter for property 'channelFactory'. * @see #getChannelFactory * * @param factory Value to set for property 'channelFactory'. */ public void setChannelFactory(ChannelFactory factory) { this.channelFactory = factory; } /** * {@inheritDoc} */ public Cache getEntityCacheInstance() { return jbcEntityCache; } /** * {@inheritDoc} */ public Cache getCollectionCacheInstance() { return jbcCollectionCache; } /** * {@inheritDoc} */ public Cache getQueryCacheInstance() { if (jbcQueryCache != null && jbcTsCache == null) { // This should only be possible if the caches are constructor injected throw new CacheException("Timestamps cache must be configured if a query cache is used"); } return jbcQueryCache; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -