This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new e0c5c6cde76 Fixes ClassPathURLLoader cannot find files under GraalVM 
Native Image (#30145)
e0c5c6cde76 is described below

commit e0c5c6cde7619afdbb90a32ed2672ca7c6aa0113
Author: Ling Hengqian <[email protected]>
AuthorDate: Wed Feb 21 23:43:38 2024 +0800

    Fixes ClassPathURLLoader cannot find files under GraalVM Native Image 
(#30145)
---
 .../reflect-config.json                            | 131 +++++++++------------
 .../infra/url/classpath/ClassPathURLLoader.java    |  19 +--
 .../jdbc/sql/JDBCRepositorySQLLoader.java          |  18 +--
 .../reflect-config.json                            |   2 +-
 .../resource-config.json                           |   2 +-
 5 files changed, 71 insertions(+), 101 deletions(-)

diff --git 
a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json
 
b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json
index 02082e72276..56e1291380d 100644
--- 
a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json
+++ 
b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json
@@ -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",
@@ -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",
@@ -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"
@@ -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",
@@ -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"
@@ -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":[] }]
 },
@@ -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":[] }]
 },
@@ -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":[] }]
 },
@@ -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":[] }]
 },
diff --git 
a/infra/url/type/classpath/src/main/java/org/apache/shardingsphere/infra/url/classpath/ClassPathURLLoader.java
 
b/infra/url/type/classpath/src/main/java/org/apache/shardingsphere/infra/url/classpath/ClassPathURLLoader.java
index dce0a566f34..df3e6412e62 100644
--- 
a/infra/url/type/classpath/src/main/java/org/apache/shardingsphere/infra/url/classpath/ClassPathURLLoader.java
+++ 
b/infra/url/type/classpath/src/main/java/org/apache/shardingsphere/infra/url/classpath/ClassPathURLLoader.java
@@ -20,10 +20,11 @@ package org.apache.shardingsphere.infra.url.classpath;
 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;
@@ -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
diff --git 
a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java
 
b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java
index 94f691d680c..8a9085e64a3 100644
--- 
a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java
+++ 
b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java
@@ -85,15 +85,10 @@ public final class JDBCRepositorySQLLoader {
     }
     
     /**
-     * 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
@@ -104,13 +99,12 @@ public final class JDBCRepositorySQLLoader {
      * @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);
     }
     
     /**
diff --git 
a/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/reflect-config.json
 
b/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/reflect-config.json
index 2c18921fb05..74ded5e33d0 100644
--- 
a/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/reflect-config.json
+++ 
b/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/reflect-config.json
@@ -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":[] }]
 }
diff --git 
a/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json
 
b/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json
index eb118f6ef69..9722265d59c 100644
--- 
a/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json
+++ 
b/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json
@@ -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"},

Reply via email to