agent扩展-自定义外部加载路径

news2024/9/23 21:21:48

自定义classLoader实现加载外部jar, 以skywalking agent 类加载器为例子

整体思路

  • 扩展findClass ,解决loadClass可以查找到
  • 扩展findResource,解决getResources可以获取到资源

基本原理

ClassLoader loadClass的加载顺序
findLoadedClass 加载本地已经加载过的class
parent.loadClass 通过父类去加载class
findClass 前面都没有加载到,就通过findClass去搜索jar并加载class
在这里插入图片描述
扩展findClass
在这里插入图片描述
在这里插入图片描述

扩展findResource

扩展后AgentClassLoader.getDefault().getResources(“skywalking-plugin.def”); 就可以获取插件定义
在这里插入图片描述

完整源代码

AgentClassLoader

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 */

package org.apache.skywalking.apm.agent.core.plugin.loader;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import lombok.RequiredArgsConstructor;
import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
import org.apache.skywalking.apm.agent.core.boot.PluginConfig;
import org.apache.skywalking.apm.agent.core.conf.Config;
import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap;

/**
 * The <code>AgentClassLoader</code> represents a classloader, which is in charge of finding plugins and interceptors.
 */
public class AgentClassLoader extends ClassLoader {

    static {
        /*
         * Try to solve the classloader dead lock. See https://github.com/apache/skywalking/pull/2016
         */
        registerAsParallelCapable();
    }

    private static final ILog LOGGER = LogManager.getLogger(AgentClassLoader.class);
    /**
     * The default class loader for the agent.
     */
    private static AgentClassLoader DEFAULT_LOADER;

    private List<File> classpath;
    private List<Jar> allJars;
    private ReentrantLock jarScanLock = new ReentrantLock();

    public static AgentClassLoader getDefault() {
        return DEFAULT_LOADER;
    }

    /**
     * Init the default class loader.
     *
     * @throws AgentPackageNotFoundException if agent package is not found.
     */
    public static void initDefaultLoader() throws AgentPackageNotFoundException {
        if (DEFAULT_LOADER == null) {
            synchronized (AgentClassLoader.class) {
                if (DEFAULT_LOADER == null) {
                    DEFAULT_LOADER = new AgentClassLoader(PluginBootstrap.class.getClassLoader());
                }
            }
        }
    }

    public AgentClassLoader(ClassLoader parent) throws AgentPackageNotFoundException {
        super(parent);
        File agentDictionary = AgentPackagePath.getPath();
        classpath = new LinkedList<>();
        Config.Plugin.MOUNT.forEach(mountFolder -> classpath.add(new File(agentDictionary, mountFolder)));
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        List<Jar> allJars = getAllJars();
        String path = name.replace('.', '/').concat(".class");
        for (Jar jar : allJars) {
            JarEntry entry = jar.jarFile.getJarEntry(path);
            if (entry == null) {
                continue;
            }
            try {
                URL classFileUrl = new URL("jar:file:" + jar.sourceFile.getAbsolutePath() + "!/" + path);
                byte[] data;
                try (final BufferedInputStream is = new BufferedInputStream(
                    classFileUrl.openStream()); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                    int ch;
                    while ((ch = is.read()) != -1) {
                        baos.write(ch);
                    }
                    data = baos.toByteArray();
                }
                return processLoadedClass(defineClass(name, data, 0, data.length));
            } catch (IOException e) {
                LOGGER.error(e, "find class fail.");
            }
        }
        throw new ClassNotFoundException("Can't find " + name);
    }

    @Override
    protected URL findResource(String name) {
        List<Jar> allJars = getAllJars();
        for (Jar jar : allJars) {
            JarEntry entry = jar.jarFile.getJarEntry(name);
            if (entry != null) {
                try {
                    return new URL("jar:file:" + jar.sourceFile.getAbsolutePath() + "!/" + name);
                } catch (MalformedURLException ignored) {
                }
            }
        }
        return null;
    }

