You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2022/05/07 15:26:26 UTC
[shardingsphere] branch master updated: Refactor BootstrapArguments (#17429)
This is an automated email from the ASF dual-hosted git repository.
yx9o 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 7cd9fa3923f Refactor BootstrapArguments (#17429)
7cd9fa3923f is described below
commit 7cd9fa3923f71fa2dbde3b2a14cd32dcfe5ddfc2
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat May 7 23:26:20 2022 +0800
Refactor BootstrapArguments (#17429)
* Refactor ConfigurationPropertyKey
* Refactor BootstrapArguments
---
.../config/props/ConfigurationPropertyKey.java | 10 ++--
.../org/apache/shardingsphere/proxy/Bootstrap.java | 7 ++-
.../proxy/arguments/BootstrapArguments.java | 61 ++++++++--------------
.../proxy/arguments/BootstrapArgumentsTest.java | 57 ++++++++------------
4 files changed, 53 insertions(+), 82 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/props/ConfigurationPropertyKey.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/props/ConfigurationPropertyKey.java
index 5ef197f5c23..8366bac4aab 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/props/ConfigurationPropertyKey.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/props/ConfigurationPropertyKey.java
@@ -110,24 +110,24 @@ public enum ConfigurationPropertyKey implements TypedPropertyKey {
PROXY_FRONTEND_MAX_CONNECTIONS("proxy-frontend-max-connections", "0", int.class, false),
/**
- * Whether enable sql federation.
+ * Whether enable SQL federation.
*/
SQL_FEDERATION_ENABLED("sql-federation-enabled", String.valueOf(Boolean.FALSE), boolean.class, false),
/**
- * Proxy backend driver type. Default driver is JDBC.
+ * Proxy backend driver type..
*/
PROXY_BACKEND_DRIVER_TYPE("proxy-backend-driver-type", "JDBC", String.class, true),
/**
- * Proxy mysql default version, default 5.7.22.
+ * Proxy MySQL default version.
*/
PROXY_MYSQL_DEFAULT_VERSION("proxy-mysql-default-version", "5.7.22", String.class, false),
/**
- * Proxy default port, default 3307.
+ * Proxy default start port.
*/
- PROXY_DEFAULT_PORT("proxy-default-port", "3307", String.class, false);
+ PROXY_DEFAULT_PORT("proxy-default-port", "3307", int.class, true);
private final String key;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
index 8e9425db269..b28e82a1cf8 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.proxy;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.proxy.arguments.BootstrapArguments;
import org.apache.shardingsphere.proxy.backend.config.ProxyConfigurationLoader;
import org.apache.shardingsphere.proxy.backend.config.YamlProxyConfiguration;
@@ -44,7 +46,8 @@ public final class Bootstrap {
public static void main(final String[] args) throws IOException, SQLException {
BootstrapArguments bootstrapArgs = new BootstrapArguments(args);
YamlProxyConfiguration yamlConfig = ProxyConfigurationLoader.load(bootstrapArgs.getConfigurationPath());
- new BootstrapInitializer().init(yamlConfig, bootstrapArgs.getPort());
- new ShardingSphereProxy().start(bootstrapArgs.getPort());
+ int port = bootstrapArgs.getPort().orElseGet(() -> new ConfigurationProperties(yamlConfig.getServerConfiguration().getProps()).getValue(ConfigurationPropertyKey.PROXY_DEFAULT_PORT));
+ new BootstrapInitializer().init(yamlConfig, port);
+ new ShardingSphereProxy().start(port);
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
index 368a8f9dcc3..095029091ec 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
@@ -17,70 +17,53 @@
package org.apache.shardingsphere.proxy.arguments;
-import lombok.Getter;
-import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
-import org.apache.shardingsphere.proxy.backend.config.ProxyConfigurationLoader;
-import org.apache.shardingsphere.proxy.backend.config.YamlProxyConfiguration;
+import lombok.RequiredArgsConstructor;
-import java.io.IOException;
+import java.util.Optional;
/**
* Bootstrap arguments.
*/
-@Getter
+@RequiredArgsConstructor
public final class BootstrapArguments {
private static final String DEFAULT_CONFIG_PATH = "/conf/";
- private static final int DEFAULT_PORT = 3307;
+ private final String[] args;
- private final int port;
-
- private final String configurationPath;
-
- public BootstrapArguments(final String[] args) {
- port = getPort(args);
- configurationPath = getConfigurationPath(args);
- }
-
- private int getPort(final String[] args) {
+ /**
+ * Get port.
+ *
+ * @return port
+ */
+ public Optional<Integer> getPort() {
if (0 == args.length) {
- return getProxyDefaultPort(DEFAULT_CONFIG_PATH);
+ return Optional.empty();
}
try {
- return Integer.parseInt(args[0]);
+ return Optional.of(Integer.parseInt(args[0]));
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException(String.format("Invalid port `%s`.", args[0]));
}
}
- private String getConfigurationPath(final String[] args) {
+ /**
+ * Get configuration path.
+ *
+ * @return configuration path
+ */
+ public String getConfigurationPath() {
return args.length < 2 ? DEFAULT_CONFIG_PATH : paddingWithSlash(args[1]);
}
- private String paddingWithSlash(final String arg) {
- StringBuilder result = new StringBuilder(arg);
- if (!arg.startsWith("/")) {
+ private String paddingWithSlash(final String pathArg) {
+ StringBuilder result = new StringBuilder(pathArg);
+ if (!pathArg.startsWith("/")) {
result.insert(0, '/');
}
- if (!arg.endsWith("/")) {
+ if (!pathArg.endsWith("/")) {
result.append('/');
}
return result.toString();
}
-
- private static int getProxyDefaultPort(final String configurationPath) {
- YamlProxyConfiguration yamlConfig;
- try {
- yamlConfig = ProxyConfigurationLoader.load(configurationPath);
- return yamlConfig.getServerConfiguration().getProps().get(ConfigurationPropertyKey.PROXY_DEFAULT_PORT.getKey()) == null
- ? Integer.parseInt(ConfigurationPropertyKey.PROXY_DEFAULT_PORT.getDefaultValue())
- : Integer.parseInt(
- yamlConfig.getServerConfiguration().getProps().get(ConfigurationPropertyKey.PROXY_DEFAULT_PORT.getKey()).toString());
- } catch (IOException e) {
- return DEFAULT_PORT;
- } catch (NullPointerException e) {
- return DEFAULT_PORT;
- }
- }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
index aba52fe1d7f..368b7e20800 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
@@ -19,62 +19,47 @@ package org.apache.shardingsphere.proxy.arguments;
import org.junit.Test;
+import java.util.Optional;
+
import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
public final class BootstrapArgumentsTest {
@Test
- public void assertNewWithEmptyArgument() {
- BootstrapArguments actual = new BootstrapArguments(new String[]{});
- assertThat(actual.getPort(), is(3307));
- assertThat(actual.getConfigurationPath(), is("/conf/"));
+ public void assertGetPortWithEmptyArgument() {
+ assertFalse(new BootstrapArguments(new String[]{}).getPort().isPresent());
}
- @SuppressWarnings("ResultOfObjectAllocationIgnored")
@Test(expected = IllegalArgumentException.class)
- public void assertNewWithWrongArgument() {
- String wrongArgument = "WrongArgument";
- try {
- new BootstrapArguments(new String[]{wrongArgument});
- } catch (final IllegalArgumentException ex) {
- assertThat(ex.getMessage(), is(String.format("Invalid port `%s`.", wrongArgument)));
- throw ex;
- }
- }
-
- @Test
- public void assertNewWithOneArgumentOnly() {
- BootstrapArguments actual = new BootstrapArguments(new String[]{"3306"});
- assertThat(actual.getPort(), is(3306));
- assertThat(actual.getConfigurationPath(), is("/conf/"));
+ public void assertGetPortWithWrongArgument() {
+ new BootstrapArguments(new String[]{"WrongArgument"}).getPort();
}
@Test
- public void assertNewWithTwoArgumentsAndConfigurationPathWithoutSlash() {
- BootstrapArguments actual = new BootstrapArguments(new String[]{"3306", "test_conf"});
- assertThat(actual.getPort(), is(3306));
- assertThat(actual.getConfigurationPath(), is("/test_conf/"));
+ public void assertGetPortWithSingleArgument() {
+ Optional<Integer> actual = new BootstrapArguments(new String[]{"3306"}).getPort();
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is(3306));
}
@Test
- public void assertNewWithTwoArgumentsAndConfigurationPathWithLeftSlash() {
- BootstrapArguments actual = new BootstrapArguments(new String[]{"3306", "/test_conf"});
- assertThat(actual.getPort(), is(3306));
- assertThat(actual.getConfigurationPath(), is("/test_conf/"));
+ public void assertGetConfigurationPathWithEmptyArgument() {
+ assertThat(new BootstrapArguments(new String[]{}).getConfigurationPath(), is("/conf/"));
}
@Test
- public void assertNewWithTwoArgumentsAndConfigurationPathWithRightSlash() {
- BootstrapArguments actual = new BootstrapArguments(new String[]{"3306", "test_conf/"});
- assertThat(actual.getPort(), is(3306));
- assertThat(actual.getConfigurationPath(), is("/test_conf/"));
+ public void assertGetConfigurationPathWithSingleArgument() {
+ assertThat(new BootstrapArguments(new String[]{"3306"}).getConfigurationPath(), is("/conf/"));
}
@Test
- public void assertNewWithTwoArgumentsAndConfigurationPathWithBothSlash() {
- BootstrapArguments actual = new BootstrapArguments(new String[]{"3306", "/test_conf/"});
- assertThat(actual.getPort(), is(3306));
- assertThat(actual.getConfigurationPath(), is("/test_conf/"));
+ public void assertGetConfigurationPathWithTwoArguments() {
+ assertThat(new BootstrapArguments(new String[]{"3306", "test_conf"}).getConfigurationPath(), is("/test_conf/"));
+ assertThat(new BootstrapArguments(new String[]{"3306", "/test_conf"}).getConfigurationPath(), is("/test_conf/"));
+ assertThat(new BootstrapArguments(new String[]{"3306", "test_conf/"}).getConfigurationPath(), is("/test_conf/"));
+ assertThat(new BootstrapArguments(new String[]{"3306", "/test_conf/"}).getConfigurationPath(), is("/test_conf/"));
}
}