Skip to content

Commit

Permalink
Improved CDI: Allowed access to SeContainer via ICdiControl and offer…
Browse files Browse the repository at this point in the history
…ed way to configure the SeContainer before booting
  • Loading branch information
SimonDan committed Feb 15, 2020
1 parent 78735e5 commit 2052a27
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
26 changes: 23 additions & 3 deletions ojcms-cdi/src/main/java/de/adito/ojcms/cdi/CdiContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import de.adito.ojcms.cdi.context.*;
import de.adito.picoservice.IPicoRegistry;

import javax.enterprise.context.*;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.se.*;
import javax.enterprise.inject.spi.*;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.function.Function;
import java.util.function.*;
import java.util.stream.Collectors;

import static java.util.function.Function.identity;
Expand All @@ -34,7 +34,7 @@ public final class CdiContainer

@Produces
@ApplicationScoped
private static ICdiControl CDI_CONTROL;
static ICdiControl CDI_CONTROL;

private CdiContainer()
{
Expand All @@ -48,11 +48,25 @@ private CdiContainer()
* @return an interface to control and create CDI elements
*/
public static ICdiControl boot()
{
return boot(config -> {
});
}

/**
* Boots the CDI container. After a successful startup process {@link ICdiControl} will be available via injection.
* To reboot the CDI container {@link ICdiControl#shutdown()} has to be called first. Otherwise a subsequent call
* will lead to a runtime exception.
*
* @return an interface to control and create CDI elements
*/
public static ICdiControl boot(Consumer<SeContainerInitializer> pConfig)
{
if (CDI_CONTROL != null)
throw new IllegalStateException("Cdi container already booted!");

final SeContainerInitializer initializer = SeContainerInitializer.newInstance();
pConfig.accept(initializer);
CDI_CONTROL = new _CdiControl(initializer.initialize());
return CDI_CONTROL;
}
Expand Down Expand Up @@ -138,6 +152,12 @@ public boolean isContextActive(Class<? extends Annotation> pScopeAnnotationType)
return _retrieveFromContext(pScopeAnnotationType, AbstractCustomContext::isActive);
}

@Override
public SeContainer getContainer()
{
return container;
}

@Override
public void shutdown()
{
Expand Down
26 changes: 24 additions & 2 deletions ojcms-cdi/src/main/java/de/adito/ojcms/cdi/ICdiControl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.adito.ojcms.cdi;

import de.adito.ojcms.cdi.context.*;
import org.jboss.weld.exceptions.IllegalStateException;

import javax.enterprise.context.*;
import javax.enterprise.context.NormalScope;
import javax.enterprise.inject.se.SeContainer;
import java.lang.annotation.Annotation;

/**
Expand All @@ -12,10 +14,23 @@
*/
public interface ICdiControl
{
/**
* Returns the current {@link ICdiControl}. Only available if the CDI container is booted.
*
* @return the cdi control element
*/
static ICdiControl current()
{
if (CdiContainer.CDI_CONTROL == null)
throw new IllegalStateException("Cdi container not booted yet!");

return CdiContainer.CDI_CONTROL;
}

/**
* Creates an instances of a specific type. The type must be managed by the CDI container.
*
* @param pType the type to create a CDI managed instance of
* @param pType the type to create a CDI managed instance of
* @param pQualifiers qualifier annotations for the instance to create
* @return the created instance
*/
Expand Down Expand Up @@ -45,6 +60,13 @@ public interface ICdiControl
*/
boolean isContextActive(Class<? extends Annotation> pScopeAnnotationType);

/**
* The underlying {@link SeContainer}.
*
* @return the CDI container
*/
SeContainer getContainer();

/**
* Terminates the CDI container.
*/
Expand Down

0 comments on commit 2052a27

Please sign in to comment.