diff --git a/pom.xml b/pom.xml index ae789da8599..662eb39af43 100644 --- a/pom.xml +++ b/pom.xml @@ -820,7 +820,7 @@ ${project.groupId} ${project.artifactId} - 10.0.0 + 11.0.0 jar 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 cd07f2abde4..2ad8e9bf806 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 @@ void testApplicationScope() @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 8d6716f73ea..3aad32974b0 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 @@ 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 @@ void testApplicationScope() @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 @@ void testNotConfigured() } - @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 fb7829efcc2..1578f43b6c4 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.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 void end() 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 78228ddc289..3444257bb29 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 @@ 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,11 @@ *
  • JNDI under java:comp/BeanManager (default location)
  • *
  • JNDI under java:comp/env/BeanManager (for servlet containers like Tomcat and Jetty)
  • *
  • CDI.current().getBeanManager() (portable lookup)
  • - *
  • {@linkplain CdiConfiguration#getFallbackBeanManager() Fallback}
  • * - * - * The last successful lookup strategy is saved and tried first next time. - * + * + * This is de default strategy used in {@link CdiConfiguration} to look for a BeanManger, unless + * one is defined in CdiConfiguration(BeanManager) + * * @author papegaaij */ public final class BeanManagerLookup @@ -45,19 +44,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 +86,27 @@ public BeanManager lookup() 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 692e83d015c..adf6976b567 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 @@ -17,10 +17,10 @@ package org.apache.wicket.cdi; import jakarta.enterprise.inject.spi.BeanManager; - import org.apache.wicket.Application; import org.apache.wicket.MetaDataKey; import org.apache.wicket.request.cycle.RequestCycleListenerCollection; +import org.apache.wicket.util.lang.Args; /** * Configures CDI integration @@ -39,8 +39,6 @@ public class CdiConfiguration private BeanManager beanManager; - private BeanManager fallbackBeanManager; - /** * Constructor */ @@ -48,6 +46,12 @@ public CdiConfiguration() { } + public CdiConfiguration(BeanManager beanManager) + { + Args.notNull(beanManager, "beanManager"); + this.beanManager = beanManager; + } + public IConversationPropagation getPropagation() { return propagation; @@ -61,46 +65,13 @@ public CdiConfiguration setPropagation(IConversationPropagation propagation) 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; + "No BeanManager was resolved during configuration. Be sure " + + "to specify a BeanManager in CdiConfiguration constructor or that one can be resolved by BeanManagerLookup, and that CdiConfiguration#configure is called."); + } + return beanManager; } /** @@ -110,6 +81,17 @@ public CdiConfiguration setFallbackBeanManager(BeanManager fallbackBeanManager) */ 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 +127,9 @@ public void configure(Application application) 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 0bb6f154cd7..4d368db0bd7 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.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 * @author igor */ @@ -48,14 +49,15 @@ public class NonContextual */ 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>> newCache = new WeakHashMap>>( cache); - newCache.remove(BeanManagerLookup.lookup()); + newCache.remove(manager); cache = Collections.unmodifiableMap(newCache); } } @@ -63,7 +65,7 @@ public static void undeploy() /** * Convenience factory method for an instance, see {@link #of(Class)}. - * + * * @param * @param t * @return The NonContextual for the instance's class @@ -76,7 +78,7 @@ public static NonContextual of(T t) { /** * Factory method for creating non-contextual instances - * + * * @param * @param clazz * @return The NonContextual for the given class @@ -98,12 +100,12 @@ public static NonContextual of(Class clazz) private static ClassMetaCache> getCache() { - ClassMetaCache> meta = cache.get(BeanManagerLookup.lookup()); + BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager(); + ClassMetaCache> meta = cache.get(manager); if (meta == null) { synchronized (lock) { - BeanManager manager = BeanManagerLookup.lookup(); meta = cache.get(manager); if (meta == null) { @@ -123,7 +125,7 @@ private static ClassMetaCache> getCache() @SuppressWarnings("unchecked") private NonContextual(Class clazz) { - BeanManager manager = BeanManagerLookup.lookup(); + BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager(); AnnotatedType type = manager.createAnnotatedType(clazz); this.it = (InjectionTarget) manager.getInjectionTargetFactory(type) .createInjectionTarget(null); @@ -131,31 +133,33 @@ private NonContextual(Class clazz) /** * Injects the instance and calls any {@link PostConstruct} methods - * + * * @param instance */ public void postConstruct(T instance) { - CreationalContext cc = BeanManagerLookup.lookup().createCreationalContext(null); + CreationalContext 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 cc = BeanManagerLookup.lookup().createCreationalContext(null); + CreationalContext 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)