
org.infinispan.jcache.JCacheManager Maven / Gradle / Ivy
package org.infinispan.jcache;
import org.infinispan.AdvancedCache;
import org.infinispan.commons.util.FileLookup;
import org.infinispan.commons.util.InfinispanCollections;
import org.infinispan.commons.util.ReflectionUtil;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
import org.infinispan.configuration.parsing.ParserRegistry;
import org.infinispan.jcache.logging.Log;
import org.infinispan.lifecycle.ComponentStatus;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.util.logging.LogFactory;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.configuration.Configuration;
import javax.cache.spi.CachingProvider;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
import static org.infinispan.jcache.RIMBeanServerRegistrationUtility.ObjectNameType.CONFIGURATION;
import static org.infinispan.jcache.RIMBeanServerRegistrationUtility.ObjectNameType.STATISTICS;
/**
* Infinispan's implementation of {@link javax.cache.CacheManager}.
*
* @author Vladimir Blagojevic
* @author Galder Zamarreño
* @since 5.3
*/
public class JCacheManager implements CacheManager {
private static final Log log =
LogFactory.getLog(JCacheManager.class, Log.class);
private final HashMap> caches = new HashMap>();
private final URI uri;
private final EmbeddedCacheManager cm;
private final CachingProvider provider;
private final StackTraceElement[] allocationStackTrace;
private final Properties properties;
/**
* Boolean flag tracking down whether the underlying Infinispan cache
* manager used by JCacheManager is unmanaged or managed. Unmanaged means
* that this JCacheManager instance controls the lifecycle of the
* Infinispan Cache Manager. When managed, it means that the cache manager
* is injected and hence JCacheManager is not the owner of the lifecycle
* of this cache manager.
*/
private final boolean managedCacheManager;
/**
* A flag indicating whether the cache manager is closed or not.
* Cache manager's status does not fit well here because even if an
* trying to stop a cache manager whose status is {@link ComponentStatus#INSTANTIATED}
* does not change it to {@link ComponentStatus#TERMINATED}
*/
private volatile boolean isClosed;
/**
* Create a new InfinispanCacheManager given a cache name and a {@link ClassLoader}. Cache name
* might refer to a file on classpath containing Infinispan configuration file.
*
* @param uri identifies the cache manager
* @param classLoader used to load classes stored in this cache manager
*/
public JCacheManager(URI uri, ClassLoader classLoader, CachingProvider provider, Properties properties) {
// Track allocation time
this.allocationStackTrace = Thread.currentThread().getStackTrace();
if (classLoader == null) {
throw new IllegalArgumentException("Classloader cannot be null");
}
if (uri == null) {
throw new IllegalArgumentException("Invalid CacheManager URI " + uri);
}
this.uri = uri;
this.provider = provider;
this.properties = properties;
ConfigurationBuilderHolder cbh = getConfigurationBuilderHolder(classLoader);
GlobalConfigurationBuilder globalBuilder = cbh.getGlobalConfigurationBuilder();
// The cache manager name has to contain all uri, class loader and
// provider information in order to guarantee JMX naming uniqueness.
// This is tested by the TCK to make sure caching provider loaded
// with different classloaders, even if the default classloader for
// the cache manager is the same, are really different cache managers.
String cacheManagerName = "uri=" + uri
+ "/classloader=" + classLoader.toString()
+ "/provider=" + provider.toString();
// Set cache manager class loader and apply name to cache manager MBean
globalBuilder.classLoader(classLoader)
.globalJmxStatistics().cacheManagerName(cacheManagerName);
cm = new DefaultCacheManager(cbh, true);
registerPredefinedCaches();
isClosed = false;
managedCacheManager = false;
}
public JCacheManager(URI uri, EmbeddedCacheManager cacheManager, CachingProvider provider) {
// Track allocation time
this.allocationStackTrace = Thread.currentThread().getStackTrace();
this.uri = uri;
this.provider = provider;
this.cm = cacheManager;
this.managedCacheManager = true;
this.properties = null;
registerPredefinedCaches();
}
private void registerPredefinedCaches() {
// TODO get predefined caches and register them
// TODO galderz find a better way to do this as spec allows predefined caches to be
// loaded (config file), instantiated and registered with CacheManager
Set cacheNames = cm.getCacheNames();
for (String cacheName : cacheNames) {
// With pre-defined caches, obey only pre-defined configuration
caches.put(cacheName, new JCache
© 2015 - 2025 Weber Informatics LLC | Privacy Policy