This is an automated email from the ASF dual-hosted git repository.
panjuan 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 03063f9843f Refactor ShardingSphereURLManager (#30128)
03063f9843f is described below
commit 03063f9843fc907c5c81442055b17dadac7c54c2
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 15 11:37:20 2024 +0800
Refactor ShardingSphereURLManager (#30128)
---
.../jdbc/core/driver/DriverDataSourceCache.java | 5 ++-
.../core/driver/url/ShardingSphereURLManager.java | 17 ++++++++
.../driver/url/ShardingSphereURLManagerTest.java | 46 ++++++----------------
3 files changed, 32 insertions(+), 36 deletions(-)
diff --git
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
index 0075226a19f..a1f97755d64 100644
---
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
+++
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
@@ -18,7 +18,9 @@
package org.apache.shardingsphere.driver.jdbc.core.driver;
import
org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURL;
import
org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURLManager;
+import
org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURLProvider;
import javax.sql.DataSource;
import java.io.IOException;
@@ -50,7 +52,8 @@ public final class DriverDataSourceCache {
@SuppressWarnings("unchecked")
private <T extends Throwable> DataSource createDataSource(final String
url) throws T {
try {
- return
YamlShardingSphereDataSourceFactory.createDataSource(ShardingSphereURLManager.getContent(url));
+ ShardingSphereURLProvider urlProvider =
ShardingSphereURLManager.getURLProvider(url);
+ return
YamlShardingSphereDataSourceFactory.createDataSource(urlProvider.getContent(ShardingSphereURL.parse(url,
urlProvider.getSourceType())));
} catch (final IOException ex) {
throw (T) new SQLException(ex);
} catch (final SQLException ex) {
diff --git
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
index 7ae284f1a02..b657e398a72 100644
---
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
+++
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManager.java
@@ -45,4 +45,21 @@ public final class ShardingSphereURLManager {
}
throw new URLProviderNotFoundException(url);
}
+
+ /**
+ * Get ShardingSphere URL provider.
+ *
+ * @param url URL
+ * @return URL provider
+ * @throws URLProviderNotFoundException thrown if URL provider not found
+ */
+ public static ShardingSphereURLProvider getURLProvider(final String url) {
+ ShardingSpherePreconditions.checkNotNull(url, () -> new
URLProviderNotFoundException(url));
+ for (ShardingSphereURLProvider each :
ShardingSphereServiceLoader.getServiceInstances(ShardingSphereURLProvider.class))
{
+ if (url.contains(each.getSourceType())) {
+ return each;
+ }
+ }
+ throw new URLProviderNotFoundException(url);
+ }
}
diff --git
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManagerTest.java
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManagerTest.java
index 125c4bb43e6..12c1c7609e8 100644
---
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManagerTest.java
+++
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLManagerTest.java
@@ -17,58 +17,34 @@
package org.apache.shardingsphere.driver.jdbc.core.driver.url;
+import
org.apache.shardingsphere.driver.jdbc.core.driver.url.type.AbsolutePathURLProvider;
+import
org.apache.shardingsphere.driver.jdbc.core.driver.url.type.ClasspathURLProvider;
import
org.apache.shardingsphere.driver.jdbc.exception.syntax.URLProviderNotFoundException;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.EnabledOnOs;
-import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtendWith;
-import java.util.Objects;
-
-import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ExtendWith(AutoMockExtension.class)
class ShardingSphereURLManagerTest {
- private final int fooDriverConfigLengthOnUnix = 999;
-
- private final int fooDriverConfigLengthOnWindows = 1040;
-
- @Test
- void assertNewConstructorWithEmptyURL() {
- assertThrows(URLProviderNotFoundException.class, () ->
ShardingSphereURLManager.getContent("invalid:xxx"));
- }
-
- @Test
- @EnabledOnOs({OS.LINUX, OS.MAC})
- void assertToClasspathConfigurationFile() {
- byte[] actual =
ShardingSphereURLManager.getContent("classpath:config/driver/foo-driver-fixture.yaml");
- assertThat(actual.length, is(fooDriverConfigLengthOnUnix));
- }
-
@Test
- @EnabledOnOs(OS.WINDOWS)
- void assertToClasspathConfigurationFileOnWindows() {
- byte[] actual =
ShardingSphereURLManager.getContent("classpath:config/driver/foo-driver-fixture.yaml");
- assertThat(actual.length, is(fooDriverConfigLengthOnWindows));
+ void assertGetInvalidURLProvider() {
+ assertThrows(URLProviderNotFoundException.class, () ->
ShardingSphereURLManager.getURLProvider("invalid:xxx"));
}
@Test
- @EnabledOnOs({OS.LINUX, OS.MAC})
- void assertToAbsolutePathConfigurationFile() {
- String absolutePath =
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-fixture.yaml")).getPath();
- byte[] actual = ShardingSphereURLManager.getContent("absolutepath:" +
absolutePath);
- assertThat(actual.length, is(fooDriverConfigLengthOnUnix));
+ void assertGetClasspathURLProvider() {
+ ShardingSphereURLProvider actual =
ShardingSphereURLManager.getURLProvider("classpath:xxx");
+ assertThat(actual, instanceOf(ClasspathURLProvider.class));
}
@Test
- @EnabledOnOs(OS.WINDOWS)
- void assertToAbsolutePathConfigurationFileOnWindows() {
- String absolutePath =
Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-fixture.yaml")).getPath();
- byte[] actual = ShardingSphereURLManager.getContent("absolutepath:" +
absolutePath);
- assertThat(actual.length, is(fooDriverConfigLengthOnWindows));
+ void assertGetAbsolutePathURLProvider() {
+ ShardingSphereURLProvider actual =
ShardingSphereURLManager.getURLProvider("absolutepath:xxx");
+ assertThat(actual, instanceOf(AbsolutePathURLProvider.class));
}
}