18
18
package cn .polarismesh .agent .core .asm .instrument .plugin ;
19
19
20
20
import cn .polarismesh .agent .core .common .conf .ConfigManager ;
21
+ import cn .polarismesh .agent .core .common .logger .CommonLogger ;
22
+ import cn .polarismesh .agent .core .common .logger .StdoutCommonLoggerFactory ;
23
+ import cn .polarismesh .agent .core .common .utils .ReflectionUtils ;
21
24
import cn .polarismesh .agent .core .common .utils .StringUtils ;
25
+
26
+ import java .lang .reflect .Method ;
22
27
import java .util .Collections ;
23
28
import java .util .HashSet ;
24
29
import java .util .Set ;
25
30
26
31
public class PluginNameFilter implements PluginFilter {
27
32
33
+ private static final CommonLogger logger = StdoutCommonLoggerFactory .INSTANCE
34
+ .getLogger (PluginNameFilter .class .getCanonicalName ());
35
+
28
36
private final Set <String > pluginNames ;
29
37
30
38
public PluginNameFilter () {
@@ -38,6 +46,7 @@ public boolean accept(PluginJar pluginJar) {
38
46
39
47
private static Set <String > getLoadablePluginNames () {
40
48
String enablePlugins = ConfigManager .INSTANCE .getConfigValue (ConfigManager .KEY_PLUGIN_ENABLE );
49
+ enablePlugins = appendSpringCloudPluginNameIfNeeded (enablePlugins );
41
50
if (StringUtils .isEmpty (enablePlugins )) {
42
51
return Collections .emptySet ();
43
52
}
@@ -51,4 +60,52 @@ private static Set<String> getLoadablePluginNames() {
51
60
}
52
61
return values ;
53
62
}
63
+
64
+ private static String appendSpringCloudPluginNameIfNeeded (String enablePlugins ) {
65
+ if (StringUtils .hasText (enablePlugins )) {
66
+ String [] names = enablePlugins .split ("," );
67
+ for (String name : names ) {
68
+ if (StringUtils .hasText (name ) && name .contains ("spring-cloud-" )) {
69
+ return enablePlugins ;
70
+ }
71
+ }
72
+ }
73
+
74
+ // check if spring-cloud and spring-boot exist.
75
+ if (ReflectionUtils .checkClassExists ("org.springframework.cloud.configuration.SpringBootVersionVerifier" )
76
+ && ReflectionUtils .checkClassExists ("org.springframework.boot.SpringBootVersion" )) {
77
+ Method method = ReflectionUtils .findMethod (ReflectionUtils .findClass ("org.springframework.boot.SpringBootVersion" ), "getVersion" );
78
+ Object version = ReflectionUtils .invokeMethod (method , null );
79
+
80
+ if (version instanceof String ) {
81
+ String versionStr = (String ) version ;
82
+ logger .info ("Spring Boot Version: " + version );
83
+ String springCloudPluginNamePattern = "spring-cloud-%s-plugin" ;
84
+ String springCloudVersion = "" ;
85
+ if (versionStr .startsWith ("2.2" ) || versionStr .startsWith ("2.3" )) {
86
+ springCloudVersion = "hoxton" ;
87
+ } else if (versionStr .startsWith ("2.4" ) || versionStr .startsWith ("2.5" )) {
88
+ springCloudVersion = "2020" ;
89
+ } else if (versionStr .startsWith ("2.6" ) || versionStr .startsWith ("2.7" )) {
90
+ springCloudVersion = "2021" ;
91
+ } else if (versionStr .startsWith ("3.0" ) || versionStr .startsWith ("3.1" )) {
92
+ springCloudVersion = "2022" ;
93
+ } else if (versionStr .startsWith ("3.2" ) || versionStr .startsWith ("3.3" )) {
94
+ springCloudVersion = "2023" ;
95
+ }
96
+ if (StringUtils .hasText (springCloudVersion )) {
97
+ String springCloudPluginName = String .format (springCloudPluginNamePattern , springCloudVersion );
98
+ logger .info ("Spring Cloud Version: " + springCloudVersion );
99
+ if (StringUtils .hasText (enablePlugins )) {
100
+ enablePlugins = enablePlugins + "," + springCloudPluginName ;
101
+ } else {
102
+ enablePlugins = springCloudPluginName ;
103
+ }
104
+ }
105
+ } else {
106
+ logger .warn ("Cannot get Spring Boot Version from MANIFEST." );
107
+ }
108
+ }
109
+ return enablePlugins ;
110
+ }
54
111
}
0 commit comments