-
Notifications
You must be signed in to change notification settings - Fork 398
WICKET-7126 simplify CdiConfiguration + BeanManagerLookup #1369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,25 +18,24 @@ | |
|
|
||
| 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): | ||
| * <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>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. | ||
| * | ||
| * | ||
| * 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the benefit here of returning
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To maintain the fallbackBeanManager logic without a exception try/catch hassle. Right now the user can: provide their own fallback option if the lookup fail. Conceptually, the new BeanManagerLookup is just a lookup and not the class encapsulating the entire BeanManager resolution code as previously done. Being so I found it ok to return null. I can change the javadoc to better explain the new type role. |
||
| } | ||
|
|
||
| static void detach() | ||
| { | ||
| lastSuccessful = BeanManagerLookupStrategy.CUSTOM; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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,15 +39,19 @@ public class CdiConfiguration | |||||||||||||||||
|
|
||||||||||||||||||
| private BeanManager beanManager; | ||||||||||||||||||
|
|
||||||||||||||||||
| private BeanManager fallbackBeanManager; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Constructor | ||||||||||||||||||
| */ | ||||||||||||||||||
| public CdiConfiguration() | ||||||||||||||||||
| { | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public CdiConfiguration(BeanManager beanManager) | ||||||||||||||||||
| { | ||||||||||||||||||
| Args.notNull(beanManager, "beanManager"); | ||||||||||||||||||
| this.beanManager = beanManager; | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a non-null check ?!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad. Indeed it would: new CdiConfiguration(null) could also end up being a object with a not null beanManager. I meant to add the non-null check but forgot. |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| 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."); | ||||||||||||||||||
|
Comment on lines
+89
to
+92
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There are no code inside the module calling BeanManagerLookup lookup more than once (CdiConfiguration#configure is meant to be called once), contrary to the previous implementation. NonContextual called BeanManagerLookup during the request response, so the number of JNDI lookups was tied to the number of requests. |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| 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; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this constructor removed ?
This is a utility class with a single static method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. Didn't noticed the constructor was private and would rather remove those three lines in benefit of static#lookup going up some lines to get a better highlight.