Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Undertow dependency from ArC #529

Merged
merged 3 commits into from
Jan 18, 2019
Merged
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 @@ -22,13 +22,11 @@

/**
* The list of capabilities.
*
*/
public final class Capabilities extends SimpleBuildItem {

public static final String CDI_ARC = "org.jboss.shamrock.cdi";
public static final String TRANSACTIONS = "org.jboss.shamrock.transactions";
public static final String UNDERTOW = "org.jboss.shamrock.undertow";

private final Set<String> capabilities;

Expand All @@ -43,5 +41,5 @@ public Capabilities(Set<String> capabilities) {
public Set<String> getCapabilities() {
return capabilities;
}

}
4 changes: 0 additions & 4 deletions extensions/arc/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
<groupId>org.jboss.protean.arc</groupId>
<artifactId>arc-processor</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-undertow-deployment</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.shamrock</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.jboss.shamrock.deployment.builditem.substrate.ReflectiveClassBuildItem;
import org.jboss.shamrock.deployment.builditem.substrate.ReflectiveFieldBuildItem;
import org.jboss.shamrock.deployment.builditem.substrate.ReflectiveMethodBuildItem;
import org.jboss.shamrock.undertow.ServletExtensionBuildItem;

public class ArcAnnotationProcessor {

Expand Down Expand Up @@ -104,7 +103,7 @@ public class ArcAnnotationProcessor {
@BuildStep(providesCapabilities = Capabilities.CDI_ARC, applicationArchiveMarkers = { "META-INF/beans.xml",
"META-INF/services/javax.enterprise.inject.spi.Extension" })
@Record(STATIC_INIT)
public BeanContainerBuildItem build(ArcDeploymentTemplate arcTemplate, BuildProducer<ServletExtensionBuildItem> extensions,
public BeanContainerBuildItem build(ArcDeploymentTemplate arcTemplate,
BuildProducer<InjectionProviderBuildItem> injectionProvider, List<BeanContainerListenerBuildItem> beanContainerListenerBuildItems,
ApplicationArchivesBuildItem applicationArchivesBuildItem, List<GeneratedBeanBuildItem> generatedBeans,
List<AnnotationsTransformerBuildItem> annotationTransformers, ShutdownContextBuildItem shutdown, BuildProducer<FeatureBuildItem> feature)
Expand Down Expand Up @@ -196,7 +195,6 @@ public void writeResource(Resource resource) throws IOException {
BeanContainer bc = arcTemplate.initBeanContainer(container,
beanContainerListenerBuildItems.stream().map(BeanContainerListenerBuildItem::getBeanContainerListener).collect(Collectors.toList()));
injectionProvider.produce(new InjectionProviderBuildItem(arcTemplate.setupInjection(container)));
extensions.produce(new ServletExtensionBuildItem(arcTemplate.setupRequestScope(container)));

return new BeanContainerBuildItem(bc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@
import org.jboss.shamrock.annotations.BuildProducer;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.deployment.Capabilities;
import org.jboss.shamrock.undertow.UndertowBuildStep;

public class BeanDefiningAnnotationsBuildStep {

@BuildStep
void beanDefiningAnnotations(Capabilities capabilities, BuildProducer<BeanDefiningAnnotationBuildItem> annotations) {
if (capabilities.isCapabilityPresent(Capabilities.UNDERTOW)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capabilities.UNDERTOW is not used anywhere else and could be removed.

// Arc integration depends on undertow so we want to avoid cyclic dependencies
annotations.produce(new BeanDefiningAnnotationBuildItem(UndertowBuildStep.WEB_FILTER));
annotations.produce(new BeanDefiningAnnotationBuildItem(UndertowBuildStep.WEB_SERVLET));
annotations.produce(new BeanDefiningAnnotationBuildItem(UndertowBuildStep.WEB_LISTENER));
}

// TODO: we should only add this when running the tests
annotations.produce(new BeanDefiningAnnotationBuildItem(DotName.createSimple("org.junit.runner.RunWith")));
}
Expand Down
4 changes: 0 additions & 4 deletions extensions/arc/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-runtime</artifactId>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.util.List;
import java.util.function.Supplier;

import javax.servlet.ServletContext;

import org.jboss.protean.arc.Arc;
import org.jboss.protean.arc.ArcContainer;
import org.jboss.protean.arc.InstanceHandle;
Expand All @@ -31,11 +29,6 @@
import org.jboss.shamrock.runtime.ShutdownContext;
import org.jboss.shamrock.runtime.Template;

import io.undertow.server.HttpServerExchange;
import io.undertow.servlet.ServletExtension;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.ThreadSetupHandler;

/**
* @author Martin Kouba
*/
Expand All @@ -53,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 @@ -69,36 +62,16 @@ public T get() {
}
};
}
};
for(BeanContainerListener i : beanConfigurators) {
i.created(beanContainer);
}
return beanContainer;
}

public ServletExtension setupRequestScope(ArcContainer arcContainer) {
return new ServletExtension() {
@Override
public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
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 {
ManagedContext requestContext = arcContainer.requestContext();
requestContext.activate();
try {
return action.call(exchange, context);
} finally {
requestContext.terminate();
}
}
};
}
});
public ManagedContext requestContext() {
return container.requestContext();
}
};
for (BeanContainerListener listener : listeners) {
listener.created(beanContainer);
}
return beanContainer;
}

