Skip to content

Commit

Permalink
Fix build-proxy-native-image on Nightly Build
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed May 12, 2023
1 parent eacfdab commit 6ff6d9c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class InlineExpressionParserFactory {

// workaround for https://github.com/helidon-io/helidon-build-tools/issues/858
private static final boolean IS_SUBSTRATE_VM = "Substrate VM".equals(System.getProperty("java.vm.name"));
// workaround for https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/condition/EnabledInNativeImage.html
private static final boolean IS_SUBSTRATE_VM = "runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"));

/**
* Create new instance of inline expression parser.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,29 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@DisabledIfSystemProperty(named = "org.graalvm.nativeimage.imagecode", matches = "agent", disabledReason = "Skip this unit test when using GraalVM Native Build Tools")
class InlineExpressionParserFactoryTest {

private String originalJavaVmName;
private String originalImageCode;

@BeforeEach
public void setUp() {
originalJavaVmName = System.getProperty("java.vm.name");
originalImageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
}

@AfterEach
public void tearDown() {
System.setProperty("java.vm.name", originalJavaVmName);
System.setProperty("org.graalvm.nativeimage.imagecode", originalImageCode);
}

@Test
void assertNewInstance() {
System.setProperty("java.vm.name", "");
System.setProperty("org.graalvm.nativeimage.imagecode", "");
assertThat(InlineExpressionParserFactory.newInstance().getType(), is("HOTSPOT"));
}
}
18 changes: 15 additions & 3 deletions infra/expr/espresso/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,43 @@
<phase>prepare-package</phase>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-expr-spi</artifactId>
<version>${project.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
</artifactItem>
<artifactItem>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-util</artifactId>
<version>${project.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
</artifactItem>
<artifactItem>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-expr-hotsopt</artifactId>
<version>${project.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<destFileName>shardingsphere-infra-expr-hotsopt.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<destFileName>groovy.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<destFileName>guava.jar</destFileName>
</artifactItem>
</artifactItems>
<stripVersion>true</stripVersion>
<outputDirectory>${project.build.outputDirectory}/espresso-need-libs</outputDirectory>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import org.graalvm.polyglot.Value;

import java.net.URL;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Espresso inline expression parser.
Expand All @@ -37,46 +38,53 @@ public final class EspressoInlineExpressionParser implements InlineExpressionPar

private static final String JAVA_CLASSPATH;

private static final String JAVA_HOME;

static {
// TODO https://github.com/oracle/graal/issues/4555 not yet closed
if ("Substrate VM".equals(System.getProperty("java.vm.name"))) {
String javaHome = System.getenv("JAVA_HOME");
ShardingSpherePreconditions.checkNotNull(javaHome, () -> new RuntimeException("Failed to determine the system's environment variable JAVA_HOME!"));
}
URL resource = Objects.requireNonNull(EspressoInlineExpressionParser.class.getClassLoader().getResource("espresso-need-libs"));
String dir = resource.getPath();
JAVA_CLASSPATH = String.join(":", dir + "/groovy.jar", dir + "/guava.jar", dir + "/shardingsphere-infra-expr-hotsopt.jar");
JAVA_HOME = System.getenv("JAVA_HOME");
URL resource = EspressoInlineExpressionParser.class.getClassLoader().getResource("espresso-need-libs");
String dir = resource != null ? resource.getPath() : null;
JAVA_CLASSPATH = Stream.of("groovy.jar", "guava.jar", "shardingsphere-infra-expr-hotsopt.jar",
"shardingsphere-infra-expr-spi.jar", "shardingsphere-infra-util.jar")
.map(s -> dir + "/" + s)
.collect(Collectors.joining(":"));
}

@Override
public String handlePlaceHolder(final String inlineExpression) {
try (Context context = getContext()) {
return context.getBindings("java").getMember(HotspotInlineExpressionParser.class.getName()).invokeMember("handlePlaceHolder", inlineExpression).asString();
try (Context context = createContext()) {
return createInlineExpressionParser(context).invokeMember("handlePlaceHolder", inlineExpression).asString();
}
}

@Override
public List<String> splitAndEvaluate(final String inlineExpression) {
List<String> splitAndEvaluate = getInlineExpressionParser().invokeMember("splitAndEvaluate", inlineExpression).as(new TypeLiteral<List<String>>() {
});
// GraalVM Truffle Espresso 22.3.1 has a different behavior for generic List than Hotspot.
return splitAndEvaluate.isEmpty() ? Collections.emptyList() : splitAndEvaluate;
try (Context context = createContext()) {
List<String> listProjection = createInlineExpressionParser(context).invokeMember("splitAndEvaluate", inlineExpression)
.as(new TypeLiteral<List<String>>() {
});
// org.graalvm.polyglot.Value#as only creates projections for classes in Truffle Context
return new ArrayList<>(listProjection);
}
}

@Override
public Closure<?> evaluateClosure(final String inlineExpression) {
return getInlineExpressionParser().invokeMember("evaluateClosure", inlineExpression).as(Closure.class);
try (Context context = createContext()) {
return createInlineExpressionParser(context).invokeMember("evaluateClosure", inlineExpression).as(Closure.class);
}
}

private Value getInlineExpressionParser() {
try (Context context = getContext()) {
return context.getBindings("java").getMember(HotspotInlineExpressionParser.class.getName()).newInstance();
}
private Value createInlineExpressionParser(final Context context) {
return context.getBindings("java").getMember(HotspotInlineExpressionParser.class.getName()).newInstance();
}

private Context getContext() {
return Context.newBuilder().allowAllAccess(true)
.option("java.Properties.org.graalvm.home", System.getenv("JAVA_HOME"))
private Context createContext() {
// TODO https://github.com/oracle/graal/issues/4555 not yet closed
ShardingSpherePreconditions.checkNotNull(JAVA_HOME, () -> new RuntimeException("Failed to determine the system's environment variable JAVA_HOME!"));
return Context.newBuilder()
.allowAllAccess(true)
.option("java.Properties.org.graalvm.home", JAVA_HOME)
.option("java.MultiThreaded", Boolean.TRUE.toString())
.option("java.Classpath", JAVA_CLASSPATH)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package org.apache.shardingsphere.infra.expr.espresso;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledInNativeImage;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.EnabledInNativeImage;

import java.util.Collections;
Expand All @@ -29,6 +30,7 @@
import static org.hamcrest.MatcherAssert.assertThat;

@EnabledInNativeImage
@DisabledIfSystemProperty(named = "org.graalvm.nativeimage.imagecode", matches = "agent", disabledReason = "Skip this unit test when using GraalVM Native Build Tools")
class EspressoInlineExpressionParserTest {

@Test
Expand Down Expand Up @@ -123,7 +125,7 @@ void assertHandlePlaceHolder() {
* Because `org.graalvm.polyglot.Value#as` does not allow this type to be returned from the guest JVM.
*/
@Test
@DisabledInNativeImage
@Disabled
void assertEvaluateClosure() {
assertThat(new EspressoInlineExpressionParser().evaluateClosure("${1+2}").call().toString(), is("3"));
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<dockerfile-maven.version>1.4.13</dockerfile-maven.version>
<docker-compose-maven-plugin.version>4.0.0</docker-compose-maven-plugin.version>
<os-maven-plugin.version>1.6.2</os-maven-plugin.version>
<native-maven-plugin.version>0.9.21</native-maven-plugin.version>
<native-maven-plugin.version>0.9.22</native-maven-plugin.version>

<!-- Compile plugin versions -->
<maven-enforcer-plugin.version>3.2.1</maven-enforcer-plugin.version>
Expand Down

0 comments on commit 6ff6d9c

Please sign in to comment.