Wire up request scope

This commit is contained in:
Stuart Douglas
2018-09-19 17:31:44 +10:00
parent 7942c6beba
commit 1972569524
7 changed files with 96 additions and 1 deletions

View File

@@ -123,6 +123,7 @@ public class ArcAnnotationProcessor implements ResourceProcessor {
ArcContainer container = template.getContainer();
template.initBeanContainer(container);
template.setupInjection(container);
template.setupRequestScope(null, null);
}
}

View File

@@ -22,6 +22,10 @@
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-runtime</artifactId>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -11,8 +11,11 @@ import org.jboss.shamrock.runtime.InjectionFactory;
import org.jboss.shamrock.runtime.InjectionInstance;
import org.jboss.shamrock.runtime.RuntimeInjector;
import io.undertow.server.HttpServerExchange;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.ThreadSetupHandler;
/**
*
* @author Martin Kouba
*/
public class ArcDeploymentTemplate {
@@ -43,6 +46,28 @@ public class ArcDeploymentTemplate {
};
}
public void setupRequestScope(@ContextObject("deploymentInfo") DeploymentInfo deploymentInfo, @ContextObject("arc.container") ArcContainer arcContainer) {
if(deploymentInfo == null) {
return;
}
deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() {
@Override
public <T, C> Action<T, C> create(Action<T, C> action) {
return new Action<T, C>() {
@Override
public T call(HttpServerExchange exchange, C context) throws Exception {
arcContainer.requestContext().activate();
try {
return action.call(exchange, context);
} finally {
arcContainer.requestContext().deactivate();
}
}
};
}
});
}
public void setupInjection(ArcContainer container) {
RuntimeInjector.setFactory(new InjectionFactory() {
@Override

View File

@@ -0,0 +1,14 @@
package org.jboss.shamrock.example.arc;
import javax.enterprise.context.RequestScoped;
@RequestScoped
public class RequestScopedBean {
int count;
public int incrementAndGet() {
return ++count;
}
}

View File

@@ -0,0 +1,23 @@
package org.jboss.shamrock.example.arc;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/request-scoped")
public class TestRequestScopeEndpoint {
@Inject
private RequestScopedBean requestScopedBean;
@GET
public String manualValidation() {
requestScopedBean.incrementAndGet();
requestScopedBean.incrementAndGet();
return "" + requestScopedBean.incrementAndGet();
}
}

View File

@@ -0,0 +1,9 @@
package org.jboss.shamrock.example.test;
import org.jboss.shamrock.junit.GraalTest;
import org.junit.runner.RunWith;
@RunWith(GraalTest.class)
public class RequestScopeITCase extends RequestScopeTestCase {
}

View File

@@ -0,0 +1,19 @@
package org.jboss.shamrock.example.test;
import org.jboss.shamrock.example.testutils.URLTester;
import org.jboss.shamrock.junit.ShamrockTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(ShamrockTest.class)
public class RequestScopeTestCase {
@Test
public void testRequestScope() {
Assert.assertEquals("3", URLTester.relative("rest/request-scoped").invokeURL().asString());
Assert.assertEquals("3", URLTester.relative("rest/request-scoped").invokeURL().asString());
}
}