1 17 18 package org.apache.commons.digester.plugins; 19 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Properties ; 23 import java.util.Iterator ; 24 25 import org.apache.commons.digester.Digester; 26 27 import org.apache.commons.logging.Log; 28 29 37 38 public class PluginManager { 39 40 41 private HashMap declarationsByClass = new HashMap (); 42 43 44 private HashMap declarationsById = new HashMap (); 45 46 47 private PluginManager parent; 48 49 53 private PluginContext pluginContext; 54 55 57 58 public PluginManager(PluginContext r) { 59 pluginContext = r; 60 } 61 62 72 public PluginManager(PluginManager parent) { 73 this.parent = parent; 74 this.pluginContext = parent.pluginContext; 75 } 76 77 79 88 public void addDeclaration(Declaration decl) { 89 Log log = LogUtils.getLogger(null); 90 boolean debug = log.isDebugEnabled(); 91 92 Class pluginClass = decl.getPluginClass(); 93 String id = decl.getId(); 94 95 declarationsByClass.put(pluginClass.getName(), decl); 96 97 if (id != null) { 98 declarationsById.put(id, decl); 99 if (debug) { 100 log.debug( 101 "Indexing plugin-id [" + id + "]" + 102 " -> class [" + pluginClass.getName() + "]"); 103 } 104 } 105 } 106 107 111 public Declaration getDeclarationByClass(String className) { 112 Declaration decl = 113 (Declaration) declarationsByClass.get(className); 114 115 if ((decl == null) && (parent != null)) { 116 decl = parent.getDeclarationByClass(className); 117 } 118 119 return decl; 120 } 121 122 129 public Declaration getDeclarationById(String id) { 130 Declaration decl = (Declaration) declarationsById.get(id); 131 132 if ((decl == null) && (parent != null)) { 133 decl = parent.getDeclarationById(id); 134 } 135 136 return decl; 137 } 138 139 146 public RuleLoader findLoader(Digester digester, String id, 147 Class pluginClass, Properties props) 148 throws PluginException { 149 150 Log log = LogUtils.getLogger(digester); 155 boolean debug = log.isDebugEnabled(); 156 log.debug("scanning ruleFinders to locate loader.."); 157 158 List ruleFinders = pluginContext.getRuleFinders(); 159 RuleLoader ruleLoader = null; 160 try { 161 for(Iterator i = ruleFinders.iterator(); 162 i.hasNext() && ruleLoader == null; ) { 163 164 RuleFinder finder = (RuleFinder) i.next(); 165 if (debug) { 166 log.debug("checking finder of type " + finder.getClass().getName()); 167 } 168 ruleLoader = finder.findLoader(digester, pluginClass, props); 169 } 170 } 171 catch(PluginException e) { 172 throw new PluginException( 173 "Unable to locate plugin rules for plugin" 174 + " with id [" + id + "]" 175 + ", and class [" + pluginClass.getName() + "]" 176 + ":" + e.getMessage(), e.getCause()); 177 } 178 log.debug("scanned ruleFinders."); 179 180 return ruleLoader; 181 } 182 } 183 | Popular Tags |