All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.ehcache.jcache.JCacheManager Maven / Gradle / Ivy

There is a newer version: 1.5.0-0.5
Show newest version
/**
 *  Copyright 2003-2010 Terracotta, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sf.ehcache.jcache;


import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.cache.Cache;
import javax.cache.CacheBuilder;
import javax.cache.CacheLoader;
import javax.cache.CacheWriter;
import javax.cache.Caching;
import javax.cache.OptionalFeature;
import javax.cache.Status;
import javax.cache.event.CacheEntryListener;
import javax.cache.event.NotificationScope;
import javax.cache.transaction.IsolationLevel;
import javax.cache.transaction.Mode;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Pattern;

/**
 * The CacheManager that allows EHCache caches to be retrieved and accessed via JSR107 APIs
 *
 * @author Ryan Gardner
 */
public class JCacheManager implements javax.cache.CacheManager {
    private static final Logger LOG = LoggerFactory.getLogger(JCacheManager.class);

    private final HashMap> caches = new HashMap>();
    private final HashSet> immutableClasses = new HashSet>();

    private final ClassLoader classLoader;
    private final CacheManager ehcacheManager;


    /**
     * Creates a JCacheManager that uses the name of the cache to configure the underlying EhCache CacheManager
     * via an ehcache-name.xml config file
     *
     * @param name
     * @param ehcacheManager
     * @param classLoader
     */
    public JCacheManager(String name, CacheManager ehcacheManager, ClassLoader classLoader) {
        if (classLoader == null) {
            throw new NullPointerException("No classLoader specified");
        }
        if (name == null) {
            throw new NullPointerException("No name specified");
        }
        this.classLoader = classLoader;

        this.ehcacheManager = ehcacheManager;
        this.ehcacheManager.setName(name);
    }

    /**
     * Retrieve the underlying ehcache manager that this JCacheManager uses
     *
     * @return the underlying ehcache manager
     */
    public CacheManager getEhcacheManager() {
        return ehcacheManager;
    }


