Search in sources :

Example 56 with Produces

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;
}
Also used : Factory(javax.cache.configuration.Factory) MutableConfiguration(javax.cache.configuration.MutableConfiguration) NamedCache(fish.payara.cdi.jsr107.impl.NamedCache) CacheDefaults(javax.cache.annotation.CacheDefaults) CacheManager(javax.cache.CacheManager) Produces(javax.enterprise.inject.Produces)

Aggregations

Produces (javax.enterprise.inject.Produces)56 Named (javax.inject.Named)32 ApplicationScoped (javax.enterprise.context.ApplicationScoped)25 Properties (java.util.Properties)12 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)12 ArrayList (java.util.ArrayList)6 Dependent (javax.enterprise.context.Dependent)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 RequestScoped (javax.enterprise.context.RequestScoped)3 InjectableResource (org.apache.deltaspike.core.api.resourceloader.InjectableResource)3 InjectableResourceProvider (org.apache.deltaspike.core.api.resourceloader.InjectableResourceProvider)3 OperationsFacade (org.gluu.site.ldap.OperationsFacade)3 LdapEntryManager (org.gluu.site.ldap.persistence.LdapEntryManager)3 FunctionMapperImpl (com.sun.el.lang.FunctionMapperImpl)2 VariableMapperImpl (com.sun.el.lang.VariableMapperImpl)2 CompositeELResolver (javax.el.CompositeELResolver)2 FieldMetaData (org.fusesource.camel.component.sap.model.rfc.FieldMetaData)2 FunctionTemplate (org.fusesource.camel.component.sap.model.rfc.FunctionTemplate)2 ListFieldMetaData (org.fusesource.camel.component.sap.model.rfc.ListFieldMetaData)2