This is an automated email from the ASF dual-hosted git repository. pedro pushed a commit to branch pedro/cdi-improvement in repository https://gitbox.apache.org/repos/asf/wicket.git
commit c250a0f8e09cffc016b2f1251dd0feb98706b155 Author: Pedro Santos <[email protected]> AuthorDate: Sat Feb 14 21:05:11 2026 -0300 WICKET-7126 simplify CdiConfiguration + BeanManagerLookup --- .../cdi/AlternativeCdiConfigurationTest.java | 2 +- .../apache/wicket/cdi/CdiConfigurationTest.java | 14 +---- .../org/apache/wicket/cdi/WicketCdiTestCase.java | 10 ++-- .../org/apache/wicket/cdi/BeanManagerLookup.java | 52 ++---------------- .../org/apache/wicket/cdi/CdiConfiguration.java | 61 +++++++--------------- .../java/org/apache/wicket/cdi/NonContextual.java | 30 ++++++----- 6 files changed, 46 insertions(+), 123 deletions(-) diff --git a/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/AlternativeCdiConfigurationTest.java b/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/AlternativeCdiConfigurationTest.java index cd07f2abde..2ad8e9bf80 100644 --- a/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/AlternativeCdiConfigurationTest.java +++ b/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/AlternativeCdiConfigurationTest.java @@ -44,7 +44,7 @@ class AlternativeCdiConfigurationTest extends WicketCdiTestCase @Test void testUsesCdiJUnitConfiguration() { - configure(new CdiConfiguration().setBeanManager(beanManager)); + configure(new CdiConfiguration(beanManager)); tester.startPage(TestPage.class); tester.assertLabel("appscope", "Alternative ok"); } diff --git a/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java b/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java index 8d6716f73e..3aad32974b 100644 --- a/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java +++ b/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java @@ -18,7 +18,6 @@ package org.apache.wicket.cdi; import jakarta.enterprise.inject.spi.BeanManager; import jakarta.inject.Inject; -import org.apache.wicket.Application; import org.apache.wicket.cdi.testapp.ModelWithInjectedDependency; import org.apache.wicket.cdi.testapp.TestConversationPage; import org.apache.wicket.cdi.testapp.TestPage; @@ -46,7 +45,7 @@ class CdiConfigurationTest extends WicketCdiTestCase @Test void testUsesCdiJUnitConfiguration() { - configure(new CdiConfiguration().setBeanManager(beanManager)); + configure(new CdiConfiguration(beanManager)); tester.startPage(TestPage.class); tester.assertLabel("appscope", "Test ok"); } @@ -72,17 +71,6 @@ class CdiConfigurationTest extends WicketCdiTestCase } - @Test - void testAlreadyConfigured() - { - configure(new CdiConfiguration()); - - assertThrows(IllegalStateException.class, () -> { - CdiConfiguration.get(Application.get()).setBeanManager(beanManager); - }); - - } - @Test void testConfigureTwice() { diff --git a/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java b/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java index fb7829efcc..1578f43b6c 100644 --- a/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java +++ b/wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java @@ -16,6 +16,9 @@ */ package org.apache.wicket.cdi; +import io.github.cdiunit.AdditionalClasses; +import io.github.cdiunit.junit5.CdiJUnit5Extension; +import jakarta.inject.Inject; import org.apache.wicket.Component; import org.apache.wicket.Page; import org.apache.wicket.ThreadContext; @@ -30,10 +33,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; -import io.github.cdiunit.AdditionalClasses; -import io.github.cdiunit.junit5.CdiJUnit5Extension; -import jakarta.inject.Inject; - /** * @author jsarman */ @@ -70,9 +69,6 @@ public abstract class WicketCdiTestCase contextManager.destroy(); } tester.destroy(); - - // make sure no leaked BeanManager are present - BeanManagerLookup.detach(); } @BeforeEach diff --git a/wicket-cdi/src/main/java/org/apache/wicket/cdi/BeanManagerLookup.java b/wicket-cdi/src/main/java/org/apache/wicket/cdi/BeanManagerLookup.java index 78228ddc28..9ee2473ac1 100644 --- a/wicket-cdi/src/main/java/org/apache/wicket/cdi/BeanManagerLookup.java +++ b/wicket-cdi/src/main/java/org/apache/wicket/cdi/BeanManagerLookup.java @@ -18,13 +18,12 @@ package org.apache.wicket.cdi; import jakarta.enterprise.inject.spi.BeanManager; import jakarta.enterprise.inject.spi.CDI; -import javax.naming.InitialContext; -import javax.naming.NamingException; - -import org.apache.wicket.Application; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.naming.InitialContext; +import javax.naming.NamingException; + /** * Defines several strategies for looking up a CDI BeanManager in a portable way. The following * strategies are tried (in order): @@ -32,11 +31,8 @@ import org.slf4j.LoggerFactory; * <li>JNDI under java:comp/BeanManager (default location)</li> * <li>JNDI under java:comp/env/BeanManager (for servlet containers like Tomcat and Jetty)</li> * <li>CDI.current().getBeanManager() (portable lookup)</li> - * <li>{@linkplain CdiConfiguration#getFallbackBeanManager() Fallback}</li> * </ul> * - * The last successful lookup strategy is saved and tried first next time. - * * @author papegaaij */ public final class BeanManagerLookup @@ -45,19 +41,6 @@ public final class BeanManagerLookup private enum BeanManagerLookupStrategy { - CUSTOM { - @Override - public BeanManager lookup() - { - CdiConfiguration cdiConfiguration = CdiConfiguration.get(Application.get()); - - if (cdiConfiguration == null) - throw new IllegalStateException( - "NonContextual injection can only be used after a CdiConfiguration is set"); - - return cdiConfiguration.getBeanManager(); - } - }, JNDI { @Override public BeanManager lookup() @@ -100,47 +83,22 @@ public final class BeanManagerLookup return null; } } - }, - FALLBACK { - @Override - public BeanManager lookup() - { - return CdiConfiguration.get(Application.get()).getFallbackBeanManager(); - } }; public abstract BeanManager lookup(); } - private static BeanManagerLookupStrategy lastSuccessful = BeanManagerLookupStrategy.CUSTOM; - - private BeanManagerLookup() - { - } - public static BeanManager lookup() { - BeanManager ret = lastSuccessful.lookup(); - if (ret != null) - return ret; - for (BeanManagerLookupStrategy curStrategy : BeanManagerLookupStrategy.values()) { - ret = curStrategy.lookup(); + BeanManager ret = curStrategy.lookup(); if (ret != null) { - lastSuccessful = curStrategy; return ret; } } - - throw new IllegalStateException( - "No BeanManager found via the CDI provider and no fallback specified. Check your " - + "CDI setup or specify a fallback BeanManager in the CdiConfiguration."); + return null; } - static void detach() - { - lastSuccessful = BeanManagerLookupStrategy.CUSTOM; - } } diff --git a/wicket-cdi/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java b/wicket-cdi/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java index 692e83d015..ad6fb03209 100644 --- a/wicket-cdi/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java +++ b/wicket-cdi/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java @@ -39,8 +39,6 @@ public class CdiConfiguration private BeanManager beanManager; - private BeanManager fallbackBeanManager; - /** * Constructor */ @@ -48,6 +46,11 @@ public class CdiConfiguration { } + public CdiConfiguration(BeanManager beanManager) + { + this.beanManager = beanManager; + } + public IConversationPropagation getPropagation() { return propagation; @@ -61,46 +64,10 @@ public class CdiConfiguration public BeanManager getBeanManager() { - return beanManager; - } - - /** - * Sets a BeanManager that should be used at first. - * - * @param beanManager - * @return this instance - */ - public CdiConfiguration setBeanManager(BeanManager beanManager) - { - - if (Application.exists() && CdiConfiguration.get(Application.get()) != null) + if (beanManager == null) throw new IllegalStateException( - "A CdiConfiguration is already set for the application."); - - this.beanManager = beanManager; - return this; - } - - public BeanManager getFallbackBeanManager() - { - return fallbackBeanManager; - } - - /** - * Sets a BeanManager that should be used if all strategies to lookup a - * BeanManager fail. This can be used in scenarios where you do not have - * JNDI available and do not want to bootstrap the CDI provider. It should - * be noted that the fallback BeanManager can only be used within the - * context of a Wicket application (ie. Application.get() should return the - * application that was configured with this CdiConfiguration). - * - * @param fallbackBeanManager - * @return this instance - */ - public CdiConfiguration setFallbackBeanManager(BeanManager fallbackBeanManager) - { - this.fallbackBeanManager = fallbackBeanManager; - return this; + "app not configured or no BeanManager was resolved during the configuration"); + return beanManager; } /** @@ -110,6 +77,13 @@ public class CdiConfiguration */ public void configure(Application application) { + if(beanManager == null) + beanManager = BeanManagerLookup.lookup(); + + if (beanManager == null) + throw new IllegalStateException( + "No BeanManager was set or found via the CDI provider. Check your CDI setup or specify a BeanManager in the CdiConfiguration."); + if (application.getMetaData(CDI_CONFIGURATION_KEY) != null) { throw new IllegalStateException("Cdi already configured for this application"); @@ -145,6 +119,9 @@ public class CdiConfiguration public static CdiConfiguration get(Application application) { - return application.getMetaData(CDI_CONFIGURATION_KEY); + CdiConfiguration configuration = application.getMetaData(CDI_CONFIGURATION_KEY); + if (configuration == null) + throw new IllegalStateException("No CdiConfiguration is set"); + return configuration; } } diff --git a/wicket-cdi/src/main/java/org/apache/wicket/cdi/NonContextual.java b/wicket-cdi/src/main/java/org/apache/wicket/cdi/NonContextual.java index 0bb6f154cd..4d368db0bd 100644 --- a/wicket-cdi/src/main/java/org/apache/wicket/cdi/NonContextual.java +++ b/wicket-cdi/src/main/java/org/apache/wicket/cdi/NonContextual.java @@ -27,11 +27,12 @@ import jakarta.enterprise.inject.spi.AnnotatedType; import jakarta.enterprise.inject.spi.BeanManager; import jakarta.enterprise.inject.spi.InjectionTarget; +import org.apache.wicket.Application; import org.apache.wicket.util.collections.ClassMetaCache; /** * Manages lifecycle of non-contextual (non-CDI-managed) objects - * + * * @param <T> * @author igor */ @@ -48,14 +49,15 @@ public class NonContextual<T> */ public static void undeploy() { - if (cache.containsKey(BeanManagerLookup.lookup())) + BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager(); + if (cache.containsKey(manager)) { synchronized (lock) { // copy-on-write the cache Map<BeanManager, ClassMetaCache<NonContextual<?>>> newCache = new WeakHashMap<BeanManager, ClassMetaCache<NonContextual<?>>>( cache); - newCache.remove(BeanManagerLookup.lookup()); + newCache.remove(manager); cache = Collections.unmodifiableMap(newCache); } } @@ -63,7 +65,7 @@ public class NonContextual<T> /** * Convenience factory method for an instance, see {@link #of(Class)}. - * + * * @param <T> * @param t * @return The NonContextual for the instance's class @@ -76,7 +78,7 @@ public class NonContextual<T> /** * Factory method for creating non-contextual instances - * + * * @param <T> * @param clazz * @return The NonContextual for the given class @@ -98,12 +100,12 @@ public class NonContextual<T> private static ClassMetaCache<NonContextual<?>> getCache() { - ClassMetaCache<NonContextual<?>> meta = cache.get(BeanManagerLookup.lookup()); + BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager(); + ClassMetaCache<NonContextual<?>> meta = cache.get(manager); if (meta == null) { synchronized (lock) { - BeanManager manager = BeanManagerLookup.lookup(); meta = cache.get(manager); if (meta == null) { @@ -123,7 +125,7 @@ public class NonContextual<T> @SuppressWarnings("unchecked") private NonContextual(Class<? extends T> clazz) { - BeanManager manager = BeanManagerLookup.lookup(); + BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager(); AnnotatedType<? extends T> type = manager.createAnnotatedType(clazz); this.it = (InjectionTarget<T>) manager.getInjectionTargetFactory(type) .createInjectionTarget(null); @@ -131,31 +133,33 @@ public class NonContextual<T> /** * Injects the instance and calls any {@link PostConstruct} methods - * + * * @param instance */ public void postConstruct(T instance) { - CreationalContext<T> cc = BeanManagerLookup.lookup().createCreationalContext(null); + CreationalContext<T> cc = CdiConfiguration.get(Application.get()).getBeanManager() + .createCreationalContext(null); it.inject(instance, cc); it.postConstruct(instance); } /** * Injects the instance - * + * * @param instance */ public void inject(T instance) { - CreationalContext<T> cc = BeanManagerLookup.lookup().createCreationalContext(null); + CreationalContext<T> cc = CdiConfiguration.get(Application.get()).getBeanManager() + .createCreationalContext(null); it.inject(instance, cc); } /** * Calls any {@link PreDestroy} methods and destroys any injected * dependencies that need to be destroyed. - * + * * @param instance */ public void preDestroy(T instance)
