You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2017/07/23 16:06:28 UTC
svn commit: r1802733 - in /openwebbeans/trunk:
webbeans-el22/src/test/java/org/apache/webbeans/el/test/
webbeans-impl/src/main/java/org/apache/webbeans/config/
webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/
webbeans-impl/src/main/java...
Author: rmannibucau
Date: Sun Jul 23 16:06:28 2017
New Revision: 1802733
URL: http://svn.apache.org/viewvc?rev=1802733&view=rev
Log:
OWB-1204 scan interceptors and decorators in annotated mode
Added:
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/discovery/
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/discovery/InterceptorAnnotatedDiscoveryTest.java
Removed:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/test/OpenWebBeansTestLifeCycle.java
Modified:
openwebbeans/trunk/webbeans-el22/src/test/java/org/apache/webbeans/el/test/AbstractUnitTest.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/test/OpenWebBeansTestMetaDataDiscoveryService.java
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java
Modified: openwebbeans/trunk/webbeans-el22/src/test/java/org/apache/webbeans/el/test/AbstractUnitTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-el22/src/test/java/org/apache/webbeans/el/test/AbstractUnitTest.java?rev=1802733&r1=1802732&r2=1802733&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-el22/src/test/java/org/apache/webbeans/el/test/AbstractUnitTest.java (original)
+++ openwebbeans/trunk/webbeans-el22/src/test/java/org/apache/webbeans/el/test/AbstractUnitTest.java Sun Jul 23 16:06:28 2017
@@ -22,6 +22,7 @@ import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Properties;
import java.util.Set;
import javax.enterprise.inject.spi.Bean;
@@ -31,16 +32,17 @@ import javax.enterprise.inject.spi.Exten
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.config.WebBeansFinder;
import org.apache.webbeans.exception.WebBeansConfigurationException;
-import org.apache.webbeans.lifecycle.test.OpenWebBeansTestLifeCycle;
+import org.apache.webbeans.lifecycle.StandaloneLifeCycle;
import org.apache.webbeans.lifecycle.test.OpenWebBeansTestMetaDataDiscoveryService;
import org.apache.webbeans.spi.ContainerLifecycle;
+import org.apache.webbeans.spi.ScannerService;
import org.apache.webbeans.util.WebBeansUtil;
import org.junit.Assert;
public abstract class AbstractUnitTest
{
- private OpenWebBeansTestLifeCycle testLifecycle;
+ private StandaloneLifeCycle testLifecycle;
private List<Extension> extensions = new ArrayList<Extension>();
private WebBeansContext webBeansContext;
@@ -58,7 +60,14 @@ public abstract class AbstractUnitTest
{
WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader());
//Creates a new container
- testLifecycle = new OpenWebBeansTestLifeCycle();
+ testLifecycle = new StandaloneLifeCycle()
+ {
+ @Override
+ public void beforeInitApplication(final Properties object)
+ {
+ WebBeansContext.getInstance().registerService(ScannerService.class, new OpenWebBeansTestMetaDataDiscoveryService());
+ }
+ };
webBeansContext = WebBeansContext.getInstance();
for (Extension ext : extensions)
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java?rev=1802733&r1=1802732&r2=1802733&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java Sun Jul 23 16:06:28 2017
@@ -1152,7 +1152,6 @@ public class BeansDeployer
for (Map.Entry<BeanArchiveInformation, Set<Class<?>>> bdaEntry : beanClassesPerBda.entrySet())
{
- BeanArchiveInformation bdaInfo = bdaEntry.getKey();
List<AnnotatedType<?>> annotatedTypes = annotatedTypesFromBdaClassPath(bdaEntry.getValue(), foundClasses);
annotatedTypesPerBda.put(bdaEntry.getKey(), annotatedTypes);
}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java?rev=1802733&r1=1802732&r2=1802733&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java Sun Jul 23 16:06:28 2017
@@ -38,6 +38,8 @@ import org.apache.xbean.finder.ClassLoad
import org.apache.xbean.finder.archive.Archive;
import org.apache.xbean.finder.filter.Filter;
+import javax.decorator.Decorator;
+import javax.interceptor.Interceptor;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.URL;
@@ -446,7 +448,9 @@ public abstract class AbstractMetaDataDi
// check whether this class has 'scope' annotations or a stereotype
for (AnnotationFinder.AnnotationInfo annotationInfo : classInfo.getAnnotations())
{
- if (isBeanAnnotation(annotationInfo))
+ if (Interceptor.class.getName().equals(annotationInfo.getName()) ||
+ Decorator.class.getName().equals(annotationInfo.getName()) ||
+ isBeanAnnotation(annotationInfo))
{
return true;
}
@@ -464,7 +468,10 @@ public abstract class AbstractMetaDataDi
{
Class<? extends Annotation> annotationType = (Class<? extends Annotation>) WebBeansUtil.getCurrentClassLoader().loadClass(annotationName);
boolean isBeanAnnotation = webBeansContext().getBeanManagerImpl().isScope(annotationType);
- isBeanAnnotation = isBeanAnnotation || webBeansContext().getBeanManagerImpl().isStereotype(annotationType);
+ if (!isBeanAnnotation)
+ {
+ isBeanAnnotation = webBeansContext().getBeanManagerImpl().isStereotype(annotationType);
+ }
return isBeanAnnotation;
}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/test/OpenWebBeansTestMetaDataDiscoveryService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/test/OpenWebBeansTestMetaDataDiscoveryService.java?rev=1802733&r1=1802732&r2=1802733&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/test/OpenWebBeansTestMetaDataDiscoveryService.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/lifecycle/test/OpenWebBeansTestMetaDataDiscoveryService.java Sun Jul 23 16:06:28 2017
@@ -28,6 +28,8 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
+import static java.util.Collections.emptySet;
+
/**
* Used by each test.
* @version $Rev$ $Date$
@@ -61,7 +63,7 @@ public class OpenWebBeansTestMetaDataDis
@Override
public Set<Class<?>> getBeanClasses()
{
- return new HashSet<>(classes);
+ return classes == null ? emptySet() : new HashSet<>(classes);
}
/**
Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java?rev=1802733&r1=1802732&r2=1802733&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java Sun Jul 23 16:06:28 2017
@@ -22,10 +22,11 @@ import org.apache.webbeans.config.WebBea
import org.apache.webbeans.config.WebBeansFinder;
import org.apache.webbeans.exception.WebBeansConfigurationException;
import org.apache.webbeans.inject.OWBInjector;
-import org.apache.webbeans.lifecycle.test.OpenWebBeansTestLifeCycle;
+import org.apache.webbeans.lifecycle.StandaloneLifeCycle;
import org.apache.webbeans.lifecycle.test.OpenWebBeansTestMetaDataDiscoveryService;
import org.apache.webbeans.spi.ContainerLifecycle;
import org.apache.webbeans.spi.ContextsService;
+import org.apache.webbeans.spi.ScannerService;
import org.apache.webbeans.util.WebBeansUtil;
import org.junit.After;
import org.junit.Assert;
@@ -39,14 +40,18 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import java.util.Properties;
import java.util.Set;
public abstract class AbstractUnitTest
{
- private OpenWebBeansTestLifeCycle testLifecycle;
- private List<Extension> extensions = new ArrayList<Extension>();
+ private StandaloneLifeCycle testLifecycle;
+ private Map<Class<?>, Object> services = new HashMap<>();
+ private List<Extension> extensions = new ArrayList<>();
private List<Class<?>> interceptors = new ArrayList<Class<?>>();
private List<Class<?>> decorators = new ArrayList<Class<?>>();
private WebBeansContext webBeansContext;
@@ -107,7 +112,25 @@ public abstract class AbstractUnitTest
WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader());
//Creates a new container
- testLifecycle = new OpenWebBeansTestLifeCycle();
+ testLifecycle = new StandaloneLifeCycle()
+ {
+ @Override
+ public void beforeInitApplication(final Properties properties)
+ {
+ final WebBeansContext instance = WebBeansContext.getInstance();
+ services.forEach((k, v) ->
+ {
+ final Class key = k;
+ instance.registerService(key, v);
+ });
+ if (!services.containsKey(ScannerService.class))
+ {
+ instance.registerService(ScannerService.class, new OpenWebBeansTestMetaDataDiscoveryService());
+ }
+
+ super.beforeInitApplication(properties);
+ }
+ };
webBeansContext = WebBeansContext.getInstance();
for (Extension ext : extensions)
@@ -134,11 +157,14 @@ public abstract class AbstractUnitTest
}
//Deploy bean classes
- OpenWebBeansTestMetaDataDiscoveryService discoveryService = (OpenWebBeansTestMetaDataDiscoveryService)webBeansContext.getScannerService();
- discoveryService.deployClasses(beanClasses);
- if (beanXmls != null)
+ if (!beanClasses.isEmpty() || beanXmls != null)
{
- discoveryService.deployXMLs(beanXmls);
+ OpenWebBeansTestMetaDataDiscoveryService discoveryService = (OpenWebBeansTestMetaDataDiscoveryService)webBeansContext.getScannerService();
+ discoveryService.deployClasses(beanClasses);
+ if (beanXmls != null)
+ {
+ discoveryService.deployXMLs(beanXmls);
+ }
}
//Start application
@@ -224,6 +250,11 @@ public abstract class AbstractUnitTest
}
+ protected <T> void addService(final Class<T> type, final T instance)
+ {
+ this.services.put(type, instance);
+ }
+
/**
* @param packageName package of the beans.xml file
* @param fileName name of the beans xml file, without '.xml'
Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/discovery/InterceptorAnnotatedDiscoveryTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/discovery/InterceptorAnnotatedDiscoveryTest.java?rev=1802733&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/discovery/InterceptorAnnotatedDiscoveryTest.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/discovery/InterceptorAnnotatedDiscoveryTest.java Sun Jul 23 16:06:28 2017
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.test.discovery;
+
+import org.apache.webbeans.corespi.scanner.xbean.CdiArchive;
+import org.apache.webbeans.corespi.se.DefaultScannerService;
+import org.apache.webbeans.spi.BeanArchiveService;
+import org.apache.webbeans.spi.ScannerService;
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.apache.webbeans.util.WebBeansUtil;
+import org.apache.webbeans.xml.DefaultBeanArchiveInformation;
+import org.apache.xbean.finder.AnnotationFinder;
+import org.junit.Test;
+
+import javax.annotation.Priority;
+import javax.enterprise.context.ApplicationScoped;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InterceptorBinding;
+import javax.interceptor.InvocationContext;
+import java.io.IOException;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.util.Map;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyMap;
+import static java.util.Collections.singletonMap;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class InterceptorAnnotatedDiscoveryTest extends AbstractUnitTest
+{
+ @Test
+ public void discover()
+ {
+ setClasses(FooInterceptor.class.getName(), FooMe.class.getName());
+
+ startContainer();
+ assertEquals("foo", getInstance(FooMe.class).foo());
+ }
+
+ private void setClasses(final String... classes) {
+ // replace the implicit BDA by an annotated one
+ addService(ScannerService.class, new DefaultScannerService()
+ {
+ @Override
+ protected AnnotationFinder initFinder()
+ {
+ if (finder != null)
+ {
+ return finder;
+ }
+
+ super.initFinder();
+ archive = new CdiArchive(webBeansContext().getBeanArchiveService(), WebBeansUtil.getCurrentClassLoader(), emptyMap(), null, null)
+ {
+ @Override
+ public Map<String, FoundClasses> classesByUrl()
+ {
+ try
+ {
+ final String url = "openwebbeans://annotated";
+ return singletonMap(url, new FoundClasses(
+ new URL("openwebbeans", null, -1, "annotated", new URLStreamHandler()
+ {
+ @Override
+ protected URLConnection openConnection(final URL u) throws IOException
+ {
+ return new URLConnection(u)
+ {
+ @Override
+ public void connect() throws IOException
+ {
+ // no-op
+ }
+ };
+ }
+ }),
+ asList(classes),
+ new DefaultBeanArchiveInformation("openwebbeans://default")
+ {{
+ setBeanDiscoveryMode(BeanArchiveService.BeanDiscoveryMode.ANNOTATED);
+ }}));
+ }
+ catch (final MalformedURLException e)
+ {
+ fail(e.getMessage());
+ throw new IllegalStateException(e);
+ }
+ }
+ };
+
+ return finder;
+ }
+ });
+ }
+
+ @Interceptor
+ @Foo
+ @Priority(0)
+ public static class FooInterceptor
+ {
+ @AroundInvoke
+ public Object foo(final InvocationContext ic)
+ {
+ return "foo";
+ }
+ }
+
+ @Foo
+ @ApplicationScoped
+ public static class FooMe {
+ String foo()
+ {
+ return "bar";
+ }
+ }
+
+ @InterceptorBinding
+ @Retention(RUNTIME)
+ @Target(TYPE)
+ public @interface Foo
+ {
+ }
+}