com.alachisoft.ncache.jsr107.spi.NCacheCachingProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
package com.alachisoft.ncache.jsr107.spi;
import com.alachisoft.ncache.jsr107.NCacheManager;
import javax.cache.CacheManager;
import javax.cache.configuration.OptionalFeature;
import javax.cache.spi.CachingProvider;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NCacheCachingProvider implements CachingProvider {
private static final URI URI_DEFAULT;
static {
URI ur = null;
URI uri = null;
try {
//URL resource=JCacheCachingProvider.class.getResource("/cache.conf");
// try {
// URL resource = JCacheCachingProvider.class.getResource("C:\\Program Files\\TayzGrid\\config\\cache.conf");
// uri = new URI(resource.toString());
// } catch (URISyntaxException e) {
// uri = null;
// }
ur = new URI("default");
} catch (URISyntaxException ex) {
Logger.getLogger(NCacheCachingProvider.class.getName()).log(Level.SEVERE, null, ex);
}
URI_DEFAULT = ur;
}
protected final Map> cacheManagers = new WeakHashMap>();
private Properties properties;
/**
* Requests a CacheManager configured according to the implementation specific URI be made available that uses the provided ClassLoader for loading underlying classes.
*
* @param uri - an implementation specific URI for the CacheManager (null means use getDefaultURI())
* @param classLoader - the ClassLoader to use for the CacheManager (null means use getDefaultClassLoader())
* @param properties - the Properties for the CachingProvider to create the CacheManager (null means no implementation specific Properties are required)
* @return
*/
@Override
public CacheManager getCacheManager(URI uri, ClassLoader classLoader, Properties properties) {
uri = uri == null ? getDefaultURI() : uri;
classLoader = classLoader == null ? getDefaultClassLoader() : classLoader;
NCacheManager cacheManager;
ConcurrentMap byURI;
synchronized (cacheManagers) {
byURI = cacheManagers.get(classLoader);
if (byURI == null) {
byURI = new ConcurrentHashMap();
cacheManagers.put(classLoader, byURI);
}
cacheManager = byURI.get(uri);
if (cacheManager == null) {
//user uri for reading tayzgrid-jcache.conf if needed
cacheManager = getCacheManagerInstance(uri, classLoader, properties);
byURI.put(uri, cacheManager);
}
}
return cacheManager;
}
protected NCacheManager getCacheManagerInstance(URI uri, ClassLoader classLoader, Properties properties) {
return new NCacheManager(this, classLoader, uri, properties);
}
/**
* Obtains the default ClassLoader that will be used by the CachingProvider.
*
* @return
*/
@Override
public ClassLoader getDefaultClassLoader() {
return getClass().getClassLoader();
}
/**
* Obtains the default URI for the CachingProvider.
*
* @return
*/
@Override
public URI getDefaultURI() {
return URI_DEFAULT;
}
/**
* Obtains the default Properties for the CachingProvider.
*
* @return
*/
@Override
public Properties getDefaultProperties() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return properties;
}
/**
* Requests a CacheManager configured according to the implementation specific URI that uses the provided ClassLoader for loading underlying classes.
*
* @param uri - an implementation specific URI for the CacheManager (null means use getDefaultURI())
* @param classLoader- the ClassLoader to use for the CacheManager (null means use getDefaultClassLoader())
* @return
*/
@Override
public CacheManager getCacheManager(URI uri, ClassLoader classLoader) {
return getCacheManager(uri, classLoader, null);
}
/**
* Requests a CacheManager configured according to the getDefaultURI() and getDefaultProperties() be made available that using the getDefaultClassLoader() for loading underlying classes.
*
* @return
*/
@Override
public CacheManager getCacheManager() {
return getCacheManager(getDefaultURI(), getDefaultClassLoader());
}
/**
* Closes all of the CacheManager instances and associated resources created and maintained by the CachingProvider across all ClassLoaders.
*/
@Override
public void close() {
// synchronized (cacheManagers) {
// cacheManagers.entrySet().stream().forEach((entry) -> {
// entry.getValue().values().stream().forEach((jCacheManager) -> {
// jCacheManager.close();
// });
// });
// cacheManagers.clear();
// }
}
/**
* Closes all CacheManager instances and associated resources created by the CachingProvider using the specified ClassLoader.
*
* @param classLoader
*/
@Override
public void close(ClassLoader classLoader) {
synchronized (cacheManagers) {
final ConcurrentMap map = cacheManagers.remove(classLoader);
if (map != null) {
// map.values().stream().forEach((cacheManager) -> {
// cacheManager.close();
// });
}
}
}
/**
* Closes all CacheManager instances and associated resources created by the CachingProvider for the specified URI and ClassLoader.
*
* @param uri
* @param classLoader
*/
@Override
public void close(URI uri, ClassLoader classLoader) {
synchronized (cacheManagers) {
final ConcurrentMap map = cacheManagers.get(classLoader);
if (map != null) {
final NCacheManager jCacheManager = map.remove(uri);
if (jCacheManager != null) {
jCacheManager.close();
}
}
}
}
/**
* Determines whether an optional feature is supported by the CachingProvider.
*
* @param optionalFeature - the feature to check for.
* @return
*/
@Override
public boolean isSupported(OptionalFeature optionalFeature) {
if (optionalFeature == optionalFeature.STORE_BY_REFERENCE)
return true;
return false;
}
public void shutdown(final NCacheManager jCacheManager) {
synchronized (cacheManagers) {
final ConcurrentMap map = cacheManagers.get(jCacheManager.getClassLoader());
if (map.remove(jCacheManager.getURI()) != null) {
jCacheManager.shutdown();
}
}
}
}