View Javadoc
1 /****************************************************************************** 2 * Copyright (C) PicoContainer Organization. All rights reserved. * 3 * ------------------------------------------------------------------------- * 4 * The software in this package is published under the terms of the BSD * 5 * style license a copy of which has been included with this distribution in * 6 * the LICENSE.txt file. * 7 * * 8 * Idea by Rachel Davies, Original code by various * 9 *****************************************************************************/ 10 package org.picocontainer.defaults; 11 12 import org.picocontainer.internals.ComponentRegistry; 13 import org.picocontainer.PicoInitializationException; 14 import org.picocontainer.PicoContainer; 15 import org.picocontainer.internals.ComponentSpecification; 16 17 import java.io.Serializable; 18 import java.util.*; 19 20 /*** 21 * The default component registry. 22 * 23 * @author Aslak Hellesoy, Paul Hammant, various 24 * @version $Revision: 1.8 $ 25 */ 26 27 public class DefaultComponentRegistry implements ComponentRegistry, Serializable { 28 29 protected final List registeredComponentSpecifications; 30 31 // Keeps track of the instantiation order 32 protected final List orderedComponents; 33 34 protected final Map componentKeyToInstanceMap; 35 36 protected final Map componentToSpec; 37 38 39 public DefaultComponentRegistry() { 40 registeredComponentSpecifications = new ArrayList(); 41 orderedComponents = new ArrayList(); 42 componentKeyToInstanceMap = new HashMap(); 43 componentToSpec = new HashMap(); 44 } 45 46 public void registerComponent(ComponentSpecification compSpec) { 47 componentToSpec.put(compSpec.getComponentImplementation(), compSpec); 48 registeredComponentSpecifications.add(compSpec); 49 } 50 51 public void unregisterComponent(Object componentKey) { 52 for (Iterator iterator = registeredComponentSpecifications.iterator(); iterator.hasNext();) { 53 ComponentSpecification currentCompSpec = (ComponentSpecification) iterator.next(); 54 55 if (currentCompSpec.getComponentKey().equals(componentKey)) { 56 registeredComponentSpecifications.remove(currentCompSpec); 57 componentKeyToInstanceMap.remove(componentKey); 58 break; 59 } 60 } 61 } 62 63 public Collection getComponentSpecifications() { 64 return registeredComponentSpecifications; 65 } 66 67 public List getOrderedComponents() { 68 return new ArrayList(orderedComponents); 69 } 70 71 public void addOrderedComponent(Object component) { 72 orderedComponents.add(component); 73 } 74 75 public void putComponent(Object componentKey, Object component) { 76 componentKeyToInstanceMap.put(componentKey, component); 77 } 78 79 public boolean contains(Object componentKey) { 80 return componentKeyToInstanceMap.containsKey(componentKey); 81 } 82 83 public Object getComponentInstance(Object componentKey) { 84 return componentKeyToInstanceMap.get(componentKey); 85 } 86 87 public Set getComponentInstanceKeys() { 88 Set types = componentKeyToInstanceMap.keySet(); 89 return Collections.unmodifiableSet(types); 90 } 91 92 public Set getComponentInstances() { 93 // ArrayList list = new ArrayList(); 94 // Set types = componentKeyToInstanceMap.entrySet(); 95 // for (Iterator iterator = types.iterator(); iterator.hasNext();) { 96 // Map.Entry e = (Map.Entry) iterator.next(); 97 // list.add(e.getValue()); 98 // } 99 Set result = new HashSet(); 100 result.addAll(componentKeyToInstanceMap.values()); 101 return Collections.unmodifiableSet(result); 102 } 103 104 public boolean hasComponentInstance(Object componentKey) { 105 return componentKeyToInstanceMap.containsKey(componentKey); 106 } 107 108 public ComponentSpecification getComponentSpec(Object componentKey) { 109 return (ComponentSpecification) componentToSpec.get(componentKey); 110 } 111 112 public Object findImplementingComponent(Class componentType) throws AmbiguousComponentResolutionException { 113 List found = new ArrayList(); 114 115 for (Iterator iterator = getComponentInstanceKeys().iterator(); iterator.hasNext();) { 116 Object key = iterator.next(); 117 Object component = getComponentInstance(key); 118 if (componentType.isInstance(component)) { 119 found.add(key); 120 } 121 } 122 123 if (found.size() > 1) { 124 Object[] ambiguousKeys = found.toArray(); 125 throw new AmbiguousComponentResolutionException(componentType, ambiguousKeys); 126 } 127 128 return found.isEmpty() ? null : getComponentInstance(found.get(0)); 129 } 130 131 public ComponentSpecification findImplementingComponentSpecification(Class componentType) throws AmbiguousComponentResolutionException { 132 List found = new ArrayList(); 133 for (Iterator iterator = getComponentSpecifications().iterator(); iterator.hasNext();) { 134 ComponentSpecification componentSpecification = (ComponentSpecification) iterator.next(); 135 136 if (componentType.isAssignableFrom(componentSpecification.getComponentImplementation())) { 137 found.add(componentSpecification); 138 } 139 } 140 141 if (found.size() > 1) { 142 Class[] foundClasses = new Class[found.size()]; 143 for (int i = 0; i < foundClasses.length; i++) { 144 foundClasses[i] = ((ComponentSpecification) found.get(i)).getComponentImplementation(); 145 } 146 throw new AmbiguousComponentResolutionException(componentType, foundClasses); 147 } 148 149 return found.isEmpty() ? null : ((ComponentSpecification) found.get(0)); 150 } 151 152 153 public Object createComponent(ComponentSpecification componentSpecification) throws PicoInitializationException { 154 if (!contains(componentSpecification.getComponentKey())) { 155 Object component = componentSpecification.instantiateComponent(this); 156 addOrderedComponent(component); 157 158 putComponent(componentSpecification.getComponentKey(), component); 159 160 return component; 161 } else { 162 return getComponentInstance(componentSpecification.getComponentKey()); 163 } 164 } 165 166 }

This page was automatically generated by Maven