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

Rest client: TEST #13

Closed
wants to merge 3 commits into from
Closed
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 @@ -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);
}
6 changes: 6 additions & 0 deletions examples/strict/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-rest-client-deployment</artifactId>
<scope>provided</scope>
</dependency>


<!-- test dependencies -->
Expand Down Expand Up @@ -162,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
@@ -0,0 +1,12 @@
package org.jboss.shamrock.example.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

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

@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,6 +10,8 @@
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.microprofile.rest.client.RestClientBuilder;

import io.reactivex.Single;

@Path("/test")
Expand All @@ -18,6 +22,16 @@ public String getTest() {
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
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<jboss-transaction-spi.version>7.6.0.Final</jboss-transaction-spi.version>
<javax.persistence-api.version>2.2</javax.persistence-api.version>
<rxjava.version>2.1.12</rxjava.version>
<microprofile-rest-client-api.version>1.0</microprofile-rest-client-api.version>
</properties>

<modules>
Expand All @@ -76,6 +77,7 @@
<module>bean-validation</module>
<module>transactions</module>
<module>agroal</module>
<module>rest-client</module>
</modules>
<build>
<pluginManagement>
Expand Down Expand Up @@ -211,6 +213,16 @@
<artifactId>shamrock-junit</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-rest-client-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-rest-client-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-shared-library-example</artifactId>
Expand Down Expand Up @@ -422,6 +434,11 @@
<artifactId>microprofile-config-api</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
<version>${microprofile-rest-client-api.version}</version>
</dependency>
<dependency>
<groupId>org.fakereplace</groupId>
<artifactId>fakereplace</artifactId>
Expand Down Expand Up @@ -490,6 +507,11 @@
<artifactId>resteasy-cdi</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
Expand Down
27 changes: 27 additions & 0 deletions rest-client/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shamrock-rest-client</artifactId>
<groupId>org.jboss.shamrock</groupId>
<version>1.0.0.Alpha1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shamrock-rest-client-deployment</artifactId>

<dependencies>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shamrock</groupId>
<artifactId>shamrock-rest-client-runtime</artifactId>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.jboss.shamrock.restclient;

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 {

private static final DotName REGISTER_REST_CLIENT = DotName.createSimple(RegisterRestClient.class.getName());
@Inject
private BeanDeployment beanDeployment;

@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 {
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());

}
}
}

@Override
public int getPriority() {
return 1;
}
}
Loading