Created
September 4, 2012 21:03
-
-
Save volgar1x/3626432 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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