aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/AbstractPluginLoader.java
blob: f85d69c354c6c87a1249de5440408ed9a8fc7d01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver;

import java.util.logging.Level;
import com.yahoo.plugin.Plugin;
import com.yahoo.plugin.SystemPropertyConfig;

import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;

/**
 * TODO: describe class
 *
 * @author Stig Bakken
 */
public abstract class AbstractPluginLoader implements PluginLoader {

    private static final Logger log = Logger.getLogger(AbstractPluginLoader.class.getName());

    public abstract void loadPlugins();

    protected void loadFromClass(Class<? extends Plugin> pluginClass) {
        Plugin plugin;
        try {
            plugin = pluginClass.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            log.log(Level.SEVERE, pluginClass.getName() + ": load failed: " + e);
            throw new RuntimeException(e);
        }

        String pname = plugin.getPluginName();
        String prefix = Server.APPNAME + "." + pname + ".";
        SystemPropertyConfig config = new SystemPropertyConfig(prefix);
        String enable = config.get("enable", "true");

        if (! enable.equals("true")) {
            log.log(Level.INFO, pname + ": plugin disabled by config");
            return;
        }

        try {
            plugin.initPlugin(config);
            log.log(Level.FINE, pname + ": plugin loaded");
        } catch (Exception e) {
            log.log(Level.SEVERE, pname + ": init failed", e);
        }
    }
}