Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the option to request a specific jcache provider in cache multi… #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-core</artifactId>
<version>4.7.1</version>
<version>4.7.4</version>
<scope>test</scope>
</dependency>

<!-- Use Hazelcast as JCache provider for test -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.4.1</version>
<version>3.7.4</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/tomitribe/jcache/cdi/DeclarativeCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.tomitribe.jcache.cdi;

import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Qualifier
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface DeclarativeCache
{
@Nonbinding String value() default "";

@Nonbinding Class<?> keyType() default Object.class;

@Nonbinding Class<?> valueType() default Object.class;
}
69 changes: 67 additions & 2 deletions src/main/java/org/tomitribe/jcache/cdi/ExtraJCacheExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,44 @@
*/
package org.tomitribe.jcache.cdi;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import javax.enterprise.context.Dependent;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessBean;
import java.util.Properties;

// add default CacheProvider and CacheManager
public class ExtraJCacheExtension implements Extension
{
private static final boolean ACTIVATED = "true".equals(System.getProperty("org.apache.jcs.extra.cdi", "true"));

private static final String PROPERTY_LEGACY_ACTIVATION = "org.apache.jcs.extra.cdi";
private static final String PROPERTY_ACTIVATION = "org.apache.jcache.extra.cdi";
private static final boolean ACTIVATED = isExtensionActivated();
private static final String JCACHE_PROVIDER_CLASS = System.getProperty("cdi.jcache.cachingprovider");

private boolean cacheManagerFound = false;
private boolean cacheProviderFound = false;
private CacheManager cacheManager;
private CachingProvider cachingProvider;

public void addCacheProducer(final @Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager)
{
AnnotatedType<DeclarativeCacheProducer> annotatedType = beanManager.createAnnotatedType(DeclarativeCacheProducer.class);
beforeBeanDiscovery.addAnnotatedType(annotatedType);
}

public <A> void processBean(final @Observes ProcessBean<A> processBeanEvent)
{
if (!ACTIVATED)
Expand Down Expand Up @@ -78,7 +95,7 @@ public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery
return;
}

cachingProvider = Caching.getCachingProvider();
cachingProvider = findCachingProvider();
if (!cacheManagerFound)
{
cacheManager = cachingProvider.getCacheManager(
Expand All @@ -104,4 +121,52 @@ public void destroyIfCreated(final @Observes BeforeShutdown beforeShutdown)
cachingProvider.close();
}
}

private static boolean isExtensionActivated() {
String val = System.getProperty(PROPERTY_ACTIVATION);
if (val == null)
{
val = System.getProperty(PROPERTY_LEGACY_ACTIVATION);
}
if (val == null)
{
val = "true";
}
return "true".equals(val);
}

private CachingProvider findCachingProvider()
{
if (JCACHE_PROVIDER_CLASS == null)
{
return Caching.getCachingProvider();
}
return Caching.getCachingProvider(JCACHE_PROVIDER_CLASS);
}

public static final class DeclarativeCacheProducer {

@Produces
@Dependent
@DeclarativeCache
public Cache injectDeclarativeCache(final InjectionPoint injectionPoint, final CacheManager cacheManager)
{
DeclarativeCache annotation = injectionPoint.getAnnotated().getAnnotation(DeclarativeCache.class);
if (annotation == null)
{
return null;
}

String cacheName = annotation.value();

Class<?> keyType = annotation.keyType();
Class<?> valueType = annotation.valueType();

if (keyType == Object.class && valueType == Object.class)
{
return cacheManager.getCache(cacheName);
}
return cacheManager.getCache(cacheName, keyType, valueType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@
import org.junit.Before;
import org.junit.Test;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.configuration.Configuration;
import javax.cache.spi.CachingProvider;
import javax.ejb.embeddable.EJBContainer;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.Random;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

public class ExtraJCacheExtensionTest
{
Expand Down Expand Up @@ -91,13 +96,50 @@ public void defaultCacheProvider()
assertNotNull(bean.getProvider());
}

@Test
public void declarativeCacheCache1() {
Cache<String, String> cache1 = bean.getCache1();
assertNotNull(cache1);
Configuration configuration = cache1.getConfiguration(Configuration.class);
assertSame(String.class, configuration.getKeyType());
assertSame(String.class, configuration.getValueType());
}

@Test
public void declarativeCacheCache2() {
Cache<Integer, Integer> cache2 = bean.getCache2();
assertNotNull(cache2);
Configuration configuration = cache2.getConfiguration(Configuration.class);
assertSame(Integer.class, configuration.getKeyType());
assertSame(Integer.class, configuration.getValueType());
}

@Test
public void declarativeCachesNotSame() {
Cache<String, String> cache1 = bean.getCache1();
Cache<Integer, Integer> cache2 = bean.getCache2();
assertNotSame(cache1, cache2);
}

public static class BeanWithInjections {
@Inject
private CacheManager mgr;

@Inject
private CachingProvider provider;

@Inject
@DeclarativeCache(value = "test1", keyType = String.class, valueType = String.class)
private Cache<String, String> cache1;

@Inject
@DeclarativeCache(value = "test2", keyType = Integer.class, valueType = Integer.class)
private Cache<Integer, Integer> cache2;

@Inject
@DeclarativeCache(value = "test3")
private Cache<Object, Object> cache3;

public CacheManager getMgr()
{
return mgr;
Expand All @@ -107,6 +149,16 @@ public CachingProvider getProvider()
{
return provider;
}

public Cache<String, String> getCache1()
{
return cache1;
}

public Cache<Integer, Integer> getCache2()
{
return cache2;
}
}

}
2 changes: 1 addition & 1 deletion src/test/resources/hazelcast-client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<hazelcast-client xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.3.xsd"
<hazelcast-client xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.7.xsd"
xmlns="http://www.hazelcast.com/schema/client-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/hazelcast.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<cache name="test1">
<key-type class-name="java.lang.String"/>
<value-type class-name="java.lang.String"/>
</cache>
<cache name="test2">
<key-type class-name="java.lang.Integer"/>
<value-type class-name="java.lang.Integer"/>
</cache>
<cache name="test3">
</cache>
</hazelcast>