    @Override
    protected Enumeration<URL> findResources(String name) throws IOException {
        List<URL> allResources = new LinkedList<>();
        List<Jar> allJars = getAllJars();
        for (Jar jar : allJars) {
            JarEntry entry = jar.jarFile.getJarEntry(name);
            if (entry != null) {
                allResources.add(new URL("jar:file:" + jar.sourceFile.getAbsolutePath() + "!/" + name));
            }
        }

        final Iterator<URL> iterator = allResources.iterator();
        return new Enumeration<URL>() {
            @Override
            public boolean hasMoreElements() {
                return iterator.hasNext();
            }

            @Override
            public URL nextElement() {
                return iterator.next();
            }
        };
    }

    private Class<?> processLoadedClass(Class<?> loadedClass) {
        final PluginConfig pluginConfig = loadedClass.getAnnotation(PluginConfig.class);
        if (pluginConfig != null) {
            // Set up the plugin config when loaded by class loader at the first time.
            // Agent class loader just loaded limited classes in the plugin jar(s), so the cost of this
            // isAssignableFrom would be also very limited.
            SnifferConfigInitializer.initializeConfig(pluginConfig.root());
        }

        return loadedClass;
    }

    private List<Jar> getAllJars() {
        if (allJars == null) {
            jarScanLock.lock();
            try {
                if (allJars == null) {
                    allJars = doGetJars();
                }
            } finally {
                jarScanLock.unlock();
            }
        }

        return allJars;
    }

    private LinkedList<Jar> doGetJars() {
        LinkedList<Jar> jars = new LinkedList<>();
        for (File path : classpath) {
            if (path.exists() && path.isDirectory()) {
                String[] jarFileNames = path.list((dir, name) -> name.endsWith(".jar"));
                for (String fileName : jarFileNames) {
                    try {
                        File file = new File(path, fileName);
                        Jar jar = new Jar(new JarFile(file), file);
                        jars.add(jar);
                        LOGGER.info("{} loaded.", file.toString());
                    } catch (IOException e) {
                        LOGGER.error(e, "{} jar file can't be resolved", fileName);
                    }
                }
            }
        }
        return jars;
    }

    @RequiredArgsConstructor
    private static class Jar {
        private final JarFile jarFile;
        private final File sourceFile;
    }
}

PluginResourcesResolver 获取skywalking-plugin.def插件定义

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 */

package org.apache.skywalking.apm.agent.core.plugin;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import org.apache.skywalking.apm.agent.core.plugin.loader.AgentClassLoader;

/**
 * Use the current classloader to read all plugin define file. The file must be named 'skywalking-plugin.def'
 */
public class PluginResourcesResolver {
    private static final ILog LOGGER = LogManager.getLogger(PluginResourcesResolver.class);

