Author: rmannibucau Date: Mon Feb 23 22:13:56 2015 New Revision: 1661801 URL: http://svn.apache.org/r1661801 Log: trying to tolerate ears on cdi 1.0 - still weak but better + pasting a sample of beans.xml
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/MakeJCacheCDIInterceptorFriendly.java Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/MakeJCacheCDIInterceptorFriendly.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/MakeJCacheCDIInterceptorFriendly.java?rev=1661801&r1=1661800&r2=1661801&view=diff ============================================================================== --- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/MakeJCacheCDIInterceptorFriendly.java (original) +++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/MakeJCacheCDIInterceptorFriendly.java Mon Feb 23 22:13:56 2015 @@ -23,6 +23,7 @@ import java.lang.reflect.Type; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import javax.cache.annotation.CachePut; import javax.cache.annotation.CacheRemove; import javax.cache.annotation.CacheRemoveAll; @@ -46,17 +47,32 @@ import javax.enterprise.util.AnnotationL import static java.util.Arrays.asList; -// TODO: observe annotated type (or maybe sthg else) to cache data and inecjt this extension (used as metadata cache) +// TODO: observe annotated type (or maybe sthg else) to cache data and inject this extension (used as metadata cache) // to get class model and this way allow to add cache annotation on the fly - == avoid java pure reflection to get metadata public class MakeJCacheCDIInterceptorFriendly implements Extension { + private static final AtomicInteger id = new AtomicInteger(); + private boolean needHelper = true; protected void discoverInterceptorBindings(final @Observes BeforeBeanDiscovery beforeBeanDiscoveryEvent, final BeanManager bm) { // CDI 1.1 will just pick createAnnotatedType(X) as beans so we'll skip our HelperBean - // but CDI 1.0 needs our HelperBean + interceptors in beans.xml + // but CDI 1.0 needs our HelperBean + interceptors in beans.xml like: + /* + <beans xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> + <interceptors> + <class>org.apache.commons.jcs.jcache.cdi.CacheResultInterceptor</class> + <class>org.apache.commons.jcs.jcache.cdi.CacheRemoveAllInterceptor</class> + <class>org.apache.commons.jcs.jcache.cdi.CacheRemoveInterceptor</class> + <class>org.apache.commons.jcs.jcache.cdi.CachePutInterceptor</class> + </interceptors> + </beans> + */ bm.createAnnotatedType(CDIJCacheHelper.class); for (final Class<?> interceptor : asList( CachePutInterceptor.class, CacheRemoveInterceptor.class, @@ -101,18 +117,30 @@ public class MakeJCacheCDIInterceptorFri } private String findIdSuffix(final BeanManager bm) { + boolean useId = false; try { - final Class<?> sc = Thread.currentThread().getContextClassLoader().loadClass("javax.servlet.ServletContext"); - final Set<Bean<?>> beans = bm.getBeans(sc); - if (beans != null && !beans.isEmpty()) { - final Bean<?> b = bm.resolve(beans); - if (b != null) { - final Object instance = bm.getReference(b, sc, bm.createCreationalContext(null)); - return "web#" + sc.getMethod("getContextPath").invoke(instance).toString(); + final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + try { // CDI > 1.0 + contextClassLoader.loadClass("javax.enterprise.inject.spi.InjectionTargetFactory"); + } catch (final Throwable th) { + useId = true; // CDI 1.0 + } + if (!useId) { + final Class<?> sc = contextClassLoader.loadClass("javax.servlet.ServletContext"); + final Set<Bean<?>> beans = bm.getBeans(sc); + if (beans != null && !beans.isEmpty()) { + final Bean<?> b = bm.resolve(beans); + if (b != null) { + final Object instance = bm.getReference(b, sc, bm.createCreationalContext(null)); + return "web#" + sc.getMethod("getContextPath").invoke(instance).toString(); + } } } } catch (final Throwable e) { - // no-op + useId = true; + } + if (useId) { // CDI 1.0, no idea how to differentiate with an id ear parts + return "lib" + id.incrementAndGet(); } return "lib"; }