package ie.dcu.plugin; import java.io.File; import java.util.*; import java.util.logging.Logger; /** * I should probably use OSGi or something for this, but I couldn't be * bothered... * * * @author Kevin McGuinness */ public class PluginManager { private static final Logger log = Logger.getLogger("PluginManager"); private List searchPath; private List plugins; public PluginManager() { searchPath = new ArrayList(); plugins = new ArrayList(); } public List searchPath() { return searchPath; } public List plugins() { return Collections.unmodifiableList(plugins); } public void loadPlugins() { for (String folder : searchPath()) { loadPluginsFrom(folder); } } private void loadPluginsFrom(String path) { log.info("Loading plugins from: " + path); File file = new File(path); if (file.isDirectory()) { loadPluginFromFromFolder(file); } } private void loadPluginFromFromFolder(File file) { for (File f : file.listFiles()) { if (f.isDirectory() && f.getName().endsWith(".plugin")) { loadPlugin(f); } } } public boolean hasPlugin(File pluginFolder) { String name = pluginFolder.getName(); for (Plugin p : plugins) { if (p.getRootFolder().getName().equals(name)) { return true; } } return false; } private void loadPlugin(File pluginFile) { if (!hasPlugin(pluginFile)) { log.info("Loading plugin: " + pluginFile.getName()); PluginLoader loader = new PluginLoader(pluginFile); try { Plugin plugin = loader.load(); plugins.add(plugin); } catch (PluginException ex) { log.severe("Error loading plugin: " + ex.getMessage()); } } else { log.info("Skipping plugin (already loaded): " + pluginFile.getName()); } } }