Top Description Inners Fields Constructors Methods
javax.cache

public final Class Caching

extends Object
Class Inheritance
Imports
javax.cache.spi.CachingProvider, java.security.AccessController, .PrivilegedAction, java.util.Iterator, .LinkedHashMap, .ServiceLoader, .WeakHashMap

The Caching class provides a convenient means for an application to acquire an appropriate CachingProvider implementation.

While defined as part of the specification, its use is not required. Applications and/or containers may instead choose to directly instantiate a CachingProvider implementation based on implementation specific instructions.

When using the Caching class, CachingProvider implementations are automatically discovered when they follow the conventions outlined by the Java Development Kit ServiceLoader class.

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() or getCachingProviders(ClassLoader) methods.

For a CachingProvider to be automatically discoverable by the Caching class, the fully qualified class name of the CachingProvider implementation 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.RICachingProvider

Alternatively when the fully qualified class name of a CachingProvider implementation is specified using the system property javax.cache.spi.cachingprovider, that implementation will be used as the default CachingProvider.

All CachingProviders that are automatically detected or explicitly declared and loaded by the Caching class are maintained in an internal registry. Consequently when a previously loaded CachingProvider is 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:

  1. getCachingProviders()
  2. getCachingProviders(ClassLoader)
To request a specific CachingProvider implementation, a developer should use either the getCachingProvider(String) or getCachingProvider(String, ClassLoader) method.

Where multiple CachingProviders are present, the CachingProvider returned by getters getCachingProvider() and getCachingProvider(ClassLoader) is undefined and as a result a CacheException will be thrown when attempted.

Authors
Brian Oliver, Greg Luck, Yannis Cosmadopoulos
Since
1.0
See Also
ServiceLoader, CachingProvider

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Caching.CachingProviderRegistry

Maintains a registry of loaded CachingProviders scoped by ClassLoader.

Field Summary

Modifier and TypeField and Description
private static final Caching.CachingProviderRegistry
public static final String
JAVAX_CACHE_CACHING_PROVIDER

The javax.cache.spi.cachingprovider constant.

Constructor Summary

AccessConstructor and Description
private
Caching()

No public constructor as all methods are static.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of key
K
,
the type of value
V
>
Cache<K, V>

Returns:

the Cache or null if it does exist or can't be pre-configured
getCache
(String
the name of the managed Cache to acquire
cacheName
,
Class<K>
the expected Class of the key
keyType
,
Class<V>
the expected Class of the value
valueType
)

A convenience that method that looks up a managed Cache given its name.

public static CachingProvider

Returns:

the CachingProvider
getCachingProvider
()

Obtains the default CachingProvider available via the getDefaultClassLoader().

public static CachingProvider

Returns:

the CachingProvider
getCachingProvider
(ClassLoader
the ClassLoader to use for loading the CachingProvider
classLoader
)

Obtains the single CachingProvider visible to the specified ClassLoader.

public static CachingProvider

Returns:

the CachingProvider
getCachingProvider
(String
the fully qualified class name of the CachingProvider
fullyQualifiedClassName
)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the getDefaultClassLoader().

public static CachingProvider

Returns:

the CachingProvider
getCachingProvider
(String
the fully qualified class name of the CachingProvider
fullyQualifiedClassName
,
ClassLoader classLoader)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the provided ClassLoader.

public static Iterable<CachingProvider>

Returns:

an Iterable of CachingProviders loaded by the specified ClassLoader
getCachingProviders
()

Obtains the CachingProviders that are available via the getDefaultClassLoader().

public static Iterable<CachingProvider>

Returns:

an Iterable of CachingProviders loaded by the specified ClassLoader
getCachingProviders
(ClassLoader
the ClassLoader of the returned CachingProviders
classLoader
)

Obtains the CachingProviders that are available via the specified ClassLoader.

public static ClassLoader

Returns:

the default ClassLoader
getDefaultClassLoader
()

Obtains the ClassLoader to use for API methods that don't explicitly require a ClassLoader but internally require one.

