Skip to content

Commit

Permalink
Prevent ClasspathResourceDirectoryReader from accessing closed Native…
Browse files Browse the repository at this point in the history
…ImageResourceFileSystem
  • Loading branch information
linghengqian committed Feb 29, 2024
1 parent ce4cbad commit bd2af0f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 40 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 All @@ -37,8 +37,10 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -66,7 +68,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 +81,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 @@ -125,7 +122,7 @@ public static Stream<String> read(final ClassLoader classLoader, final String di
if (JAR_URL_PROTOCOLS.contains(directoryUrl.getProtocol())) {
return readDirectoryInJar(directory, directoryUrl);
} else {
return readDirectoryInFileSystem(directoryUrl);
return readDirectoryInFileSystem(directoryUrl).stream();
}
});
}
Expand Down Expand Up @@ -162,24 +159,32 @@ 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
* @return list of resource name
*/
@SneakyThrows({IOException.class, URISyntaxException.class})
private static Stream<String> readDirectoryInFileSystem(final URL directoryUrl) {
private static List<String> readDirectoryInFileSystem(final URL directoryUrl) {
if ("resource".equals(directoryUrl.getProtocol())) {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
try (FileSystem ignored = FileSystems.getFileSystem(directoryUrl.toURI())) {
return loadFromDirectory(directoryUrl);
} catch (FileSystemNotFoundException exception) {
try (FileSystem ignored = FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap())) {
return loadFromDirectory(directoryUrl);
}
}
}
return loadFromDirectory(directoryUrl);
}

private static Stream<String> loadFromDirectory(final URL directoryUrl) throws URISyntaxException, IOException {
private static List<String> loadFromDirectory(final URL directoryUrl) throws URISyntaxException, IOException {
Path directoryPath = Paths.get(directoryUrl.toURI());
// noinspection resource
Stream<Path> walkStream = Files.find(directoryPath, Integer.MAX_VALUE, (path, basicFileAttributes) -> !basicFileAttributes.isDirectory(), FileVisitOption.FOLLOW_LINKS);
return walkStream.map(path -> path.subpath(directoryPath.getNameCount() - 1, path.getNameCount()).toString());
return walkStream.map(path -> path.subpath(directoryPath.getNameCount() - 1, path.getNameCount()).toString())
.collect(Collectors.toList());
}
}

0 comments on commit bd2af0f

Please sign in to comment.