use of javax.enterprise.inject.Produces in project Payara by payara.
the class JSR107Producer method createCache.
/**
* Produces a Cache for injection. If the @NamedCache annotation is present the
* cache will be created based on the values of the annotation field
*
* Otherwise the cache will be created with the cache name being equal
* to the fully qualified name of the class into which it is injected
* @param ip
* @return
*/
@Produces
public <K, V> Cache<K, V> createCache(InjectionPoint ip) {
Cache<K, V> result;
if (!hazelcastCore.isEnabled()) {
logger.warning("Unable to inject Cache as Hazelcast is Disabled");
return null;
}
// determine the cache name first start with the default name
String cacheName = ip.getMember().getDeclaringClass().getCanonicalName();
NamedCache ncqualifier = ip.getAnnotated().getAnnotation(NamedCache.class);
CacheManager manager = getCacheManager(ip);
if (ncqualifier != null) {
// configure the cache based on the annotation
String qualifierName = ncqualifier.cacheName();
if (!"".equals(cacheName)) {
cacheName = qualifierName;
}
Class keyClass = ncqualifier.keyClass();
Class valueClass = ncqualifier.valueClass();
result = manager.getCache(cacheName, keyClass, valueClass);
if (result == null) {
MutableConfiguration<K, V> config = new MutableConfiguration<>();
config.setTypes(keyClass, valueClass);
// determine the expiry policy
Class expiryPolicyFactoryClass = ncqualifier.expiryPolicyFactoryClass();
if (!"Object".equals(expiryPolicyFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(expiryPolicyFactoryClass);
config.setExpiryPolicyFactory(factory);
}
// determine the cache writer if any
Class writerFactoryClass = ncqualifier.cacheWriterFactoryClass();
if (!"Object".equals(writerFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(writerFactoryClass);
config.setCacheWriterFactory(factory);
}
config.setWriteThrough(ncqualifier.writeThrough());
// determine the cache loader if any
Class loaderFactoryClass = ncqualifier.cacheLoaderFactoryClass();
if (!"Object".equals(loaderFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(loaderFactoryClass);
config.setCacheLoaderFactory(factory);
}
config.setReadThrough(ncqualifier.readThrough());
config.setManagementEnabled(ncqualifier.managementEnabled());
config.setStatisticsEnabled(ncqualifier.statisticsEnabled());
result = manager.createCache(cacheName, config);
}
} else {
// configure a "raw" cache
Bean<?> bean = ip.getBean();
if (bean != null) {
Class<?> beanClass = bean.getBeanClass();
CacheDefaults defaults = beanClass.getAnnotation(CacheDefaults.class);
if (defaults != null) {
String cacheNameFromAnnotation = defaults.cacheName();
if (!"".equals(cacheNameFromAnnotation)) {
cacheName = cacheNameFromAnnotation;
}
}
}
result = manager.getCache(cacheName);
if (result == null) {
MutableConfiguration<K, V> config = new MutableConfiguration<>();
config.setManagementEnabled(true);
config.setStatisticsEnabled(true);
result = manager.createCache(cacheName, config);
}
}
return result;
}
Aggregations