Clover coverage report - Code Coverage for hivemind release 1.0-rc-1
Coverage timestamp: Wed Aug 25 2004 13:06:02 EDT
file stats: LOC: 145   Methods: 4
NCLOC: 81   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
ProxyUtils.java - 96.8% 75% 94.3%
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;
 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.ShutdownCoordinator;
 22   
 import org.apache.hivemind.events.RegistryShutdownListener;
 23   
 import org.apache.hivemind.internal.ServiceModel;
 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   
  * Contains some common code used to create proxies that defer to a service model method
 31   
  * for thier service.
 32   
  *
 33   
  * @author Howard Lewis Ship
 34   
  */
 35   
 public final class ProxyUtils
 36   
 {
 37   
     public static final String SERVICE_ACCESSOR_METHOD_NAME = "_service";
 38   
 
 39  0
     private ProxyUtils()
 40   
     {
 41   
         // Prevent instantiation
 42   
     }
 43   
 
 44   
     /**
 45   
      * Creates a class that implements the service interface. Implements
 46   
      * a private synchronized method, _service(), that constructs the service
 47   
      * as needed, and has each service interface method re-invoke on _service().
 48   
      * Adds a toString() method if the service interface does not define toString().
 49   
      */
 50  11
     public static Object createDelegatingProxy(
 51   
         String type,
 52   
         ServiceModel serviceModel,
 53   
         String delegationMethodName,
 54   
         ServicePoint servicePoint,
 55   
         ShutdownCoordinator shutdownCoordinator)
 56   
     {
 57  11
         ProxyBuilder builder = new ProxyBuilder(type, servicePoint);
 58   
 
 59  11
         ClassFab classFab = builder.getClassFab();
 60   
 
 61  11
         addConstructor(classFab, serviceModel);
 62   
 
 63  11
         addServiceAccessor(classFab, delegationMethodName, servicePoint);
 64   
 
 65  11
         builder.addServiceMethods(SERVICE_ACCESSOR_METHOD_NAME + "()");
 66   
 
 67  11
         Class proxyClass = classFab.createClass();
 68   
 
 69  11
         try
 70   
         {
 71  11
             Constructor c = proxyClass.getConstructor(new Class[] { serviceModel.getClass()});
 72   
 
 73  11
             RegistryShutdownListener result =
 74   
                 (RegistryShutdownListener) c.newInstance(new Object[] { serviceModel });
 75   
 
 76  11
             shutdownCoordinator.addRegistryShutdownListener(result);
 77   
 
 78  11
             return result;
 79   
         }
 80   
         catch (Exception ex)
 81   
         {
 82  0
             throw new ApplicationRuntimeException(ex);
 83   
         }
 84   
     }
 85   
 
 86   
     /**
 87   
      * Adds a field, _serviceExtensionPoint, whose type
 88   
      * matches this class, and a constructor which sets
 89   
      * the field.
 90   
      */
 91  11
     private static void addConstructor(ClassFab classFab, ServiceModel model)
 92   
     {
 93  11
         Class modelClass = model.getClass();
 94   
 
 95  11
         classFab.addField("_serviceModel", modelClass);
 96   
 
 97  11
         classFab.addConstructor(
 98   
             new Class[] { modelClass },
 99   
             null,
 100   
             "{ super(); _serviceModel = $1; }");
 101   
     }
 102   
 
 103   
     /**
 104   
      * We
 105   
      * construct a method that always goes through this service model's
 106   
      * {@link #getServiceImplementationForCurrentThread())} method.
 107   
      */
 108  11
     private static void addServiceAccessor(
 109   
         ClassFab classFab,
 110   
         String serviceModelMethodName,
 111   
         ServicePoint servicePoint)
 112   
     {
 113  11
         Class serviceInterface = servicePoint.getServiceInterface();
 114   
 
 115  11
         classFab.addField(SERVICE_ACCESSOR_METHOD_NAME, serviceInterface);
 116  11
         classFab.addField("_shutdown", boolean.class);
 117   
 
 118  11
         BodyBuilder builder = new BodyBuilder();
 119  11
         builder.begin();
 120   
 
 121  11
         builder.addln("if (_shutdown)");
 122  11
         builder.addln("  throw org.apache.hivemind.HiveMind#createRegistryShutdownException();");
 123   
 
 124  11
         builder.add("return (");
 125  11
         builder.add(serviceInterface.getName());
 126  11
         builder.add(") _serviceModel.");
 127  11
         builder.add(serviceModelMethodName);
 128  11
         builder.add("();");
 129   
 
 130  11
         builder.end();
 131   
 
 132  11
         classFab.addMethod(
 133   
             Modifier.PRIVATE | Modifier.FINAL,
 134   
             new MethodSignature(serviceInterface, SERVICE_ACCESSOR_METHOD_NAME, null, null),
 135   
             builder.toString());
 136   
 
 137  11
         classFab.addInterface(RegistryShutdownListener.class);
 138   
 
 139  11
         classFab.addMethod(
 140   
             Modifier.PUBLIC | Modifier.FINAL,
 141   
             new MethodSignature(void.class, "registryDidShutdown", null, null),
 142   
             "{ _serviceModel = null; _shutdown = true; }");
 143   
     }
 144   
 }
 145