Skip to content

Commit

Permalink
feature: support SPI scope (apache#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
booogu authored Mar 24, 2020
1 parent 2842e5d commit 8c4ca8f
Show file tree
Hide file tree
Showing 20 changed files with 648 additions and 218 deletions.
577 changes: 391 additions & 186 deletions common/src/main/java/io/seata/common/loader/EnhancedServiceLoader.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.common.loader;

import io.seata.common.util.StringUtils;

/**
* The type ExtensionDefinition
*
* @author haozhibei
*/
final class ExtensionDefinition {
private String name;
private Class serviceClass;
private Integer order;
private Scope scope;

public Integer getOrder() {
return this.order;
}

public Class getServiceClass() {
return this.serviceClass;
}

public Scope getScope() {
return this.scope;
}

public ExtensionDefinition(String name, Integer order, Scope scope, Class clazz) {
this.name = name;
this.order = order;
this.scope = scope;
this.serviceClass = clazz;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((serviceClass == null) ? 0 : serviceClass.hashCode());
result = prime * result + ((order == null) ? 0 : order.hashCode());
result = prime * result + ((scope == null) ? 0 : scope.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ExtensionDefinition other = (ExtensionDefinition)obj;
if (!StringUtils.equals(name, other.name)) {
return false;
}
if (!serviceClass.equals(other.serviceClass)) {
return false;
}
if (!order.equals(other.order)) {
return false;
}
return !scope.equals(other.scope);
}


}
6 changes: 6 additions & 0 deletions common/src/main/java/io/seata/common/loader/LoadLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@
* @return the int
*/
int order() default 0;

/**
* Scope enum.
* @return
*/
Scope scope() default Scope.SINGLETON;
}
34 changes: 34 additions & 0 deletions common/src/main/java/io/seata/common/loader/Scope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.common.loader;

/**
* the scope of the extension
*
* @author haozhibei
*/
public enum Scope {
/**
* The extension will be loaded in singleton mode
*/
SINGLETON,

/**
* The extension will be loaded in multi instance mode
*/
PROTOTYPE

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class EnhancedServiceLoaderTest {
@Test
public void testLoadByClassAndClassLoader() {
Hello load = EnhancedServiceLoader.load(Hello.class, Hello.class.getClassLoader());
Assertions.assertEquals(load.say(), "Bonjour");
Assertions.assertEquals(load.say(), "Olá.");
}

/**
Expand All @@ -54,7 +54,7 @@ public void testLoadException() {
@Test
public void testLoadByClass() {
Hello load = EnhancedServiceLoader.load(Hello.class);
assertThat(load.say()).isEqualTo("Bonjour");
assertThat(load.say()).isEqualTo("Olá.");
}

/**
Expand Down Expand Up @@ -82,10 +82,10 @@ public void testLoadByClassAndClassLoaderAndActivateName() {
@Test
public void getAllExtensionClass() {
List<Class> allExtensionClass = EnhancedServiceLoader.getAllExtensionClass(Hello.class);
assertThat(allExtensionClass.get(3).getSimpleName()).isEqualTo((LatinHello.class.getSimpleName()));
assertThat(allExtensionClass.get(2).getSimpleName()).isEqualTo((FrenchHello.class.getSimpleName()));
assertThat(allExtensionClass.get(1).getSimpleName()).isEqualTo((EnglishHello.class.getSimpleName()));
assertThat(allExtensionClass.get(0).getSimpleName()).isEqualTo((ChineseHello.class.getSimpleName()));

}

/**
Expand All @@ -98,4 +98,32 @@ public void getAllExtensionClass1() {
assertThat(allExtensionClass).isNotEmpty();
}

@Test
public void getSingletonExtensionInstance(){
Hello hello1 = EnhancedServiceLoader.load(Hello.class, "ChineseHello");
Hello hello2 = EnhancedServiceLoader.load(Hello.class, "ChineseHello");
assertThat(hello1 == hello2).isTrue();
}

@Test
public void getMultipleExtensionInstance(){
Hello hello1 = EnhancedServiceLoader.load(Hello.class, "LatinHello");
Hello hello2 = EnhancedServiceLoader.load(Hello.class, "LatinHello");
assertThat(hello1 == hello2).isFalse();
}

@Test
public void getAllInstances(){
List<Hello> hellows1 = EnhancedServiceLoader.loadAll(Hello.class);
List<Hello> hellows2 = EnhancedServiceLoader.loadAll(Hello.class);
for (Hello hello : hellows1){
if(hello.say()!="Olá."){
assertThat(hellows2.contains(hello)).isTrue();
}
else{
assertThat(hellows2.contains(hello)).isFalse();
}
}
}

}
29 changes: 29 additions & 0 deletions common/src/test/java/io/seata/common/loader/LatinHello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.common.loader;

/**
* The type LatinHello
*
* @author haozhibei
*/
@LoadLevel(name = "LatinHello",order = 3, scope = Scope.PROTOTYPE)
public class LatinHello implements Hello {
@Override
public String say() {
return "Olá.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
#


io.seata.common.loader.EnglishHello
io.seata.common.loader.EnglishHello
io.seata.common.loader.LatinHello
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
#

io.seata.common.loader.FrenchHello
io.seata.common.loader.EnglishHello
io.seata.common.loader.EnglishHello
io.seata.common.loader.LatinHello
27 changes: 18 additions & 9 deletions core/src/main/java/io/seata/core/store/StoreMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@ public enum StoreMode {
/**
* file store
*/
FILE,
FILE("file"),

/**
* database store
*/
DB;
DB("db");

private String name;

StoreMode(String name) {
this.name = name;
}

public String getName() {
return name;
}

/**
* Valueof store mode.
*
* @param mode the mode
* get value of store mode
* @param name the mode name
* @return the store mode
*/
public static StoreMode valueof(String mode) {
for (StoreMode sm : values()) {
if (sm.name().equalsIgnoreCase(mode)) {
public static StoreMode get(String name) {
for (StoreMode sm : StoreMode.class.getEnumConstants()) {
if (sm.name.equalsIgnoreCase(name)) {
return sm;
}
}
throw new IllegalArgumentException("unknown store mode:" + mode);
throw new IllegalArgumentException("unknown store mode:" + name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static List<Exporter> getInstanceList() {
try {
exporterType = ExporterType.getType(exporterTypeName);
exporters.add(
EnhancedServiceLoader.load(Exporter.class, Objects.requireNonNull(exporterType).name()));
EnhancedServiceLoader.load(Exporter.class, Objects.requireNonNull(exporterType).getName()));
} catch (Exception exx) {
LOGGER.error("not support metrics exporter type: {}",exporterTypeName, exx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ public enum ExporterType {
/**
* Export metrics data to Prometheus
*/
PROMETHEUS;
PROMETHEUS("prometheus");

private String name;

ExporterType(String name) {
this.name = name;
}

public String getName() {
return name;
}

public static ExporterType getType(String name) {
if (PROMETHEUS.name().equalsIgnoreCase(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Registry getInstance() {
} catch (Exception exx) {
throw new NotSupportYetException("not support metrics registry type: " + registryTypeName);
}
return EnhancedServiceLoader.load(Registry.class, Objects.requireNonNull(registryType).name());
return EnhancedServiceLoader.load(Registry.class, Objects.requireNonNull(registryType).getName());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ public enum RegistryType {
/**
* Built-in compact metrics registry
*/
COMPACT;
COMPACT("compact");

private String name;

RegistryType(String name) {
this.name = name;
}

public String getName() {
return name;
}

public static RegistryType getType(String name) {
if (COMPACT.name().equalsIgnoreCase(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*
* @author zhengyangyong
*/
@LoadLevel(name = "Prometheus", order = 1)
@LoadLevel(name = "prometheus", order = 1)
public class PrometheusExporter extends Collector implements Collector.Describable, Exporter {

private final HTTPServer server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*
* @author zhengyangyong
*/
@LoadLevel(name = "Compact", order = 1)
@LoadLevel(name = "compact", order = 1)
public class CompactRegistry implements Registry {
private static final Map<UUID, Meter> METERS = new ConcurrentHashMap<>();

Expand Down
Loading

0 comments on commit 8c4ca8f

Please sign in to comment.