1 10 11 package org.nanocontainer.reflection; 12 13 import java.io.Serializable ; 14 import java.net.URL ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import org.nanocontainer.ClassPathElement; 20 import org.nanocontainer.DefaultNanoContainer; 21 import org.nanocontainer.NanoContainer; 22 import org.nanocontainer.NanoPicoContainer; 23 import org.picocontainer.ComponentAdapter; 24 import org.picocontainer.MutablePicoContainer; 25 import org.picocontainer.Parameter; 26 import org.picocontainer.PicoContainer; 27 import org.picocontainer.PicoException; 28 import org.picocontainer.PicoIntrospectionException; 29 import org.picocontainer.PicoRegistrationException; 30 import org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer; 31 32 39 public abstract class AbstractNanoPicoContainer extends AbstractDelegatingMutablePicoContainer implements NanoPicoContainer, Serializable { 40 41 protected Map namedChildContainers = new HashMap (); 42 43 protected transient NanoContainer container; 46 47 48 protected AbstractNanoPicoContainer(MutablePicoContainer delegate, ClassLoader classLoader) { 49 super(delegate); 50 container = new DefaultNanoContainer(classLoader, delegate); 51 } 52 53 public final Object getComponentInstance(Object componentKey) throws PicoException { 54 55 Object instance = getDelegate().getComponentInstance(componentKey); 56 57 if (instance != null) { 58 return instance; 59 } 60 61 ComponentAdapter componentAdapter = null; 62 if (componentKey.toString().startsWith("*")) { 63 String candidateClassName = componentKey.toString().substring(1); 64 Collection cas = getComponentAdapters(); 65 for (Iterator it = cas.iterator(); it.hasNext();) { 66 ComponentAdapter ca = (ComponentAdapter) it.next(); 67 Object key = ca.getComponentKey(); 68 if (key instanceof Class && candidateClassName.equals(((Class ) key).getName())) { 69 componentAdapter = ca; 70 break; 71 } 72 } 73 } 74 if (componentAdapter != null) { 75 return componentAdapter.getComponentInstance(this); 76 } else { 77 return getComponentInstanceFromChildren(componentKey); 78 } 79 } 80 81 private Object getComponentInstanceFromChildren(Object componentKey) { 82 String componentKeyPath = componentKey.toString(); 83 int ix = componentKeyPath.indexOf('/'); 84 if (ix != -1) { 85 String firstElement = componentKeyPath.substring(0, ix); 86 String remainder = componentKeyPath.substring(ix + 1, componentKeyPath.length()); 87 Object o = getNamedContainers().get(firstElement); 88 if (o != null) { 89 MutablePicoContainer child = (MutablePicoContainer) o; 90 return child.getComponentInstance(remainder); 91 } 92 } 93 return null; 94 } 95 96 public final MutablePicoContainer makeChildContainer() { 97 return makeChildContainer("containers" + namedChildContainers.size()); 98 } 99 100 106 public MutablePicoContainer makeChildContainer(String name) { 107 AbstractNanoPicoContainer child = createChildContainer(); 108 MutablePicoContainer parentDelegate = getDelegate(); 109 parentDelegate.removeChildContainer(child.getDelegate()); 110 parentDelegate.addChildContainer(child); 111 namedChildContainers.put(name, child); 112 return child; 113 } 114 115 protected abstract AbstractNanoPicoContainer createChildContainer(); 116 117 public boolean removeChildContainer(PicoContainer child) { 118 boolean result = getDelegate().removeChildContainer(child); 119 Iterator children = namedChildContainers.entrySet().iterator(); 120 while (children.hasNext()) { 121 Map.Entry e = (Map.Entry ) children.next(); 122 PicoContainer pc = (PicoContainer) e.getValue(); 123 if (pc == child) { 124 children.remove(); 125 } 126 } 127 return result; 128 } 129 130 protected final Map getNamedContainers() { 131 return namedChildContainers; 132 } 133 134 public Object getComponentInstanceOfType(String componentType) { 135 return container.getComponentInstanceOfType(componentType); 136 } 137 138 public MutablePicoContainer addDecoratingPicoContainer(Class picoContainerClass) { 139 return container.addDecoratingPicoContainer(picoContainerClass); 140 } 141 142 143 public ClassPathElement addClassLoaderURL(URL url) { 144 return container.addClassLoaderURL(url); 145 } 146 147 public ComponentAdapter registerComponentImplementation(String componentImplementationClassName) throws PicoRegistrationException, ClassNotFoundException , PicoIntrospectionException { 148 return container.registerComponentImplementation(componentImplementationClassName); 149 } 150 151 public ComponentAdapter registerComponentImplementation(Object key, String componentImplementationClassName) throws ClassNotFoundException { 152 return container.registerComponentImplementation(key, componentImplementationClassName); 153 } 154 155 public ComponentAdapter registerComponentImplementation(Object key, String componentImplementationClassName, Parameter[] parameters) throws ClassNotFoundException { 156 return container.registerComponentImplementation(key, componentImplementationClassName, parameters); 157 } 158 159 public ComponentAdapter registerComponentImplementation(Object key, String componentImplementationClassName, String [] parameterTypesAsString, String [] parameterValuesAsString) throws PicoRegistrationException, ClassNotFoundException , PicoIntrospectionException { 160 return container.registerComponentImplementation(key, componentImplementationClassName, parameterTypesAsString, parameterValuesAsString); 161 } 162 163 public ComponentAdapter registerComponentImplementation(String componentImplementationClassName, String [] parameterTypesAsString, String [] parameterValuesAsString) throws PicoRegistrationException, ClassNotFoundException , PicoIntrospectionException { 164 return container.registerComponentImplementation(componentImplementationClassName, parameterTypesAsString, parameterValuesAsString); 165 } 166 167 168 public MutablePicoContainer getPico() { 170 return this; 171 } 172 173 public ClassLoader getComponentClassLoader() { 174 return container.getComponentClassLoader(); 175 } 176 177 public boolean addChildContainer(PicoContainer child) { 178 boolean result = getDelegate().addChildContainer(child); 179 180 181 namedChildContainers.put("containers" + namedChildContainers.size(), child); 182 return result; 183 } 184 185 public void addChildContainer(String name, PicoContainer child) { 186 187 super.addChildContainer(child); 188 189 namedChildContainers.put(name, child); 190 } 191 192 } 193 | Popular Tags |