Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Sep 7, 2018
1 parent 9a72b24 commit ee1db88
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -37,7 +37,6 @@
import org.jboss.jandex.Indexer;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
import org.jboss.protean.gizmo.BytecodeCreator;
import org.jboss.protean.gizmo.CatchBlockCreator;
import org.jboss.protean.gizmo.ClassCreator;
import org.jboss.protean.gizmo.ExceptionTable;
Expand Down Expand Up @@ -169,6 +168,7 @@ private final class ProcessorContextImpl implements ProcessorContext {
private final Set<String> resources = new HashSet<>();
private final Set<String> resourceBundles = new HashSet<>();
private final Set<String> runtimeInitializedClasses = new HashSet<>();
private final Set<List<String>> proxyClasses = new HashSet<>();

@Override
public BytecodeRecorder addStaticInitTask(int priority) {
Expand Down Expand Up @@ -256,6 +256,12 @@ public void addRuntimeInitializedClasses(String... classes) {
runtimeInitializedClasses.addAll(Arrays.asList(classes));
}

@Override
public void addProxyDefinition(String... proxyClasses) {
this.proxyClasses.add(Arrays.asList(proxyClasses));
}


void writeMainClass() throws IOException {

Collections.sort(tasks);
Expand Down Expand Up @@ -313,7 +319,7 @@ void writeReflectionAutoFeature() throws IOException {

//TODO: at some point we are going to need to break this up, as if it get too big it will hit the method size limit

if(!runtimeInitializedClasses.isEmpty()) {
if (!runtimeInitializedClasses.isEmpty()) {
ExceptionTable tc = beforeAn.addTryCatch();
ResultHandle array = beforeAn.newArray(Class.class, beforeAn.load(runtimeInitializedClasses.size()));
int count = 0;
Expand All @@ -330,6 +336,21 @@ void writeReflectionAutoFeature() throws IOException {
tc.complete();
}

if (!proxyClasses.isEmpty()) {
ResultHandle proxySupportClass = beforeAn.loadClass("com.oracle.svm.core.jdk.proxy.DynamicProxyRegistry");
ResultHandle proxySupport = beforeAn.invokeStaticMethod(ofMethod("org.graalvm.nativeimage.ImageSingletons", "lookup", Object.class, Class.class), proxySupportClass);
for (List<String> proxy : proxyClasses) {
ResultHandle array = beforeAn.newArray(Class.class, beforeAn.load(proxy.size()));
int i = 0;
for (String p : proxy) {
ResultHandle clazz = beforeAn.invokeStaticMethod(ofMethod(Class.class, "forName", Class.class, String.class), beforeAn.load(p));
beforeAn.writeArrayValue(array, beforeAn.load(i++), clazz);

}
beforeAn.invokeInterfaceMethod(ofMethod("com.oracle.svm.core.jdk.proxy.DynamicProxyRegistry", "addProxyClass", void.class, Class[].class), proxySupport, array);
}
}

for (String i : resources) {
beforeAn.invokeStaticMethod(ofMethod(ResourceHelper.class, "registerResources", void.class, String.class), beforeAn.load(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ public interface ProcessorContext {
void addResourceBundle(String bundle);

void addRuntimeInitializedClasses(String ... classes);

void addProxyDefinition(String ... proxyClasses);
}
2 changes: 2 additions & 0 deletions examples/strict/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-rest-client-deployment</artifactId>
<scope>provided</scope>
</dependency>


Expand Down Expand Up @@ -166,6 +167,7 @@
</goals>
<configuration>
<cleanupServer>true</cleanupServer>
<enableHttpUrlHandler>true</enableHttpUrlHandler>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/foo")
@Path("/test")
public interface RestInterface {

@GET
void get();
String get();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jboss.shamrock.example.rest;

import java.net.URL;

import javax.json.Json;
import javax.json.JsonObject;
import javax.ws.rs.GET;
Expand All @@ -8,18 +10,28 @@
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlRootElement;

import io.reactivex.Single;
import org.eclipse.microprofile.rest.client.RestClientBuilder;

import io.reactivex.Single;

@Path("/test")
public class TestResource {

@GET
public String getTest() {
RestInterface iface = RestClientBuilder.newBuilder().build(RestInterface.class);
return "TEST";
}

@GET
@Path("/client")
public String client() throws Exception {

RestInterface iface = RestClientBuilder.newBuilder()
.baseUrl(new URL("http", "localhost", 8080, "/rest"))
.build(RestInterface.class);
return iface.get();
}

@GET
@Path("/int/{val}")
public Integer getInt(@PathParam("val") Integer val) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 RestClientITCase extends RestClientTestCase {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jboss.shamrock.example.test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.jboss.shamrock.junit.ShamrockTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(ShamrockTest.class)
public class RestClientTestCase {

@Test
public void testMicroprofileClient() throws Exception {
URL uri = new URL("http://localhost:8080/rest/test/client");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
Assert.assertEquals("TEST", new String(out.toByteArray()));
}
}
4 changes: 4 additions & 0 deletions jaxrs/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-jaxrs-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.graalvm</groupId>
<artifactId>graal-annotations</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class NativeImageMojo extends AbstractMojo {
@Parameter(defaultValue = "${native-image.new-server}")
private boolean cleanupServer;

@Parameter
private boolean enableHttpUrlHandler;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand Down Expand Up @@ -75,6 +77,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
command.add("-jar");
command.add(finalName + "-runner.jar");
//https://github.com/oracle/graal/issues/660
command.add("-J-Djava.util.concurrent.ForkJoinPool.common.parallelism=1");
if (reportErrorsAtRuntime) {
command.add("-H:+ReportUnsupportedElementsAtRuntime");
}
Expand All @@ -87,6 +91,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
command.add("-J-Djava.compiler=NONE");
command.add("-J-Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y");
}
if(enableHttpUrlHandler) {
command.add("-H:EnableURLProtocols=http");
}
//command.add("-H:+AllowVMInspection");
System.out.println(command);
Process process = Runtime.getRuntime().exec(command.toArray(new String[0]), null, outputDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
import java.lang.reflect.Modifier;

import javax.inject.Inject;
import javax.ws.rs.Path;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.ClientResponseFilter;

import org.apache.commons.logging.impl.Jdk14Logger;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.resteasy.client.jaxrs.internal.proxy.ResteasyClientProxy;
import org.jboss.resteasy.spi.ResteasyConfiguration;
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.ShamrockConfig;
import org.jboss.shamrock.restclient.runtime.DefaultResponseExceptionMapper;
import org.jboss.shamrock.restclient.runtime.RestClientProxy;

class RestClientProcessor implements ResourceProcessor {

Expand All @@ -24,17 +33,51 @@ class RestClientProcessor implements ResourceProcessor {
@Inject
private ShamrockConfig config;

private static final DotName[] CLIENT_ANNOTATIONS = {
DotName.createSimple("javax.ws.rs.GET"),
DotName.createSimple("javax.ws.rs.HEAD"),
DotName.createSimple("javax.ws.rs.DELETE"),
DotName.createSimple("javax.ws.rs.OPTIONS"),
DotName.createSimple("javax.ws.rs.PATCH"),
DotName.createSimple("javax.ws.rs.POST"),
DotName.createSimple("javax.ws.rs.PUT"),
DotName.createSimple("javax.ws.rs.PUT"),
DotName.createSimple(RegisterRestClient.class.getName()),
DotName.createSimple(Path.class.getName())
};

@Override
public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception {
int count = 0;
for(AnnotationInstance annotation : archiveContext.getCombinedIndex().getAnnotations(REGISTER_REST_CLIENT)) {
AnnotationTarget target = annotation.target();
ClassInfo clazz = target.asClass();
if(!Modifier.isInterface(clazz.flags())) {
throw new RuntimeException("RegisterRestClient can only be applied to interfaces: " + clazz.name());
}
processorContext.addReflectiveClass(false, false,
DefaultResponseExceptionMapper.class.getName(),
LogFactoryImpl.class.getName(),
Jdk14Logger.class.getName());
processorContext.addReflectiveClass(false, false, ClientRequestFilter[].class.getName());
processorContext.addReflectiveClass(false, false, ClientResponseFilter[].class.getName());
processorContext.addResource("META-INF/services/javax.ws.rs.ext.Providers");
//TODO: fix this, we don't want to just add all the providers
processorContext.addReflectiveClass(false, false, "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
processorContext.addReflectiveClass(false, false, "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
processorContext.addProxyDefinition(ResteasyConfiguration.class.getName());
for (DotName type : CLIENT_ANNOTATIONS) {
for (AnnotationInstance annotation : archiveContext.getCombinedIndex().getAnnotations(type)) {
AnnotationTarget target = annotation.target();
ClassInfo theInfo;
if (target.kind() == AnnotationTarget.Kind.CLASS) {
theInfo = target.asClass();
} else if (target.kind() == AnnotationTarget.Kind.METHOD) {
theInfo = target.asMethod().declaringClass();
} else {
continue;
}
if (!Modifier.isInterface(theInfo.flags())) {
continue;
}
processorContext.addProxyDefinition( theInfo.name().toString(), ResteasyClientProxy.class.getName());
processorContext.addProxyDefinition( theInfo.name().toString(), RestClientProxy.class.getName());
processorContext.addReflectiveClass(true, false, theInfo.name().toString());

}
}
}

Expand Down
4 changes: 4 additions & 0 deletions rest-client/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.graalvm</groupId>
<artifactId>graal-annotations</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Created by hbraun on 17.01.18.
*/
class DefaultResponseExceptionMapper implements ResponseExceptionMapper {
public class DefaultResponseExceptionMapper implements ResponseExceptionMapper {

@Override
public Throwable toThrowable(Response response) {
Expand Down
Loading

0 comments on commit ee1db88

Please sign in to comment.