Search in sources :

Example 1 with Plugin

use of io.gomint.proxprox.api.plugin.Plugin in project ProxProx by GoMint.

the class Commandplugins method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    Collection<Plugin> plugins = pluginManager.getPlugins();
    sender.sendMessage(ProxProx.PROX_PREFIX + ChatColor.RED + "Plugins (" + ChatColor.YELLOW + plugins.size() + ChatColor.RED + ")");
    for (Plugin plugin : plugins) {
        sender.sendMessage(ProxProx.PROX_PREFIX + ChatColor.RED + " - " + ChatColor.GREEN + plugin.getMeta().getName() + ChatColor.RED + " version " + ChatColor.YELLOW + plugin.getMeta().getVersion());
    }
}
Also used : Plugin(io.gomint.proxprox.api.plugin.Plugin)

Example 2 with Plugin

use of io.gomint.proxprox.api.plugin.Plugin in project ProxProx by GoMint.

the class PluginManager method loadPlugins.

/**
 * Load all plugins
 */
public void loadPlugins() {
    Map<PluginMeta, Boolean> pluginStatuses = new HashMap<>();
    // Put in all currently loaded Plugins
    for (Map.Entry<String, Plugin> namePluginEntry : plugins.entrySet()) {
        pluginStatuses.put(namePluginEntry.getValue().getMeta(), true);
    }
    for (Map.Entry<String, PluginMeta> entry : toLoad.entrySet()) {
        PluginMeta plugin = entry.getValue();
        if (!enablePlugin(pluginStatuses, new Stack<>(), plugin)) {
            logger.warn("Failed to enable " + entry.getKey());
        }
    }
    toLoad.clear();
    toLoad = null;
}
Also used : PluginMeta(io.gomint.proxprox.api.plugin.PluginMeta) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Plugin(io.gomint.proxprox.api.plugin.Plugin)

Example 3 with Plugin

use of io.gomint.proxprox.api.plugin.Plugin in project ProxProx by GoMint.

the class PluginManager method disablePlugin.

/**
 * Disable a specific given Plugin
 *
 * @param plugin The plugin which should be disabled
 */
public void disablePlugin(Plugin plugin) {
    // Check for security
    if (!CallerDetectorUtil.getCallerClassName(2).equals(plugin.getClass().getName())) {
        throw new SecurityException("Plugins can only disable themselves");
    }
    // Check if this plugin is still enabled
    if (!getPlugins().contains(plugin)) {
        return;
    }
    // Check which Plugins also need to be disabled
    for (Plugin plugin1 : new ArrayList<>(getPlugins())) {
        if (!plugin1.equals(plugin) && plugin1.getMeta().getDepends().contains(plugin.getMeta().getName())) {
            disablePlugin(plugin1);
        }
    }
    // Tell the Plugin we disable
    logger.info("Disabling plugin " + plugin.getMeta().getName());
    // CHECKSTYLE:OFF
    try {
        plugin.onUninstall();
    } catch (Exception e) {
        logger.warn("Error while disabling " + plugin.getMeta().getName(), e);
    }
    // CHECKSTYLE:ON
    // Disable this
    unregisterCommands(plugin);
    unregisterListeners(plugin);
    ((PluginScheduler) plugin.getScheduler()).cleanup();
    ((PluginClassLoader) plugin.getClass().getClassLoader()).remove();
    // Remove from Pluginslist
    plugins.remove(plugin.getMeta().getName());
}
Also used : Plugin(io.gomint.proxprox.api.plugin.Plugin) PluginScheduler(io.gomint.proxprox.scheduler.PluginScheduler)

Example 4 with Plugin

use of io.gomint.proxprox.api.plugin.Plugin in project ProxProx by GoMint.

the class PluginManager method enablePlugin.

private boolean enablePlugin(Map<PluginMeta, Boolean> pluginStatuses, Stack<PluginMeta> dependStack, PluginMeta plugin) {
    // Fast out when already loaded
    if (pluginStatuses.containsKey(plugin)) {
        return pluginStatuses.get(plugin);
    }
    // combine all dependencies for 'for loop'
    Set<String> dependencies = new HashSet<>();
    if (plugin.getDepends() != null) {
        dependencies.addAll(plugin.getDepends());
    }
    // success status
    boolean status = true;
    // try to load dependencies first
    for (String dependName : dependencies) {
        PluginMeta depend = toLoad.containsKey(dependName) ? toLoad.get(dependName) : (plugins.containsKey(dependName)) ? plugins.get(dependName).getMeta() : null;
        Boolean dependStatus = (depend != null) ? pluginStatuses.get(depend) : Boolean.FALSE;
        if (dependStatus == null) {
            if (dependStack.contains(depend)) {
                StringBuilder dependencyGraph = new StringBuilder();
                for (PluginMeta element : dependStack) {
                    dependencyGraph.append(element.getName()).append(" -> ");
                }
                dependencyGraph.append(plugin.getName()).append(" -> ").append(dependName);
                logger.warn("Circular dependency detected: " + dependencyGraph);
                status = false;
            } else {
                dependStack.push(plugin);
                dependStatus = this.enablePlugin(pluginStatuses, dependStack, depend);
                dependStack.pop();
            }
        }
        if (dependStatus == Boolean.FALSE) {
            logger.warn("%s (required by %s) is unavailable", String.valueOf(dependName), plugin.getName());
            status = false;
        }
        if (!status) {
            break;
        }
    }
    // do actual loading
    if (status) {
        try {
            URLClassLoader loader = new PluginClassLoader(new URL[] { plugin.getPluginFile().toURI().toURL() });
            Class<?> main = loader.loadClass(plugin.getMainClass());
            Plugin clazz = (Plugin) main.getConstructor().newInstance();
            // TODO: Secure this
            clazz.setMeta(plugin);
            clazz.setPluginManager(this);
            clazz.setLogger(LoggerFactory.getLogger(main));
            clazz.setScheduler(new PluginScheduler(clazz, this.scheduler));
            toEnable.put(plugin.getName(), clazz);
            clazz.onStartup();
            logger.info("Loaded plugin {} version {}", plugin.getName(), plugin.getVersion());
        } catch (Throwable t) {
            logger.warn("Error enabling plugin " + plugin.getName(), t);
        }
    }
    pluginStatuses.put(plugin, status);
    return status;
}
Also used : PluginMeta(io.gomint.proxprox.api.plugin.PluginMeta) URLClassLoader(java.net.URLClassLoader) Plugin(io.gomint.proxprox.api.plugin.Plugin) PluginScheduler(io.gomint.proxprox.scheduler.PluginScheduler)

Aggregations

Plugin (io.gomint.proxprox.api.plugin.Plugin)4 PluginMeta (io.gomint.proxprox.api.plugin.PluginMeta)2 PluginScheduler (io.gomint.proxprox.scheduler.PluginScheduler)2 URLClassLoader (java.net.URLClassLoader)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1