Clover coverage report - Code Coverage for hivemind release 1.1
Coverage timestamp: Tue Oct 25 2005 10:47:07 EDT
file stats: LOC: 207   Methods: 9
NCLOC: 106   Classes: 2
 
 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  19 public ThreadedServiceModel(ConstructableServicePoint servicePoint)
 52    {
 53  19 super(servicePoint);
 54   
 55  19 _serviceInterface = servicePoint.getServiceInterface();
 56    }
 57   
 58    class CleanupListener implements ThreadCleanupListener
 59    {
 60    // The core itself
 61    private Object _core;
 62   
 63  18 CleanupListener(Object core)
 64    {
 65  18 _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  19 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  19 if (_activeService == null)
 98    {
 99  19 _activeService = new ThreadLocal();
 100   
 101  19 Module module = getServicePoint().getModule();
 102   
 103  19 _notifier = (ThreadEventNotifier) module.getService(
 104    HiveMind.THREAD_EVENT_NOTIFIER_SERVICE,
 105    ThreadEventNotifier.class);
 106   
 107  19 _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  19 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  19 private Object createServiceProxy()
 124    {
 125  19 ConstructableServicePoint servicePoint = getServicePoint();
 126   
 127  19 if (_log.isDebugEnabled())
 128  1 _log.debug("Creating ThreadedProxy for service " + servicePoint.getExtensionPointId());
 129   
 130  19 Object proxy = ProxyUtils.createDelegatingProxy(
 131    "ThreadedProxy",
 132    this,
 133    "getServiceImplementationForCurrentThread",
 134    servicePoint);
 135   
 136  19 Object intercepted = addInterceptors(proxy);
 137   
 138  19 RegistryShutdownListener outerProxy = ProxyUtils
 139    .createOuterProxy(intercepted, servicePoint);
 140   
 141  19 ShutdownCoordinator coordinator = servicePoint.getShutdownCoordinator();
 142   
 143  19 coordinator.addRegistryShutdownListener(outerProxy);
 144   
 145  19 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  31 public Object getServiceImplementationForCurrentThread()
 153    {
 154  31 Object result = _activeService.get();
 155   
 156  31 if (result == null)
 157  18 result = constructServiceForCurrentThread();
 158   
 159  31 return result;
 160    }
 161   
 162  18 private synchronized Object constructServiceForCurrentThread()
 163    {
 164  18 try
 165    {
 166  18 Object core = constructCoreServiceImplementation();
 167   
 168  18 if (core instanceof RegistryShutdownListener)
 169  2 _log.error(ServiceModelMessages.registryCleanupIgnored(getServicePoint()));
 170   
 171  18 _notifier.addThreadCleanupListener(new CleanupListener(core));
 172   
 173    // Once more ... with bean services, its possible that
 174    // the factory generated bean does not implement the (synthetic) service
 175    // interface, so create a bridge to it.
 176   
 177  18 if (!_serviceInterface.isInstance(core))
 178  2 core = constructBridgeProxy(core);
 179   
 180  18 _activeService.set(core);
 181   
 182  18 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    }