PluginBootstrap.java 2.2 KB
Newer Older
1
package org.skywalking.apm.agent.core.plugin;
2

A
ascrutae 已提交
3
import net.bytebuddy.pool.TypePool;
P
pengys5 已提交
4 5
import org.skywalking.apm.logging.ILog;
import org.skywalking.apm.logging.LogManager;
6

7
import java.net.URL;
8
import java.util.ArrayList;
9
import java.util.List;
10

11 12 13 14 15 16 17
/**
 * Plugins finder.
 * Use {@link PluginResourcesResolver} to find all plugins,
 * and ask {@link PluginCfg} to load all plugin definitions.
 *
 * @author wusheng
 */
18
public class PluginBootstrap {
19
    private static final ILog logger = LogManager.getLogger(PluginBootstrap.class);
20

21 22
    /**
     * load all plugins.
23
     *
24 25
     * @return plugin definition list.
     */
26
    public List<AbstractClassEnhancePluginDefine> loadPlugins() {
wu-sheng's avatar
wu-sheng 已提交
27
        TypePool classTypePool = TypePool.Default.ofClassPath();
28 29 30 31 32

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

        if (resources == null || resources.size() == 0) {
33
            logger.info("no plugin files (skywalking-plugin.properties) found, continue to start application.");
34
            return new ArrayList<AbstractClassEnhancePluginDefine>();
35 36 37 38
        }

        for (URL pluginUrl : resources) {
            try {
wu-sheng's avatar
wu-sheng 已提交
39
                PluginCfg.INSTANCE.load(pluginUrl.openStream());
40
            } catch (Throwable t) {
41
                logger.error(t, "plugin [{}] init failure.", pluginUrl);
42 43 44
            }
        }

45
        List<PluginDefine> pluginClassList = PluginCfg.INSTANCE.getPluginClassList();
46

47
        List<AbstractClassEnhancePluginDefine> plugins = new ArrayList<AbstractClassEnhancePluginDefine>();
48
        for (PluginDefine pluginDefine : pluginClassList) {
49
            try {
50
                logger.debug("loading plugin class {}.", pluginDefine.getDefineClass());
51
                AbstractClassEnhancePluginDefine plugin =
52
                    (AbstractClassEnhancePluginDefine) Class.forName(pluginDefine.getDefineClass()).newInstance();
wu-sheng's avatar
wu-sheng 已提交
53
                plugin.setClassTypePool(classTypePool);
54
                plugins.add(plugin);
55
            } catch (Throwable t) {
56
                logger.error(t, "load plugin [{}] failure.", pluginDefine.getDefineClass());
57 58
            }
        }
59

60 61
        return plugins;

62
    }
63

64
}