Skip to content

Commit c250a0f

Browse files
committed
WICKET-7126 simplify CdiConfiguration + BeanManagerLookup
1 parent 78fad30 commit c250a0f

6 files changed

Lines changed: 46 additions & 123 deletions

File tree

wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/AlternativeCdiConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void testApplicationScope()
4444
@Test
4545
void testUsesCdiJUnitConfiguration()
4646
{
47-
configure(new CdiConfiguration().setBeanManager(beanManager));
47+
configure(new CdiConfiguration(beanManager));
4848
tester.startPage(TestPage.class);
4949
tester.assertLabel("appscope", "Alternative ok");
5050
}

wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import jakarta.enterprise.inject.spi.BeanManager;
2020
import jakarta.inject.Inject;
21-
import org.apache.wicket.Application;
2221
import org.apache.wicket.cdi.testapp.ModelWithInjectedDependency;
2322
import org.apache.wicket.cdi.testapp.TestConversationPage;
2423
import org.apache.wicket.cdi.testapp.TestPage;
@@ -46,7 +45,7 @@ void testApplicationScope()
4645
@Test
4746
void testUsesCdiJUnitConfiguration()
4847
{
49-
configure(new CdiConfiguration().setBeanManager(beanManager));
48+
configure(new CdiConfiguration(beanManager));
5049
tester.startPage(TestPage.class);
5150
tester.assertLabel("appscope", "Test ok");
5251
}
@@ -72,17 +71,6 @@ void testNotConfigured()
7271

7372
}
7473

75-
@Test
76-
void testAlreadyConfigured()
77-
{
78-
configure(new CdiConfiguration());
79-
80-
assertThrows(IllegalStateException.class, () -> {
81-
CdiConfiguration.get(Application.get()).setBeanManager(beanManager);
82-
});
83-
84-
}
85-
8674
@Test
8775
void testConfigureTwice()
8876
{

wicket-cdi-tests/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package org.apache.wicket.cdi;
1818

19+
import io.github.cdiunit.AdditionalClasses;
20+
import io.github.cdiunit.junit5.CdiJUnit5Extension;
21+
import jakarta.inject.Inject;
1922
import org.apache.wicket.Component;
2023
import org.apache.wicket.Page;
2124
import org.apache.wicket.ThreadContext;
@@ -30,10 +33,6 @@
3033
import org.junit.jupiter.api.BeforeEach;
3134
import org.junit.jupiter.api.extension.ExtendWith;
3235

33-
import io.github.cdiunit.AdditionalClasses;
34-
import io.github.cdiunit.junit5.CdiJUnit5Extension;
35-
import jakarta.inject.Inject;
36-
3736
/**
3837
* @author jsarman
3938
*/
@@ -70,9 +69,6 @@ public void end()
7069
contextManager.destroy();
7170
}
7271
tester.destroy();
73-
74-
// make sure no leaked BeanManager are present
75-
BeanManagerLookup.detach();
7672
}
7773

7874
@BeforeEach

wicket-cdi/src/main/java/org/apache/wicket/cdi/BeanManagerLookup.java

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,21 @@
1818

1919
import jakarta.enterprise.inject.spi.BeanManager;
2020
import jakarta.enterprise.inject.spi.CDI;
21-
import javax.naming.InitialContext;
22-
import javax.naming.NamingException;
23-
24-
import org.apache.wicket.Application;
2521
import org.slf4j.Logger;
2622
import org.slf4j.LoggerFactory;
2723

24+
import javax.naming.InitialContext;
25+
import javax.naming.NamingException;
26+
2827
/**
2928
* Defines several strategies for looking up a CDI BeanManager in a portable way. The following
3029
* strategies are tried (in order):
3130
* <ul>
3231
* <li>JNDI under java:comp/BeanManager (default location)</li>
3332
* <li>JNDI under java:comp/env/BeanManager (for servlet containers like Tomcat and Jetty)</li>
3433
* <li>CDI.current().getBeanManager() (portable lookup)</li>
35-
* <li>{@linkplain CdiConfiguration#getFallbackBeanManager() Fallback}</li>
3634
* </ul>
3735
*
38-
* The last successful lookup strategy is saved and tried first next time.
39-
*
4036
* @author papegaaij
4137
*/
4238
public final class BeanManagerLookup
@@ -45,19 +41,6 @@ public final class BeanManagerLookup
4541

