From de6ce812ca5b01913944a3af65750b9b889c4fee Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Wed, 14 Nov 2018 14:47:00 +1100 Subject: [PATCH] Change to new API --- .../jaxrs/JaxrsProviderBuildItem.java | 20 +++ .../jaxrs/JaxrsScanningProcessor.java | 14 ++ opentracing/deployment/pom.xml | 4 + .../opentracing/OpentracingProcessor.java | 76 ++++++----- .../opentracing/OpentracingSetup.java | 12 -- ...rg.jboss.shamrock.deployment.ShamrockSetup | 1 - .../runtime/TracingDeploymentTemplate.java | 44 +------ pom.xml | 4 + .../shamrock/undertow/FilterBuildItem.java | 122 ++++++++++++++++++ .../shamrock/undertow/UndertowBuildStep.java | 43 +++--- .../runtime/UndertowDeploymentTemplate.java | 5 +- 11 files changed, 243 insertions(+), 102 deletions(-) create mode 100644 jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsProviderBuildItem.java delete mode 100644 opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingSetup.java delete mode 100644 opentracing/deployment/src/main/resources/META-INF/services/org.jboss.shamrock.deployment.ShamrockSetup create mode 100644 undertow/deployment/src/main/java/org/jboss/shamrock/undertow/FilterBuildItem.java diff --git a/jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsProviderBuildItem.java b/jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsProviderBuildItem.java new file mode 100644 index 0000000000000..54652f44c3089 --- /dev/null +++ b/jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsProviderBuildItem.java @@ -0,0 +1,20 @@ +package org.jboss.shamrock.jaxrs; + +import org.jboss.builder.item.MultiBuildItem; + +/** + * A build item that represents a JAX-RS provider class, these items will be merged + * into the 'resteasy.providers' context param. + */ +public final class JaxrsProviderBuildItem extends MultiBuildItem { + + private final String name; + + public JaxrsProviderBuildItem(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsScanningProcessor.java b/jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsScanningProcessor.java index b9af772372d7c..bb3eb4e303ac8 100755 --- a/jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsScanningProcessor.java +++ b/jaxrs/deployment/src/main/java/org/jboss/shamrock/jaxrs/JaxrsScanningProcessor.java @@ -33,6 +33,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; +import java.util.List; import javax.servlet.Servlet; import javax.ws.rs.container.DynamicFeature; @@ -99,6 +100,19 @@ public class JaxrsScanningProcessor { DotName.createSimple("javax.ws.rs.PUT"), }; + + @BuildStep + ServletContextParamBuildItem registerProviders(List providers) { + StringBuilder sb = new StringBuilder(); + for(int i = 0; i < providers.size(); ++i) { + if(i != 0) { + sb.append(","); + } + sb.append(providers.get(i).getName()); + } + return new ServletContextParamBuildItem("resteasy.providers", sb.toString()); + } + @BuildStep SubstrateConfigBuildItem config() { return SubstrateConfigBuildItem.builder() diff --git a/opentracing/deployment/pom.xml b/opentracing/deployment/pom.xml index 09a4b105fdb8e..225e533a53931 100644 --- a/opentracing/deployment/pom.xml +++ b/opentracing/deployment/pom.xml @@ -17,6 +17,10 @@ org.jboss.shamrock shamrock-core-deployment + + org.jboss.shamrock + shamrock-jaxrs-deployment + org.jboss.shamrock shamrock-undertow-deployment diff --git a/opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingProcessor.java b/opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingProcessor.java index 102da3371c329..418996f401499 100644 --- a/opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingProcessor.java +++ b/opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingProcessor.java @@ -1,45 +1,63 @@ package org.jboss.shamrock.opentracing; import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; import javax.enterprise.inject.spi.ObserverMethod; -import javax.inject.Inject; - -import io.opentracing.contrib.interceptors.OpenTracingInterceptor; -import org.eclipse.microprofile.opentracing.Traced; -import org.jboss.shamrock.deployment.ArchiveContext; -import org.jboss.shamrock.deployment.BeanDeployment; -import org.jboss.shamrock.deployment.ProcessorContext; -import org.jboss.shamrock.deployment.ResourceProcessor; -import org.jboss.shamrock.deployment.RuntimePriority; -import org.jboss.shamrock.deployment.codegen.BytecodeRecorder; +import javax.servlet.DispatcherType; + +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.deployment.builditem.AdditionalBeanBuildItem; +import org.jboss.shamrock.deployment.builditem.ReflectiveMethodBuildItem; +import org.jboss.shamrock.jaxrs.JaxrsProviderBuildItem; +import org.jboss.shamrock.opentracing.runtime.ShamrockTracingDynamicFeature; import org.jboss.shamrock.opentracing.runtime.TracerProducer; import org.jboss.shamrock.opentracing.runtime.TracingDeploymentTemplate; +import org.jboss.shamrock.undertow.DeploymentInfoBuildItem; +import org.jboss.shamrock.undertow.FilterBuildItem; +import org.jboss.shamrock.undertow.ServletContextParamBuildItem; -public class OpentracingProcessor implements ResourceProcessor { +import io.opentracing.contrib.interceptors.OpenTracingInterceptor; +import io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter; - @Inject - BeanDeployment beanDeployment; +public class OpentracingProcessor { - @Override - public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception { - System.err.println( "PROCESS OPENTRACING"); - this.beanDeployment.addAdditionalBean(OpenTracingInterceptor.class); - this.beanDeployment.addAdditionalBean(TracerProducer.class); - processorContext.addReflectiveClass(false, false, Traced.class.getName()); + @BuildStep + List registerBeans() { + return Arrays.asList(new AdditionalBeanBuildItem(OpenTracingInterceptor.class, TracerProducer.class)); + } + @BuildStep + ReflectiveMethodBuildItem registerMethod() throws Exception { Method isAsync = ObserverMethod.class.getMethod("isAsync"); - processorContext.addReflectiveMethod(isAsync); - - try (BytecodeRecorder recorder = processorContext.addStaticInitTask(RuntimePriority.JAXRS_DEPLOYMENT + 1)) { - TracingDeploymentTemplate tracing = recorder.getRecordingProxy(TracingDeploymentTemplate.class); - tracing.registerTracer(); - tracing.integrateJaxrs(null); - } + return new ReflectiveMethodBuildItem(isAsync); } - @Override - public int getPriority() { - return 0; + @BuildStep + @Record(ExecutionTime.STATIC_INIT) + void setupFilter(BuildProducer providers, + BuildProducer filterProducer, + TracingDeploymentTemplate tracing) { + + //TODO: this needs to be a BuildItem so that more than one can be registered + providers.produce(new JaxrsProviderBuildItem(ShamrockTracingDynamicFeature.class.getName())); + + FilterBuildItem filterInfo = new FilterBuildItem("tracingFilter", SpanFinishingFilter.class.getName()); + filterInfo.setAsyncSupported(true); + filterInfo.addFilterUrlMapping("*", DispatcherType.FORWARD); + filterInfo.addFilterUrlMapping("*", DispatcherType.INCLUDE); + filterInfo.addFilterUrlMapping("*", DispatcherType.REQUEST); + filterInfo.addFilterUrlMapping("*", DispatcherType.ASYNC); + filterInfo.addFilterUrlMapping("*", DispatcherType.ERROR); + filterProducer.produce(filterInfo); + + //note that we can't put this into its own method as we need this to be registered before Undertow is started + tracing.registerTracer(); + } + } diff --git a/opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingSetup.java b/opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingSetup.java deleted file mode 100644 index 896be4600941d..0000000000000 --- a/opentracing/deployment/src/main/java/org/jboss/shamrock/opentracing/OpentracingSetup.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.jboss.shamrock.opentracing; - -import org.jboss.shamrock.deployment.SetupContext; -import org.jboss.shamrock.deployment.ShamrockSetup; - -public class OpentracingSetup implements ShamrockSetup { - @Override - public void setup(SetupContext context) { - context.addResourceProcessor( new OpentracingProcessor() ); - - } -} diff --git a/opentracing/deployment/src/main/resources/META-INF/services/org.jboss.shamrock.deployment.ShamrockSetup b/opentracing/deployment/src/main/resources/META-INF/services/org.jboss.shamrock.deployment.ShamrockSetup deleted file mode 100644 index 25370b843c4a1..0000000000000 --- a/opentracing/deployment/src/main/resources/META-INF/services/org.jboss.shamrock.deployment.ShamrockSetup +++ /dev/null @@ -1 +0,0 @@ -org.jboss.shamrock.opentracing.OpentracingSetup \ No newline at end of file diff --git a/opentracing/runtime/src/main/java/org/jboss/shamrock/opentracing/runtime/TracingDeploymentTemplate.java b/opentracing/runtime/src/main/java/org/jboss/shamrock/opentracing/runtime/TracingDeploymentTemplate.java index d22db5bc20d4d..2f8a22e90917e 100644 --- a/opentracing/runtime/src/main/java/org/jboss/shamrock/opentracing/runtime/TracingDeploymentTemplate.java +++ b/opentracing/runtime/src/main/java/org/jboss/shamrock/opentracing/runtime/TracingDeploymentTemplate.java @@ -1,22 +1,13 @@ package org.jboss.shamrock.opentracing.runtime; -import java.util.EnumSet; +import org.jboss.shamrock.runtime.Template; -import javax.servlet.DispatcherType; -import javax.servlet.Filter; -import javax.servlet.FilterRegistration; - -import io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter; import io.opentracing.util.GlobalTracer; -import io.smallrye.opentracing.SmallRyeTracingDynamicFeature; -import io.undertow.servlet.api.DeploymentInfo; -import io.undertow.servlet.api.FilterInfo; -import io.undertow.servlet.api.InstanceHandle; -import org.jboss.shamrock.runtime.ContextObject; /** * Created by bob on 8/6/18. */ +@Template public class TracingDeploymentTemplate { public void registerTracer() { System.err.println("REGISTER TRACER"); @@ -24,36 +15,5 @@ public void registerTracer() { GlobalTracer.register(new ShamrockTracer()); } - public void integrateJaxrs(@ContextObject("deploymentInfo") DeploymentInfo info) { - System.err.println("adding integration " + info); - info.addInitParameter("resteasy.providers", ShamrockTracingDynamicFeature.class.getName()); - - FilterInfo filterInfo = new FilterInfo("tracingFilter", SpanFinishingFilter.class, () -> { - SpanFinishingFilter filter = new SpanFinishingFilter(GlobalTracer.get()); - return new InstanceHandle() { - @Override - public Filter getInstance() { - System.err.println("get instance of filter"); - return filter; - } - - @Override - public void release() { - System.err.println("release instance of filter"); - // no-op - } - }; - }); - filterInfo.setAsyncSupported(true); - info.addFilter(filterInfo); - EnumSet enums = EnumSet.allOf(DispatcherType.class); - info.addFilterUrlMapping("tracingFilter", "*", DispatcherType.FORWARD); - info.addFilterUrlMapping("tracingFilter", "*", DispatcherType.INCLUDE); - info.addFilterUrlMapping("tracingFilter", "*", DispatcherType.REQUEST); - info.addFilterUrlMapping("tracingFilter", "*", DispatcherType.ASYNC); - info.addFilterUrlMapping("tracingFilter", "*", DispatcherType.ERROR); - } - - private ShamrockTracer tracer; } diff --git a/pom.xml b/pom.xml index c178f7447bf69..fc181ca267dfe 100644 --- a/pom.xml +++ b/pom.xml @@ -608,6 +608,10 @@ org.jboss.logging jboss-logging + + javax + javaee-api + diff --git a/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/FilterBuildItem.java b/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/FilterBuildItem.java new file mode 100644 index 0000000000000..ddf8dea2e676d --- /dev/null +++ b/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/FilterBuildItem.java @@ -0,0 +1,122 @@ +package org.jboss.shamrock.undertow; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.DispatcherType; + +import org.jboss.builder.item.MultiBuildItem; + +public final class FilterBuildItem extends MultiBuildItem { + + private final String name; + private final String filterClass; + private int loadOnStartup; + private boolean asyncSupported; + private final List mappings = new ArrayList<>(); + + public FilterBuildItem(String name, String filterClass) { + this.name = name; + this.filterClass = filterClass; + } + + public String getName() { + return name; + } + + public String getFilterClass() { + return filterClass; + } + + public List getMappings() { + return mappings; + } + + public int getLoadOnStartup() { + return loadOnStartup; + } + + public FilterBuildItem setLoadOnStartup(int loadOnStartup) { + this.loadOnStartup = loadOnStartup; + return this; + } + + public FilterBuildItem addMapping(FilterMappingInfo mappingPath) { + mappings.add(mappingPath); + return this; + } + + public boolean isAsyncSupported() { + return asyncSupported; + } + + public FilterBuildItem setAsyncSupported(boolean asyncSupported) { + this.asyncSupported = asyncSupported; + return this; + } + + public FilterBuildItem addFilterUrlMapping(final String mapping, DispatcherType dispatcher) { + mappings.add(new FilterMappingInfo(FilterMappingInfo.MappingType.URL, mapping, dispatcher)); + return this; + } + + public FilterBuildItem addFilterServletNameMapping(final String mapping, DispatcherType dispatcher) { + mappings.add(new FilterMappingInfo(FilterMappingInfo.MappingType.SERVLET, mapping, dispatcher)); + return this; + } + + public FilterBuildItem insertFilterUrlMapping(final int pos, final String mapping, DispatcherType dispatcher) { + mappings.add(pos, new FilterMappingInfo(FilterMappingInfo.MappingType.URL, mapping, dispatcher)); + return this; + } + + public FilterBuildItem insertFilterServletNameMapping(final int pos, final String filterName, final String mapping, DispatcherType dispatcher) { + mappings.add(pos, new FilterMappingInfo(FilterMappingInfo.MappingType.SERVLET, mapping, dispatcher)); + return this; + } + + + public static class FilterMappingInfo { + + private MappingType mappingType; + private String mapping; + private DispatcherType dispatcher; + + public FilterMappingInfo(final MappingType mappingType, final String mapping, final DispatcherType dispatcher) { + this.mappingType = mappingType; + this.mapping = mapping; + this.dispatcher = dispatcher; + } + + public void setMappingType(MappingType mappingType) { + this.mappingType = mappingType; + } + + public void setMapping(String mapping) { + this.mapping = mapping; + } + + public void setDispatcher(DispatcherType dispatcher) { + this.dispatcher = dispatcher; + } + + public MappingType getMappingType() { + return mappingType; + } + + public String getMapping() { + return mapping; + } + + public DispatcherType getDispatcher() { + return dispatcher; + } + + public enum MappingType { + URL, + SERVLET; + } + + } + +} diff --git a/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/UndertowBuildStep.java b/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/UndertowBuildStep.java index 41815f53dce9b..adf468175f4af 100644 --- a/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/UndertowBuildStep.java +++ b/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/UndertowBuildStep.java @@ -66,7 +66,6 @@ import org.jboss.metadata.web.spec.WebMetaData; 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.deployment.ApplicationArchive; import org.jboss.shamrock.deployment.builditem.ApplicationArchivesBuildItem; @@ -100,13 +99,6 @@ public class UndertowBuildStep { private static final DotName multipartConfig = DotName.createSimple(MultipartConfig.class.getName()); private static final DotName servletSecurity = DotName.createSimple(ServletSecurity.class.getName()); - - @Inject - BuildProducer reflectiveClasses; - - @Inject - ApplicationArchivesBuildItem applicationArchivesBuildItem; - @Inject CombinedIndexBuildItem combinedIndexBuildItem; @@ -116,7 +108,8 @@ public class UndertowBuildStep { @BuildStep @Record(STATIC_INIT) - public DeploymentInfoBuildItem createDeploymentInfo(UndertowDeploymentTemplate template) throws Exception { + public DeploymentInfoBuildItem createDeploymentInfo(UndertowDeploymentTemplate template, + ApplicationArchivesBuildItem applicationArchivesBuildItem) throws Exception { //we need to check for web resources in order to get welcome files to work //this kinda sucks Set knownFiles = new HashSet<>(); @@ -163,14 +156,14 @@ SubstrateConfigBuildItem config() { @Record(STATIC_INIT) @BuildStep public ServletHandlerBuildItem build(List servlets, + List filters, List contextParams, - ArchiveRootBuildItem root, - UndertowDeploymentTemplate template, BytecodeRecorder context, DeploymentInfoBuildItem deployment, BeanFactory beanFactory) throws Exception { + UndertowDeploymentTemplate template, BytecodeRecorder context, DeploymentInfoBuildItem deployment, + BeanFactory beanFactory, + BuildProducer reflectiveClasses) throws Exception { reflectiveClasses.produce(new ReflectiveClassBuildItem(false, false, DefaultServlet.class.getName(), "io.undertow.server.protocol.http.HttpRequestParser$$generated")); - handleResources(root); - final IndexView index = combinedIndexBuildItem.getIndex(); WebMetaData result = processAnnotations(index); @@ -226,11 +219,11 @@ public ServletHandlerBuildItem build(List servlets, for (FilterMappingMetaData mapping : result.getFilterMappings()) { for (String m : mapping.getUrlPatterns()) { if (mapping.getDispatchers() == null || mapping.getDispatchers().isEmpty()) { - template.addFilterMapping(deployment.getValue(), mapping.getFilterName(), m, REQUEST); + template.addFilterURLMapping(deployment.getValue(), mapping.getFilterName(), m, REQUEST); } else { for (DispatcherType dispatcher : mapping.getDispatchers()) { - template.addFilterMapping(deployment.getValue(), mapping.getFilterName(), m, javax.servlet.DispatcherType.valueOf(dispatcher.name())); + template.addFilterURLMapping(deployment.getValue(), mapping.getFilterName(), m, javax.servlet.DispatcherType.valueOf(dispatcher.name())); } } } @@ -257,15 +250,31 @@ public ServletHandlerBuildItem build(List servlets, template.addServletMapping(deployment.getValue(), servlet.getName(), m); } } + + for (FilterBuildItem filter : filters) { + String filterClass = filter.getFilterClass(); + InjectionInstance injection = (InjectionInstance) beanFactory.newInstanceFactory(filterClass); + InstanceFactory factory = template.createInstanceFactory(injection); + template.registerFilter(deployment.getValue(), filter.getName(), context.classProxy(filterClass), filter.isAsyncSupported(), factory); + for (FilterBuildItem.FilterMappingInfo m : filter.getMappings()) { + if(m.getMappingType() == FilterBuildItem.FilterMappingInfo.MappingType.URL) { + template.addFilterURLMapping(deployment.getValue(), filter.getName(), m.getMapping(), m.getDispatcher()); + } else { + template.addFilterServletNameMapping(deployment.getValue(), filter.getName(), m.getMapping(), m.getDispatcher()); + } + } + } for(ServletContextParamBuildItem i : contextParams) { template.addServletContextParameter(deployment.getValue(), i.getKey(), i.getValue()); } return new ServletHandlerBuildItem(template.bootServletContainer(deployment.getValue())); - } - private void handleResources(ArchiveRootBuildItem root) throws IOException { + @BuildStep + void registerSubstrateResources(ArchiveRootBuildItem root, + BuildProducer resourceProducer, + ApplicationArchivesBuildItem applicationArchivesBuildItem) throws IOException { Path resources = applicationArchivesBuildItem.getRootArchive().getChildPath("META-INF/resources"); if (resources != null) { Files.walk(resources).forEach(new Consumer() { diff --git a/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java b/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java index d40a447fb4730..f541ea8d0772f 100644 --- a/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java +++ b/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java @@ -129,10 +129,13 @@ public void addFilterInitParam(AtomicReference info, String name, St info.get().addInitParam(name, value); } - public void addFilterMapping(RuntimeValue info, String name, String mapping, DispatcherType dispatcherType) throws Exception { + public void addFilterURLMapping(RuntimeValue info, String name, String mapping, DispatcherType dispatcherType) throws Exception { info.getValue().addFilterUrlMapping(name, mapping, dispatcherType); } + public void addFilterServletNameMapping(RuntimeValue info, String name, String mapping, DispatcherType dispatcherType) throws Exception { + info.getValue().addFilterServletNameMapping(name, mapping, dispatcherType); + } public void registerListener(RuntimeValue info, Class listenerClass, InstanceFactory factory) { info.getValue().addListener(new ListenerInfo((Class) listenerClass, factory));