    /**
     * {@inheritDoc}
     * 

* The name returned will be that passed in to the constructor {@link #JCacheManager(String, net.sf.ehcache.CacheManager, ClassLoader)} */ @Override public String getName() { return ehcacheManager.getName(); } /** * Returns the status of this CacheManager. *

* * @return one of {@link javax.cache.Status} */ @Override public Status getStatus() { return JCacheStatusAdapter.adaptStatus(ehcacheManager.getStatus()); } @Override public CacheBuilder createCacheBuilder(String cacheName) { return new JCacheBuilder(cacheName, this, classLoader); } /** * {@inheritDoc} */ @Override public Cache getCache(String cacheName) { if (getStatus() != Status.STARTED) { throw new IllegalStateException("CacheManager must be started before retrieving a cache"); } synchronized (caches) { if (caches.containsKey(cacheName)) { return (Cache) caches.get(cacheName); } else { Ehcache ehcache = ehcacheManager.getEhcache(cacheName); if (ehcache == null) { return null; } JCacheEhcacheDecorator decoratedCache = new JCacheEhcacheDecorator(ehcache); final JCache cache = new JCache(decoratedCache, this, this.classLoader); decoratedCache.setJcache(cache); caches.put(cacheName, cache); return cache; } } } /** * {@inheritDoc} */ @Override public Iterable> getCaches() { synchronized (caches) { HashSet> cacheSet = new HashSet>(caches.size(), 1.0f); for (Cache cache : caches.values()) { cacheSet.add(cache); } return Collections.unmodifiableSet(cacheSet); } } /** * {@inheritDoc} */ @Override public boolean removeCache(String cacheName) { if (getStatus() != Status.STARTED) { throw new IllegalStateException(); } if (cacheName == null) { throw new NullPointerException(); } Cache oldCache; synchronized (caches) { oldCache = caches.remove(cacheName); } if (oldCache != null) { oldCache.stop(); } return oldCache != null; } @Override public javax.transaction.UserTransaction getUserTransaction() { throw new UnsupportedOperationException(); } @Override public boolean isSupported(OptionalFeature optionalFeature) { return Caching.isSupported(optionalFeature); } /** * {@inheritDoc} */ @Override public void registerImmutableClass(Class immutableClass) { if (immutableClass == null) { throw new NullPointerException(); } immutableClasses.add(immutableClass); } /** * {@inheritDoc} */ @Override public void shutdown() { if (getStatus() != Status.STARTED) { throw new IllegalStateException(); } synchronized (immutableClasses) { immutableClasses.clear(); } synchronized (caches) { ehcacheManager.shutdown(); caches.clear(); } } @Override public T unwrap(java.lang.Class cls) { if (cls.isAssignableFrom(this.getClass())) { return cls.cast(this); } throw new IllegalArgumentException("Unwapping to " + cls + " is not a supported by this implementation"); } /** * Construct a CacheBuilder * * @param the type of keys used by the Cache built by this CacheBuilder * @param the type of values that are loaded by the Cache built by this CacheBuilder * @author Ryan Gardner */ private class JCacheBuilder implements CacheBuilder { private final JCache.Builder cacheBuilder; private Pattern namePattern = Pattern.compile("\\S+"); public JCacheBuilder(String cacheName, JCacheManager jCacheManager, ClassLoader cl) { if (cacheName == null) { throw new NullPointerException("Cache name cannot be null"); } if (!(namePattern.matcher(cacheName).find())) { throw new IllegalArgumentException("Cache name must contain one or more non-whitespace characters"); } cacheBuilder = new JCache.Builder(cacheName, jCacheManager, cl); } @Override public JCache build() { JCache cache = cacheBuilder.build(); addInternal(cache); return cache; } @Override public CacheBuilder setCacheLoader(CacheLoader cacheLoader) { cacheBuilder.setCacheLoader(cacheLoader); return this; } @Override public CacheBuilder setCacheWriter(CacheWriter cacheWriter) { cacheBuilder.setCacheWriter(cacheWriter); return this; } @Override public CacheBuilder registerCacheEntryListener(CacheEntryListener listener, NotificationScope scope, boolean synchronous) { cacheBuilder.registerCacheEntryListener(listener, scope, synchronous); return this; } @Override public CacheBuilder setStoreByValue(boolean storeByValue) { cacheBuilder.setStoreByValue(storeByValue); return this; } @Override public CacheBuilder setTransactionEnabled(IsolationLevel isolationLevel, Mode mode) { cacheBuilder.setTransactionEnabled(isolationLevel, mode); return this; } @Override public CacheBuilder setStatisticsEnabled(boolean enableStatistics) { cacheBuilder.setStatisticsEnabled(enableStatistics); return this; } @Override public CacheBuilder setReadThrough(boolean readThrough) { cacheBuilder.setReadThrough(readThrough); return this; } @Override public CacheBuilder setWriteThrough(boolean writeThrough) { cacheBuilder.setWriteThrough(writeThrough); return this; } @Override public CacheBuilder setExpiry(javax.cache.CacheConfiguration.ExpiryType type, javax.cache.CacheConfiguration.Duration timeToLive) { cacheBuilder.setExpiry(type, timeToLive); return this; } } private void addInternal(JCache cache) { synchronized (caches) { if (caches.containsKey(cache.getName())) { ehcacheManager.removeCache(cache.getName()); } // remove the cache if it already exists if (ehcacheManager.getEhcache(cache.getName()) != null) { ehcacheManager.removeCache(cache.getName()); } caches.remove(cache.getName()); caches.put(cache.getName(), cache); // decorate the cache with a reference to the JCache ehcacheManager.addCache(new JCacheEhcacheDecorator(cache.getEhcache(),cache)); } } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } JCacheManager that = (JCacheManager) o; if (caches != null ? !caches.equals(that.caches) : that.caches != null) { return false; } if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) { return false; } if (ehcacheManager != null ? !ehcacheManager.equals(that.ehcacheManager) : that.ehcacheManager != null) { return false; } if (immutableClasses != null ? !immutableClasses.equals(that.immutableClasses) : that.immutableClasses != null) { return false; } return true; } @Override public int hashCode() { int result = caches != null ? caches.hashCode() : 0; result = 31 * result + (immutableClasses != null ? immutableClasses.hashCode() : 0); result = 31 * result + (classLoader != null ? classLoader.hashCode() : 0); result = 31 * result + (ehcacheManager != null ? ehcacheManager.hashCode() : 0); return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy