View Javadoc
1 /* 2 * (C) 2002 David Carr david@carr.name 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package net.sourceforge.mflow.impl; 20 21 import java.io.File; 22 import java.io.FilenameFilter; 23 import java.lang.reflect.InvocationTargetException; 24 import java.util.ArrayList; 25 import java.util.Arrays; 26 import java.util.Iterator; 27 import java.util.List; 28 import java.util.logging.Logger; 29 import java.util.prefs.BackingStoreException; 30 import java.util.prefs.Preferences; 31 32 import net.sourceforge.mflow.api.AccountManager; 33 import net.sourceforge.mflow.api.ComponentManager; 34 import net.sourceforge.mflow.api.ContactManager; 35 import net.sourceforge.mflow.api.ContactMethodManager; 36 import net.sourceforge.mflow.api.MFlowUIManager; 37 import net.sourceforge.mflow.api.Plugin; 38 import net.sourceforge.mflow.api.PluginManager; 39 import net.sourceforge.mflow.util.FilenameExtensionFilter; 40 41 /*** 42 * Manages MFlow plugins 43 * 44 * @author <a href="mailto:david@carr.name">David Carr</a> 45 * @version $Revision: 1.4 $ 46 */ 47 public class PluginManagerImpl implements PluginManager { 48 private static Logger log = 49 Logger.getLogger(PluginManagerImpl.class.getName()); 50 51 private PluginLocator pluginLocator = new PluginLocator(this); 52 53 private static final String APP_DIRECTORY = ".mflow"; 54 private static final String PLUGIN_PATH = 55 APP_DIRECTORY + File.separator + "plugins"; 56 57 private List plugins = new ArrayList(); 58 59 private ContactMethodManager contactMethodManager = 60 new ContactMethodManagerImpl(this); 61 private ComponentManager componentManager = new ComponentManagerImpl(this); 62 private MFlowUIManager uiManager = new MFlowUIManagerImpl(); 63 private ContactManager contactManager = new ContactManagerImpl(); 64 private AccountManager accountManager = new AccountManagerImpl(); 65 66 /*** 67 * Default constructor; 68 * makes appropriate managers and populates the plugin list 69 */ 70 public PluginManagerImpl() { 71 log.entering("PluginManager", "PluginManager"); 72 MsgFactoryImpl.register(); 73 ContentHandlerFactoryImpl.register(); 74 refreshPluginList(); 75 log.exiting("PluginManager", "PluginManager"); 76 } 77 78 /*** 79 * Returns an array of all known plugins 80 * 81 * @return an array of all known plugins 82 */ 83 public synchronized Plugin[] getKnownPlugins() { 84 return (Plugin[]) this.plugins.toArray(new Plugin[this.plugins.size()]); 85 } 86 87 /*** 88 * Updates the list of known plugins 89 */ 90 public synchronized void refreshPluginList() { 91 log.entering("PluginManager", "refreshPluginList"); 92 this.plugins.clear(); 93 94 String userDir = System.getProperty("user.dir"); 95 String userHome = System.getProperty("user.home"); 96 String javaHome = System.getProperty("java.home"); 97 98 log.fine("User dir: " + userDir); 99 log.fine("User home: " + userHome); 100 log.fine("Java home: " + javaHome); 101 102 List files = new ArrayList(); 103 addJarFiles(new File(userDir, PLUGIN_PATH), files); 104 addJarFiles(new File(userHome, PLUGIN_PATH), files); 105 addJarFiles(new File(javaHome, PLUGIN_PATH), files); 106 addPlugins(files); 107 loadPlugins(); 108 log.exiting("PluginManager", "refreshPluginList"); 109 } 110 111 private void addJarFiles(File directory, List files) { 112 FilenameFilter filter = new FilenameExtensionFilter("jar"); 113 File[] children = directory.listFiles(filter); 114 if (children != null) { 115 files.addAll(Arrays.asList(children)); 116 } 117 } 118 119 private void addPlugins(List files) { 120 log.entering("PluginManager", "addPluginsToList"); 121 this.pluginLocator.addFiles(files); 122 for (Iterator itFiles = files.iterator(); itFiles.hasNext();) { 123 File file = (File) itFiles.next(); 124 log.finer("Looking for plugins in " + file.getAbsolutePath()); 125 for (Iterator itPlugins = this.pluginLocator.findPlugins(file); 126 itPlugins.hasNext(); 127 ) { 128 Plugin p = (Plugin) itPlugins.next(); 129 log.fine( 130 "Found plugin: " + p.getName() + " (" + p.getClassName() + ")"); 131 this.plugins.add(p); 132 } 133 } 134 log.exiting("PluginManager", "addPluginsToList"); 135 } 136 137 private void loadPlugins() { 138 log.entering("PluginManager", "loadPlugins"); 139 Preferences prefs = 140 Preferences.userNodeForPackage(getClass()).node("pluginStates"); 141 try { 142 String[] keys = prefs.keys(); 143 Arrays.sort(keys); 144 for (int i = 0; i < this.plugins.size(); i++) { 145 Plugin p = (Plugin) this.plugins.get(i); 146 String className = p.getClassName(); 147 if (Arrays.binarySearch(keys, className) >= 0) { 148 if (prefs.getBoolean(className, false)) { 149 log.finest("Attempting to load plugin: " + p.getName()); 150 try { 151 p.load(); 152 } catch (InvocationTargetException ite) { 153 log.throwing(getClass().getName(), "loadPlugins", ite); 154 } 155 } 156 } 157 } 158 } catch (BackingStoreException bse) { 159 log.throwing("PluginManager", "loadPlugins()", bse); 160 } 161 log.exiting("PluginManager", "loadPlugins"); 162 } 163 164 /*** 165 * Enables/disables a given plugin 166 * 167 * @param plugin the plugin to enable/disable 168 * @todo change to setEnabled 169 */ 170 public void toggleLoading(Plugin plugin) { 171 Preferences prefs = 172 Preferences.userNodeForPackage(getClass()).node("pluginStates"); 173 if (!plugin.isLoaded()) { 174 try { 175 plugin.load(); 176 } catch (InvocationTargetException ite) { 177 log.throwing(getClass().getName(), "toggleLoading", ite); 178 } 179 prefs.putBoolean(plugin.getClassName(), true); 180 } else { 181 plugin.dispose(); 182 prefs.putBoolean(plugin.getClassName(), false); 183 } 184 } 185 186 /*** 187 * Shuts down the plugin system, notifying all plugins to dispose of resources 188 */ 189 public void shutdown() { 190 for (Iterator it = this.plugins.iterator(); it.hasNext();) { 191 Plugin p = (Plugin) it.next(); 192 p.dispose(); 193 } 194 } 195 196 /*** 197 * Returns the contact method manager 198 * 199 * @return the contact method manager 200 */ 201 public ContactMethodManager getContactMethodManager() { 202 return this.contactMethodManager; 203 } 204 205 /*** 206 * Returns the component manager 207 * 208 * @return the component manager 209 */ 210 public ComponentManager getComponentManager() { 211 return this.componentManager; 212 } 213 214 /*** 215 * Returns the UI manager 216 * 217 * @return the UI manager 218 */ 219 public MFlowUIManager getUIManager() { 220 return this.uiManager; 221 } 222 223 /*** 224 * Returns the contact manager 225 * 226 * @return the contact manager 227 */ 228 public ContactManager getContactManager() { 229 return this.contactManager; 230 } 231 232 /*** 233 * Returns the account manager 234 * 235 * @return the account manager 236 */ 237 public AccountManager getAccountManager() { 238 return this.accountManager; 239 } 240 }

This page was automatically generated by Maven