17
17
18
18
package cn .polarismesh .agent .plugin .spring .cloud ;
19
19
20
- import java .security .ProtectionDomain ;
21
-
22
20
import cn .polarismesh .agent .core .extension .AgentPlugin ;
23
21
import cn .polarismesh .agent .core .extension .PluginContext ;
24
22
import cn .polarismesh .agent .core .extension .instrument .InstrumentClass ;
33
31
import cn .polarismesh .agent .plugin .spring .cloud .interceptor .RegisterBeanInterceptor ;
34
32
import cn .polarismesh .agent .plugin .spring .cloud .interceptor .SpringFactoriesLoaderInterceptor ;
35
33
34
+ import java .security .ProtectionDomain ;
35
+
36
36
/**
37
37
* Polaris Spring Cloud hoxton Plugin
38
38
*
39
39
* @author shuhanliu
40
40
*/
41
41
public class MainPlugin implements AgentPlugin {
42
42
43
- public void init (PluginContext context ) {
44
- System .setProperty (Constant .AGENT_CONF_PATH , context .getAgentDirPath ());
45
- TransformOperations operations = context .getTransformOperations ();
46
- addPolarisTransformers (operations );
47
- }
48
-
49
- /**
50
- * add polaris transformers
51
- */
52
- private void addPolarisTransformers (TransformOperations operations ) {
53
-
54
- // 注入默认配置
55
- operations .transform (Constant .CONFIGURATION_CLAZZ_POST_PROCESSOR , ConfigurationPostProcessorTransform .class );
56
-
57
- // 注入bootstrap的bean定义
58
- operations .transform (Constant .CONFIGURATION_CLAZZ_PARSER , ConfigurationParserTransform .class );
59
-
60
- // 注入bean定义的调整设置
61
- operations .transform (Constant .BEAN_DEFINITION_REGISTRY , RegisterBeanDefinitionTransform .class );
62
-
63
- // 注入JNI定义
64
- operations .transform (Constant .SPRING_FACTORIES_LOADER , SpringFactoriesLoaderTransform .class );
65
- }
66
-
67
- public static class ConfigurationParserTransform implements TransformCallback {
68
-
69
- @ Override
70
- public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
71
- InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
72
- InstrumentMethod constructMethod = target .getDeclaredMethod ("parse" , "java.util.Set" );
73
- if (constructMethod != null ) {
74
- constructMethod .addInterceptor (ConfigurationParserInterceptor .class );
75
- }
76
-
77
- return target .toBytecode ();
78
- }
79
- }
80
-
81
- public static class ConfigurationPostProcessorTransform implements TransformCallback {
82
-
83
- @ Override
84
- public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
85
- InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
86
- InstrumentMethod constructMethod = target .getDeclaredMethod ("processConfigBeanDefinitions" , "org.springframework.beans.factory.support.BeanDefinitionRegistry" );
87
- if (constructMethod != null ) {
88
- constructMethod .addInterceptor (ConfigurationPostProcessorInterceptor .class );
89
- }
90
-
91
- return target .toBytecode ();
92
- }
93
- }
94
-
95
- public static class RegisterBeanDefinitionTransform implements TransformCallback {
96
-
97
- @ Override
98
- public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
99
- InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
100
- InstrumentMethod constructMethod = target .getDeclaredMethod ("registerBeanDefinition" , "java.lang.String" , "org.springframework.beans.factory.config.BeanDefinition" );
101
- if (constructMethod != null ) {
102
- constructMethod .addInterceptor (RegisterBeanInterceptor .class );
103
- }
104
-
105
- return target .toBytecode ();
106
- }
107
- }
108
-
109
- public static class SpringFactoriesLoaderTransform implements TransformCallback {
110
-
111
- @ Override
112
- public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
113
- InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
114
- InstrumentMethod constructMethod = target .getDeclaredMethod ( "loadSpringFactories " , "java.lang.ClassLoader " );
115
- if (constructMethod != null ) {
116
- constructMethod .addInterceptor (SpringFactoriesLoaderInterceptor .class );
117
- }
118
-
119
- return target .toBytecode ();
120
- }
121
- }
43
+ public void init (PluginContext context ) {
44
+ System .setProperty (Constant .AGENT_CONF_PATH , context .getAgentDirPath ());
45
+ TransformOperations operations = context .getTransformOperations ();
46
+ addPolarisTransformers (operations );
47
+ }
48
+
49
+ /**
50
+ * add polaris transformers
51
+ */
52
+ private void addPolarisTransformers (TransformOperations operations ) {
53
+
54
+ // 注入默认配置
55
+ operations .transform (Constant .CONFIGURATION_CLAZZ_POST_PROCESSOR , ConfigurationPostProcessorTransform .class );
56
+
57
+ // 注入bootstrap的bean定义
58
+ operations .transform (Constant .CONFIGURATION_CLAZZ_PARSER , ConfigurationParserTransform .class );
59
+
60
+ // 注入bean定义的调整设置
61
+ operations .transform (Constant .BEAN_DEFINITION_REGISTRY , RegisterBeanDefinitionTransform .class );
62
+
63
+ // 注入JNI定义
64
+ operations .transform (Constant .SPRING_FACTORIES_LOADER , SpringFactoriesLoaderTransform .class );
65
+ }
66
+
67
+ public static class ConfigurationParserTransform implements TransformCallback {
68
+
69
+ @ Override
70
+ public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
71
+ InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
72
+ InstrumentMethod constructMethod = target .getDeclaredMethod ("parse" , "java.util.Set" );
73
+ if (constructMethod != null ) {
74
+ constructMethod .addInterceptor (ConfigurationParserInterceptor .class );
75
+ }
76
+
77
+ return target .toBytecode ();
78
+ }
79
+ }
80
+
81
+ public static class ConfigurationPostProcessorTransform implements TransformCallback {
82
+
83
+ @ Override
84
+ public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
85
+ InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
86
+ InstrumentMethod constructMethod = target .getDeclaredMethod ("processConfigBeanDefinitions" , "org.springframework.beans.factory.support.BeanDefinitionRegistry" );
87
+ if (constructMethod != null ) {
88
+ constructMethod .addInterceptor (ConfigurationPostProcessorInterceptor .class );
89
+ }
90
+
91
+ return target .toBytecode ();
92
+ }
93
+ }
94
+
95
+ public static class RegisterBeanDefinitionTransform implements TransformCallback {
96
+
97
+ @ Override
98
+ public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
99
+ InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
100
+ InstrumentMethod constructMethod = target .getDeclaredMethod ("registerBeanDefinition" , "java.lang.String" , "org.springframework.beans.factory.config.BeanDefinition" );
101
+ if (constructMethod != null ) {
102
+ constructMethod .addInterceptor (RegisterBeanInterceptor .class );
103
+ }
104
+
105
+ return target .toBytecode ();
106
+ }
107
+ }
108
+
109
+ public static class SpringFactoriesLoaderTransform implements TransformCallback {
110
+
111
+ @ Override
112
+ public byte [] doInTransform (Instrumentor instrumentor , ClassLoader classLoader , String className , Class <?> classBeingRedefined , ProtectionDomain protectionDomain , byte [] classFileBuffer ) throws InstrumentException {
113
+ InstrumentClass target = instrumentor .getInstrumentClass (classLoader , className , classFileBuffer );
114
+ InstrumentMethod constructMethod = target .getConstructor ( "java.lang.ClassLoader " , "java.util.Map " );
115
+ if (constructMethod != null ) {
116
+ constructMethod .addInterceptor (SpringFactoriesLoaderInterceptor .class );
117
+ }
118
+
119
+ return target .toBytecode ();
120
+ }
121
+ }
122
122
123
123
}
0 commit comments