Skip to content

Commit 8c4ca8f

Browse files
authored
feature: support SPI scope (apache#2135)
1 parent 2842e5d commit 8c4ca8f

20 files changed

Lines changed: 648 additions & 218 deletions

File tree

‎common/src/main/java/io/seata/common/loader/EnhancedServiceLoader.java‎

Lines changed: 391 additions & 186 deletions
Large diffs are not rendered by default.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.seata.common.loader;
17+
18+
import io.seata.common.util.StringUtils;
19+
20+
/**
21+
* The type ExtensionDefinition
22+
*
23+
* @author haozhibei
24+
*/
25+
final class ExtensionDefinition {
26+
private String name;
27+
private Class serviceClass;
28+
private Integer order;
29+
private Scope scope;
30+
31+
public Integer getOrder() {
32+
return this.order;
33+
}
34+
35+
public Class getServiceClass() {
36+
return this.serviceClass;
37+
}
38+
39+
public Scope getScope() {
40+
return this.scope;
41+
}
42+
43+
public ExtensionDefinition(String name, Integer order, Scope scope, Class clazz) {
44+
this.name = name;
45+
this.order = order;
46+
this.scope = scope;
47+
this.serviceClass = clazz;
48+
}
49+
50+
@Override
51+
public int hashCode() {
52+
final int prime = 31;
53+
int result = 1;
54+
result = prime * result + ((name == null) ? 0 : name.hashCode());
55+
result = prime * result + ((serviceClass == null) ? 0 : serviceClass.hashCode());
56+
result = prime * result + ((order == null) ? 0 : order.hashCode());
57+
result = prime * result + ((scope == null) ? 0 : scope.hashCode());
58+
return result;
59+
}
60+
61+
@Override
62+
public boolean equals(Object obj) {
63+
if (this == obj) {
64+
return true;
65+
}
66+
if (obj == null) {
67+
return false;
68+
}
69+
if (getClass() != obj.getClass()) {
70+
return false;
71+
}
72+
ExtensionDefinition other = (ExtensionDefinition)obj;
73+
if (!StringUtils.equals(name, other.name)) {
74+
return false;
75+
}
76+
if (!serviceClass.equals(other.serviceClass)) {
77+
return false;
78+
}
79+
if (!order.equals(other.order)) {
80+
return false;
81+
}
82+
return !scope.equals(other.scope);
83+
}
84+
85+
86+
}

‎common/src/main/java/io/seata/common/loader/LoadLevel.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@
4343
* @return the int
4444
*/
4545
int order() default 0;
46+
47+
/**
48+
* Scope enum.
49+
* @return
50+
*/
51+
Scope scope() default Scope.SINGLETON;
4652
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.seata.common.loader;
17+
18+
/**
19+
* the scope of the extension
20+
*
21+
* @author haozhibei
22+
*/
23+
public enum Scope {
24+
/**
25+
* The extension will be loaded in singleton mode
26+
*/
27+
SINGLETON,
28+
29+
/**
30+
* The extension will be loaded in multi instance mode
31+
*/
32+
PROTOTYPE
33+
34+
}

‎common/src/test/java/io/seata/common/loader/EnhancedServiceLoaderTest.java‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class EnhancedServiceLoaderTest {
3535
@Test
3636
public void testLoadByClassAndClassLoader() {
3737
Hello load = EnhancedServiceLoader.load(Hello.class, Hello.class.getClassLoader());
38-
Assertions.assertEquals(load.say(), "Bonjour");
38+
Assertions.assertEquals(load.say(), "Olá.");
3939
}
4040

4141
/**
@@ -54,7 +54,7 @@ public void testLoadException() {
5454
@Test
5555
public void testLoadByClass() {
5656
Hello load = EnhancedServiceLoader.load(Hello.class);
57-
assertThat(load.say()).isEqualTo("Bonjour");
57+
assertThat(load.say()).isEqualTo("Olá.");
5858
}
5959

6060
/**
@@ -82,10 +82,10 @@ public void testLoadByClassAndClassLoaderAndActivateName() {
8282
@Test
8383
public void getAllExtensionClass() {
8484
List<Class> allExtensionClass = EnhancedServiceLoader.getAllExtensionClass(Hello.class);
85+
assertThat(allExtensionClass.get(3).getSimpleName()).isEqualTo((LatinHello.class.getSimpleName()));
8586
assertThat(allExtensionClass.get(2).getSimpleName()).isEqualTo((FrenchHello.class.getSimpleName()));
8687
assertThat(allExtensionClass.get(1).getSimpleName()).isEqualTo((EnglishHello.class.getSimpleName()));
8788
assertThat(allExtensionClass.get(0).getSimpleName()).isEqualTo((ChineseHello.class.getSimpleName()));
88-
8989
}
9090

9191
/**
@@ -98,4 +98,32 @@ public void getAllExtensionClass1() {
9898
assertThat(allExtensionClass).isNotEmpty();
9999
}
100100

101+
@Test
102+
public void getSingletonExtensionInstance(){
103+
Hello hello1 = EnhancedServiceLoader.load(Hello.class, "ChineseHello");
104+
Hello hello2 = EnhancedServiceLoader.load(Hello.class, "ChineseHello");
105+
assertThat(hello1 == hello2).isTrue();
106+
}
107+
108+
@Test
109+
public void getMultipleExtensionInstance(){
110+
Hello hello1 = EnhancedServiceLoader.load(Hello.class, "LatinHello");
111+
Hello hello2 = EnhancedServiceLoader.load(Hello.class, "LatinHello");
112+
assertThat(hello1 == hello2).isFalse();
113+
}
114+
115+
@Test
116+
public void getAllInstances(){
117+
List<Hello> hellows1 = EnhancedServiceLoader.loadAll(Hello.class);
118+
List<Hello> hellows2 = EnhancedServiceLoader.loadAll(Hello.class);
119+
for (Hello hello : hellows1){
120+
if(hello.say()!="Olá."){
121+
assertThat(hellows2.contains(hello)).isTrue();
122+
}
123+
else{
124+
assertThat(hellows2.contains(hello)).isFalse();
125+
}
126+
}
127+
}
128+
101129
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.seata.common.loader;
17+
18+
/**
19+
* The type LatinHello
20+
*
21+
* @author haozhibei
22+
*/
23+
@LoadLevel(name = "LatinHello",order = 3, scope = Scope.PROTOTYPE)
24+
public class LatinHello implements Hello {
25+
@Override
26+
public String say() {
27+
return "Olá.";
28+
}
29+
}

‎common/src/test/resources/META-INF/seata/frenchhello/io.seata.common.loader.Hello‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
#
1616

1717

18-
io.seata.common.loader.EnglishHello
18+
io.seata.common.loader.EnglishHello
19+
io.seata.common.loader.LatinHello

‎common/src/test/resources/META-INF/seata/io.seata.common.loader.Hello‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
#
1616

1717
io.seata.common.loader.FrenchHello
18-
io.seata.common.loader.EnglishHello
18+
io.seata.common.loader.EnglishHello
19+
io.seata.common.loader.LatinHello

‎core/src/main/java/io/seata/core/store/StoreMode.java‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,35 @@ public enum StoreMode {
2525
/**
2626
* file store
2727
*/
28-
FILE,
28+
FILE("file"),
2929

3030
/**
3131
* database store
3232
*/
33-
DB;
33+
DB("db");
34+
35+
private String name;
36+
37+
StoreMode(String name) {
38+
this.name = name;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
3444

3545
/**
36-
* Valueof store mode.
37-
*
38-
* @param mode the mode
46+
* get value of store mode
47+
* @param name the mode name
3948
* @return the store mode
4049
*/
41-
public static StoreMode valueof(String mode) {
42-
for (StoreMode sm : values()) {
43-
if (sm.name().equalsIgnoreCase(mode)) {
50+
public static StoreMode get(String name) {
51+
for (StoreMode sm : StoreMode.class.getEnumConstants()) {
52+
if (sm.name.equalsIgnoreCase(name)) {
4453
return sm;
4554
}
4655
}
47-
throw new IllegalArgumentException("unknown store mode:" + mode);
56+
throw new IllegalArgumentException("unknown store mode:" + name);
4857
}
4958

5059
}

‎metrics/seata-metrics-core/src/main/java/io/seata/metrics/exporter/ExporterFactory.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static List<Exporter> getInstanceList() {
4545
try {
4646
exporterType = ExporterType.getType(exporterTypeName);
4747
exporters.add(
48-
EnhancedServiceLoader.load(Exporter.class, Objects.requireNonNull(exporterType).name()));
48+
EnhancedServiceLoader.load(Exporter.class, Objects.requireNonNull(exporterType).getName()));
4949
} catch (Exception exx) {
5050
LOGGER.error("not support metrics exporter type: {}",exporterTypeName, exx);
5151
}

0 commit comments

Comments
 (0)