    public List<URL> getResources() {
        List<URL> cfgUrlPaths = new ArrayList<URL>();
        Enumeration<URL> urls;
        try {
            urls = AgentClassLoader.getDefault().getResources("skywalking-plugin.def");

            while (urls.hasMoreElements()) {
                URL pluginUrl = urls.nextElement();
                cfgUrlPaths.add(pluginUrl);
                LOGGER.info("find skywalking plugin define in {}", pluginUrl);
            }

            return cfgUrlPaths;
        } catch (IOException e) {
            LOGGER.error("read resources failure.", e);
        }
        return null;
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/342642.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spring Boot学习篇(十三)

Spring Boot学习篇(十三) shiro安全框架使用篇(五) 1 准备工作 1.1 在SysUserMapper.xml中书写自定义标签 <select id"findRoles" resultType"string">select name from sys_role where id (select roleid from sys_user_role where userid (S…

Matlab论文插图绘制模板第77期—对数刻度横向柱状图

在之前的文章中&#xff0c;分享了Matlab对数刻度柱状图的绘制模板&#xff1a; 进一步&#xff0c;再来看一下对数刻度横向柱状图的绘制模板。 先来看一下成品效果&#xff1a; 特别提示&#xff1a;Matlab论文插图绘制模板系列&#xff0c;旨在降低大家使用Matlab进行科研绘…

[WTL/ATL]_[初级]_[TreeView控件如何显示ToolTip]

场景 在开发界面程序时&#xff0c;CTreeViewCtrl(它实际内部封装的就是Win32的TreeView控件)一般会用来作为选择某些类型的树形菜单&#xff0c;点击某项的时候&#xff0c;右边能显示某些对应的数据。当这个控件的宽度固定时&#xff0c;有时候每行的文本项可能由于过长从而…

【图像分类】基于PyTorch搭建LSTM实现MNIST手写数字体识别(双向LSTM,附完整代码和数据集)

写在前面&#xff1a; 首先感谢兄弟们的关注和订阅&#xff0c;让我有创作的动力&#xff0c;在创作过程我会尽最大能力&#xff0c;保证作品的质量&#xff0c;如果有问题&#xff0c;可以私信我&#xff0c;让我们携手共进&#xff0c;共创辉煌。 在https://blog.csdn.net/A…

【CSS】元素居中总结-水平居中、垂直居中、水平垂直居中

【CSS】元素居中一、 水平居中1.行内元素水平居中&#xff08;1&#xff09;text-align2.块级元素水平居中2.1 margin&#xff08;1&#xff09;margin2.2布局&#xff08;1&#xff09;flex justify-content&#xff08;推荐&#xff09;&#xff08;2&#xff09; flexmargin…

张驰咨询:关于六西格玛,有一些常见的疑惑!

​ 很多想要学习六西格玛的学员&#xff0c;经常会有这些困惑&#xff1a; 以前没有接触过六西格玛&#xff0c;需要什么基础吗&#xff1f;自学还是培训&#xff1f;哪些行业会用到六西格玛呢&#xff1f;学习六西格玛对以后的工作有哪些帮助&#xff1f;如何选择六西格玛培…

STM32配置读取双路24位模数转换(24bit ADC)芯片CS1238数据

STM32配置读取双路24位模数转换&#xff08;24bit ADC&#xff09;芯片CS1238数据 CS1238是一款国产双路24位ADC芯片&#xff0c;与CS1238对应的单路24位ADC芯片是CS1237&#xff0c;功能上相当于HX711和TM7711的组合。其功能如下所示&#xff1a; 市面上的模块&#xff1a; …

股票买卖接口怎么来的?

现在股票买卖接口主要是在线上研发&#xff0c;有专业的开发团队进行源码开发和完善&#xff0c;但是&#xff0c;常常会在开发过程中出现问题&#xff0c;也就是遇到一些特殊的情况需要及时处理&#xff0c;那么股票买卖接口怎么开发实现出来的&#xff1f;一、股票买卖接口开…

案例分享| 助力数字化转型:广州期货交易所全栈信创项目管理平台上线

广州期货交易所项目管理平台基于易趋&#xff08;easytrack&#xff09;进行实施&#xff0c;通过近半年的开发及试运行&#xff0c;现已成功交付上线、推广使用&#xff0c;取得了良好的应用效果。1. 关于广州期货交易所&#xff08;以下简称广期所&#xff09;广期所于2021年…

MySQL8.0安装教程

文章目录1.官网下载MySQL2.下载完记住解压的地址&#xff08;一会用到&#xff09;3.进入刚刚解压的文件夹下&#xff0c;创建data和my.ini在根目录下创建一个txt文件&#xff0c;名字叫my&#xff0c;文件后缀为ini&#xff0c;之后复制下面这个代码放在my.ini文件下&#xff…

华为手表开发:WATCH 3 Pro(4)创建项目 + 首页新建按钮,修改初始文本

华为手表开发&#xff1a;WATCH 3 Pro&#xff08;4&#xff09;创建项目 首页新建按钮&#xff0c;修改初始文本初环境与设备创建项目创建项目入口配置项目认识目录结构修改首页初始文本文件名&#xff1a;index.hml新建按钮 “ 按钮 ”index.hml初 鸿蒙可穿戴开发 希望能写…

直播预告 | 对谈谷歌云 DORA 布道师:聊聊最关键的四个 DevOps 表现指标

本期分享 DORA 的全称是 DevOps Research and Assessment&#xff0c;是一个致力于 DevOps 调研与研究的组织&#xff0c;2018 年加入 Google Cloud。自 2014 年起&#xff0c;DORA 每年会发布一份行业报告&#xff0c;基于对数千名从业者的调研&#xff0c;分析高效能团队与低…

联想K14电脑开机全屏变成绿色无法使用怎么U盘重装系统?

联想K14电脑开机全屏变成绿色无法使用怎么U盘重装系统&#xff1f;最近有用户使用联想K14电脑的时候&#xff0c;开机后桌面就变成了绿色的背景显示&#xff0c;无法进行任何的操作。而且通过强制重启之后还是会出现这个问题&#xff0c;那么这个情况如何去进行系统重装呢&…

PMP证书要怎么考,含金量怎么样?

很多朋友在对PMP不是了解的时候&#xff0c;会有些犹豫&#xff0c;PMP证书到底值不值得考。考下来有用吗&#xff1f; PMP证书当然有用&#xff0c;要含金量有含金量&#xff0c;要专业知识有专业知识&#xff0c;不过要是考了不用&#xff0c;久而久之忘了学习的内容&#x…

怿星科技校招礼盒:我想开了

校招礼盒大揭秘为了帮助2023届新同学快速了解怿星文化增强认同感经过1个多月的精心准备我们的校招大礼盒终于跟大家见面啦&#xff01;&#xff01;我们用了大量的公司IP形象-小怿通过各式各样的姿势和表情欢迎新同学的到来搭配着IP的蓝色色调传递出一种科幻与探索的感觉希望加…

计算机组成原理:1. 计算机系统概论

更好的阅读体验\huge{\color{red}{更好的阅读体验}}更好的阅读体验 文章目录1.1 计算机系统简介1.1.1 计算机软硬件概念1.1.2 计算机的层次1.1.3计算机组成和计算机体系结构1.2 计算机的基本组成1.2.1 冯诺伊曼计算机的特点1.2.2 计算机的硬件框图1.2.3 计算机的工作步骤1.3 计…

问卷调查会遇到哪些问题?怎么解决?

提到问卷调查我们并不陌生&#xff0c;它经常被用作调查市场、观察某类群体的行为特征等多种调查中。通过问卷调查得出的数据能够非常真实反映出是市场的现状和变化趋势&#xff0c;所以大家经常使用这个方法进行调查研究。不过&#xff0c;很多人在进行问卷调查的时候也会遇到…

JAVA八股、JAVA面经

还有三天面一个JAVA软件开发岗&#xff0c;之前完全没学过JAVA&#xff0c;整理一些面经...... 大佬整理的&#xff1a;Java面试必备八股文_-半度的博客-CSDN博客 另JAVA学习资料&#xff1a;Java | CS-Notes Java 基础Java 容器Java 并发Java 虚拟机Java IO目录 int和Inte…

电商新趋势来临!?解析Dtop 环球嘉年华电商是否值得加入!

近年来,电商平台的发展瞬息万变,加上疫情的推波助澜,让全球的电子商务来到前所未有的光景,营业销售额直达颠覆性的增长。 许多商家也因此纷涌而入,谋划分得电子商务的一杯羹。随着参与成为电商的商家日益剧增,商家们想从中谋利也不是件易事。再加上市场不断洗牌的形势下,传统电…

楔形文字的起源全2课-北京大学拱玉书 笔记

楔形文字的起源全2课-北京大学拱玉书 说明&#xff1a;以下图片素材均出自视频 【楔形文字的文源】 《吉尔伽美什史诗》记载的楔形文字起源&#xff1a; 学术界对楔形文字起源的总结&#xff1a; 【关于文字的演化理论发展史】 威廉.瓦尔伯顿提出文字“叙事图画”演变说…