This is an automated email from the ASF dual-hosted git repository. pedro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/wicket.git
commit 4e9b4ba5d0656ce20c62f553c6f56c19b068716c Author: Pedro Santos <[email protected]> AuthorDate: Mon Nov 4 15:54:01 2024 -0300 WICKET-7125 Adds setBeanManager method to CdiConfiguration --- .../org/apache/wicket/cdi/BeanManagerLookup.java | 34 +++++++++++++++++----- .../org/apache/wicket/cdi/CdiConfiguration.java | 24 +++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) 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 34287fb781..78228ddc28 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 @@ -26,12 +26,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Defines several strategies for looking up a CDI BeanManager in a portable - * way. The following strategies are tried (in order): + * Defines several strategies for looking up a CDI BeanManager in a portable way. The following + * strategies are tried (in order): * <ul> * <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>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> @@ -44,7 +43,21 @@ public final class BeanManagerLookup { private static final Logger log = LoggerFactory.getLogger(BeanManagerLookup.class); - private enum BeanManagerLookupStrategy { + 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() @@ -99,7 +112,7 @@ public final class BeanManagerLookup public abstract BeanManager lookup(); } - private static BeanManagerLookupStrategy lastSuccessful = BeanManagerLookupStrategy.JNDI; + private static BeanManagerLookupStrategy lastSuccessful = BeanManagerLookupStrategy.CUSTOM; private BeanManagerLookup() { @@ -122,7 +135,12 @@ public final class BeanManagerLookup } 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."); + "No BeanManager found via the CDI provider and no fallback specified. Check your " + + "CDI setup or specify a fallback BeanManager in the CdiConfiguration."); + } + + 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 e3f9620464..692e83d015 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 @@ -37,6 +37,8 @@ public class CdiConfiguration private IConversationPropagation propagation = ConversationPropagation.NONBOOKMARKABLE; + private BeanManager beanManager; + private BeanManager fallbackBeanManager; /** @@ -57,6 +59,28 @@ public class CdiConfiguration return this; } + 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) + throw new IllegalStateException( + "A CdiConfiguration is already set for the application."); + + this.beanManager = beanManager; + return this; + } + public BeanManager getFallbackBeanManager() { return fallbackBeanManager;
