Class Caching
- java.lang.Object
-
- javax.cache.Caching
-
public final class Caching extends Object
TheCachingclass provides a convenient means for an application to acquire an appropriateCachingProviderimplementation.While defined as part of the specification, its use is not required. Applications and/or containers may instead choose to directly instantiate a
CachingProviderimplementation based on implementation specific instructions.When using the
Cachingclass,CachingProviderimplementations are automatically discovered when they follow the conventions outlined by the Java Development KitServiceLoaderclass.Although automatically discovered, applications that choose to use this class should not make assumptions regarding the order in which implementations are returned by the
getCachingProviders()orgetCachingProviders(ClassLoader)methods.For a
CachingProviderto be automatically discoverable by theCachingclass, the fully qualified class name of theCachingProviderimplementation must be declared in the following file:META-INF/services/javax.cache.spi.CachingProvider
This file must be resolvable via the class path.For example, in the reference implementation the contents of this file are:
org.jsr107.ri.RICachingProviderAlternatively when the fully qualified class name of a
CachingProviderimplementation is specified using the system propertyjavax.cache.spi.cachingprovider, that implementation will be used as the defaultCachingProvider.All
CachingProviders that are automatically detected or explicitly declared and loaded by theCachingclass are maintained in an internal registry. Consequently when a previously loadedCachingProvideris requested, it will be simply returned from the internal registry, without reloading and/or instantiating the said implementation again.As required by some applications and containers, multiple co-existing
CachingProviders implementations, from the same or different implementors are permitted at runtime.To iterate through those that are currently registered a developer may use the following methods:
To request a specificCachingProviderimplementation, a developer should use either thegetCachingProvider(String)orgetCachingProvider(String, ClassLoader)method.Where multiple
CachingProviders are present, theCachingProviderreturned by gettersgetCachingProvider()andgetCachingProvider(ClassLoader)is undefined and as a result aCacheExceptionwill be thrown when attempted.- Since:
- 1.0
- Author:
- Brian Oliver, Greg Luck, Yannis Cosmadopoulos
- See Also:
ServiceLoader,CachingProvider
-
-
Field Summary
Fields Modifier and Type Field Description static StringJAVAX_CACHE_CACHING_PROVIDERThejavax.cache.spi.cachingproviderconstant.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K,V>
Cache<K,V>getCache(String cacheName, Class<K> keyType, Class<V> valueType)A convenience that method that looks up a managedCachegiven its name.static CachingProvidergetCachingProvider()Obtains the defaultCachingProvideravailable via thegetDefaultClassLoader().static CachingProvidergetCachingProvider(ClassLoader classLoader)Obtains the singleCachingProvidervisible to the specifiedClassLoader.static CachingProvidergetCachingProvider(String fullyQualifiedClassName)Obtain theCachingProviderthat is implemented by the specified fully qualified class name using thegetDefaultClassLoader().static CachingProvidergetCachingProvider(String fullyQualifiedClassName, ClassLoader classLoader)Obtain theCachingProviderthat is implemented by the specified fully qualified class name using the providedClassLoader.static Iterable<CachingProvider>getCachingProviders()Obtains theCachingProviders that are available via thegetDefaultClassLoader().static Iterable<CachingProvider>getCachingProviders(ClassLoader classLoader)Obtains theCachingProviders that are available via the specifiedClassLoader.static ClassLoadergetDefaultClassLoader()Obtains theClassLoaderto use for API methods that don't explicitly require aClassLoaderbut internally require one.static voidsetDefaultClassLoader(ClassLoader classLoader)Set theClassLoaderto use for API methods that don't explicitly require aClassLoader, but internally use one.
-
-
-
Field Detail
-
JAVAX_CACHE_CACHING_PROVIDER
public static final String JAVAX_CACHE_CACHING_PROVIDER
Thejavax.cache.spi.cachingproviderconstant.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDefaultClassLoader
public static ClassLoader getDefaultClassLoader()
Obtains theClassLoaderto use for API methods that don't explicitly require aClassLoaderbut internally require one.By default this is the
Thread.getContextClassLoader().- Returns:
- the default
ClassLoader
-
setDefaultClassLoader
public static void setDefaultClassLoader(ClassLoader classLoader)
Set theClassLoaderto use for API methods that don't explicitly require aClassLoader, but internally use one.- Parameters:
classLoader- theClassLoaderornullif the callingThread.getContextClassLoader()should be used
-
getCachingProvider
public static CachingProvider getCachingProvider()
Obtains the defaultCachingProvideravailable via thegetDefaultClassLoader().- Returns:
- the
CachingProvider - Throws:
CacheException- should zero, or more than oneCachingProviderbe available on the classpath, or it could not be loadedSecurityException- when the operation could not be performed due to the current security settings
-
getCachingProvider
public static CachingProvider getCachingProvider(ClassLoader classLoader)
Obtains the singleCachingProvidervisible to the specifiedClassLoader.- Parameters:
classLoader- theClassLoaderto use for loading theCachingProvider- Returns:
- the
CachingProvider - Throws:
CacheException- should zero, or more than oneCachingProviderbe available on the classpath, or it could not be loadedSecurityException- when the operation could not be performed due to the current security settings- See Also:
getCachingProviders(ClassLoader)
-
getCachingProviders
public static Iterable<CachingProvider> getCachingProviders()
Obtains theCachingProviders that are available via thegetDefaultClassLoader().If a
javax.cache.spi.cachingprovidersystem property is defined, only thatCachingProviderspecified by that property is returned. Otherwise allCachingProviders that are available via aServiceLoaderforCachingProviders using the defaultClassLoader(including those previously requested viagetCachingProvider(String)) are returned.- Returns:
- an
IterableofCachingProviders loaded by the specifiedClassLoader
-
getCachingProviders
public static Iterable<CachingProvider> getCachingProviders(ClassLoader classLoader)
Obtains theCachingProviders that are available via the specifiedClassLoader.If a
javax.cache.spi.cachingprovidersystem property is defined, only thatCachingProviderspecified by that property is returned. Otherwise allCachingProviders that are available via aServiceLoaderforCachingProviders using the specifiedClassLoader(including those previously requested viagetCachingProvider(String, ClassLoader)) are returned.- Parameters:
classLoader- theClassLoaderof the returnedCachingProviders- Returns:
- an
IterableofCachingProviders loaded by the specifiedClassLoader
-
getCachingProvider
public static CachingProvider getCachingProvider(String fullyQualifiedClassName)
Obtain theCachingProviderthat is implemented by the specified fully qualified class name using thegetDefaultClassLoader(). Should thisCachingProvideralready be loaded it is simply returned, otherwise an attempt will be made to load and instantiate the specified class (using a no-args constructor).- Parameters:
fullyQualifiedClassName- the fully qualified class name of theCachingProvider- Returns:
- the
CachingProvider - Throws:
CacheException- if theCachingProvidercannot be createdSecurityException- when the operation could not be performed due to the current security settings
-
getCachingProvider
public static CachingProvider getCachingProvider(String fullyQualifiedClassName, ClassLoader classLoader)
Obtain theCachingProviderthat is implemented by the specified fully qualified class name using the providedClassLoader. Should thisCachingProvideralready be loaded it is returned, otherwise an attempt will be made to load and instantiate the specified class (using a no-args constructor).- Parameters:
fullyQualifiedClassName- the fully qualified class name of theCachingProviderclassLoader- theClassLoaderto load theCachingProvider- Returns:
- the
CachingProvider - Throws:
CacheException- if theCachingProvidercannot be createdSecurityException- when the operation could not be performed due to the current security settings
-
getCache
public static <K,V> Cache<K,V> getCache(String cacheName, Class<K> keyType, Class<V> valueType)
A convenience that method that looks up a managedCachegiven its name. using the defaultCachingProviderandCacheManager. For the full range ofCachelook up methods seeCacheManager.This method must be used for
Caches that were configured with runtime key and value types. UseCacheManager.getCache(String)forCaches where these were not specified.Implementations must ensure that the key and value types are the same as those configured for the
Cacheprior to returning from this method.Implementations may further perform type checking on mutative cache operations and throw a
ClassCastExceptionif these checks fail.Implementations that support declarative mechanisms for pre-configuring
Caches may return a pre-configuredCacheinstead ofnull.- Type Parameters:
K- the type of keyV- the type of value- Parameters:
cacheName- the name of the managedCacheto acquirekeyType- the expectedClassof the keyvalueType- the expectedClassof the value- Returns:
- the Cache or null if it does exist or can't be pre-configured
- Throws:
IllegalStateException- if the CacheManager isCacheManager.isClosed()IllegalArgumentException- if the specified key and/or value types are incompatible with the configured cache.SecurityException- when the operation could not be performed due to the current security settings- See Also:
CacheManager.getCache(String, Class, Class),CacheManager.getCache(String)
-
-