1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.IOException ; 40 import java.io.Serializable ; 41 import java.util.Arrays ; 42 import java.util.Collections ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.LinkedList ; 46 import java.util.List ; 47 import java.util.Map ; 48 import java.util.Properties ; 49 50 import org.apache.log4j.Logger; 51 import org.jdom.Attribute; 52 import org.jdom.Element; 53 54 55 73 public final class PluginRegistry implements Serializable { 74 75 private static final Logger LOG = Logger.getLogger(PluginRegistry.class); 76 77 82 private static final PluginRegistry ROOTREGISTRY = loadDefaultPluginRegistry(); 83 84 87 public static PluginRegistry createRegistry() { 88 return createRegistry(ROOTREGISTRY); 89 } 90 91 94 public static PluginRegistry createRegistry(PluginRegistry parent) { 95 return new PluginRegistry(parent); 96 } 97 98 102 private final PluginRegistry parentRegistry; 103 104 109 private final Map plugins = new HashMap (); 110 111 116 private final Map pluginConfigs = new HashMap (); 117 118 122 private PluginRegistry(PluginRegistry parentRegistry) { 123 this.parentRegistry = parentRegistry; 124 } 125 126 134 public void register(String pluginName, String pluginClassname) { 135 plugins.put(pluginName.toLowerCase(), pluginClassname); 136 } 137 138 143 public void register(Element pluginElement) throws CruiseControlException { 144 String pluginName = pluginElement.getAttributeValue("name").toLowerCase(); 145 String pluginClassName = pluginElement.getAttributeValue("classname"); 146 if (pluginClassName != null) { 147 register(pluginName, pluginClassName); 148 } else { 149 if (!isPluginRegistered(pluginName)) { 151 throw new CruiseControlException("Unknown plugin '" 152 + pluginName + "'; maybe you forgot to specify a classname?"); 153 } 154 } 155 156 Element clonedPluginElement = (Element) pluginElement.clone(); 157 clonedPluginElement.removeAttribute("name"); 158 clonedPluginElement.removeAttribute("classname"); 159 clonedPluginElement.setName(pluginName); 160 if (LOG.isDebugEnabled()) { 161 LOG.debug("storing plugin configuration " + pluginName); 162 } 163 pluginConfigs.put(pluginName, clonedPluginElement); 164 } 165 166 171 static void registerToRoot(Element pluginElement) throws CruiseControlException { 172 ROOTREGISTRY.register(pluginElement); 173 } 174 175 179 static void resetRootRegistry() { 180 ROOTREGISTRY.pluginConfigs.clear(); 181 ROOTREGISTRY.plugins.clear(); 182 ROOTREGISTRY.plugins.putAll(loadDefaultPluginRegistry().plugins); 183 } 184 185 193 public String getPluginClassname(String pluginName) { 194 pluginName = pluginName.toLowerCase(); 195 String className = internalGetPluginClassname(pluginName); 196 if (className == null && parentRegistry != null) { 197 className = parentRegistry.getPluginClassname(pluginName); 198 } 199 return className; 200 } 201 202 206 private String internalGetPluginClassname(String pluginName) { 207 return (String ) plugins.get(pluginName); 208 } 209 210 218 public Class getPluginClass(String pluginName) throws CruiseControlException { 219 if (!isPluginRegistered(pluginName)) { 220 return null; 221 } 222 String pluginClassname = getPluginClassname(pluginName); 223 224 try { 225 return Class.forName(pluginClassname); 226 } catch (ClassNotFoundException e) { 227 String msg = "Attemping to load plugin named [" + pluginName 228 + "], but couldn't load corresponding class [" 229 + pluginClassname + "]."; 230 throw new CruiseControlException(msg); 232 } 233 } 234 235 public String getPluginName(Class pluginClass) { 236 String pluginName = null; 237 238 if (parentRegistry != null) { 239 pluginName = parentRegistry.getPluginName(pluginClass); 240 } 241 242 if (pluginName == null) { 243 for (Iterator i = plugins.entrySet().iterator(); i.hasNext();) { 244 Map.Entry entry = (Map.Entry ) i.next(); 245 String value = (String ) entry.getValue(); 246 if (value.equals(pluginClass.getName())) { 247 pluginName = ((String ) entry.getKey()); 248 break; 249 } 250 } 251 } 252 253 return pluginName; 254 } 255 256 public PluginDetail[] getPluginDetails() throws CruiseControlException { 257 List availablePlugins = new LinkedList (); 258 259 if (parentRegistry != null) { 260 availablePlugins.addAll(Arrays.asList(parentRegistry.getPluginDetails())); 261 } 262 263 for (Iterator i = plugins.keySet().iterator(); i.hasNext();) { 264 String pluginName = (String ) i.next(); 265 try { 266 Class pluginClass = getPluginClass(pluginName); 267 availablePlugins.add(new GenericPluginDetail(pluginName, pluginClass)); 268 } catch (CruiseControlException e) { 269 String message = e.getMessage(); 270 if (message.indexOf("starteam") < 0) { 271 throw e; 272 } 273 } 274 } 275 276 return (PluginDetail[]) availablePlugins.toArray(new PluginDetail[availablePlugins.size()]); 277 } 278 279 public PluginType[] getPluginTypes() { 280 return PluginType.getTypes(); 281 } 282 283 295 public boolean isPluginRegistered(String pluginName) { 296 boolean isRegistered = plugins.containsKey(pluginName.toLowerCase()); 297 if (!isRegistered && parentRegistry != null) { 298 isRegistered = parentRegistry.isPluginRegistered(pluginName); 299 } 300 return isRegistered; 301 } 302 303 310 static PluginRegistry loadDefaultPluginRegistry() { 311 PluginRegistry rootRegistry = new PluginRegistry(null); 312 Properties pluginDefinitions = new Properties (); 313 try { 314 pluginDefinitions.load(PluginRegistry.class.getResourceAsStream("default-plugins.properties")); 315 } catch (IOException e) { 316 throw new RuntimeException ("Failed to load plugin-definitions from default-plugins.properties: " + e); 317 } 318 for (Iterator iter = pluginDefinitions.entrySet().iterator(); iter.hasNext(); ) { 319 Map.Entry entry = (Map.Entry ) iter.next(); 320 rootRegistry.register((String ) entry.getKey(), (String ) entry.getValue()); 321 } 322 return rootRegistry; 323 } 324 325 329 public Element getPluginConfig(String pluginName) { 330 pluginName = pluginName.toLowerCase(); 331 String className = getPluginClassname(pluginName); 332 return overridePluginConfig(pluginName, className, null); 333 } 334 335 353 private Element overridePluginConfig(final String pluginName, final String pluginClass, Element pluginConfig) { 354 Element pluginElement = (Element) this.pluginConfigs.get(pluginName); 355 if (pluginElement != null && pluginConfig == null) { 357 pluginElement = (Element) pluginElement.clone(); 358 } 359 if (pluginConfig == null) { 360 pluginConfig = pluginElement; 361 } else { 362 if (pluginElement != null && pluginClass.equals(this.internalGetPluginClassname(pluginName))) { 364 List attributes = pluginElement.getAttributes(); 366 for (int i = 0; i < attributes.size(); i++) { 367 Attribute attribute = (Attribute) attributes.get(i); 368 String name = attribute.getName(); 369 if (pluginConfig.getAttribute(name) == null) { 370 pluginConfig.setAttribute(name, attribute.getValue()); 371 } 372 } 373 List children = pluginElement.getChildren(); 375 for (int i = 0; i < children.size(); i++) { 376 Element child = (Element) children.get(i); 377 pluginConfig.addContent((Element) child.clone()); 378 } 379 } 380 } 381 if (this.parentRegistry != null) { 382 pluginConfig = this.parentRegistry.overridePluginConfig(pluginName, pluginClass, pluginConfig); 383 } 384 return pluginConfig; 385 } 386 387 394 public Map getDefaultProperties(String pluginName) { 395 Map defaultProperties = new HashMap (); 396 Element pluginConfig = this.getPluginConfig(pluginName); 397 if (pluginConfig != null) { 398 List attributes = pluginConfig.getAttributes(); 399 for (Iterator iter = attributes.iterator(); iter.hasNext(); ) { 400 Attribute attr = (Attribute) iter.next(); 401 String name = attr.getName(); 402 if (name.equals("name") || name.equals("classname")) { 403 continue; 404 } 405 if (LOG.isDebugEnabled()) { 406 LOG.debug("setting default property " + name + " to '" + attr.getValue() 407 + "' for " + pluginName); 408 } 409 defaultProperties.put(name, attr.getValue()); 410 } 411 } 412 return Collections.unmodifiableMap(defaultProperties); 413 } 414 } 415 | Popular Tags |