Skip to content

Commit

Permalink
Change to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Nov 14, 2018
1 parent 925a311 commit de6ce81
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,6 +100,19 @@ public class JaxrsScanningProcessor {
DotName.createSimple("javax.ws.rs.PUT"),
};


@BuildStep
ServletContextParamBuildItem registerProviders(List<JaxrsProviderBuildItem> 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()
Expand Down
4 changes: 4 additions & 0 deletions opentracing/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-jaxrs-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-undertow-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AdditionalBeanBuildItem> 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<JaxrsProviderBuildItem> providers,
BuildProducer<FilterBuildItem> 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();

}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,59 +1,19 @@
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");
//this.tracer = new ShamrockTracer();
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<Filter>() {
@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<DispatcherType> 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;
}

4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</exclusion>
<exclusion>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FilterMappingInfo> 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<FilterMappingInfo> 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;
}

}

}
Loading

0 comments on commit de6ce81

Please sign in to comment.