Skip to content

Commit 7554491

Browse files
Merge pull request #3 from mkouba/arc-ut-swap
Make BeanContainer.RequestAction more generic
2 parents 71eb854 + 6490f42 commit 7554491

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

extensions/arc/runtime/src/main/java/org/jboss/shamrock/arc/runtime/ArcDeploymentTemplate.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void run() {
4646
return container;
4747
}
4848

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

5252
@Override
@@ -64,18 +64,12 @@ public T get() {
6464
}
6565

6666
@Override
67-
public <T, U, R> R withinRequestContext(RequestAction<T, U, R> action, T t, U u) throws Exception {
68-
ManagedContext ctx = container.requestContext();
69-
ctx.activate();
70-
try {
71-
return action.run(t, u);
72-
} finally {
73-
ctx.terminate();
74-
}
67+
public ManagedContext requestContext() {
68+
return container.requestContext();
7569
}
7670
};
77-
for (BeanContainerListener i : beanConfigurators) {
78-
i.created(beanContainer);
71+
for (BeanContainerListener listener : listeners) {
72+
listener.created(beanContainer);
7973
}
8074
return beanContainer;
8175
}

extensions/arc/runtime/src/main/java/org/jboss/shamrock/arc/runtime/BeanContainer.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.lang.annotation.Annotation;
2020

21+
import org.jboss.protean.arc.ManagedContext;
22+
2123
public interface BeanContainer {
2224

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

2931
/**
30-
* Runs the given action within the scope of the CDI request context
31-
*
32-
* @param action The action to run
33-
* @param t The first context parameter, this is passed to the action
34-
* @param u The second context parameter, this is passed to the action
35-
* @return the value returned by the action
36-
* @throws Exception
32+
* <pre>
33+
* ManagedContext requestContext = beanContainer.requestContext();
34+
* if (requestContext.isActive()) {
35+
* // Perform action
36+
* } else {
37+
* try {
38+
* requestContext.activate();
39+
* // Perform action
40+
* } finally {
41+
* requestContext.terminate();
42+
* }
43+
* }
44+
* </pre>
45+
*
46+
* @return the context for {@link javax.enterprise.context.RequestScoped}
47+
* @throws IllegalStateException If the container is not running
3748
*/
38-
<T, U, R> R withinRequestContext(RequestAction<T, U, R> action, T t, U u) throws Exception;
49+
ManagedContext requestContext();
3950

4051
interface Factory<T> {
4152

4253
T get();
4354
}
4455

45-
interface RequestAction<T, U, R> {
46-
47-
R run(T t, U u) throws Exception;
48-
}
49-
5056
}

extensions/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.servlet.ServletContext;
3232
import javax.servlet.ServletException;
3333

34+
import org.jboss.protean.arc.ManagedContext;
3435
import org.jboss.shamrock.arc.runtime.BeanContainer;
3536
import org.jboss.shamrock.runtime.InjectionFactory;
3637
import org.jboss.shamrock.runtime.InjectionInstance;
@@ -299,16 +300,20 @@ public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servl
299300
deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() {
300301
@Override
301302
public <T, C> ThreadSetupHandler.Action<T, C> create(Action<T, C> action) {
302-
BeanContainer.RequestAction<HttpServerExchange, C, T> function = new BeanContainer.RequestAction<HttpServerExchange, C, T>() {
303-
@Override
304-
public T run(HttpServerExchange exchange, C c) throws Exception {
305-
return action.call(exchange, c);
306-
}
307-
};
308303
return new Action<T, C>() {
309304
@Override
310305
public T call(HttpServerExchange exchange, C context) throws Exception {
311-
return beanContainer.withinRequestContext(function, exchange, context);
306+
ManagedContext requestContext = beanContainer.requestContext();
307+
if (requestContext.isActive()) {
308+
return action.call(exchange, context);
309+
} else {
310+
try {
311+
requestContext.activate();
312+
return action.call(exchange, context);
313+
} finally {
314+
requestContext.terminate();
315+
}
316+
}
312317
}
313318
};
314319
}

independent-projects/arc/runtime/src/main/java/org/jboss/protean/arc/ArcContainerImpl.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ public ManagedContext requestContext() {
190190
@Override
191191
public Runnable withinRequest(Runnable action) {
192192
return () -> {
193-
requireRunning();
194193
ManagedContext requestContext = requestContext();
195194
if (requestContext.isActive()) {
196195
action.run();
@@ -208,7 +207,6 @@ public Runnable withinRequest(Runnable action) {
208207
@Override
209208
public <T> Supplier<T> withinRequest(Supplier<T> action) {
210209
return () -> {
211-
requireRunning();
212210
ManagedContext requestContext = requestContext();
213211
if (requestContext.isActive()) {
214212
return action.get();

0 commit comments

Comments
 (0)