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: 275   Methods: 7
NCLOC: 141   Classes: 1
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
SingletonServiceModel.java 90% 98.8% 100% 98%
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 java.lang.reflect.Constructor;
 18   
 import java.lang.reflect.Modifier;
 19   
 
 20   
 import org.apache.hivemind.ApplicationRuntimeException;
 21   
 import org.apache.hivemind.events.RegistryShutdownListener;
 22   
 import org.apache.hivemind.impl.ConstructableServicePoint;
 23   
 import org.apache.hivemind.impl.ProxyBuilder;
 24   
 import org.apache.hivemind.internal.ServicePoint;
 25   
 import org.apache.hivemind.service.BodyBuilder;
 26   
 import org.apache.hivemind.service.ClassFab;
 27   
 import org.apache.hivemind.service.MethodSignature;
 28   
 
 29   
 /**
 30   
  * Subclass of {@link org.apache.hivemind.impl.servicemodel.AbstractServiceModelImpl} which
 31   
  * supports creation of a singleton service proxy (deferring the actual construction of the service
 32   
  * until absolutely necessary). This is used with the singleton service type, which is the default.
 33   
  * 
 34   
  * @author Howard Lewis Ship
 35   
  */
 36   
 public final class SingletonServiceModel 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 SingletonInnerProxy _innerProxy;
 46   
 
 47   
     private Object _constructedService;
 48   
 
 49  936
     public SingletonServiceModel(ConstructableServicePoint servicePoint)
 50   
     {
 51  936
         super(servicePoint);
 52   
     }
 53   
 
 54  937
     public synchronized Object getService()
 55   
     {
 56  937
         if (_serviceProxy == null)
 57  936
             _serviceProxy = createSingletonProxy();
 58   
 
 59  937
         return _serviceProxy;
 60   
     }
 61   
 
 62   
     /**
 63   
      * This is invoked by the proxy to create the actual implementation.
 64   
      */
 65  464
     public synchronized Object getActualServiceImplementation()
 66   
     {
 67  464
         if (_constructedService == null)
 68  464
             _constructedService = constructServiceImplementation();
 69   
 
 70  464
         if( _constructedService instanceof RegistryShutdownListener )
 71   
         {
 72  3
             getShutdownCoordinatorService().addRegistryShutdownListener( ( RegistryShutdownListener )_constructedService );
 73   
         }
 74   
         // The inner proxy needs the service to implement the service interface.
 75   
         // For bean services (not interface services) with no interceptors,
 76   
         // the implementation may be the bean provided by the factory ... which
 77   
         // does not implement the service interface (which was created at runtime).
 78   
         // So we introduce a "bridge" between the two.
 79   
 
 80  464
         Class serviceInterface = getServicePoint().getServiceInterface();
 81   
     
 82  464
         if (!serviceInterface.isInstance(_constructedService))
 83  4
             _constructedService = constructBridgeProxy(_constructedService);
 84   
 
 85  464
         return _constructedService;
 86   
     }
 87   
 
 88   
     /**
 89   
      * Creates a proxy class for the service and then construct the class itself.
 90   
      */
 91  936
     private Object createSingletonProxy()
 92   
     {
 93  936
         if (_log.isDebugEnabled())
 94  4
             _log.debug("Creating SingletonProxy for service "
 95   
                     + getServicePoint().getExtensionPointId());
 96   
 
 97  936
         try
 98   
         {
 99   
 
 100   
             // Create the outer proxy, the one visible to client code (including
 101   
             // other services). It is dependent on an inner proxy.
 102   
 
 103  936
             Class proxyClass = createSingletonProxyClass();
 104   
 
 105   
             // Create the inner proxy, whose job is to replace itself
 106   
             // when the first service method is invoked.
 107   
 
 108  936
             Class innerProxyClass = createInnerProxyClass(proxyClass);
 109   
 
 110   
             // Create the outer proxy.
 111   
 
 112  936
             Object result = proxyClass.newInstance();
 113   
 
 114   
             // The inner proxy's construct invokes a method on the
 115   
             // outer proxy to connect the two.
 116   
 
 117  936
             Constructor c = innerProxyClass.getConstructor(new Class[]
 118   
             { proxyClass, getClass() });
 119   
 
 120  936
             _innerProxy = (SingletonInnerProxy) c.newInstance(new Object[]
 121   
             { result, this });
 122   
 
 123  936
             getServicePoint().getShutdownCoordinator().addRegistryShutdownListener(
 124   
                     (RegistryShutdownListener) result);
 125   
 
 126  936
             return result;
 127   
         }
 128   
         catch (Exception ex)
 129   
         {
 130  0
             throw new ApplicationRuntimeException(ex);
 131   
         }
 132   
 
 133   
     }
 134   
 
 135   
     /**
 136   
      * Creates a class that implements the service interface. Implements a private synchronized
 137   
      * method, _service(), that constructs the service as needed, and has each service interface
 138   
      * method re-invoke on _service(). Adds a toString() method if the service interface does not
 139   
      * define toString().
 140   
      */
 141  936
     private Class createSingletonProxyClass()
 142   
     {
 143  936
         ConstructableServicePoint servicePoint = getServicePoint();
 144   
 
 145  936
         ProxyBuilder proxyBuilder = new ProxyBuilder("SingletonProxy", servicePoint, true);
 146   
 
 147  936
         ClassFab classFab = proxyBuilder.getClassFab();
 148   
 
 149  936
         Class serviceInterface = servicePoint.getServiceInterface();
 150   
 
 151   
         // This will initally be the inner proxy, then switch over to the
 152   
         // service implementation.
 153   
 
 154  936
         classFab.addField("_inner", serviceInterface);
 155  936
         classFab.addField("_shutdown", boolean.class);
 156   
 
 157  936
         classFab.addInterface(RegistryShutdownListener.class);
 158   
 
 159  936
         classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class,
 160   
                 "registryDidShutdown", null, null), "{ _shutdown = true; }");
 161   
 
 162  936
         classFab.addMethod(
 163   
                 Modifier.PUBLIC | Modifier.SYNCHRONIZED | Modifier.FINAL,
 164   
                 new MethodSignature(void.class, "_setInner", new Class[]
 165   
                 { serviceInterface }, null),
 166   
                 "{ _inner = $1; }");
 167   
 
 168  936
         BodyBuilder builder = new BodyBuilder();
 169  936
         builder.begin();
 170  936
         builder.addln("if (_shutdown)");
 171  936
         builder.begin();
 172  936
         builder.addln("_inner = null;");
 173  936
         builder.addln("throw org.apache.hivemind.HiveMind#createRegistryShutdownException();");
 174  936
         builder.end();
 175   
 
 176  936
         builder.addln("return _inner;");
 177  936
         builder.end();
 178   
 
 179  936
         classFab.addMethod(Modifier.PRIVATE, new MethodSignature(serviceInterface, "_getInner",
 180   
                 null, null), builder.toString());
 181   
 
 182  936
         proxyBuilder.addServiceMethods("_getInner()");
 183   
 
 184  936
         return classFab.createClass();
 185   
     }
 186   
 
 187  936
     private Class createInnerProxyClass(Class deferredProxyClass)
 188   
     {
 189  936
         ServicePoint servicePoint = getServicePoint();
 190   
 
 191  936
         Class serviceInterface = servicePoint.getServiceInterface();
 192  936
         ProxyBuilder builder = new ProxyBuilder("InnerProxy", servicePoint);
 193   
 
 194  936
         ClassFab classFab = builder.getClassFab();
 195   
 
 196  936
         classFab.addField("_deferredProxy", deferredProxyClass);
 197  936
         classFab.addField("_service", serviceInterface);
 198  936
         classFab.addField("_serviceModel", getClass());
 199   
 
 200  936
         BodyBuilder body = new BodyBuilder();
 201   
 
 202   
         // The constructor remembers the outer proxy and registers itself
 203   
         // with the outer proxy.
 204   
 
 205  936
         body.begin();
 206   
 
 207  936
         body.addln("super();");
 208  936
         body.addln("_deferredProxy = $1;");
 209  936
         body.addln("_serviceModel = $2;");
 210  936
         body.addln("_deferredProxy._setInner(this);");
 211   
 
 212  936
         body.end();
 213   
 
 214  936
         classFab.addConstructor(new Class[]
 215   
         { deferredProxyClass, getClass() }, null, body.toString());
 216   
 
 217   
         // Method _service() will look up the service implementation,
 218   
         // then update the deferred proxy to go directly to the
 219   
         // service implementation, bypassing itself!
 220   
 
 221  936
         body.clear();
 222  936
         body.begin();
 223   
 
 224  936
         body.add("if (_service == null)");
 225  936
         body.begin();
 226   
 
 227  936
         body.add("_service = (");
 228  936
         body.add(serviceInterface.getName());
 229  936
         body.addln(") _serviceModel.getActualServiceImplementation();");
 230   
 
 231  936
         body.add("_deferredProxy._setInner(_service);");
 232   
 
 233  936
         body.end();
 234   
 
 235  936
         body.add("return _service;");
 236   
 
 237  936
         body.end();
 238   
 
 239  936
         classFab.addMethod(
 240   
                 Modifier.PRIVATE | Modifier.FINAL | Modifier.SYNCHRONIZED,
 241   
                 new MethodSignature(serviceInterface, "_service", null, null),
 242   
                 body.toString());
 243   
 
 244  936
         builder.addServiceMethods("_service()");
 245   
 
 246   
         // Build the implementation of interface SingletonInnerProxy
 247   
 
 248  936
         body.clear();
 249  936
         body.begin();
 250   
 
 251  936
         body.add("_service();");
 252   
 
 253  936
         body.end();
 254   
 
 255  936
         classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class,
 256   
                 "_instantiateServiceImplementation", null, null), body.toString());
 257   
 
 258  936
         classFab.addInterface(SingletonInnerProxy.class);
 259   
 
 260  936
         return classFab.createClass();
 261   
     }
 262   
 
 263  1
     public void instantiateService()
 264   
     {
 265   
         // Ensure that the outer and inner proxies have been created
 266   
 
 267  1
         getService();
 268   
 
 269   
         // Force the inner proxy to resolve the service and install the result into
 270   
         // the outer proxy.
 271   
 
 272  1
         _innerProxy._instantiateServiceImplementation();
 273   
     }
 274   
 
 275   
 }