KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > PluginManager


1 /* $Id: PluginManager.java 155412 2005-02-26 12:58:36Z dirkv $
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
18 package org.apache.commons.digester.plugins;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.commons.digester.Digester;
26
27 import org.apache.commons.logging.Log;
28
29 /**
30  * Coordinates between PluginDeclarationRule and PluginCreateRule objects,
31  * providing a place to share data between instances of these rules.
32  * <p>
33  * One instance of this class exists per PluginRules instance.
34  *
35  * @since 1.6
36  */

37
38 public class PluginManager {
39
40     /** Map of classname->Declaration */
41     private HashMap JavaDoc declarationsByClass = new HashMap JavaDoc();
42
43     /** Map of id->Declaration */
44     private HashMap JavaDoc declarationsById = new HashMap JavaDoc();
45
46     /** the parent manager to which this one may delegate lookups. */
47     private PluginManager parent;
48     
49     /**
50      * The object containing data that should only exist once for each
51      * Digester instance.
52      */

53     private PluginContext pluginContext;
54     
55     //------------------- constructors ---------------------------------------
56

57     /** Construct a "root" PluginManager, ie one with no parent. */
58     public PluginManager(PluginContext r) {
59         pluginContext = r;
60     }
61
62     /**
63      * Construct a "child" PluginManager. When declarations are added to
64      * a "child", they are stored within the child and do not modify the
65      * parent, so when the child goes out of scope, those declarations
66      * disappear. When asking a "child" to retrieve a declaration, it
67      * delegates the search to its parent if it does not hold a matching
68      * entry itself.
69      * <p>
70      * @param parent must be non-null.
71      */

72     public PluginManager(PluginManager parent) {
73         this.parent = parent;
74         this.pluginContext = parent.pluginContext;
75     }
76     
77     //------------------- methods --------------------------------------------
78

79     /**
80      * Add the declaration to the set of known declarations.
81      * <p>
82      * TODO: somehow get a reference to a Digester object
83      * so that we can really log here. Currently, all
84      * logging is disabled from this method.
85      *
86      *@param decl an object representing a plugin class.
87      */

88     public void addDeclaration(Declaration decl) {
89         Log log = LogUtils.getLogger(null);
90         boolean debug = log.isDebugEnabled();
91         
92         Class JavaDoc pluginClass = decl.getPluginClass();
93         String JavaDoc 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     /**
108      * Return the declaration object with the specified class.
109      * If no such plugin is known, null is returned.
110      */

111     public Declaration getDeclarationByClass(String JavaDoc 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     /**
123      * Return the declaration object with the specified id.
124      * If no such plugin is known, null is returned.
125      *
126      *@param id Description of the Parameter
127      *@return The declaration value
128      */

129     public Declaration getDeclarationById(String JavaDoc 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     /**
140      * Given a plugin class and some associated properties, scan the
141      * list of known RuleFinder instances until one detects a source of
142      * custom rules for this plugin (aka a RuleLoader).
143      * <p>
144      * If no source of custom rules can be found, null is returned.
145      */

146     public RuleLoader findLoader(Digester digester, String JavaDoc id,
147                         Class JavaDoc pluginClass, Properties JavaDoc props)
148                         throws PluginException {
149
150         // iterate over the list of RuleFinders, trying each one
151
// until one of them locates a source of dynamic rules given
152
// this specific plugin class and the associated declaration
153
// properties.
154
Log log = LogUtils.getLogger(digester);
155         boolean debug = log.isDebugEnabled();
156         log.debug("scanning ruleFinders to locate loader..");
157         
158         List JavaDoc ruleFinders = pluginContext.getRuleFinders();
159         RuleLoader ruleLoader = null;
160         try {
161             for(Iterator JavaDoc 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