Skip to content

Commit

Permalink
Support JDK9 methods in CS/CF wrappers using a MR-JAR
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Jul 16, 2020
1 parent 2529bcf commit e3891d7
Show file tree
Hide file tree
Showing 19 changed files with 422 additions and 36 deletions.
18 changes: 10 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
java: [8, 11]
name: build with jdk ${{matrix.java}}
name: build with JDK 11

steps:
- uses: actions/checkout@v2
name: checkout

- uses: actions/[email protected]
name: set up jdk ${{matrix.java}}
name: set up JDK 11
with:
java-version: ${{matrix.java}}
java-version: 11

- uses: actions/[email protected]
name: set up JDK 8
with:
java-version: 8

- name: build with maven
run: mvn -B formatter:validate verify --file pom.xml
run: JAVA_HOME=$JAVA_HOME_11_X64 mvn -B formatter:validate verify --file pom.xml -Djava8.home=$JAVA_HOME_8_X64

quality:
needs: [build]
Expand All @@ -48,7 +50,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/[email protected]
with:
java-version: 8
java-version: 11

- name: sonar
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- uses: actions/[email protected]
with:
java-version: 8
java-version: 11

- name: maven release ${{steps.metadata.outputs.current-version}}
run: |
Expand Down
37 changes: 37 additions & 0 deletions core/bin/mrjar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>mvjar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>io.smallrye:smallrye-context-propagation-java8</include>
</includes>
<binaries>
<unpack>true</unpack>
<includeDependencies>false</includeDependencies>
</binaries>
</moduleSet>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>io.smallrye:smallrye-context-propagation-java9</include>
</includes>
<binaries>
<outputDirectory>META-INF/versions/9</outputDirectory>
<unpack>true</unpack>
<includeDependencies>false</includeDependencies>
<unpackOptions>
<excludes>
<exclude>/META-INF/**</exclude>
</excludes>
</unpackOptions>
</binaries>
</moduleSet>
</moduleSets>
</assembly>
Empty file added core/build-release-8
Empty file.
23 changes: 6 additions & 17 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
<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">
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.smallrye</groupId>
Expand All @@ -9,6 +10,10 @@
<artifactId>smallrye-context-propagation</artifactId>
<name>smallrye-context-propagation</name>

<properties>
<jdk.min.version>9</jdk.min.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -36,20 +41,4 @@
</dependency>
</dependencies>

<profiles>
<profile>
<id>coverage</id>
<properties>
<argLine>@{jacocoArgLine}</argLine>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
import java.util.function.Consumer;
import java.util.function.Function;

final class CompletableFutureWrapper<T> extends CompletableFuture<T> {
private final CompletableFuture<T> f;
private final SmallRyeThreadContext context;
import io.smallrye.context.impl.Contextualized;

public class CompletableFutureWrapper<T> extends CompletableFuture<T> implements Contextualized {
protected final CompletableFuture<T> f;
protected final SmallRyeThreadContext context;
/**
* If this executor is not null, we're wrapping a CF. If it is null, we're a dependent stage of
* another CF, so we have different behaviour
*/
private final Executor executor;
protected final Executor executor;
private final boolean minimal;

