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: 197   Methods: 6
NCLOC: 104   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 - 95.6% 83.3% 94.1%
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;
 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.internal.ServiceModel;
 23   
 import org.apache.hivemind.internal.ServicePoint;
 24   
 import org.apache.hivemind.service.BodyBuilder;
 25   
 import org.apache.hivemind.service.ClassFab;
 26   
 import org.apache.hivemind.service.ClassFabUtils;
 27   
 import org.apache.hivemind.service.MethodSignature;
 28   
 import org.apache.hivemind.util.ConstructorUtils;
 29   
 
 30   
 /**
 31   
  * Contains some common code used to create proxies that defer to a service model method for thier
 32   
  * service.
 33   
  * 
 34   
  * @author Howard Lewis Ship
 35   
  */
 36   
 public final class ProxyUtils
 37   
 {
 38   
     public static final String SERVICE_ACCESSOR_METHOD_NAME = "_service";
 39   
 
 40   
     public static final String DELEGATE_ACCESSOR_METHOD_NAME = "_delegate";
 41   
 
 42  0
     private ProxyUtils()
 43   
     {
 44   
         // Prevent instantiation
 45   
     }
 46   
 
 47   
     /**
 48   
      * Creates a class that implements the service interface. Implements a private synchronized
 49   
      * method, _service(), that constructs the service as needed, and has each service interface
 50   
      * method re-invoke on _service(). Adds a toString() method if the service interface does not
 51   
      * define toString().
 52   
      */
 53  25
     public static Object createDelegatingProxy(String type, ServiceModel serviceModel,
 54   
             String delegationMethodName, ServicePoint servicePoint)
 55   
     {
 56  25
         ProxyBuilder builder = new ProxyBuilder(type, servicePoint);
 57   
 
 58  25
         ClassFab classFab = builder.getClassFab();
 59   
 
 60  25
         addConstructor(classFab, serviceModel);
 61   
 
 62  25
         addServiceAccessor(classFab, delegationMethodName, servicePoint);
 63   
 
 64  25
         builder.addServiceMethods(SERVICE_ACCESSOR_METHOD_NAME + "()");
 65   
 
 66  25
         Class proxyClass = classFab.createClass();
 67   
 
 68  25
         try
 69   
         {
 70  25
             Constructor c = proxyClass.getConstructor(new Class[]
 71   
             { serviceModel.getClass() });
 72   
 
 73  25
             return c.newInstance(new Object[]
 74   
             { serviceModel });
 75   
         }
 76   
         catch (Exception ex)
 77   
         {
 78  0
             throw new ApplicationRuntimeException(ex);
 79   
         }
 80   
     }
 81   
 
 82   
     /**
 83   
      * Constructs an outer proxy (for the threaded or pooled service). The outer proxy listens to
 84   
      * the shutdown coordinator, and delegates from the declared interface (which may in fact be a
 85   
      * bean) to the service interface.
 86   
      * <p>
 87   
      * The outer proxy is a {@link RegistryShutdownListener}; it can be registered for
 88   
      * notifications and will respond by throwing an exception when service methods are invoked.
 89   
      * 
 90   
      * @param delegate
 91   
      *            An object, implementing the service interface, that the proxy should delegate to.
 92   
      * @param servicePoint
 93   
      *            for which the proxy is being constructed
 94   
      * @since 1.1
 95   
      */
 96   
 
 97  25
     public static RegistryShutdownListener createOuterProxy(Object delegate,
 98   
             ServicePoint servicePoint)
 99   
     {
 100  25
         ProxyBuilder builder = new ProxyBuilder("OuterProxy", servicePoint, true);
 101   
 
 102  25
         ClassFab classFab = builder.getClassFab();
 103   
 
 104  25
         addDelegateAccessor(classFab, servicePoint, delegate);
 105   
 
 106  25
         builder.addServiceMethods(DELEGATE_ACCESSOR_METHOD_NAME + "()");
 107   
 
 108  25
         Class proxyClass = classFab.createClass();
 109   
 
 110  25
         try
 111   
         {
 112  25
             return (RegistryShutdownListener) ConstructorUtils.invokeConstructor(
 113   
                     proxyClass,
 114   
                     new Object[]
 115   
                     { delegate });
 116   
         }
 117   
         catch (Exception ex)
 118   
         {
 119  0
             throw new ApplicationRuntimeException(ex);
 120   
         }
 121   
     }
 122   
 
 123   
     /** @since 1.1 */
 124   
 
 125  25
     private static void addDelegateAccessor(ClassFab classFab, ServicePoint servicePoint,
 126   
             Object delegate)
 127   
     {
 128  25
         classFab.addField("_shutdown", boolean.class);
 129   
 
 130  25
         Class delegateClass = ClassFabUtils.getInstanceClass(delegate, servicePoint
 131   
                 .getServiceInterface());
 132   
 
 133  25
         classFab.addField("_delegate", delegateClass);
 134   
 
 135  25
         classFab.addConstructor(new Class[]
 136   
         { delegateClass }, null, "{ super(); _delegate = $1; }");
 137   
 
 138  25
         classFab.addInterface(RegistryShutdownListener.class);
 139   
 
 140  25
         classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class,
 141   
                 "registryDidShutdown", null, null), "{ _delegate = null; _shutdown = true; }");
 142   
 
 143  25
         BodyBuilder builder = new BodyBuilder();
 144   
 
 145  25
         builder.begin();
 146   
 
 147  25
         builder.addln("if (_shutdown)");
 148  25
         builder.addln("  throw org.apache.hivemind.HiveMind#createRegistryShutdownException();");
 149   
 
 150  25
         builder.add("return _delegate;");
 151   
 
 152  25
         builder.end();
 153   
 
 154  25
         classFab.addMethod(Modifier.FINAL | Modifier.PRIVATE, new MethodSignature(delegateClass,
 155   
                 DELEGATE_ACCESSOR_METHOD_NAME, null, null), builder.toString());
 156   
     }
 157   
 
 158   
     /**
 159   
      * Adds a field, _serviceExtensionPoint, whose type matches this class, and a constructor which
 160   
      * sets the field.
 161   
      */
 162  25
     private static void addConstructor(ClassFab classFab, ServiceModel model)
 163   
     {
 164  25
         Class modelClass = model.getClass();
 165   
 
 166  25
         classFab.addField("_serviceModel", modelClass);
 167   
 
 168  25
         classFab.addConstructor(new Class[]
 169   
         { modelClass }, null, "{ super(); _serviceModel = $1; }");
 170   
     }
 171   
 
 172   
     /**
 173   
      * We construct a method that always goes through this service model's
 174   
      * {@link #getServiceImplementationForCurrentThread())}method.
 175   
      */
 176  25
     private static void addServiceAccessor(ClassFab classFab, String serviceModelMethodName,
 177   
             ServicePoint servicePoint)
 178   
     {
 179  25
         Class serviceInterface = servicePoint.getServiceInterface();
 180   
 
 181  25
         classFab.addField(SERVICE_ACCESSOR_METHOD_NAME, serviceInterface);
 182   
 
 183  25
         BodyBuilder builder = new BodyBuilder();
 184  25
         builder.begin();
 185   
 
 186  25
         builder.add("return (");
 187  25
         builder.add(serviceInterface.getName());
 188  25
         builder.add(") _serviceModel.");
 189  25
         builder.add(serviceModelMethodName);
 190  25
         builder.add("();");
 191   
 
 192  25
         builder.end();
 193   
 
 194  25
         classFab.addMethod(Modifier.PRIVATE | Modifier.FINAL, new MethodSignature(serviceInterface,
 195   
                 SERVICE_ACCESSOR_METHOD_NAME, null, null), builder.toString());
 196   
     }
 197   
 }