Search in sources :

Example 1 with BotPlugin

use of org.zeroturnaround.skypebot.plugins.BotPlugin in project skype-bot by toomasr.

the class PluginScanner method scan.

public Collection<BotPlugin> scan(File file) {
    List<BotPlugin> plugins = new ArrayList<BotPlugin>();
    try (JarFile jar = new JarFile(file)) {
        URLClassLoader classloader = new URLClassLoader(new URL[] { file.toURI().toURL() });
        Plugins.registerPluginClassloader(file, classloader);
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.isDirectory() || !entry.getName().startsWith("org/zeroturnaround/skypebot") || !entry.getName().endsWith(".class")) {
                continue;
            }
            // we loaded all the classes from this jar file. we can close the classloader
            Class<?> clazz = classloader.loadClass(entry.getName().replace('/', '.').replace(".class", ""));
            if (clazz.getAnnotation(SkypeBotPlugin.class) != null) {
                Object pluginInstance = clazz.newInstance();
                if (BotPlugin.class.isInstance(pluginInstance)) {
                    log.info("Found a plugin: {}", clazz.getSimpleName());
                    plugins.add(BotPlugin.class.cast(pluginInstance));
                }
            }
        }
        return plugins;
    } catch (Exception e) {
        log.error("Failed to scan file " + file + " for plugin", e);
        return Collections.emptyList();
    }
}
Also used : SkypeBotPlugin(org.zeroturnaround.skypebot.plugins.SkypeBotPlugin) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) SkypeBotPlugin(org.zeroturnaround.skypebot.plugins.SkypeBotPlugin) BotPlugin(org.zeroturnaround.skypebot.plugins.BotPlugin) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Aggregations

URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 BotPlugin (org.zeroturnaround.skypebot.plugins.BotPlugin)1 SkypeBotPlugin (org.zeroturnaround.skypebot.plugins.SkypeBotPlugin)1