public static void
setDefaultClassLoader(ClassLoader
the ClassLoader or null if the calling Thread#getContextClassLoader() should be used
classLoader
)

Set the ClassLoader to use for API methods that don't explicitly require a ClassLoader, but internally use one.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CACHING_PROVIDERSback to summary
private static final Caching.CachingProviderRegistry CACHING_PROVIDERS

The CachingProviderRegistry that tracks the CachingProviders.

JAVAX_CACHE_CACHING_PROVIDERback to summary
public static final String JAVAX_CACHE_CACHING_PROVIDER

The javax.cache.spi.cachingprovider constant.

Constructor Detail

Cachingback to summary
private Caching()

No public constructor as all methods are static.

Method Detail

getCacheback to summary
public static <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valueType)

A convenience that method that looks up a managed Cache given its name. using the default CachingProvider and CacheManager . For the full range of Cache look up methods see CacheManager.

This method must be used for Caches that were configured with runtime key and value types. Use CacheManager#getCache(String) for Caches where these were not specified.

Implementations must ensure that the key and value types are the same as those configured for the Cache prior to returning from this method.

Implementations may further perform type checking on mutative cache operations and throw a ClassCastException if these checks fail.

Implementations that support declarative mechanisms for pre-configuring Caches may return a pre-configured Cache instead of null.

Parameters
<K>
the type of key
<V>
the type of value
cacheName:String

the name of the managed Cache to acquire

keyType:Class<K>

the expected Class of the key

valueType:Class<V>

the expected Class of the value

Returns:Cache<K, V>

the Cache or null if it does exist or can't be pre-configured

Exceptions
IllegalStateException:
if the CacheManager is CacheManager#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)
getCachingProviderback to summary
public static CachingProvider getCachingProvider()

Obtains the default CachingProvider available via the getDefaultClassLoader().

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
should zero, or more than one CachingProvider be available on the classpath, or it could not be loaded
SecurityException:
when the operation could not be performed due to the current security settings
getCachingProviderback to summary
public static CachingProvider getCachingProvider(ClassLoader classLoader)

Obtains the single CachingProvider visible to the specified ClassLoader.

Parameters
classLoader:ClassLoader

the ClassLoader to use for loading the CachingProvider

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
should zero, or more than one CachingProvider be available on the classpath, or it could not be loaded
SecurityException:
when the operation could not be performed due to the current security settings
See Also
getCachingProviders(ClassLoader)
getCachingProviderback to summary
public static CachingProvider getCachingProvider(String fullyQualifiedClassName)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the getDefaultClassLoader(). Should this CachingProvider already 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:String

the fully qualified class name of the CachingProvider

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
if the CachingProvider cannot be created
SecurityException:
when the operation could not be performed due to the current security settings
getCachingProviderback to summary
public static CachingProvider getCachingProvider(String fullyQualifiedClassName, ClassLoader classLoader)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the provided ClassLoader. Should this CachingProvider already 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:String

the fully qualified class name of the CachingProvider

classLoader:ClassLoader

the ClassLoader to load the CachingProvider

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
if the CachingProvider cannot be created
SecurityException:
when the operation could not be performed due to the current security settings
getCachingProvidersback to summary
public static Iterable<CachingProvider> getCachingProviders()

Obtains the CachingProviders that are available via the getDefaultClassLoader().

If a javax.cache.spi.cachingprovider system property is defined, only that CachingProvider specified by that property is returned. Otherwise all CachingProviders that are available via a ServiceLoader for CachingProviders using the default ClassLoader (including those previously requested via getCachingProvider(String)) are returned.

Returns:Iterable<CachingProvider>

an Iterable of CachingProviders loaded by the specified ClassLoader

getCachingProvidersback to summary
public static Iterable<CachingProvider> getCachingProviders(ClassLoader classLoader)

Obtains the CachingProviders that are available via the specified ClassLoader.

If a javax.cache.spi.cachingprovider system property is defined, only that CachingProvider specified by that property is returned. Otherwise all CachingProviders that are available via a ServiceLoader for CachingProviders using the specified ClassLoader (including those previously requested via getCachingProvider(String, ClassLoader)) are returned.

