Clover coverage report - Code Coverage for hivemind release 1.1-beta-1
Coverage timestamp: Thu Apr 28 2005 19:53:41 EDT
file stats: LOC: 207   Methods: 9
NCLOC: 106   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
ThreadedServiceModel.java 91.7% 97.4% 100% 96.7%
coverage coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.impl.servicemodel;
 16   
 
 17   
 import org.apache.hivemind.ApplicationRuntimeException;
 18   
 import org.apache.hivemind.Discardable;
 19   
 import org.apache.hivemind.HiveMind;
 20   
 import org.apache.hivemind.ShutdownCoordinator;
 21   
 import org.apache.hivemind.events.RegistryShutdownListener;
 22   
 import org.apache.hivemind.impl.ConstructableServicePoint;
 23   
 import org.apache.hivemind.impl.ProxyUtils;
 24   
 import org.apache.hivemind.internal.Module;
 25   
 import org.apache.hivemind.service.ThreadCleanupListener;
 26   
 import org.apache.hivemind.service.ThreadEventNotifier;
 27   
 
 28   
 /**
 29   
  * Like {@link org.apache.hivemind.impl.servicemodel.SingletonServiceModel}, this method returns a proxy
 30   
  * (implementing the service interface); unlike SingletonServiceModel, it <em>always</em> returns
 31   
  * the proxy. Invoking a service method on the proxy constructs a service implementation and binds
 32   
  * it to the current thread.
 33   
  * 
 34   
  * @author Howard Lewis Ship
 35   
  */
 36   
 public final class ThreadedServiceModel extends AbstractServiceModelImpl
 37   
 {
 38   
     /**
 39   
      * Name of a method in the deferred proxy that is used to obtain the constructed service.
 40   
      */
 41   
     protected static final String SERVICE_ACCESSOR_METHOD_NAME = "_service";
 42   
 
 43   
     private Object _serviceProxy;
 44   
 
 45   
     private ThreadEventNotifier _notifier;
 46   
     
 47   
     /** @since 1.1 */
 48   
     
 49   
     private Class _serviceInterface;
 50   
 
 51  17
     public ThreadedServiceModel(ConstructableServicePoint servicePoint)
 52   
     {
 53  17
         super(servicePoint);
 54   
         
 55  17
         _serviceInterface = servicePoint.getServiceInterface();
 56   
     }
 57   
 
 58   
     class CleanupListener implements ThreadCleanupListener
 59   
     {
 60   
         // The core itself
 61   
         private Object _core;
 62   
 
 63  16
         CleanupListener(Object core)
 64   
         {
 65  16
             _core = core;
 66   
         }
 67   
 
 68  3
         public void threadDidCleanup()
 69   
         {
 70   
             // Orhpan this object
 71  3
             _notifier.removeThreadCleanupListener(this);
 72   
 
 73  3
             unbindServiceFromCurrentThread();
 74   
 
 75  3
             if (_core instanceof Discardable)
 76   
             {
 77  1
                 Discardable d = (Discardable) _core;
 78   
 
 79  1
                 d.threadDidDiscardService();
 80   
             }
 81   
         }
 82   
     }
 83   
 
 84   
     /**
 85   
      * Used to store the active service for the current thread.
 86   
      */
 87   
     private ThreadLocal _activeService;
 88   
 
 89   
     /**
 90   
      * Always returns the service proxy.
 91   
      */
 92  17
     public synchronized Object getService()
 93   
     {
 94   
         // _activeService will be null on first invocation; a good
 95   
         // time to create it, the proxy, and find the notifier.
 96   
 
 97  17
         if (_activeService == null)
 98   
         {
 99  17
             _activeService = new ThreadLocal();
 100   
 
 101  17
             Module module = getServicePoint().getModule();
 102   
 
 103  17
             _notifier = (ThreadEventNotifier) module.getService(
 104   
                     HiveMind.THREAD_EVENT_NOTIFIER_SERVICE,
 105   
                     ThreadEventNotifier.class);
 106   
 
 107  17
             _serviceProxy = createServiceProxy();
 108   
         }
 109   
 
 110   
         // The result is an interceptor stack, where the final (most deeply nested) object
 111   
         // is the serviceProxy. The serviceProxy obtains the instance for the current thread
 112   
         // and delegates to it. This is a little bit different than SingletonServiceModel, which
 113   
         // creates a pair of proxies so as to defer creation of the interceptors as well. In both
 114   
         // cases, the interceptors are only created once.
 115   
 
 116  17
         return _serviceProxy;
 117   
     }
 118   
 
 119   
     /**
 120   
      * Creates a proxy instance for the service, and returns it, wrapped in any interceptors for the
 121   
      * service.
 122   
      */
 123  17
     private Object createServiceProxy()
 124   
     {
 125  17
         ConstructableServicePoint servicePoint = getServicePoint();
 126   
 
 127  17
         if (_log.isDebugEnabled())
 128  1
             _log.debug("Creating ThreadedProxy for service " + servicePoint.getExtensionPointId());
 129   
 
 130  17
         Object proxy = ProxyUtils.createDelegatingProxy(
 131   
                 "ThreadedProxy",
 132   
                 this,
 133   
                 "getServiceImplementationForCurrentThread",
 134   
                 servicePoint);
 135   
 
 136  17
         Object intercepted = addInterceptors(proxy);
 137   
 
 138  17
         RegistryShutdownListener outerProxy = ProxyUtils
 139   
                 .createOuterProxy(intercepted, servicePoint);
 140   
 
 141  17
         ShutdownCoordinator coordinator = servicePoint.getShutdownCoordinator();
 142   
 
 143  17
         coordinator.addRegistryShutdownListener(outerProxy);
 144   
 
 145  17
         return outerProxy;
 146   
     }
 147   
 
 148   
     /**
 149   
      * Invoked by the proxy to return the active service impl for this thread, constructing it as
 150   
      * necessary.
 151   
      */
 152  28
     public Object getServiceImplementationForCurrentThread()
 153   
     {
 154  28
         Object result = _activeService.get();
 155   
 
 156  28
         if (result == null)
 157  16
             result = constructServiceForCurrentThread();
 158   
 
 159  28
         return result;
 160   
     }
 161   
 
 162  16
     private synchronized Object constructServiceForCurrentThread()
 163   
     {
 164  16
         try
 165   
         {
 166  16
             Object core = constructCoreServiceImplementation();
 167   
 
 168  16
             if (core instanceof RegistryShutdownListener)
 169  1
                 _log.error(ServiceModelMessages.registryCleanupIgnored(getServicePoint()));
 170   
 
 171  16
             _notifier.addThreadCleanupListener(new CleanupListener(core));
 172   
 
 173  16
             _activeService.set(core);
 174   
 
 175   
             // Once more ... with bean services, its possible that
 176   
             // the factory generated bean does not implement the (synthetic) service
 177   
             // interface, so create a bridge to it.
 178   
 
 179  16
             if (!_serviceInterface.isInstance(core))
 180  1
                 core = constructBridgeProxy(core);
 181   
 
 182  16
             return core;
 183   
         }
 184   
         catch (Exception ex)
 185   
         {
 186  0
             throw new ApplicationRuntimeException(ServiceModelMessages.unableToConstructService(
 187   
                     getServicePoint(),
 188   
                     ex), ex);
 189   
         }
 190   
     }
 191   
 
 192  3
     private void unbindServiceFromCurrentThread()
 193   
     {
 194  3
         _activeService.set(null);
 195   
     }
 196   
 
 197   
     /**
 198   
      * Invokes {@link #getServiceImplementationForCurrentThread()}to force the creation of the
 199   
      * service implementation.
 200   
      */
 201   
 
 202  1
     public void instantiateService()
 203   
     {
 204  1
         getServiceImplementationForCurrentThread();
 205   
     }
 206   
 
 207   
 }