Skip to content

Commit

Permalink
Merge pull request #3 from mkouba/arc-ut-swap
Browse files Browse the repository at this point in the history
Make BeanContainer.RequestAction more generic
  • Loading branch information
stuartwdouglas authored Jan 17, 2019
2 parents 71eb854 + 6490f42 commit 7554491
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void run() {
return container;
}

public BeanContainer initBeanContainer(ArcContainer container, List<BeanContainerListener> beanConfigurators) throws Exception {
public BeanContainer initBeanContainer(ArcContainer container, List<BeanContainerListener> listeners) throws Exception {
BeanContainer beanContainer = new BeanContainer() {

@Override
Expand All @@ -64,18 +64,12 @@ public T get() {
}

@Override
public <T, U, R> R withinRequestContext(RequestAction<T, U, R> action, T t, U u) throws Exception {
ManagedContext ctx = container.requestContext();
ctx.activate();
try {
return action.run(t, u);
} finally {
ctx.terminate();
}
public ManagedContext requestContext() {
return container.requestContext();
}
};
for (BeanContainerListener i : beanConfigurators) {
i.created(beanContainer);
for (BeanContainerListener listener : listeners) {
listener.created(beanContainer);
}
return beanContainer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.lang.annotation.Annotation;

import org.jboss.protean.arc.ManagedContext;

public interface BeanContainer {

default <T> T instance(Class<T> type, Annotation... qualifiers) {
Expand All @@ -27,24 +29,28 @@ default <T> T instance(Class<T> type, Annotation... qualifiers) {
<T> Factory<T> instanceFactory(Class<T> type, Annotation... qualifiers);

/**
* Runs the given action within the scope of the CDI request context
*
* @param action The action to run
* @param t The first context parameter, this is passed to the action
* @param u The second context parameter, this is passed to the action
* @return the value returned by the action
* @throws Exception
* <pre>
* ManagedContext requestContext = beanContainer.requestContext();
* if (requestContext.isActive()) {
* // Perform action
* } else {
* try {
* requestContext.activate();
* // Perform action
* } finally {
* requestContext.terminate();
* }
* }
* </pre>
*
* @return the context for {@link javax.enterprise.context.RequestScoped}
* @throws IllegalStateException If the container is not running
*/
<T, U, R> R withinRequestContext(RequestAction<T, U, R> action, T t, U u) throws Exception;
ManagedContext requestContext();

interface Factory<T> {

T get();
}

interface RequestAction<T, U, R> {

R run(T t, U u) throws Exception;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.jboss.protean.arc.ManagedContext;
import org.jboss.shamrock.arc.runtime.BeanContainer;
import org.jboss.shamrock.runtime.InjectionFactory;
import org.jboss.shamrock.runtime.InjectionInstance;
Expand Down Expand Up @@ -299,16 +300,20 @@ public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servl
deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() {
@Override
public <T, C> ThreadSetupHandler.Action<T, C> create(Action<T, C> action) {
BeanContainer.RequestAction<HttpServerExchange, C, T> function = new BeanContainer.RequestAction<HttpServerExchange, C, T>() {
@Override
public T run(HttpServerExchange exchange, C c) throws Exception {
return action.call(exchange, c);
}
};
return new Action<T, C>() {
@Override
public T call(HttpServerExchange exchange, C context) throws Exception {
return beanContainer.withinRequestContext(function, exchange, context);
ManagedContext requestContext = beanContainer.requestContext();
if (requestContext.isActive()) {
return action.call(exchange, context);
} else {
try {
requestContext.activate();
return action.call(exchange, context);
} finally {
requestContext.terminate();
}
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ public ManagedContext requestContext() {
@Override
public Runnable withinRequest(Runnable action) {
return () -> {
requireRunning();
ManagedContext requestContext = requestContext();
if (requestContext.isActive()) {
action.run();
Expand All @@ -208,7 +207,6 @@ public Runnable withinRequest(Runnable action) {
@Override
public <T> Supplier<T> withinRequest(Supplier<T> action) {
return () -> {
requireRunning();
ManagedContext requestContext = requestContext();
if (requestContext.isActive()) {
return action.get();
Expand Down

0 comments on commit 7554491

Please sign in to comment.