4642
private enum BeanManagerLookupStrategy
4743
{
48-
CUSTOM {
49-
@Override
50-
public BeanManager lookup()
51-
{
52-
CdiConfiguration cdiConfiguration = CdiConfiguration.get(Application.get());
53-
54-
if (cdiConfiguration == null)
55-
throw new IllegalStateException(
56-
"NonContextual injection can only be used after a CdiConfiguration is set");
57-
58-
return cdiConfiguration.getBeanManager();
59-
}
60-
},
6144
JNDI {
6245
@Override
6346
public BeanManager lookup()
@@ -100,47 +83,22 @@ public BeanManager lookup()
10083
return null;
10184
}
10285
}
103-
},
104-
FALLBACK {
105-
@Override
106-
public BeanManager lookup()
107-
{
108-
return CdiConfiguration.get(Application.get()).getFallbackBeanManager();
109-
}
11086
};
11187

11288
public abstract BeanManager lookup();
11389
}
11490

115-
private static BeanManagerLookupStrategy lastSuccessful = BeanManagerLookupStrategy.CUSTOM;
116-
117-
private BeanManagerLookup()
118-
{
119-
}
120-
12191
public static BeanManager lookup()
12292
{
123-
BeanManager ret = lastSuccessful.lookup();
124-
if (ret != null)
125-
return ret;
126-
12793
for (BeanManagerLookupStrategy curStrategy : BeanManagerLookupStrategy.values())
12894
{
129-
ret = curStrategy.lookup();
95+
BeanManager ret = curStrategy.lookup();
13096
if (ret != null)
13197
{
132-
lastSuccessful = curStrategy;
13398
return ret;
13499
}
135100
}
136-
137-
throw new IllegalStateException(
138-
"No BeanManager found via the CDI provider and no fallback specified. Check your "
139-
+ "CDI setup or specify a fallback BeanManager in the CdiConfiguration.");
101+
return null;
140102
}
141103

142-
static void detach()
143-
{
144-
lastSuccessful = BeanManagerLookupStrategy.CUSTOM;
145-
}
146104
}

wicket-cdi/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ public class CdiConfiguration
3939

4040
private BeanManager beanManager;
4141

42-
private BeanManager fallbackBeanManager;
43-
4442
/**
4543
* Constructor
4644
*/
4745
public CdiConfiguration()
4846
{
4947
}
5048

49+
public CdiConfiguration(BeanManager beanManager)
50+
{
51+
this.beanManager = beanManager;
52+
}
53+
5154
public IConversationPropagation getPropagation()
5255
{
5356
return propagation;
@@ -61,46 +64,10 @@ public CdiConfiguration setPropagation(IConversationPropagation propagation)
6164

6265
public BeanManager getBeanManager()
6366
{
64-
return beanManager;
65-
}
66-
67-
/**
68-
* Sets a BeanManager that should be used at first.
69-
*
70-
* @param beanManager
71-
* @return this instance
72-
*/
73-
public CdiConfiguration setBeanManager(BeanManager beanManager)
74-
{
75-
76-
if (Application.exists() && CdiConfiguration.get(Application.get()) != null)
67+
if (beanManager == null)
7768
throw new IllegalStateException(
78-
"A CdiConfiguration is already set for the application.");
79-
80-
this.beanManager = beanManager;
81-
return this;
82-
}
83-
84-
public BeanManager getFallbackBeanManager()
85-
{
86-
return fallbackBeanManager;
87-
}
88-
89-
/**
90-
* Sets a BeanManager that should be used if all strategies to lookup a
91-
* BeanManager fail. This can be used in scenarios where you do not have
92-
* JNDI available and do not want to bootstrap the CDI provider. It should
93-
* be noted that the fallback BeanManager can only be used within the
94-
* context of a Wicket application (ie. Application.get() should return the
95-
* application that was configured with this CdiConfiguration).
96-
*
97-
* @param fallbackBeanManager
98-
* @return this instance
99-
*/
100-
public CdiConfiguration setFallbackBeanManager(BeanManager fallbackBeanManager)
101-
{
102-
this.fallbackBeanManager = fallbackBeanManager;
103-
return this;
69+
"app not configured or no BeanManager was resolved during the configuration");
70+
return beanManager;
10471
}
10572

