Skip to content

Commit

Permalink
move YamlProxyConfiguration.unmarshal => Bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jul 12, 2018
1 parent 0068c0d commit dc40f6f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
30 changes: 18 additions & 12 deletions sharding-proxy/src/main/java/io/shardingsphere/proxy/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

package io.shardingsphere.proxy;

import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.proxy.config.RuleRegistry;
import io.shardingsphere.proxy.frontend.ShardingProxy;
import io.shardingsphere.proxy.yaml.YamlProxyConfiguration;
import io.shardingsphere.transaction.xa.XaTransactionListener;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.io.InputStreamReader;

/**
* Sharding-Proxy Bootstrap.
Expand All @@ -46,25 +48,29 @@ public final class Bootstrap {
*
* @param args startup arguments
* @throws InterruptedException interrupted exception
* @throws MalformedURLException URL exception
* @throws IOException IO exception
*/
public static void main(final String[] args) throws InterruptedException, MalformedURLException {
public static void main(final String[] args) throws InterruptedException, IOException {
initializeRuleRegistry();
EventBusInstance.getInstance().register(new XaTransactionListener());
new ShardingProxy().start(getPort(args));
}

private static void initializeRuleRegistry() {
YamlProxyConfiguration config;
try {
config = YamlProxyConfiguration.unmarshal(new File(Bootstrap.class.getResource(CONFIG_YAML).getFile()));
config.init();
} catch (final IOException ex) {
throw new ShardingException(ex);
}
private static void initializeRuleRegistry() throws IOException {
YamlProxyConfiguration config = unmarshal(new File(Bootstrap.class.getResource(CONFIG_YAML).getFile()));
config.init();
RuleRegistry.getInstance().init(config);
}

private static YamlProxyConfiguration unmarshal(final File yamlFile) throws IOException {
try (
FileInputStream fileInputStream = new FileInputStream(yamlFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
) {
return new Yaml(new Constructor(YamlProxyConfiguration.class)).loadAs(inputStreamReader, YamlProxyConfiguration.class);
}
}

private static int getPort(final String[] args) {
if (0 == args.length) {
return DEFAULT_PORT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@
import io.shardingsphere.jdbc.orchestration.internal.eventbus.ProxyEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.ProxyEventBusInstance;
import io.shardingsphere.proxy.config.RuleRegistry;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Collections;

Expand All @@ -46,22 +40,6 @@
*/
public final class YamlProxyConfiguration extends OrchestrationProxyConfiguration {

/**
* Unmarshal yaml sharding configuration from yaml file.
*
* @param yamlFile yaml file
* @return yaml sharding configuration
* @throws IOException IO Exception
*/
public static YamlProxyConfiguration unmarshal(final File yamlFile) throws IOException {
try (
FileInputStream fileInputStream = new FileInputStream(yamlFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
) {
return new Yaml(new Constructor(YamlProxyConfiguration.class)).loadAs(inputStreamReader, YamlProxyConfiguration.class);
}
}

/**
* Initialize yaml proxy configuration.
*/
Expand Down

0 comments on commit dc40f6f

Please sign in to comment.