Parameters
classLoader:ClassLoader

the ClassLoader of the returned CachingProviders

Returns:Iterable<CachingProvider>

an Iterable of CachingProviders loaded by the specified ClassLoader

getDefaultClassLoaderback to summary
public static ClassLoader getDefaultClassLoader()

Obtains the ClassLoader to use for API methods that don't explicitly require a ClassLoader but internally require one.

By default this is the Thread#getContextClassLoader().

Returns:ClassLoader

the default ClassLoader

setDefaultClassLoaderback to summary
public static void setDefaultClassLoader(ClassLoader classLoader)

Set the ClassLoader to use for API methods that don't explicitly require a ClassLoader, but internally use one.

Parameters
classLoader:ClassLoader

the ClassLoader or null if the calling Thread#getContextClassLoader() should be used

javax.cache back to summary

private Class Caching.CachingProviderRegistry

extends Object
Class Inheritance

Maintains a registry of loaded CachingProviders scoped by ClassLoader.

Field Summary

Modifier and TypeField and Description
private WeakHashMap<ClassLoader, LinkedHashMap<String, CachingProvider>>
cachingProviders

The CachingProviders by Class Name organized by the ClassLoader was used to load them.

private volatile ClassLoader

Constructor Summary

AccessConstructor and Description
public
CachingProviderRegistry()

Constructs a CachingProviderManager.

Method Summary

Modifier and TypeMethod and Description
public CachingProvider

Returns:

the CachingProvider
getCachingProvider
()

Obtains the only CachingProvider defined by the getDefaultClassLoader().

public CachingProvider

Returns:

the CachingProvider
getCachingProvider
(ClassLoader
the ClassLoader to use for loading the CachingProvider
classLoader
)

Obtains the only CachingProvider defined by the specified ClassLoader.

public CachingProvider

Returns:

the CachingProvider
getCachingProvider
(String
the fully qualified class name of the CachingProvider
fullyQualifiedClassName
)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the getDefaultClassLoader().

public synchronized CachingProvider

Returns:

the CachingProvider
getCachingProvider
(String
the fully qualified class name of the CachingProvider
fullyQualifiedClassName
,
ClassLoader classLoader)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the provided ClassLoader.

public Iterable<CachingProvider>

Returns:

an Iterable of CachingProviders loaded by the default ClassLoader
getCachingProviders
()

Obtain the CachingProviders that are available via the getDefaultClassLoader().

public synchronized Iterable<CachingProvider>

Returns:

an Iterable of CachingProviders loaded by the specified ClassLoader
getCachingProviders
(ClassLoader
the ClassLoader of the returned CachingProviders
classLoader
)

Obtain the CachingProviders that are available via the specified ClassLoader.

public ClassLoader

Returns:

the default ClassLoader
getDefaultClassLoader
()

Obtains the ClassLoader to use for API methods that don't explicitly require a ClassLoader but internally require one.

protected CachingProvider

Returns:

a new CachingProvider instance
loadCachingProvider
(String
the name of the CachingProvider class
fullyQualifiedClassName
,
ClassLoader
the ClassLoader to use
classLoader
)

Load and instantiate the CachingProvider with the specified fully qualified class name using the provided ClassLoader

public void
setDefaultClassLoader(ClassLoader
the ClassLoader or null if the calling Thread#getContextClassLoader() should be used
classLoader
)

Set the ClassLoader to use for API methods that don't explicitly require a ClassLoader, but internally use one.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachingProvidersback to summary
private WeakHashMap<ClassLoader, LinkedHashMap<String, CachingProvider>> cachingProviders

The CachingProviders by Class Name organized by the ClassLoader was used to load them.

classLoaderback to summary
private volatile ClassLoader classLoader

The default ClassLoader. When null the Thread#getContextClassLoader() will be used.

Constructor Detail

CachingProviderRegistryback to summary
public CachingProviderRegistry()

Constructs a CachingProviderManager.

Method Detail

getCachingProviderback to summary
public CachingProvider getCachingProvider()

