Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -72,17 +71,6 @@ void testNotConfigured()

}

@Test
void testAlreadyConfigured()
{
configure(new CdiConfiguration());

assertThrows(IllegalStateException.class, () -> {
CdiConfiguration.get(Application.get()).setBeanManager(beanManager);
});

}

@Test
void testConfigureTwice()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -70,9 +69,6 @@ public void end()
contextManager.destroy();
}
tester.destroy();

// make sure no leaked BeanManager are present
BeanManagerLookup.detach();
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,21 @@

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.
*
* @author papegaaij
*/
public final class BeanManagerLookup
Expand All @@ -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()
Expand Down Expand Up @@ -100,47 +83,22 @@ 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ public class CdiConfiguration

private BeanManager beanManager;

private BeanManager fallbackBeanManager;

/**
* Constructor
*/
public CdiConfiguration()
{
}

public CdiConfiguration(BeanManager beanManager)
{
this.beanManager = beanManager;
}

public IConversationPropagation getPropagation()
{
return propagation;
Expand All @@ -61,46 +64,10 @@ 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;
"app not configured or no BeanManager was resolved during the configuration");
return beanManager;
}

/**
Expand All @@ -110,6 +77,13 @@ 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");
Expand Down Expand Up @@ -145,6 +119,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;
}
}
30 changes: 17 additions & 13 deletions wicket-cdi/src/main/java/org/apache/wicket/cdi/NonContextual.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T>
* @author igor
*/
Expand All @@ -48,22 +49,23 @@ 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);
}
}
}

/**
* Convenience factory method for an instance, see {@link #of(Class)}.
*
*
* @param <T>
* @param t
* @return The NonContextual for the instance's class
Expand All @@ -76,7 +78,7 @@ public static <T> NonContextual<T> of(T t) {

/**
* Factory method for creating non-contextual instances
*
*
* @param <T>
* @param clazz
* @return The NonContextual for the given class
Expand All @@ -98,12 +100,12 @@ public static <T> NonContextual<T> of(Class<? extends T> clazz)

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)
{
Expand All @@ -123,39 +125,41 @@ private static ClassMetaCache<NonContextual<?>> getCache()
@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);
}

/**
* 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)
Expand Down
Loading