diff --git a/apm-sniffer/apm-agent-core/pom.xml b/apm-sniffer/apm-agent-core/pom.xml index 121f579a019e8f0f1ee6b1a9f823125d005a9b11..d835b107719d05d1211113217bb4349ab520d832 100644 --- a/apm-sniffer/apm-agent-core/pom.xml +++ b/apm-sniffer/apm-agent-core/pom.xml @@ -63,6 +63,11 @@ apm-network ${project.version} + + org.skywalking + apm-util + ${project.version} + net.bytebuddy byte-buddy diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java index 24fe3be87dd7a720aebec7ed22af5c1ea96827dd..35eba4f25ecf4e5e192f0bb39889b4db46d04931 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/AgentClassLoader.java @@ -46,18 +46,26 @@ import org.skywalking.apm.logging.LogManager; */ public class AgentClassLoader extends ClassLoader { private static final ILog logger = LogManager.getLogger(AgentClassLoader.class); - private static AgentClassLoader LOADER; + /** + * The default class loader for the agent. + */ + private static AgentClassLoader DEFAULT_LOADER; private List classpath; private List allJars; private ReentrantLock jarScanLock = new ReentrantLock(); public static AgentClassLoader getDefault() { - return LOADER; + return DEFAULT_LOADER; } + /** + * Init the default + * @return + * @throws AgentPackageNotFoundException + */ public static AgentClassLoader initDefaultLoader() throws AgentPackageNotFoundException { - LOADER = new AgentClassLoader(PluginBootstrap.class.getClassLoader()); + DEFAULT_LOADER = new AgentClassLoader(PluginBootstrap.class.getClassLoader()); return getDefault(); } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/InterceptorInstanceLoader.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/InterceptorInstanceLoader.java index 9e25d54d8d7e47e28c603e09ede8011f4ee7c9c0..6d51e286b4d1c37cba1bdaa90be23c84300da703 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/InterceptorInstanceLoader.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/InterceptorInstanceLoader.java @@ -42,21 +42,13 @@ import org.skywalking.apm.logging.LogManager; * This is a very important class in sky-walking's auto-instrumentation mechanism. If you want to fully understand why * need this, and how it works, you need have knowledge about Classloader appointment mechanism. *

- * The loader will load a class, and focus the target class loader (be intercepted class's classloader) loads it. - *

- * If the target class and target class loader are same, the loaded classes( {@link InstanceConstructorInterceptor}, - * {@link InstanceMethodsAroundInterceptor} and {@link StaticMethodsAroundInterceptor} implementations) stay in - * singleton. - *

* Created by wusheng on 16/8/2. */ public class InterceptorInstanceLoader { private static final ILog logger = LogManager.getLogger(InterceptorInstanceLoader.class); private static ConcurrentHashMap INSTANCE_CACHE = new ConcurrentHashMap(); - private static ReentrantLock INSTANCE_LOAD_LOCK = new ReentrantLock(); - private static Map EXTEND_PLUGIN_CLASSLOADERS = new HashMap(); /** @@ -64,10 +56,10 @@ public class InterceptorInstanceLoader { * Create {@link AgentClassLoader} for each targetClassLoader, as an extend classloader. * It can load interceptor classes from plugins, activations folders. * - * @param className - * @param targetClassLoader - * @param - * @return + * @param className the interceptor class, which is expected to be found + * @param targetClassLoader the class loader for current application context + * @param expected type + * @return the type reference. * @throws InvocationTargetException * @throws IllegalAccessException * @throws InstantiationException @@ -100,137 +92,4 @@ public class InterceptorInstanceLoader { return (T) inst; } - - /** - * Old load method, just for backup - * - * @param className - * @param targetClassLoader - * @param - * @return - * @throws InvocationTargetException - * @throws IllegalAccessException - * @throws InstantiationException - * @throws ClassNotFoundException - */ - public static T load0(String className, ClassLoader targetClassLoader) - throws InvocationTargetException, IllegalAccessException, InstantiationException, ClassNotFoundException { - if (targetClassLoader == null) { - targetClassLoader = InterceptorInstanceLoader.class.getClassLoader(); - } - String instanceKey = className + "_OF_" + targetClassLoader.getClass().getName() + "@" + Integer.toHexString(targetClassLoader.hashCode()); - Object inst = INSTANCE_CACHE.get(instanceKey); - if (inst == null) { - if (InterceptorInstanceLoader.class.getClassLoader().equals(targetClassLoader)) { - inst = targetClassLoader.loadClass(className).newInstance(); - } else { - INSTANCE_LOAD_LOCK.lock(); - try { - try { - inst = findLoadedClass(className, targetClassLoader); - if (inst == null) { - inst = loadBinary(className, targetClassLoader); - } - if (inst == null) { - throw new ClassNotFoundException(targetClassLoader.toString() + " load interceptor class:" + className + " failure."); - } - } catch (Exception e) { - throw new ClassNotFoundException(targetClassLoader.toString() + " load interceptor class:" + className + " failure.", e); - } - } finally { - INSTANCE_LOAD_LOCK.unlock(); - } - } - if (inst != null) { - INSTANCE_CACHE.put(instanceKey, inst); - } - } - - return (T) inst; - } - - /** - * load class from class binary files. - * Most likely all the interceptor implementations should be loaded by this. - * - * @param className interceptor class name. - * @param targetClassLoader the classloader, which should load the interceptor. - * @param - * @return interceptor instance. - * @throws InvocationTargetException - * @throws IllegalAccessException - * @throws InstantiationException - */ - private static T loadBinary(String className, - ClassLoader targetClassLoader) throws InvocationTargetException, IllegalAccessException, InstantiationException { - String path = "/" + className.replace('.', '/').concat(".class"); - byte[] data = null; - BufferedInputStream is = null; - ByteArrayOutputStream baos = null; - try { - logger.debug("Read binary code of {} using classload {}", className, InterceptorInstanceLoader.class.getClassLoader()); - is = new BufferedInputStream(InterceptorInstanceLoader.class.getResourceAsStream(path)); - baos = new ByteArrayOutputStream(); - int ch = 0; - while ((ch = is.read()) != -1) { - baos.write(ch); - } - data = baos.toByteArray(); - } catch (IOException e) { - logger.error(e.getMessage(), e); - } finally { - if (is != null) - try { - is.close(); - } catch (IOException ignored) { - } - if (baos != null) - try { - baos.close(); - } catch (IOException ignored) { - } - } - - Method defineClassMethod = null; - Class targetClassLoaderType = targetClassLoader.getClass(); - while (defineClassMethod == null && targetClassLoaderType != null) { - try { - defineClassMethod = targetClassLoaderType.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class, ProtectionDomain.class); - } catch (NoSuchMethodException e) { - targetClassLoaderType = targetClassLoaderType.getSuperclass(); - } - } - defineClassMethod.setAccessible(true); - logger.debug("load binary code of {} to classloader {}", className, targetClassLoader); - Class type = (Class) defineClassMethod.invoke(targetClassLoader, className, data, 0, data.length, null); - return (T) type.newInstance(); - } - - /** - * Find loaded class in the current classloader. - * Just in case some classes have already been loaded for some reason. - * - * @param className interceptor class name. - * @param targetClassLoader the classloader, which should load the interceptor. - * @param - * @return interceptor instance. - */ - private static T findLoadedClass(String className, - ClassLoader targetClassLoader) throws InvocationTargetException, IllegalAccessException, InstantiationException { - Method defineClassMethod = null; - Class targetClassLoaderType = targetClassLoader.getClass(); - while (defineClassMethod == null && targetClassLoaderType != null) { - try { - defineClassMethod = targetClassLoaderType.getDeclaredMethod("findLoadedClass", String.class); - } catch (NoSuchMethodException e) { - targetClassLoaderType = targetClassLoaderType.getSuperclass(); - } - } - defineClassMethod.setAccessible(true); - Class type = (Class) defineClassMethod.invoke(targetClassLoader, className); - if (type == null) { - return null; - } - return (T) type.newInstance(); - } } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/NotImplementationException.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/NotImplementationException.java deleted file mode 100644 index 790594dad9bf6d5607767fe59db1818b67384c46..0000000000000000000000000000000000000000 --- a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/plugin/loader/NotImplementationException.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2017, OpenSkywalking Organization All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Project repository: https://github.com/OpenSkywalking/skywalking - */ - -package org.skywalking.apm.agent.core.plugin.loader; - -/** - * The NotImplementationException represents that, a method didn't implement yet. Most likely, this method is not needed in current version, but be called by unexpected. - * - * @author wusheng - */ -public class NotImplementationException extends RuntimeException { -} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/pom.xml index 4ba84d647a9dde689fbe3ad4499337edc3c58b45..a27e12bae691e3cb56df29ec7fe6dcbb96f60821 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/pom.xml @@ -36,13 +36,13 @@ org.springframework spring-core 4.3.10.RELEASE - provided + compile org.springframework spring-webmvc 4.3.8.RELEASE - provided + compile javax.servlet diff --git a/apm-sniffer/pom.xml b/apm-sniffer/pom.xml index e92bd4cc8047623cb2fcd00dd4097f415845fd5a..4680003d6e815c4f011dab0b08bdfaaa815d0cc8 100644 --- a/apm-sniffer/pom.xml +++ b/apm-sniffer/pom.xml @@ -42,12 +42,4 @@ 1.6 org.skywalking.apm.dependencies - - - - org.skywalking - apm-util - ${project.version} - -