Obtains the only CachingProvider defined by the getDefaultClassLoader().

Should zero or more than one CachingProviders be available, a CacheException is thrown.

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
should zero or more than one CachingProvider be available or a CachingProvider could not be loaded
See Also
getCachingProvider(ClassLoader), getCachingProviders(ClassLoader)
getCachingProviderback to summary
public CachingProvider getCachingProvider(ClassLoader classLoader)

Obtains the only CachingProvider defined by the specified ClassLoader.

Should zero or more than one CachingProviders be available, a CacheException is thrown.

Parameters
classLoader:ClassLoader

the ClassLoader to use for loading the CachingProvider

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
should zero or more than one CachingProvider be available or a CachingProvider could not be loaded
See Also
getCachingProviders(ClassLoader)
getCachingProviderback to summary
public CachingProvider getCachingProvider(String fullyQualifiedClassName)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the getDefaultClassLoader(). Should this CachingProvider already be loaded it is simply returned, otherwise an attempt will be made to load and instantiate the specified class name (using a no-args constructor).

Parameters
fullyQualifiedClassName:String

the fully qualified class name of the CachingProvider

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
when the CachingProvider can't be created
getCachingProviderback to summary
public synchronized CachingProvider getCachingProvider(String fullyQualifiedClassName, ClassLoader classLoader)

Obtain the CachingProvider that is implemented by the specified fully qualified class name using the provided ClassLoader. Should this CachingProvider already 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:String

the fully qualified class name of the CachingProvider

classLoader:ClassLoader

the ClassLoader to load the CachingProvider

Returns:CachingProvider

the CachingProvider

Exceptions
CacheException:
when the CachingProvider can't be created
getCachingProvidersback to summary
public Iterable<CachingProvider> getCachingProviders()

Obtain the CachingProviders that are available via the getDefaultClassLoader().

If a javax.cache.spi.cachingprovider system property is defined, only that CachingProvider specified by that property is returned. Otherwise all CachingProviders that are available via a ServiceLoader for CachingProviders using the default ClassLoader (and those explicitly requested via getCachingProvider(String)) are returned.

Returns:Iterable<CachingProvider>

an Iterable of CachingProviders loaded by the default ClassLoader

getCachingProvidersback to summary
public synchronized Iterable<CachingProvider> getCachingProviders(ClassLoader classLoader)

Obtain the CachingProviders that are available via the specified ClassLoader.

If a javax.cache.spi.cachingprovider system property is defined, only that CachingProvider specified by that property is returned. Otherwise all CachingProviders that are available via a ServiceLoader for CachingProviders using the specified ClassLoader (and those explicitly requested via getCachingProvider(String, ClassLoader)) are returned.

Parameters
classLoader:ClassLoader

the ClassLoader of the returned CachingProviders

Returns:Iterable<CachingProvider>

an Iterable of CachingProviders loaded by the specified ClassLoader

getDefaultClassLoaderback to summary
public ClassLoader getDefaultClassLoader()

Obtains the ClassLoader to use for API methods that don't explicitly require a ClassLoader but internally require one.

By default this is the Thread#getContextClassLoader().

Returns:ClassLoader

the default ClassLoader

loadCachingProviderback to summary
protected CachingProvider loadCachingProvider(String fullyQualifiedClassName, ClassLoader classLoader) throws CacheException

Load and instantiate the CachingProvider with the specified fully qualified class name using the provided ClassLoader

Parameters
fullyQualifiedClassName:String

the name of the CachingProvider class

classLoader:ClassLoader

the ClassLoader to use

Returns:CachingProvider

a new CachingProvider instance

Exceptions
CacheException:
if the specified CachingProvider could not be loaded or the specified class is not a CachingProvider
setDefaultClassLoaderback to summary
public void setDefaultClassLoader(ClassLoader classLoader)

Set the ClassLoader to use for API methods that don't explicitly require a ClassLoader, but internally use one.

Parameters
classLoader:ClassLoader

the ClassLoader or null if the calling Thread#getContextClassLoader() should be used