1 10 package org.picocontainer.alternatives; 11 12 import org.picocontainer.ComponentAdapter; 13 import org.picocontainer.MutablePicoContainer; 14 import org.picocontainer.Parameter; 15 import org.picocontainer.PicoContainer; 16 import org.picocontainer.PicoRegistrationException; 17 import org.picocontainer.LifecycleManager; 18 import org.picocontainer.defaults.CachingComponentAdapter; 19 import org.picocontainer.defaults.CachingComponentAdapterFactory; 20 import org.picocontainer.defaults.ComponentAdapterFactory; 21 import org.picocontainer.defaults.DefaultComponentAdapterFactory; 22 import org.picocontainer.defaults.DefaultLifecycleManager; 23 24 import java.io.Serializable ; 25 26 35 public class ImplementationHidingCachingPicoContainer extends AbstractDelegatingMutablePicoContainer implements Serializable { 36 37 private CachingComponentAdapterFactory caf; 38 private LifecycleManager lifecycleManager; 39 40 43 44 public ImplementationHidingCachingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent) { 45 this(parent, new CachingComponentAdapterFactory(caf), new DefaultLifecycleManager()); 46 } 47 48 public ImplementationHidingCachingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent, LifecycleManager lifecyleManager) { 49 this(parent, new CachingComponentAdapterFactory(caf), lifecyleManager); 50 } 51 52 private ImplementationHidingCachingPicoContainer(PicoContainer parent, CachingComponentAdapterFactory caf, LifecycleManager lifecycleManager) { 53 super(new ImplementationHidingPicoContainer(caf, parent, lifecycleManager)); 54 this.caf = caf; 55 this.lifecycleManager = lifecycleManager; 56 } 57 58 61 public ImplementationHidingCachingPicoContainer(PicoContainer parent) { 62 this(new DefaultComponentAdapterFactory(), parent); 63 } 64 65 66 69 public ImplementationHidingCachingPicoContainer() { 70 this(null); 71 } 72 73 public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation) throws PicoRegistrationException { 74 if (componentKey instanceof Class ) { 75 Class clazz = (Class ) componentKey; 76 if (clazz.isInterface()) { 77 ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, null); 78 return getDelegate().registerComponent(new CachingComponentAdapter(new ImplementationHidingComponentAdapter(delegate, true))); 79 } 80 } 81 return getDelegate().registerComponentImplementation(componentKey, componentImplementation); 82 } 83 84 public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, Parameter[] parameters) throws PicoRegistrationException { 85 if (componentKey instanceof Class ) { 86 Class clazz = (Class ) componentKey; 87 if (clazz.isInterface()) { 88 ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, parameters); 89 ImplementationHidingComponentAdapter ihDelegate = new ImplementationHidingComponentAdapter(delegate, true); 90 return getDelegate().registerComponent(new CachingComponentAdapter(ihDelegate)); 91 } 92 } 93 return getDelegate().registerComponentImplementation(componentKey, componentImplementation, parameters); 94 } 95 96 97 public MutablePicoContainer makeChildContainer() { 98 ImplementationHidingCachingPicoContainer pc = new ImplementationHidingCachingPicoContainer(this, caf, lifecycleManager); 99 getDelegate().addChildContainer(pc); 100 return pc; 101 102 } 103 104 } 105 | Popular Tags |