1 10 11 package org.picocontainer.gems; 12 13 import com.thoughtworks.proxy.ProxyFactory; 14 import com.thoughtworks.proxy.factory.StandardProxyFactory; 15 import com.thoughtworks.proxy.toys.delegate.Delegating; 16 17 import org.picocontainer.ComponentAdapter; 18 import org.picocontainer.PicoContainer; 19 import org.picocontainer.PicoInitializationException; 20 import org.picocontainer.PicoIntrospectionException; 21 import org.picocontainer.defaults.DecoratingComponentAdapter; 22 23 24 56 public class AssimilatingComponentAdapter extends DecoratingComponentAdapter { 57 58 private final Class type; 59 private final ProxyFactory proxyFactory; 60 private final boolean isCompatible; 61 62 73 public AssimilatingComponentAdapter(final Class type, final ComponentAdapter delegate, final ProxyFactory proxyFactory) 74 throws PicoIntrospectionException { 75 super(delegate); 76 this.type = type; 77 this.proxyFactory = proxyFactory; 78 this.isCompatible = type.isAssignableFrom(delegate.getComponentImplementation()); 79 if (!isCompatible) { 80 if (!proxyFactory.canProxy(type)) { 81 throw new PicoIntrospectionException("Cannot create proxy for type " + type.getName()); 82 } 83 } 85 } 86 87 96 public AssimilatingComponentAdapter(final Class type, final ComponentAdapter delegate) { 97 this(type, delegate, new StandardProxyFactory()); 98 } 99 100 107 public Object getComponentInstance(final PicoContainer container) 108 throws PicoInitializationException, PicoIntrospectionException { 109 return isCompatible ? super.getComponentInstance(container) : Delegating.object( 110 type, super.getComponentInstance(container), proxyFactory); 111 } 112 113 119 public Class getComponentImplementation() { 120 return isCompatible ? super.getComponentImplementation() : type; 121 } 122 123 130 public Object getComponentKey() { 131 final Object key = super.getComponentKey(); 132 if (key instanceof Class && (!isCompatible || !type.isAssignableFrom((Class ) key))) { 133 return type; 134 } 135 return key; 136 } 137 } 138
| Popular Tags
|