public InjectionFactory setupInjection(ArcContainer container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,35 @@

import java.lang.annotation.Annotation;

import org.jboss.protean.arc.ManagedContext;

public interface BeanContainer {

default <T> T instance(Class<T> type, Annotation... qualifiers) {
return instanceFactory(type, qualifiers).get();
}

<T> Factory<T> instanceFactory(Class<T> type, Annotation... qualifiers);

/**
* <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
*/
ManagedContext requestContext();

interface Factory<T> {

Expand Down
4 changes: 4 additions & 0 deletions extensions/undertow/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-undertow-runtime</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.shamrock.undertow;

import org.jboss.shamrock.annotations.BuildProducer;
import org.jboss.shamrock.annotations.BuildStep;
import org.jboss.shamrock.annotations.ExecutionTime;
import org.jboss.shamrock.annotations.Record;
import org.jboss.shamrock.arc.deployment.BeanContainerBuildItem;
import org.jboss.shamrock.arc.deployment.BeanDefiningAnnotationBuildItem;
import org.jboss.shamrock.undertow.runtime.UndertowDeploymentTemplate;

public class UndertowArcIntegrationBuildStep {

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
ServletExtensionBuildItem integrateRequestContext(BeanContainerBuildItem beanContainerBuildItem, UndertowDeploymentTemplate template) {
return new ServletExtensionBuildItem(template.setupRequestScope(beanContainerBuildItem.getValue()));
}

@BuildStep
void beanDefiningAnnotations(BuildProducer<BeanDefiningAnnotationBuildItem> annotations) {
annotations.produce(new BeanDefiningAnnotationBuildItem(UndertowBuildStep.WEB_FILTER));
annotations.produce(new BeanDefiningAnnotationBuildItem(UndertowBuildStep.WEB_SERVLET));
annotations.produce(new BeanDefiningAnnotationBuildItem(UndertowBuildStep.WEB_LISTENER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ SubstrateConfigBuildItem config() {

.build();
}

@BuildStep
HotDeploymentConfigFileBuildItem configFile() {
return new HotDeploymentConfigFileBuildItem(WEB_XML);
}

@Record(STATIC_INIT)
@BuildStep(providesCapabilities = Capabilities.UNDERTOW)
@BuildStep
public ServletDeploymentBuildItem build(ApplicationArchivesBuildItem applicationArchivesBuildItem,
List<ServletBuildItem> servlets,
List<FilterBuildItem> filters,
Expand Down
4 changes: 4 additions & 0 deletions extensions/undertow/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<name>Shamrock - Undertow - Runtime</name>

<dependencies>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-arc-runtime</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.substratevm</groupId>
<artifactId>svm</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;
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;
import org.jboss.shamrock.runtime.RuntimeValue;
Expand Down Expand Up @@ -59,6 +62,7 @@
import io.undertow.servlet.api.ServletContainer;
import io.undertow.servlet.api.ServletInfo;
import io.undertow.servlet.api.ServletSecurityInfo;
import io.undertow.servlet.api.ThreadSetupHandler;
import io.undertow.servlet.handlers.DefaultServlet;
import io.undertow.servlet.handlers.ServletPathMatches;

Expand Down Expand Up @@ -289,6 +293,35 @@ public void addServletExtension(RuntimeValue<DeploymentInfo> deployment, Servlet
deployment.getValue().addServletExtension(extension);
}

public ServletExtension setupRequestScope(BeanContainer beanContainer) {
return new ServletExtension() {
@Override
public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() {
@Override
public <T, C> ThreadSetupHandler.Action<T, C> create(Action<T, C> action) {
return new Action<T, C>() {
@Override
public T call(HttpServerExchange exchange, C context) throws Exception {
ManagedContext requestContext = beanContainer.requestContext();
if (requestContext.isActive()) {
return action.call(exchange, context);
} else {
try {
requestContext.activate();
return action.call(exchange, context);
} finally {
requestContext.terminate();
}
}
}
};
}
});
}
};
}

/**
* we can't have SecureRandom in the native image heap, so we need to lazy init
*/
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