HHH-11265 - Improve potential JCacheRegionFactory subclassing

Add protected `getCacheManager()` and `getCacheProvider()` methods that subclasses can override.
Change fields and methods that would be useful to subclasses from package/private scope to protected.
This commit is contained in:
Craig Andrews 2016-11-21 11:00:53 -05:00 committed by Vlad Mihalcea
parent 0bd7b8eac1
commit 10972ffe30
1 changed files with 50 additions and 37 deletions

View File

@ -18,8 +18,6 @@ import javax.cache.configuration.Configuration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;
import org.jboss.logging.Logger;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.jcache.time.Timestamper;
@ -32,6 +30,8 @@ import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.TimestampsRegion;
import org.hibernate.cache.spi.access.AccessType;
import org.jboss.logging.Logger;
/**
* @author Alex Snaps
*/
@ -47,6 +47,14 @@ public class JCacheRegionFactory implements RegionFactory {
JCacheRegionFactory.class.getName()
);
static long nextTS() {
return Timestamper.next();
}
static int timeOut() {
return (int) TimeUnit.SECONDS.toMillis( 60 ) * Timestamper.ONE_MS;
}
private final AtomicBoolean started = new AtomicBoolean( false );
private volatile CacheManager cacheManager;
private SessionFactoryOptions options;
@ -57,30 +65,7 @@ public class JCacheRegionFactory implements RegionFactory {
synchronized ( this ) {
this.options = options;
try {
final CachingProvider cachingProvider;
final String provider = getProp( properties, PROVIDER );
if ( provider != null ) {
cachingProvider = Caching.getCachingProvider( provider );
}
else {
cachingProvider = Caching.getCachingProvider();
}
final CacheManager cacheManager;
final String cacheManagerUri = getProp( properties, CONFIG_URI );
if ( cacheManagerUri != null ) {
URI uri;
try {
uri = new URI( cacheManagerUri );
}
catch ( URISyntaxException e ) {
throw new CacheException( "Couldn't create URI from " + cacheManagerUri, e );
}
cacheManager = cachingProvider.getCacheManager( uri, cachingProvider.getDefaultClassLoader() );
}
else {
cacheManager = cachingProvider.getCacheManager();
}
this.cacheManager = cacheManager;
this.cacheManager = getCacheManager( properties );
}
finally {
if ( this.cacheManager == null ) {
@ -161,6 +146,42 @@ public class JCacheRegionFactory implements RegionFactory {
return started.get() && cacheManager != null;
}
protected SessionFactoryOptions getOptions() {
return options;
}
protected CachingProvider getCachingProvider(final Properties properties){
final CachingProvider cachingProvider;
final String provider = getProp( properties, PROVIDER );
if ( provider != null ) {
cachingProvider = Caching.getCachingProvider( provider );
}
else {
cachingProvider = Caching.getCachingProvider();
}
return cachingProvider;
}
protected CacheManager getCacheManager(final Properties properties){
final CachingProvider cachingProvider = getCachingProvider( properties );
final CacheManager cacheManager;
final String cacheManagerUri = getProp( properties, CONFIG_URI );
if ( cacheManagerUri != null ) {
URI uri;
try {
uri = new URI( cacheManagerUri );
}
catch ( URISyntaxException e ) {
throw new CacheException( "Couldn't create URI from " + cacheManagerUri, e );
}
cacheManager = cachingProvider.getCacheManager( uri, cachingProvider.getDefaultClassLoader() );
}
else {
cacheManager = cachingProvider.getCacheManager();
}
return cacheManager;
}
protected Cache<Object, Object> getOrCreateCache(String regionName, Properties properties, CacheDataDescription metadata) {
checkStatus();
final Cache<Object, Object> cache = cacheManager.getCache( regionName );
@ -183,23 +204,15 @@ public class JCacheRegionFactory implements RegionFactory {
return new MutableConfiguration<Object, Object>();
}
CacheManager getCacheManager() {
protected CacheManager getCacheManager() {
return cacheManager;
}
static long nextTS() {
return Timestamper.next();
}
static int timeOut() {
return (int) TimeUnit.SECONDS.toMillis( 60 ) * Timestamper.ONE_MS;
}
private String getProp(Properties properties, String prop) {
protected String getProp(Properties properties, String prop) {
return properties != null ? properties.getProperty( prop ) : null;
}
private void checkStatus() {
protected void checkStatus() {
if(!isStarted()) {
throw new IllegalStateException("JCacheRegionFactory not yet started!");
}