CompletableFutureWrapper(SmallRyeThreadContext context, CompletableFuture<T> f, Executor executor, boolean minimal) {
public CompletableFutureWrapper(SmallRyeThreadContext context, CompletableFuture<T> f, Executor executor, boolean minimal) {
this.context = context;
this.f = f;
f.whenComplete((r, t) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import java.util.function.Consumer;
import java.util.function.Function;

final class CompletionStageWrapper<T> implements CompletionStage<T> {
import io.smallrye.context.impl.Contextualized;

public class CompletionStageWrapper<T> implements CompletionStage<T>, Contextualized {
private final CompletionStage<T> f;
private final SmallRyeThreadContext context;

CompletionStageWrapper(SmallRyeThreadContext context, CompletionStage<T> f) {
public CompletionStageWrapper(SmallRyeThreadContext context, CompletionStage<T> f) {
this.context = context;
this.f = f;
}
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/io/smallrye/context/JdkSpecific.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.smallrye.context;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

import io.smallrye.context.impl.JdkSpecificImpl;

public class JdkSpecific {

private final static JdkSpecificImpl impl = new JdkSpecificImpl();

public interface Contract {
public <T> CompletionStage<T> newCompletionStageWrapper(SmallRyeThreadContext threadContext,
CompletionStage<T> future);

public <T> CompletableFuture<T> newCompletableFutureWrapper(SmallRyeThreadContext threadContext,
CompletableFuture<T> future, Executor executor, boolean minimal);
}

public static <T> CompletionStage<T> newCompletionStageWrapper(SmallRyeThreadContext threadContext,
CompletionStage<T> future) {
return impl.newCompletionStageWrapper(threadContext, future);
}

public static <T> CompletableFuture<T> newCompletableFutureWrapper(SmallRyeThreadContext threadContext,
CompletableFuture<T> future, Executor executor, boolean minimal) {
return impl.newCompletableFutureWrapper(threadContext, future, executor, minimal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ public <T> CompletableFuture<T> withContextCapture(CompletableFuture<T> future)
}

public <T> CompletableFuture<T> withContextCapture(CompletableFuture<T> future, Executor executor) {
return new CompletableFutureWrapper<>(this, future, executor, false);
return JdkSpecific.newCompletableFutureWrapper(this, future, executor, false);
}

@Override
public <T> CompletionStage<T> withContextCapture(CompletionStage<T> future) {
if (future instanceof CompletableFuture)
// the MP-CP TCK insists we cannot complete instances returned by this API
return new CompletableFutureWrapper<>(this, (CompletableFuture<T>) future, null, true);
return new CompletionStageWrapper<>(this, future);
return JdkSpecific.newCompletableFutureWrapper(this, (CompletableFuture<T>) future, null, true);
return JdkSpecific.newCompletionStageWrapper(this, future);
}

@Override
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/java/io/smallrye/context/impl/JdkSpecificImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.smallrye.context.impl;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

import io.smallrye.context.CompletableFutureWrapper;
import io.smallrye.context.CompletionStageWrapper;
import io.smallrye.context.JdkSpecific;
import io.smallrye.context.SmallRyeThreadContext;

public class JdkSpecificImpl implements JdkSpecific.Contract {

@Override
public <T> CompletionStage<T> newCompletionStageWrapper(SmallRyeThreadContext threadContext,
CompletionStage<T> future) {
return new CompletionStageWrapper<>(threadContext, future);
}

@Override
public <T> CompletableFuture<T> newCompletableFutureWrapper(SmallRyeThreadContext threadContext,
CompletableFuture<T> future, Executor executor, boolean minimal) {
return new CompletableFutureWrapper<>(threadContext, future, executor, minimal);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.smallrye.context;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public class Jdk9CompletableFutureWrapper<T> extends CompletableFutureWrapper<T> {

public Jdk9CompletableFutureWrapper(SmallRyeThreadContext context, CompletableFuture<T> f, Executor executor,
boolean minimal) {
super(context, f, executor, minimal);
}

@Override
public <U> CompletableFuture<U> newIncompleteFuture() {
CompletableFuture<U> ret = new CompletableFuture<>();
return context.withContextCapture(ret, executor);
}

@Override
public Executor defaultExecutor() {
return executor;
}

@Override
public CompletableFuture<T> copy() {
return context.withContextCapture(f.copy(), executor);
}

@Override
public CompletionStage<T> minimalCompletionStage() {
// this creates a new MinimalStage we need to wrap
return context.withContextCapture(f.minimalCompletionStage());
}

@Override
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier) {
// just forward
return context.withContextCapture(f.completeAsync(context.contextualSupplierUnlessContextualized(supplier), executor),
executor);
}

@Override
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier, Executor executor) {
// just forward
return context.withContextCapture(f.completeAsync(context.contextualSupplierUnlessContextualized(supplier), executor),
this.executor);
}

@Override
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit) {
// just forward
return context.withContextCapture(f.orTimeout(timeout, unit), executor);
}

@Override
public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit) {
// just forward
return context.withContextCapture(f.completeOnTimeout(value, timeout, unit), executor);
}
}
26 changes: 26 additions & 0 deletions core/src/main/java9/io/smallrye/context/impl/JdkSpecificImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.smallrye.context.impl;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

import io.smallrye.context.CompletionStageWrapper;
import io.smallrye.context.Jdk9CompletableFutureWrapper;
import io.smallrye.context.JdkSpecific;
import io.smallrye.context.SmallRyeThreadContext;

public class JdkSpecificImpl implements JdkSpecific.Contract {

@Override
public <T> CompletionStage<T> newCompletionStageWrapper(SmallRyeThreadContext threadContext,
CompletionStage<T> future) {
return new CompletionStageWrapper<>(threadContext, future);
}

@Override
public <T> CompletableFuture<T> newCompletableFutureWrapper(SmallRyeThreadContext threadContext,
CompletableFuture<T> future, Executor executor, boolean minimal) {
return new Jdk9CompletableFutureWrapper<>(threadContext, future, executor, minimal);
}

}
12 changes: 12 additions & 0 deletions core/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd
http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">
<weld:scan>
<!-- Don't let Arquillian choke on MR-Jars -->
<weld:exclude weld:pattern="META-INF\.versions\..*"></weld:exclude>
</weld:scan>
</beans>
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@
<!-- TODO - to remve once these changes are in the Parent POM -->
<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>9</version>
<message>This project must be built with Java 9 or later.</message>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
Expand Down
Empty file added tests/build-release-8
Empty file.
Empty file added tests/build-test-java8
Empty file.
Empty file added tests/build-test-java9
Empty file.
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<properties>
<maven.javadoc.skip>true</maven.javadoc.skip>
<jdk.min.version>9</jdk.min.version>
</properties>

<dependencies>
Expand Down
Loading

0 comments on commit e3891d7

Please sign in to comment.