Clover coverage report - Code Coverage for hivemind release 1.0-rc-2
Coverage timestamp: Sat Sep 11 2004 09:09:48 EDT
file stats: LOC: 190   Methods: 9
NCLOC: 100   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 90% 96.8% 100% 96%
coverage coverage
 1   
 //  Copyright 2004 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.events.RegistryShutdownListener;
 21   
 import org.apache.hivemind.impl.ConstructableServicePoint;
 22   
 import org.apache.hivemind.impl.ProxyUtils;
 23   
 import org.apache.hivemind.internal.Module;
 24   
 import org.apache.hivemind.service.ThreadCleanupListener;
 25   
 import org.apache.hivemind.service.ThreadEventNotifier;
 26   
 
 27   
 /**
 28   
  * Like
 29   
  * {@link org.apache.hivemind.impl.SingletonServiceModel}, 
 30   
  * this method returns a proxy (implementing the service interface); unlike
 31   
  * SingletonServiceModel, it <em>always</em> returns the proxy.
 32   
  * Invoking a service method on the proxy constructs a service implementation
 33   
  * and binds it to the current thread. 
 34   
  *
 35   
  * @author Howard Lewis Ship
 36   
  */
 37   
 public final class ThreadedServiceModel extends AbstractServiceModelImpl
 38   
 {
 39   
     /**
 40   
      * Name of a method in the deferred proxy that is used to obtain
 41   
      * the constructed service.
 42   
      */
 43   
     protected static final String SERVICE_ACCESSOR_METHOD_NAME = "_service";
 44   
 
 45   
     private Object _serviceProxy;
 46   
     private ThreadEventNotifier _notifier;
 47   
 
 48  6
     public ThreadedServiceModel(ConstructableServicePoint servicePoint)
 49   
     {
 50  6
         super(servicePoint);
 51   
     }
 52   
 
 53   
     class CleanupListener implements ThreadCleanupListener
 54   
     {
 55   
         // The core itself
 56   
         private Object _core;
 57   
 
 58  8
         CleanupListener(Object core)
 59   
         {
 60  8
             _core = core;
 61   
         }
 62   
 
 63  2
         public void threadDidCleanup()
 64   
         {
 65   
             // Orhpan this object
 66  2
             _notifier.removeThreadCleanupListener(this);
 67   
 
 68  2
             unbindServiceFromCurrentThread();
 69   
 
 70  2
             if (_core instanceof Discardable)
 71   
             {
 72  1
                 Discardable d = (Discardable) _core;
 73   
 
 74  1
                 d.threadDidDiscardService();
 75   
             }
 76   
         }
 77   
     }
 78   
 
 79   
     /**
 80   
      * Used to store the active service for the current thread.
 81   
      */
 82   
     private ThreadLocal _activeService;
 83   
 
 84   
     /**
 85   
      * Always returns the service proxy.
 86   
      */
 87  6
     public synchronized Object getService()
 88   
     {
 89   
         // _activeService will be null on first invocation; a good
 90   
         // time to create it, the proxy, and find the notifier.
 91   
 
 92  6
         if (_activeService == null)
 93   
         {
 94  6
             _activeService = new ThreadLocal();
 95   
 
 96  6
             Module module = getServicePoint().getModule();
 97   
 
 98  6
             _notifier =
 99   
                 (ThreadEventNotifier) module.getService(
 100   
                     HiveMind.THREAD_EVENT_NOTIFIER_SERVICE,
 101   
                     ThreadEventNotifier.class);
 102   
 
 103  6
             _serviceProxy = createServiceProxy();
 104   
         }
 105   
 
 106   
         // The result is an interceptor stack, where the final (most deeply nested) object
 107   
         // is the serviceProxy.  The serviceProxy obtains the instance for the current thread
 108   
         // and delegates to it.  This is a little bit different than SingletonServiceModel, which
 109   
         // creates a pair of proxies so as to defer creation of the interceptors as well. In both
 110   
         // cases, the interceptors are only created once.
 111   
 
 112  6
         return _serviceProxy;
 113   
     }
 114   
 
 115   
     /**
 116   
      * Creates a proxy instance for the service, and returns it, wrapped in any
 117   
      * interceptors for the service.
 118   
      */
 119  6
     private Object createServiceProxy()
 120   
     {
 121  6
         if (_log.isDebugEnabled())
 122  1
             _log.debug(
 123   
                 "Creating ThreadedProxy for service " + getServicePoint().getExtensionPointId());
 124   
 
 125  6
         Object proxy =
 126   
             ProxyUtils.createDelegatingProxy(
 127   
                 "ThreadedProxy",
 128   
                 this,
 129   
                 "getServiceImplementationForCurrentThread",
 130   
                 getServicePoint(),
 131   
                 getServicePoint().getShutdownCoordinator());
 132   
 
 133  6
         return addInterceptors(proxy);
 134   
     }
 135   
 
 136   
     /**
 137   
      * Invoked by the proxy to return 
 138   
      * the active service impl for this thread,
 139   
      * constructing it as necessary.
 140   
      */
 141  17
     public Object getServiceImplementationForCurrentThread()
 142   
     {
 143  17
         Object result = _activeService.get();
 144   
 
 145  17
         if (result == null)
 146  8
             result = constructServiceForCurrentThread();
 147   
 
 148  17
         return result;
 149   
     }
 150   
 
 151  8
     private synchronized Object constructServiceForCurrentThread()
 152   
     {
 153  8
         try
 154   
         {
 155  8
             Object core = constructCoreServiceImplementation();
 156   
 
 157  8
             if (core instanceof RegistryShutdownListener)
 158  1
                 _log.error(ServiceModelMessages.registryCleanupIgnored(getServicePoint()));
 159   
 
 160  8
             _notifier.addThreadCleanupListener(new CleanupListener(core));
 161   
 
 162  8
             _activeService.set(core);
 163   
 
 164  8
             return core;
 165   
         }
 166   
         catch (Exception ex)
 167   
         {
 168  0
             throw new ApplicationRuntimeException(
 169   
                 ServiceModelMessages.unableToConstructService(getServicePoint(), ex),
 170   
                 ex);
 171   
         }
 172   
     }
 173   
 
 174  2
     private void unbindServiceFromCurrentThread()
 175   
     {
 176  2
         _activeService.set(null);
 177   
     }
 178   
 
 179   
     /**
 180   
      * Invokes {@link #getServiceImplementationForCurrentThread()} to force the creation
 181   
      * of the service implementation.
 182   
      */
 183   
 
 184  1
     public void instantiateService()
 185   
     {
 186  1
         getServiceImplementationForCurrentThread();
 187   
     }
 188   
 
 189   
 }
 190