01: /*
02: * ComponentRegistry.java
03: *
04: * Created on April 18, 2003, 1:01 PM
05: */
06:
07: package org.jaffa.tools.patternmetaengine;
08:
09: import java.util.Iterator;
10: import java.util.List;
11: import java.util.ArrayList;
12: import org.apache.log4j.Logger;
13:
14: /** This class holds all the generated components (ie their builders). It is used
15: * to keep a registry of all components across the multiple phases of the build.
16: * It also provides some convienance methods for accessing components from the
17: * repository
18: *
19: * @version 1.0
20: * @author PaulE
21: */
22: public class ComponentRegistry {
23:
24: /** Set up Logging for Log4J */
25: private static Logger log = Logger.getLogger(AppBuilder.class);
26:
27: private List allComponents = new ArrayList();
28:
29: /** Add a new component (IBuilder) to the registry
30: * @param comp New component to be added
31: */
32: public void addComponent(IBuilder comp) {
33: allComponents.add(comp);
34: }
35:
36: /** Iterator for all components in registry
37: * @return a interator of IBuiler objects
38: */
39: public Iterator iterator() {
40: return allComponents.iterator();
41: }
42:
43: /** Iterator for all components in registry of a given type
44: * @param type Type of component (Finder, Viewer, etc)
45: * @return a interator of IBuiler objects
46: */
47: public Iterator iterateByType(String type) {
48: return findComponents(type, null).iterator();
49: }
50:
51: /** Iterator for all components in registry for a given
52: * domain object
53: * @param domain full class name (including package) of domain object
54: * @return a interator of IBuiler objects
55: */
56: public Iterator iterateByDomain(String domain) {
57: return findComponents(null, domain).iterator();
58: }
59:
60: /** Find a given component. Assumes there is only one instance of
61: * a given component type per domain object. If multiple are found
62: * the first one is returned.
63: * @param domain full class name (including package) of domain object
64: * @param type Type of component (Finder, Viewer, etc)
65: * @return the located component, null of no component is found
66: */
67: public IBuilder findComponent(String domain, String type) {
68: List l = findComponents(type, domain);
69: if (l.size() == 0)
70: return null;
71: if (l.size() != 1) {
72: log.error("Mutiple Components For " + type + "/" + domain);
73: // we'll just return the first one though
74: return null;
75: }
76: return (IBuilder) l.get(0);
77: }
78:
79: private List findComponents(String type, String domain) {
80: List results = new ArrayList();
81: for (Iterator i = allComponents.iterator(); i.hasNext();) {
82: IBuilder c = (IBuilder) i.next();
83: if ((type == null || type.equals(c.getComponentType()))
84: && (domain == null || domain.equals(c.getDomain())))
85: results.add(c);
86: }
87: return results;
88: }
89:
90: }
|