10673
/**
@@ -110,6 +77,13 @@ public CdiConfiguration setFallbackBeanManager(BeanManager fallbackBeanManager)
11077
*/
11178
public void configure(Application application)
11279
{
80+
if(beanManager == null)
81+
beanManager = BeanManagerLookup.lookup();
82+
83+
if (beanManager == null)
84+
throw new IllegalStateException(
85+
"No BeanManager was set or found via the CDI provider. Check your CDI setup or specify a BeanManager in the CdiConfiguration.");
86+
11387
if (application.getMetaData(CDI_CONFIGURATION_KEY) != null)
11488
{
11589
throw new IllegalStateException("Cdi already configured for this application");
@@ -145,6 +119,9 @@ public void configure(Application application)
145119

146120
public static CdiConfiguration get(Application application)
147121
{
148-
return application.getMetaData(CDI_CONFIGURATION_KEY);
122+
CdiConfiguration configuration = application.getMetaData(CDI_CONFIGURATION_KEY);
123+
if (configuration == null)
124+
throw new IllegalStateException("No CdiConfiguration is set");
125+
return configuration;
149126
}
150127
}

wicket-cdi/src/main/java/org/apache/wicket/cdi/NonContextual.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
import jakarta.enterprise.inject.spi.BeanManager;
2828
import jakarta.enterprise.inject.spi.InjectionTarget;
2929

30+
import org.apache.wicket.Application;
3031
import org.apache.wicket.util.collections.ClassMetaCache;
3132

3233
/**
3334
* Manages lifecycle of non-contextual (non-CDI-managed) objects
34-
*
35+
*
3536
* @param <T>
3637
* @author igor
3738
*/
@@ -48,22 +49,23 @@ public class NonContextual<T>
4849
*/
4950
public static void undeploy()
5051
{
51-
if (cache.containsKey(BeanManagerLookup.lookup()))
52+
BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager();
53+
if (cache.containsKey(manager))
5254
{
5355
synchronized (lock)
5456
{
5557
// copy-on-write the cache
5658
Map<BeanManager, ClassMetaCache<NonContextual<?>>> newCache = new WeakHashMap<BeanManager, ClassMetaCache<NonContextual<?>>>(
5759
cache);
58-
newCache.remove(BeanManagerLookup.lookup());
60+
newCache.remove(manager);
5961
cache = Collections.unmodifiableMap(newCache);
6062
}
6163
}
6264
}
6365

6466
/**
6567
* Convenience factory method for an instance, see {@link #of(Class)}.
66-
*
68+
*
6769
* @param <T>
6870
* @param t
6971
* @return The NonContextual for the instance's class
@@ -76,7 +78,7 @@ public static <T> NonContextual<T> of(T t) {
7678

7779
/**
7880
* Factory method for creating non-contextual instances
79-
*
81+
*
8082
* @param <T>
8183
* @param clazz
8284
* @return The NonContextual for the given class
@@ -98,12 +100,12 @@ public static <T> NonContextual<T> of(Class<? extends T> clazz)
98100

99101
private static ClassMetaCache<NonContextual<?>> getCache()
100102
{
101-
ClassMetaCache<NonContextual<?>> meta = cache.get(BeanManagerLookup.lookup());
103+
BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager();
104+
ClassMetaCache<NonContextual<?>> meta = cache.get(manager);
102105
if (meta == null)
103106
{
104107
synchronized (lock)
105108
{
106-
BeanManager manager = BeanManagerLookup.lookup();
107109
meta = cache.get(manager);
108110
if (meta == null)
109111
{
@@ -123,39 +125,41 @@ private static ClassMetaCache<NonContextual<?>> getCache()
123125
@SuppressWarnings("unchecked")
124126
private NonContextual(Class<? extends T> clazz)
125127
{
126-
BeanManager manager = BeanManagerLookup.lookup();
128+
BeanManager manager = CdiConfiguration.get(Application.get()).getBeanManager();
127129
AnnotatedType<? extends T> type = manager.createAnnotatedType(clazz);
128130
this.it = (InjectionTarget<T>) manager.getInjectionTargetFactory(type)
129131
.createInjectionTarget(null);
130132
}
131133

132134
/**
133135
* Injects the instance and calls any {@link PostConstruct} methods
134-
*
136+
*
135137
* @param instance
136138
*/
137139
public void postConstruct(T instance)
138140
{
139-
CreationalContext<T> cc = BeanManagerLookup.lookup().createCreationalContext(null);
141+
CreationalContext<T> cc = CdiConfiguration.get(Application.get()).getBeanManager()
142+
.createCreationalContext(null);
140143
it.inject(instance, cc);
141144
it.postConstruct(instance);
142145
}
143146

144147
/**
145148
* Injects the instance
146-
*
149+
*
147150
* @param instance
148151
*/
149152
public void inject(T instance)
150153
{
151-
CreationalContext<T> cc = BeanManagerLookup.lookup().createCreationalContext(null);
154+
CreationalContext<T> cc = CdiConfiguration.get(Application.get()).getBeanManager()
155+
.createCreationalContext(null);
152156
it.inject(instance, cc);
153157
}
154158

155159
/**
156160
* Calls any {@link PreDestroy} methods and destroys any injected
157161
* dependencies that need to be destroyed.
158-
*
162+
*
159163
* @param instance
160164
*/
161165
public void preDestroy(T instance)

0 commit comments

Comments
 (0)