Skip to content

Commit

Permalink
Fixes ClassPathURLLoader cannot find files under GraalVM Native Image (
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian authored Feb 21, 2024
1 parent 00bf31e commit e0c5c6c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,6 @@
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.driver.api.yaml.YamlJDBCConfigurationCustomizer"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager"},
"name":"org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath.AbsolutePathURLProvider",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager"},
"name":"org.apache.shardingsphere.driver.jdbc.core.driver.spi.absolutepath.AbsolutePathWithEnvironmentURLProvider",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager"},
"name":"org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath.ClasspathURLProvider",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager"},
"name":"org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath.ClasspathWithEnvironmentURLProvider",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager"},
"name":"org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath.ClasspathWithSystemPropsURLProvider",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.encrypt.rule.EncryptRule"},
"name":"org.apache.shardingsphere.encrypt.algorithm.assisted.MD5AssistedEncryptAlgorithm",
Expand Down Expand Up @@ -174,6 +149,45 @@
"condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.shortcut.YamlRuleConfigurationShortcuts"},
"name":"org.apache.shardingsphere.globalclock.core.yaml.config.YamlGlobalClockRuleConfiguration"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.encrypt.yaml.swapper.NewYamlEncryptRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"queryAllPublicMethods":true,
"methods":[{"name":"<init>","parameterTypes":[] }, {"name":"setProps","parameterTypes":["java.util.Properties"] }, {"name":"setType","parameterTypes":["java.lang.String"] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mask.yaml.swapper.NewYamlMaskRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.shadow.yaml.swapper.NewYamlShadowRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.yaml.swapper.NewYamlShardingRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationBeanInfo"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationCustomizer"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"},
"name":"org.apache.shardingsphere.infra.instance.ComputeNodeData",
Expand All @@ -189,6 +203,16 @@
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"},
"name":"org.apache.shardingsphere.infra.instance.ComputeNodeDataCustomizer"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.rule.ShardingRule"},
"name":"org.apache.shardingsphere.infra.keygen.snowflake.algorithm.SnowflakeKeyGenerateAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.rule.ShardingRule"},
"name":"org.apache.shardingsphere.infra.keygen.uuid.algorithm.UUIDKeyGenerateAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheBuilder"},
"name":"org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheLoader"
Expand All @@ -208,45 +232,6 @@
"name":"org.apache.shardingsphere.infra.util.yaml.YamlConfiguration",
"queryAllPublicMethods":true
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.encrypt.yaml.swapper.NewYamlEncryptRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"queryAllPublicMethods":true,
"methods":[{"name":"<init>","parameterTypes":[] }, {"name":"setProps","parameterTypes":["java.util.Properties"] }, {"name":"setType","parameterTypes":["java.lang.String"] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mask.yaml.swapper.NewYamlMaskRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.shadow.yaml.swapper.NewYamlShadowRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.yaml.swapper.NewYamlShardingRuleConfigurationSwapper"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration",
"allDeclaredFields":true,
"methods":[{"name":"getProps","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfigurationBeanInfo"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfigurationCustomizer"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration",
Expand Down Expand Up @@ -365,16 +350,6 @@
"condition":{"typeReachable":"org.apache.shardingsphere.metadata.persist.service.schema.NewTableMetaDataPersistService"},
"name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTableCustomizer"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.rule.ShardingRule"},
"name":"org.apache.shardingsphere.keygen.snowflake.algorithm.SnowflakeKeyGenerateAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.rule.ShardingRule"},
"name":"org.apache.shardingsphere.keygen.uuid.algorithm.UUIDKeyGenerateAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.shortcut.YamlRuleConfigurationShortcuts"},
"name":"org.apache.shardingsphere.logging.yaml.config.YamlLoggingRuleConfiguration"
Expand Down Expand Up @@ -979,7 +954,7 @@
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.core.database.visitor.SQLStatementVisitorFactory"},
"name":"org.apache.shardingsphere.sql.parser.mysql.visitor.statement.type.MySQLDMLStatementVisitor",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
Expand All @@ -1004,7 +979,7 @@
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.core.database.visitor.SQLStatementVisitorFactory"},
"name":"org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.type.OpenGaussDMLStatementVisitor",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
Expand Down Expand Up @@ -1034,7 +1009,7 @@
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.core.database.visitor.SQLStatementVisitorFactory"},
"name":"org.apache.shardingsphere.sql.parser.postgresql.visitor.statement.type.PostgreSQLDMLStatementVisitor",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
Expand Down Expand Up @@ -1124,7 +1099,7 @@
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.core.database.visitor.SQLStatementVisitorFactory"},
"name":"org.apache.shardingsphere.sql.parser.sqlserver.visitor.statement.type.SQLServerDMLStatementVisitor",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.url.spi.ShardingSphereURLLoader;

import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;
Expand All @@ -36,12 +37,12 @@ public final class ClassPathURLLoader implements ShardingSphereURLLoader {
@Override
@SneakyThrows(IOException.class)
public String load(final String configurationSubject, final Properties queryProps) {
return Files.readAllLines(getResourceFile(configurationSubject).toPath()).stream().collect(Collectors.joining(System.lineSeparator()));
}

@SneakyThrows(URISyntaxException.class)
private File getResourceFile(final String configurationSubject) {
return new File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(configurationSubject)).toURI().getPath());
try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configurationSubject)) {
Objects.requireNonNull(inputStream);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,10 @@ public static JDBCRepositorySQL load(final String type) {
}

/**
* Under the GraalVM Native Image, although there is
* `com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystemProvider`, the corresponding
* `com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystem` does not autoload. This is mainly to align the
* behavior of `ZipFileSystemProvider`, so ShardingSphere need to manually open and close the FileSystem
* corresponding to the `resource:/` scheme. For more background reference <a href="https://github.com/oracle/graal/issues/7682">oracle/graal#7682</a>.
* <p/>
* ShardingSphere use the System Property of `org.graalvm.nativeimage.imagecode` to identify whether this class is in the
* GraalVM Native Image environment. The background of this property comes from
* <a href="https://junit.org/junit5/docs/5.10.0/api/org.junit.jupiter.api/org/junit/jupiter/api/condition/DisabledInNativeImage.html">Annotation Interface DisabledInNativeImage</a>.
* Under the GraalVM Native Image, `com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystem` does not autoload.
* This is mainly to align the behavior of `jdk.nio.zipfs.ZipFileSystem`,
* so ShardingSphere need to manually open and close the FileSystem corresponding to the `resource:/` scheme.
* For more background reference <a href="https://github.com/oracle/graal/issues/7682">oracle/graal#7682</a>.
*
* @param url url
* @param type type of JDBC repository SQL
Expand All @@ -104,13 +99,12 @@ public static JDBCRepositorySQL load(final String type) {
* @see sun.nio.fs.UnixFileSystemProvider
*/
private static JDBCRepositorySQL loadFromDirectory(final URL url, final String type) throws URISyntaxException, IOException {
if (null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))) {
return loadFromDirectoryLegacy(url, type);
} else {
if ("resource".equals(url.getProtocol())) {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
return loadFromDirectoryInNativeImage(url, type);
}
}
return loadFromDirectoryLegacy(url, type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.encrypt.rule.EncryptRule"},
"condition":{"typeReachable":"org.apache.shardingsphere.test.natived.jdbc.features.EncryptTest"},
"name":"org.apache.shardingsphere.test.natived.jdbc.commons.algorithm.TestQueryAssistedShardingEncryptAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"resources":{
"includes":[{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.NewMetaDataContextsFactory"},
"condition":{"typeReachable":"org.apache.shardingsphere.test.natived.jdbc.databases.SQLServerTest"},
"pattern":"\\Qcontainer-license-acceptance.txt\\E"
}, {
"condition":{"typeReachable":"ch.qos.logback.core.util.Loader"},
Expand Down

0 comments on commit e0c5c6c

Please sign in to comment.