Commit c64ce242 authored by 吴晟's avatar 吴晟
Browse files

1.完成类构造函数、方法拦截的主要代码。#36

parent 670b5f8f
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.plugin;

import java.net.URL;
import java.util.Enumeration;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -18,15 +19,14 @@ public class PluginBootstrap {
		}

		PluginResourcesResolver resolver = new PluginResourcesResolver();
		Enumeration<URL> resources = resolver.getResources();
		List<URL> resources = resolver.getResources();

		if (resources == null || !resources.hasMoreElements()) {
		if (resources == null || resources.size() == 0) {
			logger.info("no plugin files (skywalking-plugin.properties) found, continue to start application.");
			return;
		}

		while (resources.hasMoreElements()) {
			URL pluginUrl = resources.nextElement();
		for (URL pluginUrl : resources) {
			try {
				PluginCfg.CFG.load(pluginUrl.openStream());
			} catch (Throwable t) {
@@ -36,5 +36,6 @@ public class PluginBootstrap {

		EnhanceClazz4Interceptor enhanceClazz4Interceptor = new EnhanceClazz4Interceptor();
		enhanceClazz4Interceptor.enhance();

	}
}
+7 −3
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@ package com.ai.cloud.skywalking.plugin;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -10,10 +12,11 @@ import org.apache.logging.log4j.Logger;
public class PluginResourcesResolver {
	private static Logger logger = LogManager.getLogger(PluginResourcesResolver.class);
	
	public Enumeration<URL> getResources(){
	public List<URL> getResources(){
		List<URL> cfgUrlPaths = new ArrayList<URL>();
		Enumeration<URL> urls;
		try {
			urls = getDefaultClassLoader().getResources("skywalking-plugin.properties");
			urls = getDefaultClassLoader().getResources("skywalking-plugin.def");
			
			if(!urls.hasMoreElements()){
				logger.info("no plugin files (skywalking-plugin.properties) found");
@@ -21,10 +24,11 @@ public class PluginResourcesResolver {
			
			while(urls.hasMoreElements()){
				URL pluginUrl = urls.nextElement();
				cfgUrlPaths.add(pluginUrl);
				logger.info("find skywalking plugin define in {}", pluginUrl);
			}
			
			return urls;
			return cfgUrlPaths;
		} catch (IOException e) {
			logger.error("read resources failure.", e);
		}
+11 −1
Original line number Diff line number Diff line
package com.ai.cloud.skywalking.plugin;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@@ -16,7 +19,9 @@ public class TracingBootstrap {
	private TracingBootstrap() {
	}

	public static void main(String[] args) {
	public static void main(String[] args) throws IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException, ClassNotFoundException {
		if (args.length == 0) {
			throw new RuntimeException(
					"bootstrap failure. need args[0] to be main class.");
@@ -29,5 +34,10 @@ public class TracingBootstrap {
			logger.error("PluginBootstrap start failure.", t);
		}

		String[] newArgs = Arrays.copyOfRange(args, 1, args.length);

		
		Class.forName(args[0]).getMethod("main", String[].class)
				.invoke(null, new Object[]{newArgs});
	}
}
+44 −0
Original line number Diff line number Diff line
package com.ai.cloud.skywalking.plugin.interceptor;

import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.This;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ClassConstructorInterceptor {
	private static Logger logger = LogManager
			.getLogger(ClassConstructorInterceptor.class);

	private IAroundInterceptor interceptor;

	public ClassConstructorInterceptor(IAroundInterceptor interceptor) {
		this.interceptor = interceptor;
	}

	@RuntimeType
	public void intercept(
			@This Object obj,
			@FieldProxy(EnhanceClazz4Interceptor.contextAttrName) FieldSetter accessor,
			@AllArguments Object[] allArguments) {
		try {
			accessor.setValue(new EnhancedClassInstanceContext());
			ConstructorContext interceptorContext = new ConstructorContext(
					allArguments);
			interceptor.onConstruct(null, interceptorContext);
		} catch (Throwable t) {
			logger.error("ClassConstructorInterceptor failue.", t);
		}

	}
	
	public interface FieldGetter {
		Object getValue();
	}

	public interface FieldSetter {
		void setValue(Object value);
	}
}
+59 −0
Original line number Diff line number Diff line
package com.ai.cloud.skywalking.plugin.interceptor;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.FieldValue;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 类方法拦截、控制器
 * 
 * @author wusheng
 *
 */
public class ClassMethodInterceptor {
	private static Logger logger = LogManager
			.getLogger(ClassMethodInterceptor.class);

	private IAroundInterceptor interceptor;

	public ClassMethodInterceptor(IAroundInterceptor interceptor) {
		this.interceptor = interceptor;
	}

	@RuntimeType
	public Object intercept(
			@This Object obj,
			@AllArguments Object[] allArguments,
			@Origin Method method,
			@SuperCall Callable<?> zuper,
			@FieldValue(EnhanceClazz4Interceptor.contextAttrName) EnhancedClassInstanceContext instanceContext)
			throws Exception {
		InterceptorContext interceptorContext = new InterceptorContext(obj,
				method.getName(), allArguments);
		try {
			interceptor.beforeMethod(instanceContext, interceptorContext);
		} catch (Throwable t) {
			logger.error("class[{}] before method[{}] intercept failue:{}",
					obj.getClass(), method.getName(), t.getMessage(), t);
		}
		try {
			return zuper.call();
		} finally {
			try {
				interceptor.afterMethod(instanceContext, interceptorContext);
			} catch (Throwable t) {
				logger.error("class[{}] after method[{}] intercept failue:{}",
						obj.getClass(), method.getName(), t.getMessage(), t);
			}
		}
	}
}
Loading