1 10 package org.picocontainer.gems; 11 12 import com.thoughtworks.proxy.Invoker; 13 import com.thoughtworks.proxy.ProxyFactory; 14 import com.thoughtworks.proxy.factory.StandardProxyFactory; 15 import com.thoughtworks.proxy.toys.multicast.ClassHierarchyIntrospector; 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.AssignabilityRegistrationException; 22 import org.picocontainer.defaults.CachingComponentAdapter; 23 import org.picocontainer.defaults.DecoratingComponentAdapter; 24 import org.picocontainer.defaults.NotConcreteRegistrationException; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.Proxy ; 29 30 31 46 public class ThreadLocalComponentAdapter extends DecoratingComponentAdapter { 47 48 private transient Class [] interfaces; 49 private ProxyFactory proxyFactory; 50 51 59 public ThreadLocalComponentAdapter(final ComponentAdapter delegate, final ProxyFactory proxyFactory) 60 throws PicoIntrospectionException { 61 super(new CachingComponentAdapter(delegate, new ThreadLocalReference())); 62 this.proxyFactory = proxyFactory; 63 interfaces = getInterfaces(); 64 } 65 66 73 public ThreadLocalComponentAdapter(final ComponentAdapter delegate) throws PicoIntrospectionException { 74 this(new CachingComponentAdapter(delegate, new ThreadLocalReference()), new StandardProxyFactory()); 75 } 76 77 80 public Object getComponentInstance(final PicoContainer pico) 81 throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, 82 NotConcreteRegistrationException { 83 84 if (interfaces == null) { 85 interfaces = getInterfaces(); 86 } 87 88 final ComponentAdapter delegate = getDelegate(); 89 final Invoker invoker = new ThreadLocalInvoker(pico, delegate); 90 return proxyFactory.createProxy(interfaces, invoker); 91 } 92 93 final private Class [] getInterfaces() { 94 final Object componentKey = getComponentKey(); 95 final Class [] interfaces; 96 if (componentKey instanceof Class && ((Class ) componentKey).isInterface()) { 97 interfaces = new Class []{(Class ) componentKey}; 98 } else { 99 interfaces = ClassHierarchyIntrospector.getAllInterfaces(getComponentImplementation()); 100 } 101 if (interfaces.length == 0) { 102 throw new PicoIntrospectionException("Can't proxy implementation for " 103 + getComponentImplementation().getName() 104 + ". It does not implement any interfaces."); 105 } 106 return interfaces; 107 } 108 109 final static private class ThreadLocalInvoker implements Invoker { 110 111 private final PicoContainer pico; 112 private final ComponentAdapter delegate; 113 114 private ThreadLocalInvoker(final PicoContainer pico, final ComponentAdapter delegate) { 115 this.pico = pico; 116 this.delegate = delegate; 117 } 118 119 123 public Object invoke(final Object proxy, final Method method, final Object [] args) throws Throwable { 124 final Object delegatedInstance = delegate.getComponentInstance(pico); 125 try { 126 return method.invoke(delegatedInstance, args); 127 } catch (final InvocationTargetException e) { 128 throw e.getTargetException(); 129 } 130 } 131 } 132 }
| Popular Tags
|