-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ClasspathURLProvider implementation that reads system properties (#…
…29995) * Add ClasspathURLProvider implementation that reads system properties * Small refactoring
- Loading branch information
1 parent
bd8fbed
commit 68c07e1
Showing
14 changed files
with
402 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...dingsphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithSystemPropsURLProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath; | ||
|
||
import com.google.common.base.Strings; | ||
import lombok.SneakyThrows; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Classpath with system properties URL provider. | ||
*/ | ||
public final class ClasspathWithSystemPropsURLProvider extends AbstractClasspathURLProvider { | ||
|
||
private static final String PATH_TYPE = "classpath-system-props:"; | ||
|
||
private static final String KEY_VALUE_SEPARATOR = "::"; | ||
|
||
private static final Pattern PATTERN = Pattern.compile("\\$\\$\\{(.+::.*)}$"); | ||
|
||
@Override | ||
public boolean accept(final String url) { | ||
return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE); | ||
} | ||
|
||
@Override | ||
@SneakyThrows(IOException.class) | ||
public byte[] getContent(final String url, final String urlPrefix) { | ||
String file = getConfigurationFile(url, urlPrefix, PATH_TYPE); | ||
try ( | ||
InputStream stream = getResourceAsStream(file); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { | ||
StringBuilder builder = new StringBuilder(); | ||
String line; | ||
while (null != (line = reader.readLine())) { | ||
if (!line.startsWith("#")) { | ||
line = replaceSystemProperties(line); | ||
builder.append(line).append(System.lineSeparator()); | ||
} | ||
} | ||
return builder.toString().getBytes(StandardCharsets.UTF_8); | ||
} | ||
} | ||
|
||
private String replaceSystemProperties(final String line) { | ||
Matcher matcher = PATTERN.matcher(line); | ||
if (!matcher.find()) { | ||
return line; | ||
} | ||
String[] systemPropNameAndDefaultValue = matcher.group(1).split(KEY_VALUE_SEPARATOR, 2); | ||
String systemPropName = systemPropNameAndDefaultValue[0]; | ||
String systemPropValue = System.getProperty(systemPropName, systemPropNameAndDefaultValue[1]); | ||
if (Strings.isNullOrEmpty(systemPropValue)) { | ||
String modifiedLineWithSpace = matcher.replaceAll(""); | ||
return modifiedLineWithSpace.substring(0, modifiedLineWithSpace.length() - 1); | ||
} | ||
return matcher.replaceAll(systemPropValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...sphere/driver/jdbc/core/driver/spi/classpath/ClasspathWithSystemPropsURLProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shardingsphere.driver.jdbc.core.driver.spi.classpath; | ||
|
||
import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLManager; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class ClasspathWithSystemPropsURLProviderTest { | ||
|
||
private static final String FIXTURE_JDBC_URL_KEY = "fixture.config.driver.jdbc-url"; | ||
|
||
private static final String FIXTURE_USERNAME_KEY = "fixture.config.driver.username"; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
assertThat(System.getProperty(FIXTURE_JDBC_URL_KEY), Matchers.is(nullValue())); | ||
assertThat(System.getProperty(FIXTURE_USERNAME_KEY), Matchers.is(nullValue())); | ||
System.setProperty(FIXTURE_JDBC_URL_KEY, "jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"); | ||
System.setProperty(FIXTURE_USERNAME_KEY, "sa"); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
System.clearProperty(FIXTURE_JDBC_URL_KEY); | ||
System.clearProperty(FIXTURE_USERNAME_KEY); | ||
} | ||
|
||
@Test | ||
void assertReplaceEnvironmentVariables() { | ||
final String urlPrefix = "jdbc:shardingsphere:"; | ||
ClasspathWithSystemPropsURLProvider urlProvider = new ClasspathWithSystemPropsURLProvider(); | ||
byte[] actual = urlProvider.getContent("jdbc:shardingsphere:classpath-system-props:config/driver/foo-driver-system-properties-fixture.yaml", urlPrefix); | ||
byte[] actualOrigin = ShardingSphereURLManager.getContent("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml", urlPrefix); | ||
assertThat(actual, is(actualOrigin)); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
jdbc/core/src/test/resources/config/driver/foo-driver-system-properties-fixture.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# After `ShardingSphereURLManager.getContent`, this file should be equivalent to `foo-driver-fixture.yaml` in the same folder. | ||
databaseName: foo_driver_fixture_db | ||
|
||
dataSources: | ||
ds_0: | ||
dataSourceClassName: com.zaxxer.hikari.HikariDataSource | ||
driverClassName: org.h2.Driver | ||
jdbcUrl: jdbc:h2:mem:foo_ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL | ||
username: sa | ||
password: | ||
ds_1: | ||
dataSourceClassName: com.zaxxer.hikari.HikariDataSource | ||
driverClassName: $${fixture.config.driver.driver-class-name::org.h2.Driver} | ||
jdbcUrl: $${fixture.config.driver.jdbc-url::jdbc:h2:mem:foo_ds_do_not_use} | ||
username: $${fixture.config.driver.username::} | ||
password: $${fixture.config.driver.password::} | ||
|
||
rules: | ||
- !SHARDING | ||
autoTables: | ||
t_order: | ||
actualDataSources: ds_0,ds_1 | ||
shardingStrategy: | ||
standard: | ||
shardingColumn: order_id | ||
shardingAlgorithmName: auto_mod | ||
keyGenerateStrategy: | ||
column: user_id | ||
keyGeneratorName: snowflake | ||
shardingAlgorithms: | ||
auto_mod: | ||
type: HASH_MOD | ||
props: | ||
sharding-count: 2 | ||
|
||
keyGenerators: | ||
snowflake: | ||
type: SNOWFLAKE | ||
|
||
props: | ||
sql-show: true |
Oops, something went wrong.