}
}
}
public ExtensionsManager require(String name) throws Exception { //todo: improve the Exceptions
Extension extension = null;
Map<String, Object> yaml = extensionsConfigMap.get(name);
File extensionLibDir;
if (yaml != null && !registeredExtensions.contains(name)) {
final String extensionLibFolderName = name + "/lib";
if (extensionsFolder == null) {
extensionLibDir = new File(site.getApplicationConfigPath(), "/extensions/" + extensionLibFolderName);
} else {
extensionLibDir = new File(extensionsFolder, extensionLibFolderName);
}
Class[] parameters = new Class[]{URL.class};
URLClassLoader microClassLoader = (URLClassLoader) Micro.class.getClassLoader();
Class sysclass = URLClassLoader.class;
if (extensionLibDir.exists() && extensionLibDir.isDirectory()) {
for (File file : site.files(extensionLibDir, ".jar")) {
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(microClassLoader, file.toURI().toURL());
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}
}
try {
Class classToLoad = Class.forName((String) yaml.get("class"), true, microClassLoader);
extension = (Extension) classToLoad.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new Exception(String.format("Class: %s, not found.", yaml.get("class")));
}
}
if (extension == null) {
// check if the Extension is using a Micro class...
final Class klass = ClassUtilities.loadClass((String) yaml.get("class"));
if (klass != null) {
extension = (Extension) klass.newInstance();
}
}
if (extension != null) {
extension.register(name, site, yaml);
if (registeredExtensions.isEmpty()) { //cosmetics
site.getLog().info("Extensions:");
}
registeredExtensions.add(extension.getName());
site.getLog().info(String.format(" - %s, loaded.", extension.getName()));
} else {
site.getLog().error(String.format(" %s, not loaded.", name));
}
}
return this;