未验证 提交 abacab79 编写于 作者: L Liang Zhang 提交者: GitHub

Refactor TypeBasedSPI (#5026)

* remove EncryptorServiceLoader

* remove MasterSlaveLoadBalanceAlgorithmServiceLoader

* remove TypedSPIFixtureServiceLoader

* remove RegistryCenterRepositoryServiceLoader

* remove ConfigCenterRepositoryServiceLoader

* remove ConfigCenterRepositoryServiceLoader

* remove KeyGenerateAlgorithmServiceLoader

* remove TypedSPIServiceLoader
上级 a8340ff1
......@@ -27,7 +27,8 @@ import org.apache.shardingsphere.encrypt.api.EncryptorRuleConfiguration;
import org.apache.shardingsphere.encrypt.strategy.EncryptTable;
import org.apache.shardingsphere.encrypt.strategy.spi.Encryptor;
import org.apache.shardingsphere.encrypt.strategy.spi.QueryAssistedEncryptor;
import org.apache.shardingsphere.encrypt.strategy.spi.loader.EncryptorServiceLoader;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import java.util.Collection;
......@@ -45,6 +46,10 @@ import java.util.stream.Collectors;
*/
public final class EncryptRule implements BaseRule {
static {
ShardingSphereServiceLoader.register(Encryptor.class);
}
private final Map<String, Encryptor> encryptors = new LinkedHashMap<>();
private final Map<String, EncryptTable> tables = new LinkedHashMap<>();
......@@ -83,14 +88,11 @@ public final class EncryptRule implements BaseRule {
}
private void initEncryptors(final Map<String, EncryptorRuleConfiguration> encryptors) {
EncryptorServiceLoader serviceLoader = new EncryptorServiceLoader();
for (Entry<String, EncryptorRuleConfiguration> entry : encryptors.entrySet()) {
this.encryptors.put(entry.getKey(), createEncryptor(serviceLoader, entry.getValue()));
}
encryptors.forEach((key, value) -> this.encryptors.put(key, createEncryptor(value)));
}
private Encryptor createEncryptor(final EncryptorServiceLoader serviceLoader, final EncryptorRuleConfiguration encryptorRuleConfig) {
Encryptor result = serviceLoader.newService(encryptorRuleConfig.getType(), encryptorRuleConfig.getProperties());
private Encryptor createEncryptor(final EncryptorRuleConfiguration encryptorRuleConfig) {
Encryptor result = TypedSPIRegistry.getRegisteredService(Encryptor.class, encryptorRuleConfig.getType(), encryptorRuleConfig.getProperties());
result.init();
return result;
}
......
/*
* 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.encrypt.strategy.spi.loader;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.TypedSPIServiceLoader;
import org.apache.shardingsphere.encrypt.strategy.spi.Encryptor;
/**
* Encryptor service loader.
*/
public final class EncryptorServiceLoader extends TypedSPIServiceLoader<Encryptor> {
static {
ShardingSphereServiceLoader.register(Encryptor.class);
}
public EncryptorServiceLoader() {
super(Encryptor.class);
}
}
/*
* 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.encrypt.strategy.spi.loader;
import org.apache.shardingsphere.encrypt.strategy.fixture.TestEncryptor;
import org.apache.shardingsphere.encrypt.strategy.impl.AESEncryptor;
import org.apache.shardingsphere.encrypt.strategy.impl.MD5Encryptor;
import org.junit.Test;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
public final class EncryptorServiceLoaderTest {
private EncryptorServiceLoader serviceLoader = new EncryptorServiceLoader();
@Test
public void assertNewMD5Encryptor() {
assertThat(serviceLoader.newService("MD5", new Properties()), instanceOf(MD5Encryptor.class));
}
@Test
public void assertNewAESEncryptor() {
assertThat(serviceLoader.newService("AES", new Properties()), instanceOf(AESEncryptor.class));
}
@Test
public void assertNewDefaultEncryptor() {
assertThat(serviceLoader.newService(), instanceOf(TestEncryptor.class));
}
}
......@@ -20,8 +20,9 @@ package org.apache.shardingsphere.core.rule;
import lombok.Getter;
import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.spi.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmServiceLoader;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.masterslave.MasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import java.util.List;
......@@ -32,6 +33,10 @@ import java.util.List;
@Getter
public class MasterSlaveRule implements BaseRule {
static {
ShardingSphereServiceLoader.register(MasterSlaveLoadBalanceAlgorithm.class);
}
private final String name;
private final String masterDataSourceName;
......@@ -46,7 +51,7 @@ public class MasterSlaveRule implements BaseRule {
this.name = name;
this.masterDataSourceName = masterDataSourceName;
this.slaveDataSourceNames = slaveDataSourceNames;
this.loadBalanceAlgorithm = null == loadBalanceAlgorithm ? new MasterSlaveLoadBalanceAlgorithmServiceLoader().newService() : loadBalanceAlgorithm;
this.loadBalanceAlgorithm = null == loadBalanceAlgorithm ? TypedSPIRegistry.getRegisteredService(MasterSlaveLoadBalanceAlgorithm.class) : loadBalanceAlgorithm;
ruleConfiguration = new MasterSlaveRuleConfiguration(name, masterDataSourceName, slaveDataSourceNames,
new LoadBalanceStrategyConfiguration(this.loadBalanceAlgorithm.getType(), this.loadBalanceAlgorithm.getProperties()));
}
......@@ -60,9 +65,8 @@ public class MasterSlaveRule implements BaseRule {
}
private MasterSlaveLoadBalanceAlgorithm createMasterSlaveLoadBalanceAlgorithm(final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration) {
MasterSlaveLoadBalanceAlgorithmServiceLoader serviceLoader = new MasterSlaveLoadBalanceAlgorithmServiceLoader();
return null == loadBalanceStrategyConfiguration
? serviceLoader.newService() : serviceLoader.newService(loadBalanceStrategyConfiguration.getType(), loadBalanceStrategyConfiguration.getProperties());
return null == loadBalanceStrategyConfiguration ? TypedSPIRegistry.getRegisteredService(MasterSlaveLoadBalanceAlgorithm.class)
: TypedSPIRegistry.getRegisteredService(MasterSlaveLoadBalanceAlgorithm.class, loadBalanceStrategyConfiguration.getType(), loadBalanceStrategyConfiguration.getProperties());
}
/**
......
......@@ -30,8 +30,9 @@ import org.apache.shardingsphere.core.strategy.route.ShardingStrategyFactory;
import org.apache.shardingsphere.core.strategy.route.none.NoneShardingStrategy;
import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.spi.algorithm.keygen.KeyGenerateAlgorithmServiceLoader;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import org.apache.shardingsphere.underlying.common.rule.DataNode;
......@@ -51,6 +52,10 @@ import java.util.stream.Collectors;
@Getter
public class ShardingRule implements BaseRule {
static {
ShardingSphereServiceLoader.register(KeyGenerateAlgorithm.class);
}
private final ShardingRuleConfiguration ruleConfiguration;
private final ShardingDataSourceNames shardingDataSourceNames;
......@@ -109,8 +114,7 @@ public class ShardingRule implements BaseRule {
}
private KeyGenerateAlgorithm createDefaultKeyGenerateAlgorithm(final KeyGeneratorConfiguration keyGeneratorConfiguration) {
KeyGenerateAlgorithmServiceLoader serviceLoader = new KeyGenerateAlgorithmServiceLoader();
return containsKeyGenerateAlgorithm(keyGeneratorConfiguration) ? keyGeneratorConfiguration.getKeyGenerateAlgorithm() : serviceLoader.newService();
return containsKeyGenerateAlgorithm(keyGeneratorConfiguration) ? keyGeneratorConfiguration.getKeyGenerateAlgorithm() : TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class);
}
private boolean containsKeyGenerateAlgorithm(final KeyGeneratorConfiguration keyGeneratorConfiguration) {
......
......@@ -19,8 +19,9 @@ package org.apache.shardingsphere.core.yaml.swapper;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.core.yaml.config.sharding.YamlKeyGeneratorConfiguration;
import org.apache.shardingsphere.spi.algorithm.keygen.KeyGenerateAlgorithmServiceLoader;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.yaml.swapper.YamlSwapper;
/**
......@@ -28,6 +29,10 @@ import org.apache.shardingsphere.underlying.common.yaml.swapper.YamlSwapper;
*/
public final class KeyGeneratorConfigurationYamlSwapper implements YamlSwapper<YamlKeyGeneratorConfiguration, KeyGeneratorConfiguration> {
static {
ShardingSphereServiceLoader.register(KeyGenerateAlgorithm.class);
}
@Override
public YamlKeyGeneratorConfiguration swap(final KeyGeneratorConfiguration data) {
YamlKeyGeneratorConfiguration result = new YamlKeyGeneratorConfiguration();
......@@ -39,7 +44,7 @@ public final class KeyGeneratorConfigurationYamlSwapper implements YamlSwapper<Y
@Override
public KeyGeneratorConfiguration swap(final YamlKeyGeneratorConfiguration yamlConfiguration) {
KeyGenerateAlgorithm keyGenerateAlgorithm = new KeyGenerateAlgorithmServiceLoader().newService(yamlConfiguration.getType(), yamlConfiguration.getProps());
KeyGenerateAlgorithm keyGenerateAlgorithm = TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class, yamlConfiguration.getType(), yamlConfiguration.getProps());
return new KeyGeneratorConfiguration(yamlConfiguration.getColumn(), keyGenerateAlgorithm);
}
}
/*
* 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.spi.algorithm.keygen;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIServiceLoader;
/**
* Key generate algorithm service loader.
*/
public final class KeyGenerateAlgorithmServiceLoader extends TypedSPIServiceLoader<KeyGenerateAlgorithm> {
static {
ShardingSphereServiceLoader.register(KeyGenerateAlgorithm.class);
}
public KeyGenerateAlgorithmServiceLoader() {
super(KeyGenerateAlgorithm.class);
}
}
/*
* 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.spi.algorithm.masterslave;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.TypedSPIServiceLoader;
import org.apache.shardingsphere.spi.masterslave.MasterSlaveLoadBalanceAlgorithm;
/**
* Master-slave database load-balance algorithm service loader.
*/
public final class MasterSlaveLoadBalanceAlgorithmServiceLoader extends TypedSPIServiceLoader<MasterSlaveLoadBalanceAlgorithm> {
static {
ShardingSphereServiceLoader.register(MasterSlaveLoadBalanceAlgorithm.class);
}
public MasterSlaveLoadBalanceAlgorithmServiceLoader() {
super(MasterSlaveLoadBalanceAlgorithm.class);
}
}
......@@ -17,18 +17,6 @@
package org.apache.shardingsphere.core.rule;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
......@@ -42,12 +30,25 @@ import org.apache.shardingsphere.core.strategy.keygen.fixture.IncrementKeyGenera
import org.apache.shardingsphere.core.strategy.route.ShardingStrategy;
import org.apache.shardingsphere.core.strategy.route.inline.InlineShardingStrategy;
import org.apache.shardingsphere.core.strategy.route.none.NoneShardingStrategy;
import org.apache.shardingsphere.spi.algorithm.keygen.KeyGenerateAlgorithmServiceLoader;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.underlying.common.rule.DataNode;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class ShardingRuleTest {
@Test(expected = IllegalArgumentException.class)
......@@ -398,7 +399,7 @@ public final class ShardingRuleTest {
ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
shardingRuleConfiguration.setDefaultDataSourceName("ds_0");
TableRuleConfiguration tableRuleConfiguration = createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");
KeyGenerateAlgorithm keyGenerateAlgorithm = new KeyGenerateAlgorithmServiceLoader().newService("INCREMENT", new Properties());
KeyGenerateAlgorithm keyGenerateAlgorithm = TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class, "INCREMENT", new Properties());
tableRuleConfiguration.setKeyGeneratorConfig(new KeyGeneratorConfiguration("id", keyGenerateAlgorithm));
TableRuleConfiguration subTableRuleConfiguration = createTableRuleConfiguration("SUB_LOGIC_TABLE", "ds_${0..1}.sub_table_${0..2}");
shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);
......@@ -407,7 +408,7 @@ public final class ShardingRuleTest {
shardingRuleConfiguration.getBroadcastTables().add("BROADCAST_TABLE");
shardingRuleConfiguration.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("ds_id", "ds_%{ds_id % 2}"));
shardingRuleConfiguration.setDefaultTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("table_id", "table_%{table_id % 2}"));
KeyGenerateAlgorithm defaultKeyGenerateAlgorithm = new KeyGenerateAlgorithmServiceLoader().newService("INCREMENT", new Properties());
KeyGenerateAlgorithm defaultKeyGenerateAlgorithm = TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class, "INCREMENT", new Properties());
shardingRuleConfiguration.setDefaultKeyGeneratorConfig(new KeyGeneratorConfiguration("id", defaultKeyGenerateAlgorithm));
return new ShardingRule(shardingRuleConfiguration, createDataSourceNames());
}
......
......@@ -17,30 +17,31 @@
package org.apache.shardingsphere.core.rule;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.Sets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.NoneShardingStrategyConfiguration;
import org.apache.shardingsphere.core.strategy.keygen.fixture.IncrementKeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.algorithm.keygen.KeyGenerateAlgorithmServiceLoader;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.underlying.common.rule.DataNode;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class TableRuleTest {
@Test
......@@ -60,7 +61,7 @@ public final class TableRuleTest {
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}");
tableRuleConfig.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
tableRuleConfig.setTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
KeyGenerateAlgorithm keyGenerateAlgorithm = new KeyGenerateAlgorithmServiceLoader().newService("INCREMENT", new Properties());
KeyGenerateAlgorithm keyGenerateAlgorithm = TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class, "INCREMENT", new Properties());
tableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("col_1", keyGenerateAlgorithm));
TableRule actual = new TableRule(tableRuleConfig, createShardingDataSourceNames(), null);
assertThat(actual.getLogicTable(), is("logic_table"));
......
/*
* 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.core.strategy.keygen;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import java.util.Properties;
import org.apache.shardingsphere.spi.algorithm.keygen.KeyGenerateAlgorithmServiceLoader;
import org.junit.Test;
public final class KeyGenerateAlgorithmServiceLoaderTest {
private KeyGenerateAlgorithmServiceLoader serviceLoader = new KeyGenerateAlgorithmServiceLoader();
@Test
public void assertNewSnowflakeKeyGenerateAlgorithm() {
assertThat(serviceLoader.newService("SNOWFLAKE", new Properties()), instanceOf(SnowflakeKeyGenerateAlgorithm.class));
}
@Test
public void assertNewUUIDKeyGenerateAlgorithm() {
assertThat(serviceLoader.newService("UUID", new Properties()), instanceOf(UUIDKeyGenerateAlgorithm.class));
}
@Test
public void assertNewDefaultKeyGenerateAlgorithm() {
assertThat(serviceLoader.newService(), instanceOf(SnowflakeKeyGenerateAlgorithm.class));
}
}
/*
* 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.core.strategy.masterslave;
import org.apache.shardingsphere.spi.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmServiceLoader;
import org.junit.Test;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
public final class MasterSlaveLoadBalanceAlgorithmServiceLoaderTest {
private MasterSlaveLoadBalanceAlgorithmServiceLoader serviceLoader = new MasterSlaveLoadBalanceAlgorithmServiceLoader();
@Test
public void assertNewRoundRobinMasterSlaveLoadBalanceAlgorithm() {
assertThat(serviceLoader.newService("ROUND_ROBIN", new Properties()), instanceOf(RoundRobinMasterSlaveLoadBalanceAlgorithm.class));
}
@Test
public void assertNewRandomMasterSlaveLoadBalanceAlgorithm() {
assertThat(serviceLoader.newService("RANDOM", new Properties()), instanceOf(RandomMasterSlaveLoadBalanceAlgorithm.class));
}
@Test
public void assertNewDefaultMasterSlaveLoadBalanceAlgorithm() {
assertThat(serviceLoader.newService(), instanceOf(RoundRobinMasterSlaveLoadBalanceAlgorithm.class));
}
}
......@@ -17,21 +17,22 @@
package org.apache.shardingsphere.core.yaml.swapper;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Properties;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.core.yaml.config.sharding.YamlKeyGeneratorConfiguration;
import org.apache.shardingsphere.spi.algorithm.keygen.KeyGenerateAlgorithmServiceLoader;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.junit.Test;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class KeyGeneratorConfigurationYamlSwapperTest {
@Test
public void assertSwapToYaml() {
KeyGenerateAlgorithm keyGenerateAlgorithm = new KeyGenerateAlgorithmServiceLoader().newService("UUID", new Properties());
KeyGenerateAlgorithm keyGenerateAlgorithm = TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class, "UUID", new Properties());
YamlKeyGeneratorConfiguration actual = new KeyGeneratorConfigurationYamlSwapper().swap(new KeyGeneratorConfiguration("id", keyGenerateAlgorithm));
assertThat(actual.getType(), is("UUID"));
assertThat(actual.getColumn(), is("id"));
......
/*
* 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.orchestration.core.configcenter;
import com.google.common.base.Preconditions;
import org.apache.shardingsphere.orchestration.center.ConfigCenterRepository;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.TypedSPIServiceLoader;
/**
* Config center repository loader from SPI.
*/
public final class ConfigCenterRepositoryServiceLoader extends TypedSPIServiceLoader<ConfigCenterRepository> {
static {
ShardingSphereServiceLoader.register(ConfigCenterRepository.class);
}
public ConfigCenterRepositoryServiceLoader() {
super(ConfigCenterRepository.class);
}
/**
* Load config center repository from SPI.
*
* @param config configuration for config center
* @return config center repository
*/
public ConfigCenterRepository load(final CenterConfiguration config) {
Preconditions.checkNotNull(config, "Config center configuration cannot be null.");
ConfigCenterRepository result = newService(config.getType(), config.getProperties());
result.init(config);
return result;
}
}
/*
* 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.orchestration.core.configcenter;
import org.apache.shardingsphere.orchestration.center.ConfigCenterRepository;
import org.apache.shardingsphere.orchestration.core.configcenter.fixture.FirstTestConfigCenterRepository;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
public final class ConfigCenterRepositoryServiceLoaderTest {
@Test
public void assertLoad() {
ConfigCenterRepository configCenterRepository = new ConfigCenterRepositoryServiceLoader().load(new CenterConfiguration("FirstTestConfigCenter"));
assertThat(configCenterRepository, instanceOf(FirstTestConfigCenterRepository.class));
}
}
......@@ -24,18 +24,18 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.core.rule.Authentication;
import org.apache.shardingsphere.orchestration.center.ConfigCenterRepository;
import org.apache.shardingsphere.orchestration.center.RegistryCenterRepository;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.core.common.CenterType;
import org.apache.shardingsphere.orchestration.core.configcenter.ConfigCenter;
import org.apache.shardingsphere.orchestration.core.configcenter.ConfigCenterRepositoryServiceLoader;
import org.apache.shardingsphere.orchestration.core.facade.listener.ShardingOrchestrationListenerManager;
import org.apache.shardingsphere.orchestration.core.facade.properties.OrchestrationProperties;
import org.apache.shardingsphere.orchestration.core.facade.properties.OrchestrationPropertyKey;
import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenter;
import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenterRepositoryServiceLoader;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.config.DataSourceConfiguration;
import org.apache.shardingsphere.underlying.common.config.RuleConfiguration;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import java.util.Collection;
import java.util.Map;
......@@ -48,37 +48,46 @@ import java.util.Properties;
*/
@Slf4j
public final class ShardingOrchestrationFacade implements AutoCloseable {
private final RegistryCenterRepository registryCenterRepository;
static {
ShardingSphereServiceLoader.register(ConfigCenterRepository.class);
ShardingSphereServiceLoader.register(RegistryCenterRepository.class);
}
private final ConfigCenterRepository configCenterRepository;
private final RegistryCenterRepository registryCenterRepository;
private final boolean isOverwrite;
@Getter
private final ConfigCenter configCenter;
private final RegistryCenter registryCenter;
private final ShardingOrchestrationListenerManager listenerManager;
public ShardingOrchestrationFacade(final OrchestrationConfiguration orchestrationConfig, final Collection<String> shardingSchemaNames) {
Optional<String> registryCenterName = getInstanceNameByOrchestrationType(orchestrationConfig.getInstanceConfigurationMap(), CenterType.REGISTRY_CENTER.getValue());
Preconditions.checkArgument(registryCenterName.isPresent(), "Can not find instance configuration with registry center orchestration type.");
CenterConfiguration registryCenterConfiguration = orchestrationConfig.getInstanceConfigurationMap().get(registryCenterName.get());
registryCenterRepository = new RegistryCenterRepositoryServiceLoader().load(registryCenterConfiguration);
registryCenter = new RegistryCenter(registryCenterName.get(), registryCenterRepository);
Optional<String> configCenterName = getInstanceNameByOrchestrationType(orchestrationConfig.getInstanceConfigurationMap(), CenterType.CONFIG_CENTER.getValue());
Preconditions.checkArgument(configCenterName.isPresent(), "Can not find instance configuration with config center orchestration type.");
CenterConfiguration configCenterConfiguration = orchestrationConfig.getInstanceConfigurationMap().get(configCenterName.get());
configCenterRepository = new ConfigCenterRepositoryServiceLoader().load(configCenterConfiguration);
Preconditions.checkNotNull(configCenterConfiguration, "Config center configuration cannot be null.");
configCenterRepository = TypedSPIRegistry.getRegisteredService(
ConfigCenterRepository.class, configCenterConfiguration.getType(), configCenterConfiguration.getProperties());
configCenterRepository.init(configCenterConfiguration);
isOverwrite = new OrchestrationProperties(configCenterConfiguration.getProperties()).getValue(OrchestrationPropertyKey.OVERWRITE);
configCenter = new ConfigCenter(configCenterName.get(), configCenterRepository);
Optional<String> registryCenterName = getInstanceNameByOrchestrationType(orchestrationConfig.getInstanceConfigurationMap(), CenterType.REGISTRY_CENTER.getValue());
Preconditions.checkArgument(registryCenterName.isPresent(), "Can not find instance configuration with registry center orchestration type.");
CenterConfiguration registryCenterConfiguration = orchestrationConfig.getInstanceConfigurationMap().get(registryCenterName.get());
Preconditions.checkNotNull(registryCenterConfiguration, "Registry center configuration cannot be null.");
registryCenterRepository = TypedSPIRegistry.getRegisteredService(RegistryCenterRepository.class, registryCenterConfiguration.getType(), registryCenterConfiguration.getProperties());
registryCenterRepository.init(registryCenterConfiguration);
registryCenter = new RegistryCenter(registryCenterName.get(), registryCenterRepository);
listenerManager = shardingSchemaNames.isEmpty()
? new ShardingOrchestrationListenerManager(registryCenterName.get(), registryCenterRepository,
configCenterName.get(), configCenterRepository, configCenter.getAllShardingSchemaNames())
: new ShardingOrchestrationListenerManager(registryCenterName.get(), registryCenterRepository,
configCenterName.get(), configCenterRepository, shardingSchemaNames);
? new ShardingOrchestrationListenerManager(
registryCenterName.get(), registryCenterRepository, configCenterName.get(), configCenterRepository, configCenter.getAllShardingSchemaNames())
: new ShardingOrchestrationListenerManager(registryCenterName.get(), registryCenterRepository, configCenterName.get(), configCenterRepository, shardingSchemaNames);
}
/**
......
/*
* 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.orchestration.core.registrycenter;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.orchestration.center.RegistryCenterRepository;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.TypedSPIServiceLoader;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
/**
* Registry center loader from SPI.
*/
@Slf4j
public final class RegistryCenterRepositoryServiceLoader extends TypedSPIServiceLoader<RegistryCenterRepository> {
static {
ShardingSphereServiceLoader.register(RegistryCenterRepository.class);
}
public RegistryCenterRepositoryServiceLoader() {
super(RegistryCenterRepository.class);
}
/**
* Load registry center from SPI.
*
* @param config configuration for registry center
* @return registry center
*/
public RegistryCenterRepository load(final CenterConfiguration config) {
Preconditions.checkNotNull(config, "Registry center configuration cannot be null.");
RegistryCenterRepository registryCenterRepository = newService(config.getType(), config.getProperties());
registryCenterRepository.init(config);
return registryCenterRepository;
}
}
/*
* 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.orchestration.core.registrycenter;
import org.apache.shardingsphere.orchestration.center.RegistryCenterRepository;
import org.apache.shardingsphere.orchestration.core.registrycenter.fixture.SecondTestRegistryCenterRepository;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
public final class RegistryCenterRepositoryServiceLoaderTest {
@Test
public void assertLoad() {
RegistryCenterRepository registryCenterRepository = new RegistryCenterRepositoryServiceLoader().load(new CenterConfiguration("SecondTestRegistryCenter"));
assertThat(registryCenterRepository, instanceOf(SecondTestRegistryCenterRepository.class));
}
}
......@@ -21,8 +21,9 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.spi.algorithm.keygen.KeyGenerateAlgorithmServiceLoader;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
......@@ -35,13 +36,17 @@ import java.util.Properties;
@Getter
public final class KeyGenerateAlgorithmFactoryBean implements FactoryBean<KeyGenerateAlgorithm>, InitializingBean {
static {
ShardingSphereServiceLoader.register(KeyGenerateAlgorithm.class);
}
private String type;
private Properties properties = new Properties();
@Override
public KeyGenerateAlgorithm getObject() {
return new KeyGenerateAlgorithmServiceLoader().newService(type, properties);
return TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class, type, properties);
}
@Override
......
/*
* 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.spi.type;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.exception.ServiceProviderNotFoundException;
import java.util.Collection;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* Typed SPI service loader.
*
* @param <T> type of SPI class
*/
@RequiredArgsConstructor
public abstract class TypedSPIServiceLoader<T extends TypedSPI> {
private final Class<T> classType;
/**
* Create new instance for typed SPI.
*
* @param type type of SPI
* @param props properties of SPI
* @return SPI instance
*/
public final T newService(final String type, final Properties props) {
Collection<T> typedServices = loadTypedServices(type);
if (typedServices.isEmpty()) {
throw new RuntimeException(String.format("Invalid `%s` SPI type `%s`.", classType.getName(), type));
}
T result = typedServices.iterator().next();
result.setProperties(props);
return result;
}
/**
* Create new service by default SPI type.
*
* @return typed SPI instance
*/
public final T newService() {
T result = loadFirstTypedService();
result.setProperties(new Properties());
return result;
}
private Collection<T> loadTypedServices(final String type) {
return ShardingSphereServiceLoader.newServiceInstances(classType).stream().filter(each -> type.equalsIgnoreCase(each.getType())).collect(Collectors.toList());
}
private T loadFirstTypedService() {
Optional<T> result = ShardingSphereServiceLoader.newServiceInstances(classType).stream().findFirst();
if (result.isPresent()) {
return result.get();
}
throw new ServiceProviderNotFoundException(classType);
}
}
/*
* 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.spi;
import org.apache.shardingsphere.spi.fixture.TypedSPIFixture;
import org.apache.shardingsphere.spi.fixture.TypedSPIFixtureImpl;
import org.apache.shardingsphere.spi.fixture.TypedSPIFixtureServiceLoader;
import org.junit.Test;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class TypedSPIServiceLoaderTest {
private TypedSPIFixtureServiceLoader serviceLoader = new TypedSPIFixtureServiceLoader();
@Test
public void assertNewServiceSuccess() {
Properties properties = new Properties();
properties.setProperty("key", "value");
TypedSPIFixture actual = serviceLoader.newService("FIXTURE", properties);
assertThat(actual, instanceOf(TypedSPIFixtureImpl.class));
assertThat(actual.getProperties().getProperty("key"), is("value"));
}
@Test
public void assertNewServiceByDefault() {
TypedSPIFixture actual = serviceLoader.newService();
assertThat(actual, instanceOf(TypedSPIFixtureImpl.class));
assertTrue(actual.getProperties().isEmpty());
}
@Test(expected = RuntimeException.class)
public void assertNewServiceFailure() {
serviceLoader.newService("INVALID", new Properties());
}
}
/*
* 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.spi.fixture;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.type.TypedSPIServiceLoader;
public final class TypedSPIFixtureServiceLoader extends TypedSPIServiceLoader<TypedSPIFixture> {
static {
ShardingSphereServiceLoader.register(TypedSPIFixture.class);
}
public TypedSPIFixtureServiceLoader() {
super(TypedSPIFixture.class);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册