Skip to content

Instantly share code, notes, and snippets.

@volgar1x
Created September 4, 2012 21:03
Show Gist options
  • Save volgar1x/3626432 to your computer and use it in GitHub Desktop.
Save volgar1x/3626432 to your computer and use it in GitHub Desktop.
URLClassLoader loader;
try {
loader = new URLClassLoader(new URL[] {
file.toURI().toURL()
}, ClassLoader.getSystemClassLoader());
} catch (Exception e) {
log.error("can't load {} because : {}", file.getPath(), e.getMessage());
return null;
}
InputStream is = loader.getResourceAsStream("plugin_class");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String className;
try {
className = reader.readLine();
} catch (IOException e) {
log.error("can't read plugin_class file");
try {
loader.close();
} catch (IOException ignored) { }
return null;
}
Class<?> clazz;
try {
clazz = loader.loadClass(className);
} catch (ClassNotFoundException e) {
log.error("can't load {} class", className);
try {
loader.close();
} catch (IOException ignored) { }
return null;
}
if (!clazz.isAssignableFrom(Plugin.class)) {
log.error("{} is not a plugin", className);
try {
loader.close();
} catch (IOException ignored) { }
return null;
}
try {
try {
loader.close();
} catch (IOException ignored) { }
return (Plugin) clazz.newInstance();
} catch (Exception e) {
log.error("can't instantiate {} because : {}", className, e.getMessage());
}
try {
loader.close();
} catch (IOException ignored) { }
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment