| 1 package org.objectweb.celtix.application; 2 3 import java.text.MessageFormat ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import java.util.logging.Level ; 7 import java.util.logging.Logger ; 8 9 import org.objectweb.celtix.common.i18n.Message; 10 import org.objectweb.celtix.common.logging.LogUtils; 11 import org.objectweb.celtix.configuration.Configuration; 12 import org.objectweb.celtix.plugins.PluginException; 13 import org.objectweb.celtix.plugins.PluginManager; 14 15 24 public class ApplicationPluginManager implements PluginManager { 25 26 private static final Logger LOG = LogUtils.getL7dLogger(ApplicationPluginManager.class); 27 28 private static final MessageFormat PLUGINS_CLASSNAME_FMT = 29 new MessageFormat ("plugins:{0}:className"); 30 31 private static final MessageFormat PLUGINS_PREREQUISITES_FMT = 32 new MessageFormat ("plugins:{0}:prerequisites"); 33 34 private final List <PluginInfo> plugins; 35 36 public ApplicationPluginManager() { 37 plugins = new ArrayList <PluginInfo>(); 38 } 39 40 45 public Object getPlugin(String className) throws PluginException { 46 return getPlugin(null, className, null); 47 } 48 49 54 public Object getPluginByName(String pluginName) throws PluginException { 55 return getPluginByName(pluginName, null); 56 } 57 58 63 public synchronized void registerPlugin(Object plugin) throws PluginException { 64 PluginInfo info = findPluginInfo(plugin); 65 if (info.isRegisteredWith(this)) { 66 throw new PluginException(new Message("ALREADY_REGISTERED_EXC", LOG, info.getClassName())); 67 } else { 68 info.register(this); 69 } 70 } 71 72 77 public synchronized void unloadPlugin(Object plugin) throws PluginException { 78 PluginInfo info = findPluginInfo(plugin); 79 if (info.isRegistered()) { 80 throw new PluginException(new Message("STILL_REGISTERED_EXC", LOG, info.getClassName())); 81 } else { 82 plugins.remove(plugin); 83 info = null; 84 } 85 } 86 87 92 public synchronized void unregisterPlugin(Object plugin) throws PluginException { 93 PluginInfo info = findPluginInfo(plugin); 94 if (info.isRegisteredWith(this)) { 95 info.unregister(this); 96 } else { 97 throw new PluginException(new Message("NOT_REGISTERED_EXC", LOG, info.getClassName())); 98 } 99 } 100 101 106 public ClassLoader getPluginClassLoader() { 107 return getClass().getClassLoader(); 108 } 109 110 113 public Configuration getConfiguration() { 114 return Application.getInstance().getConfiguration(); 115 } 116 117 Object getPluginByName(String pluginName, PluginInfo dependent) throws PluginException { 118 String key = PLUGINS_CLASSNAME_FMT.format(pluginName); 119 Configuration configuration = getConfiguration(); 120 String pluginClassName = (String )configuration.getObject(key); 121 122 return getPlugin(pluginName, pluginClassName, dependent); 123 } 124 125 Object getPlugin(String pluginName, String pluginClassName, PluginInfo dependent) throws PluginException { 126 LOG.entering(getClass().getName(), "getPlugin"); 127 PluginInfo info = null; 128 PluginStateMachine state = null; 129 synchronized (this) { 130 info = findPluginByClassname(pluginClassName); 131 if (info == null) { 132 info = new PluginInfo(pluginClassName, this); 133 plugins.add(info); 134 } 135 state = info.getState(); 136 if (PluginStateMachine.PluginState.LOADING == state.getCurrentState()) { 137 state.waitForState(PluginStateMachine.PluginState.LOADED); 138 LOG.exiting(getClass().getName(), "getPlugin", "object currently being loaded"); 139 return info.getPlugin(); 140 } else if (PluginStateMachine.PluginState.LOADED == state.getCurrentState()) { 141 LOG.exiting(getClass().getName(), "getPlugin", "object already loaded"); 142 return info.getPlugin(); 143 } 144 state.setNextState(PluginStateMachine.PluginState.LOADING); 145 } 146 147 149 if (dependent != null) { 150 info.setRequiredFor(dependent); 151 if (info.isCircularDependency()) { 152 throw new PluginException(new Message("CIRCULAR_DEPENDENCY_EXC", LOG, pluginClassName)); 153 } 154 } 155 156 if (null != pluginName) { 157 Configuration configuration = getConfiguration(); 158 159 String key = PLUGINS_PREREQUISITES_FMT.format(pluginName); 160 String [] prerequisites = (String [])configuration.getObject(key); 161 162 if (prerequisites != null) { 163 for (String p : prerequisites) { 164 getPluginByName(p, info); 165 } 166 } 167 } 168 169 Object plugin = createPlugin(pluginClassName); 170 info.setPlugin(plugin); 171 state.setNextState(PluginStateMachine.PluginState.LOADED); 172 LOG.exiting(getClass().getName(), "getPlugin", "object newly created"); 173 return plugin; 174 } 175 176 Object createPlugin(String pluginClassName) throws PluginException { 177 178 ClassLoader cl = getPluginClassLoader(); 179 Object plugin = null; 180 try { 181 Class <?> pluginClass = Class.forName(pluginClassName, true, cl); 182 plugin = pluginClass.newInstance(); 183 } catch (Exception ex) { 184 LogUtils.log(LOG, Level.SEVERE, "PLUGIN_LOAD_FAILURE_MSG", ex, pluginClassName); 185 throw new PluginException(new Message("LOAD_FAILED_EXC", LOG, pluginClassName), ex); 186 } 187 return plugin; 188 } 189 190 PluginInfo findPluginByClassname(String className) { 191 for (PluginInfo info : plugins) { 192 if (info.getClassName().equals(className) 193 && info.getClassLoader() == getPluginClassLoader()) { 194 return info; 195 } 196 } 197 LOG.info("Could not find plugin info for class " + className); 198 return null; 199 } 200 201 PluginInfo findPluginInfo(Object plugin) { 202 203 for (PluginInfo info : plugins) { 204 if (plugin == info.getPlugin()) { 205 return info; 206 } 207 } 208 LOG.info("Could not find plugin info for plugin " + plugin); 209 return null; 210 } 211 } 212 | Popular Tags |