Skip to content

Commit

Permalink
Prevent ClasspathResourceDirectoryReader from accessing closed Native…
Browse files Browse the repository at this point in the history
…ImageResourceFileSystem (#30346)

* Prevent ClasspathResourceDirectoryReader from accessing closed NativeImageResourceFileSystem

* Prevent automatic closing of NativeImageResourceFileSystem

* Removes redundant FileSystem decisions
  • Loading branch information
linghengqian authored Feb 29, 2024
1 parent 5212afd commit 8a18305
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,46 @@
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationCustomizer"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.rule.ShardingRule"},
"name":"org.apache.shardingsphere.infra.algorithm.keygen.snowflake.SnowflakeKeyGenerateAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.sharding.rule.ShardingRule"},
"name":"org.apache.shardingsphere.infra.algorithm.keygen.uuid.UUIDKeyGenerateAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule"},
"name":"org.apache.shardingsphere.infra.algorithm.load.balancer.random.RandomLoadBalanceAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule"},
"name":"org.apache.shardingsphere.infra.algorithm.load.balancer.round.robin.RoundRobinLoadBalanceAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule"},
"name":"org.apache.shardingsphere.infra.algorithm.load.balancer.weight.WeightLoadBalanceAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory"},
"name":"org.apache.shardingsphere.infra.expr.groovy.GroovyInlineExpressionParser",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory"},
"name":"org.apache.shardingsphere.infra.expr.interval.IntervalInlineExpressionParser",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory"},
"name":"org.apache.shardingsphere.infra.expr.literal.LiteralInlineExpressionParser",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.service.ComputeNodeStatusService"},
"name":"org.apache.shardingsphere.infra.instance.ComputeNodeData",
Expand All @@ -203,16 +243,6 @@
"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 Down Expand Up @@ -545,7 +575,7 @@
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader$2"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader"},
"name":"org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQL",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
Expand Down Expand Up @@ -597,21 +627,6 @@
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.parser.yaml.config.YamlSQLParserRuleConfigurationCustomizer"
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule"},
"name":"org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RandomReadQueryLoadBalanceAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule"},
"name":"org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RoundRobinReadQueryLoadBalanceAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule"},
"name":"org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.WeightReadQueryLoadBalanceAlgorithm",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.yaml.YamlEngine"},
"name":"org.apache.shardingsphere.readwritesplitting.yaml.config.YamlReadwriteSplittingRuleConfiguration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"condition":{"typeReachable":"org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager"},
"pattern":"\\QMETA-INF/services/java.sql.Driver\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.infra.util.directory.ClasspathResourceDirectoryReader"},
"pattern":"\\Qsql\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.transaction.xa.atomikos.manager.AtomikosTransactionManagerProvider"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
Expand Down Expand Up @@ -66,7 +66,7 @@ public static boolean isDirectory(final String name) {
* @param name resource name
* @return true if the resource is a directory; false if the resource does not exist, is not a directory, or it cannot be determined if the resource is a directory or not.
*/
@SneakyThrows({IOException.class, URISyntaxException.class})
@SneakyThrows(URISyntaxException.class)
public static boolean isDirectory(final ClassLoader classLoader, final String name) {
URL resourceUrl = classLoader.getResource(name);
if (null == resourceUrl) {
Expand All @@ -79,11 +79,6 @@ public static boolean isDirectory(final ClassLoader classLoader, final String na
}
return jarFile.getJarEntry(name).isDirectory();
} else {
if ("resourceUrl".equals(resourceUrl.getProtocol())) {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
return Files.isDirectory(Paths.get(resourceUrl.toURI()));
}
}
return Files.isDirectory(Paths.get(resourceUrl.toURI()));
}
}
Expand Down Expand Up @@ -162,15 +157,27 @@ private static JarFile getJarFile(final URL url) {
* 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>.
* Under the context of third-party dependencies such as Spring Framework OSS,
* `com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystem` will be automatically created during the life cycle of the context,
* so additional determination is required.
*
* @param directoryUrl directory url
* @return stream of resource name
*/
@SneakyThrows({IOException.class, URISyntaxException.class})
private static Stream<String> readDirectoryInFileSystem(final URL directoryUrl) {
if ("resource".equals(directoryUrl.getProtocol())) {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
try {
return loadFromDirectory(directoryUrl);
} catch (FileSystemNotFoundException exception) {
FileSystem nativeImageResourceFileSystem = FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap());
return loadFromDirectory(directoryUrl).onClose(() -> {
try {
nativeImageResourceFileSystem.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
return loadFromDirectory(directoryUrl);
Expand Down

0 comments on commit 8a18305

Please sign in to comment.