1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package com.alibaba.cola.extension;
import com.alibaba.cola.extension.register.AbstractComponentExecutor;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* ExtensionExecutor
*
*/
@Component
public class ExtensionExecutor extends AbstractComponentExecutor {
private static final String EXTENSION_NOT_FOUND = "extension_not_found";
private Logger logger = LoggerFactory.getLogger(ExtensionExecutor.class);
@Resource
private ExtensionRepository extensionRepository;
@Override
protected <C> C locateComponent(Class<C> targetClz, BizScenario bizScenario) {
C extension = locateExtension(targetClz, bizScenario);
logger.debug("[Located Extension]: " + extension.getClass().getSimpleName());
return extension;
}
/**
* if the bizScenarioUniqueIdentity is "ali.tmall.supermarket"
* <p>
* the search path is as below:
* 1、first try to get extension by "ali.tmall.supermarket", if get, return it.
* 2、loop try to get extension by "ali.tmall", if get, return it.
* 3、loop try to get extension by "ali", if get, return it.
* 4、if not found, try the default extension
*
* @param targetClz
*/
protected <Ext> Ext locateExtension(Class<Ext> targetClz, BizScenario bizScenario) {
checkNull(bizScenario);
Ext extension;
logger.debug("BizScenario in locateExtension is : " + bizScenario.getUniqueIdentity());
// first try with full namespace
extension = firstTry(targetClz, bizScenario);
if (extension != null) {
return extension;
}
// second try with default scenario
extension = secondTry(targetClz, bizScenario);
if (extension != null) {
return extension;
}
// third try with default use case + default scenario
extension = defaultUseCaseTry(targetClz, bizScenario);
if (extension != null) {
return extension;
}
String errMessage = "Can not find extension with ExtensionPoint: " +
targetClz + " BizScenario:" + bizScenario.getUniqueIdentity();
throw new ExtensionException(EXTENSION_NOT_FOUND, errMessage);
}
/**
* first try with full namespace
* <p>
* example: biz1.useCase1.scenario1
*/
private <Ext> Ext firstTry(Class<Ext> targetClz, BizScenario bizScenario) {
logger.debug("First trying with " + bizScenario.getUniqueIdentity());
return locate(targetClz.getName(), bizScenario.getUniqueIdentity());
}
/**
* second try with default scenario
* <p>
* example: biz1.useCase1.#defaultScenario#
*/
private <Ext> Ext secondTry(Class<Ext> targetClz, BizScenario bizScenario) {
logger.debug("Second trying with " + bizScenario.getIdentityWithDefaultScenario());
return locate(targetClz.getName(), bizScenario.getIdentityWithDefaultScenario());
}
/**
* third try with default use case + default scenario
* <p>
* example: biz1.#defaultUseCase#.#defaultScenario#
*/
private <Ext> Ext defaultUseCaseTry(Class<Ext> targetClz, BizScenario bizScenario) {
logger.debug("Third trying with " + bizScenario.getIdentityWithDefaultUseCase());
return locate(targetClz.getName(), bizScenario.getIdentityWithDefaultUseCase());
}
private <Ext> Ext locate(String name, String uniqueIdentity) {
final Ext ext = (Ext) extensionRepository.getExtensionRepo().
get(new ExtensionCoordinate(name, uniqueIdentity));
return ext;
}
private void checkNull(BizScenario bizScenario) {
if (bizScenario == null) {
throw new IllegalArgumentException("BizScenario can not be null for extension");
}
}
}
|