From bed4f65ce0b02ad5cb1317571039afc4b0a6546d Mon Sep 17 00:00:00 2001 From: Jonathan Gallimore Date: Tue, 28 Jan 2025 10:52:07 +0000 Subject: [PATCH 1/4] OWB-1448 Fix Issue with Cdi annotation and alternatives --- .../apache/webbeans/config/BeansDeployer.java | 32 +++++++ .../junit5/features/AlternativeTest.java | 82 ++++++++++++++++++ .../junit5/features/InterceptorTest.java | 84 +++++++++++++++++++ .../parameter/ParameterResolutionTest.java | 2 +- .../junit5/perclass/PerMethodTest.java | 2 +- 5 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/AlternativeTest.java create mode 100644 webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/InterceptorTest.java diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java b/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java index 33c1ad9a1..6ce5957b4 100644 --- a/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java +++ b/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java @@ -108,6 +108,7 @@ import jakarta.enterprise.inject.spi.ObserverMethod; import jakarta.enterprise.inject.spi.Producer; +import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; @@ -115,6 +116,8 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; import java.security.PrivilegedActionException; import java.util.ArrayList; import java.util.Collection; @@ -1756,6 +1759,35 @@ protected void deployFromXML(ScannerService scanner) } logger.fine("Deploying configurations from XML has ended successfully."); + + try + { + final URL url = new URL("openwebbeans", null, 0, "cdi-standalone", new URLStreamHandler() + { + @Override + protected URLConnection openConnection(URL u) throws IOException + { + return null; + } + }); + + final BeanArchiveInformation beanArchiveInformation = beanArchiveService.getBeanArchiveInformation(url); + if (beanArchiveInformation != null) + { + configureDecorators(url, beanArchiveInformation.getDecorators()); + configureInterceptors(url, beanArchiveInformation.getInterceptors()); + configureAlternatives(url, beanArchiveInformation.getAlternativeClasses(), false); + configureAlternatives(url, beanArchiveInformation.getAlternativeStereotypes(), true); + configureAllowProxying(url, beanArchiveInformation.getAllowProxyingClasses()); + } + + logger.fine("Deploying embedded configurations has ended successfully."); + } + catch (Exception e) + { + logger.info("Error occurred: " + e.getMessage()); + e.printStackTrace(); + } } private void configureAlternatives(URL bdaLocation, List alternatives, boolean isStereotype) diff --git a/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/AlternativeTest.java b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/AlternativeTest.java new file mode 100644 index 000000000..f979aab56 --- /dev/null +++ b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/AlternativeTest.java @@ -0,0 +1,82 @@ +/* + * 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.openwebbeans.junit5.features; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Alternative; +import jakarta.enterprise.inject.Default; +import jakarta.inject.Inject; +import org.apache.openwebbeans.junit5.Cdi; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Cdi(disableDiscovery = true, classes = { + AlternativeTest.Service.class, AlternativeTest.Provider.class, AlternativeTest.DefaultProvider.class, AlternativeTest.AlternativeProvider.class +}, alternatives = AlternativeTest.AlternativeProvider.class) +public class AlternativeTest +{ + @Inject + private Service service; + + @Test + void test1() + { + assertEquals("alternative", service.run()); + } + + public interface Provider + { + String provide(); + } + + @Alternative + public static class AlternativeProvider implements Provider + { + @Override + public String provide() + { + return "alternative"; + } + } + + @Default + public static class DefaultProvider implements Provider + { + @Override + public String provide() + { + return "default"; + } + } + + @ApplicationScoped + public static class Service + { + + @Inject + private Provider provider; + + public String run() + { + return provider.provide(); + } + + } +} diff --git a/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/InterceptorTest.java b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/InterceptorTest.java new file mode 100644 index 000000000..0ad51310a --- /dev/null +++ b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/InterceptorTest.java @@ -0,0 +1,84 @@ +/* + * 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.openwebbeans.junit5.features; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.NormalScope; +import jakarta.enterprise.inject.Alternative; +import jakarta.enterprise.inject.Default; +import jakarta.inject.Inject; +import jakarta.interceptor.AroundInvoke; +import jakarta.interceptor.Interceptor; +import jakarta.interceptor.InterceptorBinding; +import jakarta.interceptor.InvocationContext; +import org.apache.openwebbeans.junit5.Cdi; +import org.junit.jupiter.api.Test; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Cdi(disableDiscovery = true, classes = { + InterceptorTest.Service.class, InterceptorTest.MyInterceptor.class, InterceptorTest.Wrap.class +}, interceptors = InterceptorTest.MyInterceptor.class) +public class InterceptorTest +{ + @Inject + private Service service; + + @Test + void test1() + { + assertEquals("Intercepted Hello World", service.run()); + } + + @Target({ElementType.TYPE, ElementType.METHOD}) + @Retention(RetentionPolicy.RUNTIME) + @InterceptorBinding + public @interface Wrap { + + } + + @Interceptor + @Wrap + public static class MyInterceptor { + @AroundInvoke + public Object restrictAccessBasedOnTime(InvocationContext ctx) throws Exception { + final Object result = ctx.proceed(); + if (result instanceof String) { + return "Intercepted " + result; + } else { + return result; + } + } + } + + @ApplicationScoped + public static class Service + { + @Wrap + public String run() + { + return "Hello World"; + } + } +} diff --git a/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/parameter/ParameterResolutionTest.java b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/parameter/ParameterResolutionTest.java index 228d91be7..fd5f01aa0 100644 --- a/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/parameter/ParameterResolutionTest.java +++ b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/parameter/ParameterResolutionTest.java @@ -34,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; -@Cdi(classes = MyService.class) +@Cdi(classes = MyService.class, disableDiscovery = true) class ParameterResolutionTest { @Inject MyService service; diff --git a/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/perclass/PerMethodTest.java b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/perclass/PerMethodTest.java index a1344917a..f38fb4cdb 100644 --- a/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/perclass/PerMethodTest.java +++ b/webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/perclass/PerMethodTest.java @@ -184,7 +184,7 @@ public Property[] properties() @Override public boolean disableDiscovery() { - return false; + return true; } @Override From 5c0a4fc14ad2f77dfa3f1cde9fcfda3d98c0a3fa Mon Sep 17 00:00:00 2001 From: Jonathan Gallimore Date: Thu, 30 Jan 2025 10:02:59 +0000 Subject: [PATCH 2/4] OWB-1448 removing left over e.printStackTrace() --- .../src/main/java/org/apache/webbeans/config/BeansDeployer.java | 1 - 1 file changed, 1 deletion(-) diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java b/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java index 6ce5957b4..53baf2278 100644 --- a/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java +++ b/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java @@ -1786,7 +1786,6 @@ protected URLConnection openConnection(URL u) throws IOException catch (Exception e) { logger.info("Error occurred: " + e.getMessage()); - e.printStackTrace(); } } From 64c1c3e1f8af2e3a66a41521e545fd7dc611f5f9 Mon Sep 17 00:00:00 2001 From: Jonathan Gallimore Date: Fri, 21 Feb 2025 14:37:52 +0000 Subject: [PATCH 3/4] OWB-1448 rework following feedback --- .../apache/webbeans/config/BeansDeployer.java | 31 ------------------- .../scanner/AbstractMetaDataDiscovery.java | 2 +- .../openwebbeans/se/CDISeScannerService.java | 17 ++++++++-- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java b/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java index 53baf2278..33c1ad9a1 100644 --- a/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java +++ b/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java @@ -108,7 +108,6 @@ import jakarta.enterprise.inject.spi.ObserverMethod; import jakarta.enterprise.inject.spi.Producer; -import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; @@ -116,8 +115,6 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; -import java.net.URLConnection; -import java.net.URLStreamHandler; import java.security.PrivilegedActionException; import java.util.ArrayList; import java.util.Collection; @@ -1759,34 +1756,6 @@ protected void deployFromXML(ScannerService scanner) } logger.fine("Deploying configurations from XML has ended successfully."); - - try - { - final URL url = new URL("openwebbeans", null, 0, "cdi-standalone", new URLStreamHandler() - { - @Override - protected URLConnection openConnection(URL u) throws IOException - { - return null; - } - }); - - final BeanArchiveInformation beanArchiveInformation = beanArchiveService.getBeanArchiveInformation(url); - if (beanArchiveInformation != null) - { - configureDecorators(url, beanArchiveInformation.getDecorators()); - configureInterceptors(url, beanArchiveInformation.getInterceptors()); - configureAlternatives(url, beanArchiveInformation.getAlternativeClasses(), false); - configureAlternatives(url, beanArchiveInformation.getAlternativeStereotypes(), true); - configureAllowProxying(url, beanArchiveInformation.getAllowProxyingClasses()); - } - - logger.fine("Deploying embedded configurations has ended successfully."); - } - catch (Exception e) - { - logger.info("Error occurred: " + e.getMessage()); - } } private void configureAlternatives(URL bdaLocation, List alternatives, boolean isStereotype) diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java b/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java index 3080013a5..13fe0fa9a 100644 --- a/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java +++ b/webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java @@ -90,7 +90,7 @@ public abstract class AbstractMetaDataDiscovery implements BdaScannerService * We store this information since not all containers and storages do support * new URL(...). */ - private final Map beanDeploymentUrls = new HashMap<>(); + protected final Map beanDeploymentUrls = new HashMap<>(); /** * for having proper scan mode 'SCOPED' support we need to know which bean class diff --git a/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java b/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java index 9780a7820..975d54c75 100644 --- a/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java +++ b/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java @@ -34,9 +34,7 @@ import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Enumeration; +import java.util.*; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -217,4 +215,17 @@ public boolean accept(String name) return accepts; } } + + @Override + public Set getBeanXmls() + { + final Set result = new HashSet<>(super.getBeanXmls()); + final URL embeddedUrl = beanDeploymentUrls.get(CDISeBeanArchiveService.EMBEDDED_URL); + if (embeddedUrl != null) + { + result.add(embeddedUrl); + } + + return result; + } } From fec207ce85b72a914e812c6d02f25031e3a926a8 Mon Sep 17 00:00:00 2001 From: Jonathan Gallimore Date: Fri, 21 Feb 2025 18:09:12 +0000 Subject: [PATCH 4/4] OWB-1448 remove wildcard import --- .../org/apache/openwebbeans/se/CDISeScannerService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java b/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java index 975d54c75..84a95c439 100644 --- a/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java +++ b/webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java @@ -34,7 +34,11 @@ import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; import java.util.stream.Stream; import java.util.stream.StreamSupport;