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();
}
}
Aggregations