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 Aslak Hellesoy and Paul Hammant * 9 *****************************************************************************/ 10 11 package org.picocontainer.extras; 12 13 import org.picocontainer.internals.ComponentRegistry; 14 import org.picocontainer.PicoInitializationException; 15 import org.picocontainer.defaults.AmbiguousComponentResolutionException; 16 import org.picocontainer.internals.ComponentSpecification; 17 import org.picocontainer.defaults.DefaultComponentRegistry; 18 19 import java.io.Serializable; 20 import java.util.Collection; 21 import java.util.Collections; 22 import java.util.HashSet; 23 import java.util.List; 24 import java.util.Set; 25 import java.util.ArrayList; 26 27 public class HierarchicalComponentRegistry implements ComponentRegistry, Serializable { 28 29 protected final ComponentRegistry parentRegistry; 30 protected final ComponentRegistry childRegistry; 31 32 protected HierarchicalComponentRegistry(ComponentRegistry parentRegistry, ComponentRegistry childRegistry) { 33 if (parentRegistry == null) { 34 throw new NullPointerException("parentRegistry cannot be null"); 35 } 36 if (childRegistry == null) { 37 throw new NullPointerException("childRegistry cannot be null"); 38 } 39 this.parentRegistry = parentRegistry; 40 this.childRegistry = childRegistry; 41 } 42 43 public static class Default extends HierarchicalComponentRegistry { 44 public Default(ComponentRegistry parentRegistry) { 45 super(parentRegistry, new DefaultComponentRegistry()); 46 } 47 } 48 49 public static class WithChildRegistry extends HierarchicalComponentRegistry { 50 public WithChildRegistry(ComponentRegistry parentRegistry, ComponentRegistry childRegistry) { 51 super(parentRegistry, childRegistry); 52 } 53 } 54 55 public void registerComponent(ComponentSpecification compSpec) { 56 childRegistry.registerComponent(compSpec); 57 } 58 59 public void unregisterComponent(Object componentKey) { 60 childRegistry.unregisterComponent(componentKey); 61 } 62 63 public Collection getComponentSpecifications() { 64 return childRegistry.getComponentSpecifications(); 65 } 66 67 public List getOrderedComponents() { 68 // Get child types 69 List types = new ArrayList(childRegistry.getOrderedComponents()); 70 71 // Get those from parent. 72 types.addAll(parentRegistry.getOrderedComponents()); 73 74 return Collections.unmodifiableList(types); 75 } 76 77 public void addOrderedComponent(Object component) { 78 childRegistry.addOrderedComponent(component); 79 } 80 81 public void putComponent(Object componentKey, Object component) { 82 childRegistry.putComponent(componentKey, component); 83 } 84 85 public boolean contains(Object componentKey) { 86 return childRegistry.contains(componentKey); 87 } 88 89 public Object getComponentInstance(Object componentKey) { 90 91 // First look in child 92 Object result = childRegistry.getComponentInstance(componentKey); 93 94 // Then look in parent if we had nothing 95 if (result == null) { 96 result = parentRegistry.getComponentInstance(componentKey); 97 } 98 return result; 99 } 100 101 public Set getComponentInstanceKeys() { 102 103 // Get child types 104 Set types = new HashSet(childRegistry.getComponentInstanceKeys()); 105 106 // Get those from parent. 107 types.addAll(parentRegistry.getComponentInstanceKeys()); 108 109 return Collections.unmodifiableSet(types); 110 111 } 112 113 public Set getComponentInstances() { 114 // Get child types 115 Set types = new HashSet(childRegistry.getComponentInstances()); 116 117 // Get those from parent. 118 types.addAll(parentRegistry.getComponentInstances()); 119 120 return Collections.unmodifiableSet(types); 121 } 122 123 public boolean hasComponentInstance(Object componentKey) { 124 return childRegistry.hasComponentInstance(componentKey) 125 | parentRegistry.hasComponentInstance(componentKey); 126 } 127 128 public ComponentSpecification getComponentSpec(Object componentKey) { 129 // First look in child 130 ComponentSpecification result = childRegistry.getComponentSpec(componentKey); 131 132 // Then look in parent if we had nothing 133 if (result == null) { 134 result = parentRegistry.getComponentSpec(componentKey); 135 } 136 return result; 137 138 } 139 140 public Object findImplementingComponent(Class componentType) throws AmbiguousComponentResolutionException { 141 142 // First look in child 143 Object result = childRegistry.findImplementingComponent(componentType); 144 145 // Then look in parent if we had nothing 146 if (result == null) { 147 result = parentRegistry.findImplementingComponent(componentType); 148 } 149 return result; 150 151 } 152 153 public ComponentSpecification findImplementingComponentSpecification(Class componentType) throws AmbiguousComponentResolutionException { 154 155 // First look in child 156 ComponentSpecification result = childRegistry.findImplementingComponentSpecification(componentType); 157 158 // Then look in parent if we had nothing 159 if (result == null) { 160 result = parentRegistry.findImplementingComponentSpecification(componentType); 161 } 162 return result; 163 } 164 165 public Object createComponent(ComponentSpecification componentSpecification) throws PicoInitializationException { 166 if (!contains(componentSpecification.getComponentKey())) { 167 Object component = componentSpecification.instantiateComponent(this); 168 addOrderedComponent(component); 169 170 putComponent(componentSpecification.getComponentKey(), component); 171 172 return component; 173 } else { 174 return getComponentInstance(componentSpecification.getComponentKey()); 175 } 176 } 177 178 }

This page was automatically generated by Maven