1 10 package org.picocontainer.gems; 11 12 import com.thoughtworks.proxy.ProxyFactory; 13 import com.thoughtworks.proxy.factory.StandardProxyFactory; 14 import com.thoughtworks.proxy.toys.delegate.ObjectReference; 15 import com.thoughtworks.proxy.toys.hotswap.HotSwapping; 16 import com.thoughtworks.proxy.toys.multicast.ClassHierarchyIntrospector; 17 import org.picocontainer.ComponentAdapter; 18 import org.picocontainer.PicoContainer; 19 import org.picocontainer.defaults.DecoratingComponentAdapter; 20 21 40 public class HotSwappingComponentAdapter extends DecoratingComponentAdapter { 41 private final ProxyFactory proxyFactory; 42 43 private static class ImplementationHidingReference implements ObjectReference { 44 private final ComponentAdapter delegate; 45 private Object value; 46 private final PicoContainer container; 47 48 public ImplementationHidingReference(ComponentAdapter delegate, PicoContainer container) { 49 this.delegate = delegate; 50 this.container = container; 51 } 52 53 public Object get() { 54 if (value == null) { 55 value = delegate.getComponentInstance(container); 56 } 57 return value; 58 } 59 60 public void set(Object item) { 61 value = item; 62 } 63 } 64 65 public HotSwappingComponentAdapter(final ComponentAdapter delegate, ProxyFactory proxyFactory) { 66 super(delegate); 67 this.proxyFactory = proxyFactory; 68 } 69 70 public HotSwappingComponentAdapter(ComponentAdapter delegate) { 71 this(delegate, new StandardProxyFactory()); 72 } 73 74 public Object getComponentInstance(final PicoContainer container) { 75 Class [] proxyTypes; 76 if (getComponentKey() instanceof Class && proxyFactory.canProxy((Class ) getComponentKey())) { 77 proxyTypes = new Class []{(Class ) getComponentKey()}; 78 } else { 79 proxyTypes = ClassHierarchyIntrospector.addIfClassProxyingSupportedAndNotObject(getComponentImplementation(), getComponentImplementation().getInterfaces(), proxyFactory); 80 } 81 ObjectReference reference = new ImplementationHidingReference(getDelegate(), container); 82 return HotSwapping.object(proxyTypes, proxyFactory, reference, true); 83 } 84 } 85
| Popular Tags
|