Clover coverage report - PicoContainer - 1.0-RC-1
Coverage timestamp: Tue May 18 2004 16:19:33 EDT
file stats: LOC: 95   Methods: 3
NCLOC: 51   Classes: 2
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ThreadLocalComponentAdapter.java 0% 0% 0% 0%
coverage
 1   
 /*****************************************************************************
 2   
  * Copyright (c) PicoContainer Organization. All rights reserved.            *
 3   
  * ------------------------------------------------------------------------- *
 4   
  * The software in this package is published under the terms of the BSD      *
 5   
  * style license a copy of which has been included with this distribution in *
 6   
  * the LICENSE.txt file.                                                     *
 7   
  *                                                                           *
 8   
  * Original code by Joerg Schaible                                           *
 9   
  *****************************************************************************/
 10   
 package org.picocontainer.defaults;
 11   
 
 12   
 import java.lang.reflect.InvocationHandler;
 13   
 import java.lang.reflect.Method;
 14   
 import java.lang.reflect.Proxy;
 15   
 import java.util.Collections;
 16   
 import java.util.HashMap;
 17   
 import java.util.Map;
 18   
 
 19   
 import org.picocontainer.ComponentAdapter;
 20   
 import org.picocontainer.PicoInitializationException;
 21   
 import org.picocontainer.PicoIntrospectionException;
 22   
 
 23   
 
 24   
 /**
 25   
  * A {@link ComponentAdapter}that realizes a {@link ThreadLocal}component
 26   
  * instance. The adapter creates proxy instances, that will create the necessary
 27   
  * instances on-the-fly invoking the methods of the instance.
 28   
  *
 29   
  * <em>
 30   
  * IMPORTANT! This class will be moved out of the PicoContainer core
 31   
  * before release of the final version!
 32   
  * </em>
 33   
  * 
 34   
  * @author J&ouml;rg Schaible
 35   
  */
 36   
 public class ThreadLocalComponentAdapter
 37   
         extends DecoratingComponentAdapter {
 38   
 
 39   
     private static Map m_proxyMap = Collections.synchronizedMap(new HashMap());
 40   
 
 41   
     /**
 42   
      * Construct a ThreadLocalComponentAdapter.
 43   
      * @param delegate The {@link ComponentAdapter}to delegate.
 44   
      */
 45  0
     public ThreadLocalComponentAdapter(ComponentAdapter delegate) {
 46  0
         super(new CachingComponentAdapter(delegate, new ThreadLocalReference()));
 47   
     }
 48   
 
 49   
     /**
 50   
      * @see org.picocontainer.ComponentAdapter#getComponentInstance()
 51   
      */
 52  0
     public Object getComponentInstance()
 53   
             throws PicoInitializationException, PicoIntrospectionException,
 54   
             AssignabilityRegistrationException, NotConcreteRegistrationException {
 55   
 
 56  0
         final Object componentKey = getDelegate().getComponentKey();
 57  0
         final String key = String.valueOf(System.identityHashCode(componentKey))
 58   
                 + "."
 59   
                 + String.valueOf(System.identityHashCode(getContainer()));
 60  0
         Object proxy = m_proxyMap.get(key);
 61  0
         if (proxy == null) {
 62  0
             final Class[] interfaces;
 63  0
             if (componentKey instanceof Class && ((Class)componentKey).isInterface()) {
 64  0
                 interfaces = new Class[]{(Class)getDelegate().getComponentKey()};
 65   
             } else {
 66  0
                 interfaces = (Class[])ClassHierarchyIntrospector.getAllInterfaces(
 67   
                         getDelegate().getComponentImplementation()).toArray(new Class[0]);
 68   
             }
 69  0
             if (interfaces.length == 0) { throw new PicoIntrospectionException(
 70   
                     "Can't proxy implementation for "
 71   
                             + getDelegate().getComponentImplementation().getName()
 72   
                             + ". It doesn't implement any interfaces."); }
 73  0
             final ThreadLocalInvocationHandler threadLocalInvocationHandler = new ThreadLocalInvocationHandler();
 74  0
             proxy = Proxy.newProxyInstance(
 75   
                     getClass().getClassLoader(), interfaces, threadLocalInvocationHandler);
 76  0
             m_proxyMap.put(key, proxy);
 77   
         }
 78  0
         return proxy;
 79   
     }
 80   
 
 81   
     private class ThreadLocalInvocationHandler
 82   
             implements InvocationHandler {
 83   
 
 84   
         /**
 85   
          * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
 86   
          *          java.lang.reflect.Method, java.lang.Object[])
 87   
          */
 88  0
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 89  0
             final Object delegatedInstance = ThreadLocalComponentAdapter.this.getDelegate()
 90   
                     .getComponentInstance();
 91  0
             return method.invoke(delegatedInstance, args);
 92   
         }
 93   
 
 94   
     }
 95   
 }