Skip to content

Commit

Permalink
Refactor ShardingSphereURLManager (#30126)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Feb 14, 2024
1 parent 9a7eeba commit e8203c7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public DataSource get(final String url, final String urlPrefix) {
@SuppressWarnings("unchecked")
private <T extends Throwable> DataSource createDataSource(final String url, final String urlPrefix) throws T {
try {
return YamlShardingSphereDataSourceFactory.createDataSource(ShardingSphereURLManager.getContent(url, urlPrefix));
return YamlShardingSphereDataSourceFactory.createDataSource(ShardingSphereURLManager.getContent(url.substring(urlPrefix.length())));
} catch (final IOException ex) {
throw (T) new SQLException(ex);
} catch (final SQLException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ public final class ShardingSphereURL {
* Parse ShardingSphere URL.
*
* @param url ShardingSphere URL
* @param urlPrefix URL prefix
* @param sourceType source type
* @return ShardingSphere URL
*/
public static ShardingSphereURL parse(final String url, final String urlPrefix, final String sourceType) {
return new ShardingSphereURL(parseConfigurationSubject(url, urlPrefix, sourceType), parseParameters(url));
public static ShardingSphereURL parse(final String url, final String sourceType) {
return new ShardingSphereURL(parseConfigurationSubject(url, sourceType), parseParameters(url));
}

private static String parseConfigurationSubject(final String url, final String urlPrefix, final String configurationType) {
String configuredSubject = url.substring(urlPrefix.length(), url.contains("?") ? url.indexOf('?') : url.length());
private static String parseConfigurationSubject(final String url, final String configurationType) {
String configuredSubject = url.substring(0, url.contains("?") ? url.indexOf('?') : url.length());
String result = configuredSubject.substring(configurationType.length());
Preconditions.checkArgument(!result.isEmpty(), "Configuration subject is required in driver URL.");
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ public final class ShardingSphereURLManager {
* Get configuration content from URL.
*
* @param url driver URL
* @param urlPrefix URL prefix
* @return configuration content
* @throws URLProviderNotFoundException driver URL provider not found exception
*/
public static byte[] getContent(final String url, final String urlPrefix) {
public static byte[] getContent(final String url) {
ShardingSpherePreconditions.checkNotNull(url, () -> new URLProviderNotFoundException(url));
for (ShardingSphereURLProvider each : ShardingSphereServiceLoader.getServiceInstances(ShardingSphereURLProvider.class)) {
if (url.contains(each.getSourceType())) {
return each.getContent(ShardingSphereURL.parse(url, urlPrefix, each.getSourceType()));
return each.getContent(ShardingSphereURL.parse(url, each.getSourceType()));
}
}
throw new URLProviderNotFoundException(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public byte[] getContent(final ShardingSphereURL url) {
}
}

private static InputStream getResourceAsStreamFromClasspath(final String resource) {
private InputStream getResourceAsStreamFromClasspath(final String resource) {
InputStream result = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
result = null == result ? Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + resource) : result;
Preconditions.checkNotNull(result, "Can not find configuration file `%s`.", resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,38 @@ class ShardingSphereURLManagerTest {

private final int fooDriverConfigLengthOnWindows = 1040;

private final String urlPrefix = "jdbc:shardingsphere:";

@Test
void assertNewConstructorWithEmptyURL() {
assertThrows(URLProviderNotFoundException.class, () -> ShardingSphereURLManager.getContent("jdbc:shardingsphere:", urlPrefix));
assertThrows(URLProviderNotFoundException.class, () -> ShardingSphereURLManager.getContent("invalid:xxx"));
}

@Test
@EnabledOnOs({OS.LINUX, OS.MAC})
void assertToClasspathConfigurationFile() {
byte[] actual = ShardingSphereURLManager.getContent("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml", urlPrefix);
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("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml", urlPrefix);
byte[] actual = ShardingSphereURLManager.getContent("classpath:config/driver/foo-driver-fixture.yaml");
assertThat(actual.length, is(fooDriverConfigLengthOnWindows));
}

@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("jdbc:shardingsphere:absolutepath:" + absolutePath, urlPrefix);
byte[] actual = ShardingSphereURLManager.getContent("absolutepath:" + absolutePath);
assertThat(actual.length, is(fooDriverConfigLengthOnUnix));
}

@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("jdbc:shardingsphere:absolutepath:" + absolutePath, urlPrefix);
byte[] actual = ShardingSphereURLManager.getContent("absolutepath:" + absolutePath);
assertThat(actual.length, is(fooDriverConfigLengthOnWindows));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AbsolutePathURLProviderTest {
void assertGetContent() {
String path = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/driver/foo-driver-fixture.yaml")).getPath();
byte[] actual = new AbsolutePathURLProvider().getContent(mockURL(path));
byte[] expected = ShardingSphereURLManager.getContent("jdbc:shardingsphere:absolutepath:" + path, "jdbc:shardingsphere:");
byte[] expected = ShardingSphereURLManager.getContent("absolutepath:" + path);
assertThat(actual, is(expected));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ClasspathURLProviderTest {
@Test
void assertGetContent() {
byte[] actual = new ClasspathURLProvider().getContent(mockURL());
byte[] expected = ShardingSphereURLManager.getContent("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml", "jdbc:shardingsphere:");
byte[] expected = ShardingSphereURLManager.getContent("classpath:config/driver/foo-driver-fixture.yaml");
assertThat(actual, is(expected));
}

Expand Down

0 comments on commit e8203c7

Please sign in to comment.