change structure of ModuleManager

This commit is contained in:
DNx5 2015-09-15 23:11:45 +07:00
parent 24c7efbd94
commit f2e207dab1

View File

@ -1,5 +1,9 @@
package fr.xephi.authme.modules; package fr.xephi.authme.modules;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.Settings;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
@ -10,56 +14,69 @@ import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import fr.xephi.authme.AuthMe; public class ModuleManager {
import fr.xephi.authme.ConsoleLogger;
public class ModuleManager implements Module {
private AuthMe plugin; private AuthMe plugin;
private ModuleManager instance;
private List<Module> modules = new ArrayList<Module>(); private List<Module> modules = new ArrayList<>();
public ModuleManager(AuthMe plugin) { public ModuleManager(AuthMe plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@Override public boolean isModuleEnabled(String name) {
public String getName() { for (Module m : modules) {
return "AuthMe Module Manager"; if (m.getName().equalsIgnoreCase(name))
return true;
}
return false;
} }
@Override public boolean isModuleEnabled(Module.ModuleType type) {
public AuthMe getInstanceOfAuthMe() { for (Module m : modules) {
return this.plugin; if (m.getType() == type)
return true;
}
return false;
} }
@Override public Module getModule(String name) {
public Module getInstance() { for (Module m : modules) {
if (this.instance == null) if (m.getName().equalsIgnoreCase(name))
instance = new ModuleManager(AuthMe.getInstance()); return m;
return instance; }
return null;
} }
@Override public Module getModule(Module.ModuleType type) {
public ModuleType getType() { for (Module m : modules) {
return (Module.ModuleType.MANAGER); if (m.getType() == type)
return m;
}
return null;
}
public int loadModules() {
File dir = Settings.MODULE_FOLDER;
int count = 0;
if (!dir.isDirectory()) {
dir.mkdirs();
return count;
} }
@Override
public boolean load() {
File dir = new File(plugin.getDataFolder() + File.separator + "modules");
File[] files = dir.listFiles(); File[] files = dir.listFiles();
if (files == null) { if (files == null) {
return false; return count;
} }
for (File pathToJar : files) { for (File pathToJar : files) {
JarFile jarFile = null; JarFile jarFile = null;
URLClassLoader cl = null;
try { try {
jarFile = new JarFile(pathToJar); jarFile = new JarFile(pathToJar);
Enumeration<?> e = jarFile.entries();
URL[] urls = {new URL("jar:file:" + pathToJar.getAbsolutePath() + "!/")}; URL[] urls = {new URL("jar:file:" + pathToJar.getAbsolutePath() + "!/")};
URLClassLoader cl = URLClassLoader.newInstance(urls); cl = URLClassLoader.newInstance(urls);
Enumeration<?> e = jarFile.entries();
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
JarEntry je = (JarEntry) e.nextElement(); JarEntry je = (JarEntry) e.nextElement();
if (je.isDirectory() || !je.getName().endsWith("Main.class")) { if (je.isDirectory() || !je.getName().endsWith("Main.class")) {
@ -68,35 +85,40 @@ public class ModuleManager implements Module {
String className = je.getName().substring(0, je.getName().length() - 6); String className = je.getName().substring(0, je.getName().length() - 6);
className = className.replace('/', '.'); className = className.replace('/', '.');
Class<?> c = cl.loadClass(className); Class<?> c = cl.loadClass(className);
if (!Module.class.isAssignableFrom(c)) {
continue;
}
Module mod = (Module) c.newInstance(); Module mod = (Module) c.newInstance();
mod.load(); mod.load();
modules.add(mod); modules.add(mod);
count++;
break; break;
} }
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.writeStackTrace(ex);
ConsoleLogger.showError("Cannot load " + pathToJar.getName() + " jar file !"); ConsoleLogger.showError("Cannot load " + pathToJar.getName() + " jar file !");
} finally { } finally {
if (jarFile != null)
try { try {
if (jarFile != null) {
jarFile.close(); jarFile.close();
} catch (IOException e) { }
if (cl != null) {
cl.close();
}
} catch (IOException ignored) {
} }
} }
} }
return true; return count;
} }
@Override public void unloadModules() {
public boolean unload() { for (Module m : modules) {
try { m.unload();
for (Module mod : modules) { modules.remove(m);
mod.unload();
modules.remove(mod);
} }
} catch (Exception e) {
}
return true;
} }
} }