1717
1818package cn .polarismesh .agent .plugin .spring .cloud ;
1919
20- import java .security .ProtectionDomain ;
21-
2220import cn .polarismesh .agent .core .extension .AgentPlugin ;
2321import cn .polarismesh .agent .core .extension .PluginContext ;
2422import cn .polarismesh .agent .core .extension .instrument .InstrumentClass ;
3331import cn .polarismesh .agent .plugin .spring .cloud .interceptor .RegisterBeanInterceptor ;
3432import cn .polarismesh .agent .plugin .spring .cloud .interceptor .SpringFactoriesLoaderInterceptor ;
3533
34+ import java .security .ProtectionDomain ;
35+
3636/**
3737 * Polaris Spring Cloud hoxton Plugin
3838 *
3939 * @author shuhanliu
4040 */
4141public class MainPlugin implements AgentPlugin {
4242
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+ }
122122
123123}
0 commit comments