1. /* $Id: PluginManager.java,v 1.14 2004/05/10 06:44:13 skitching Exp $
  2. *
  3. * Copyright 2003-2004 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.digester.plugins;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Properties;
  21. import java.util.Iterator;
  22. import org.apache.commons.digester.Digester;
  23. import org.apache.commons.logging.Log;
  24. /**
  25. * Coordinates between PluginDeclarationRule and PluginCreateRule objects,
  26. * providing a place to share data between instances of these rules.
  27. * <p>
  28. * One instance of this class exists per PluginRules instance.
  29. *
  30. * @since 1.6
  31. */
  32. public class PluginManager {
  33. /** Map of classname->Declaration */
  34. private HashMap declarationsByClass = new HashMap();
  35. /** Map of id->Declaration */
  36. private HashMap declarationsById = new HashMap();
  37. /** the parent manager to which this one may delegate lookups. */
  38. private PluginManager parent;
  39. /**
  40. * The object containing data that should only exist once for each
  41. * Digester instance.
  42. */
  43. private PluginContext pluginContext;
  44. //------------------- constructors ---------------------------------------
  45. /** Construct a "root" PluginManager, ie one with no parent. */
  46. public PluginManager(PluginContext r) {
  47. pluginContext = r;
  48. }
  49. /**
  50. * Construct a "child" PluginManager. When declarations are added to
  51. * a "child", they are stored within the child and do not modify the
  52. * parent, so when the child goes out of scope, those declarations
  53. * disappear. When asking a "child" to retrieve a declaration, it
  54. * delegates the search to its parent if it does not hold a matching
  55. * entry itself.
  56. * <p>
  57. * @param parent must be non-null.
  58. */
  59. public PluginManager(PluginManager parent) {
  60. this.parent = parent;
  61. this.pluginContext = parent.pluginContext;
  62. }
  63. //------------------- methods --------------------------------------------
  64. /**
  65. * Add the declaration to the set of known declarations.
  66. * <p>
  67. * TODO: somehow get a reference to a Digester object
  68. * so that we can really log here. Currently, all
  69. * logging is disabled from this method.
  70. *
  71. *@param decl an object representing a plugin class.
  72. */
  73. public void addDeclaration(Declaration decl) {
  74. Log log = LogUtils.getLogger(null);
  75. boolean debug = log.isDebugEnabled();
  76. Class pluginClass = decl.getPluginClass();
  77. String id = decl.getId();
  78. declarationsByClass.put(pluginClass.getName(), decl);
  79. if (id != null) {
  80. declarationsById.put(id, decl);
  81. if (debug) {
  82. log.debug(
  83. "Indexing plugin-id [" + id + "]" +
  84. " -> class [" + pluginClass.getName() + "]");
  85. }
  86. }
  87. }
  88. /**
  89. * Return the declaration object with the specified class.
  90. * If no such plugin is known, null is returned.
  91. */
  92. public Declaration getDeclarationByClass(String className) {
  93. Declaration decl =
  94. (Declaration) declarationsByClass.get(className);
  95. if ((decl == null) && (parent != null)) {
  96. decl = parent.getDeclarationByClass(className);
  97. }
  98. return decl;
  99. }
  100. /**
  101. * Return the declaration object with the specified id.
  102. * If no such plugin is known, null is returned.
  103. *
  104. *@param id Description of the Parameter
  105. *@return The declaration value
  106. */
  107. public Declaration getDeclarationById(String id) {
  108. Declaration decl = (Declaration) declarationsById.get(id);
  109. if ((decl == null) && (parent != null)) {
  110. decl = parent.getDeclarationById(id);
  111. }
  112. return decl;
  113. }
  114. /**
  115. * Given a plugin class and some associated properties, scan the
  116. * list of known RuleFinder instances until one detects a source of
  117. * custom rules for this plugin (aka a RuleLoader).
  118. * <p>
  119. * If no source of custom rules can be found, null is returned.
  120. */
  121. public RuleLoader findLoader(Digester digester, String id,
  122. Class pluginClass, Properties props)
  123. throws PluginException {
  124. // iterate over the list of RuleFinders, trying each one
  125. // until one of them locates a source of dynamic rules given
  126. // this specific plugin class and the associated declaration
  127. // properties.
  128. Log log = LogUtils.getLogger(digester);
  129. boolean debug = log.isDebugEnabled();
  130. log.debug("scanning ruleFinders to locate loader..");
  131. List ruleFinders = pluginContext.getRuleFinders();
  132. RuleLoader ruleLoader = null;
  133. try {
  134. for(Iterator i = ruleFinders.iterator();
  135. i.hasNext() && ruleLoader == null; ) {
  136. RuleFinder finder = (RuleFinder) i.next();
  137. if (debug) {
  138. log.debug("checking finder of type " + finder.getClass().getName());
  139. }
  140. ruleLoader = finder.findLoader(digester, pluginClass, props);
  141. }
  142. }
  143. catch(PluginException e) {
  144. throw new PluginException(
  145. "Unable to locate plugin rules for plugin"
  146. + " with id [" + id + "]"
  147. + ", and class [" + pluginClass.getName() + "]"
  148. + ":" + e.getMessage(), e.getCause());
  149. }
  150. log.debug("scanned ruleFinders.");
  151. return ruleLoader;
  152. }
  153. }