Clover coverage report - Code Coverage for hivemind-lib release 1.1-alpha-1
Coverage timestamp: Tue Jan 18 2005 07:56:07 EST
file stats: LOC: 196   Methods: 7
NCLOC: 117   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
AdapterRegistryFactory.java 90% 97.6% 100% 96.6%
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.lib.adapter;
 16   
 
 17   
 import java.lang.reflect.Constructor;
 18   
 import java.lang.reflect.Modifier;
 19   
 import java.util.Iterator;
 20   
 import java.util.List;
 21   
 
 22   
 import org.apache.hivemind.ApplicationRuntimeException;
 23   
 import org.apache.hivemind.HiveMind;
 24   
 import org.apache.hivemind.ServiceImplementationFactory;
 25   
 import org.apache.hivemind.ServiceImplementationFactoryParameters;
 26   
 import org.apache.hivemind.lib.util.AdapterRegistry;
 27   
 import org.apache.hivemind.lib.util.AdapterRegistryImpl;
 28   
 import org.apache.hivemind.service.ClassFab;
 29   
 import org.apache.hivemind.service.ClassFabUtils;
 30   
 import org.apache.hivemind.service.ClassFactory;
 31   
 import org.apache.hivemind.service.MethodIterator;
 32   
 import org.apache.hivemind.service.MethodSignature;
 33   
 
 34   
 /**
 35   
  * Constructs a service where the first parameter of each method is used to selected an adapter from
 36   
  * an {@link org.apache.hivemind.lib.util.AdapterRegistry}. The method invocation is then delegated
 37   
  * to the adapter.
 38   
  * <p>
 39   
  * The service factory parameter defines a configuration (of
 40   
  * {@link org.apache.hivemind.lib.adapter.AdapterRegistryContribution}that provide the mapping from
 41   
  * Java classes (or interfaces) to adapter instances.
 42   
  * 
 43   
  * @author Howard M. Lewis Ship
 44   
  * @since 1.1
 45   
  */
 46   
 public class AdapterRegistryFactory implements ServiceImplementationFactory
 47   
 {
 48   
     private ClassFactory _classFactory;
 49   
 
 50  1
     public Object createCoreServiceImplementation(
 51   
             ServiceImplementationFactoryParameters factoryParameters)
 52   
     {
 53  1
         AdapterRegistry ar = new AdapterRegistryImpl();
 54   
 
 55  1
         buildAdapterRegistry(factoryParameters, ar);
 56   
 
 57  1
         Class implClass = buildImplementationClass(factoryParameters);
 58   
 
 59  1
         try
 60   
         {
 61  1
             Constructor c = implClass.getConstructors()[0];
 62   
 
 63  1
             return c.newInstance(new Object[]
 64   
             { ar });
 65   
         }
 66   
         catch (Exception ex)
 67   
         {
 68  0
             throw new ApplicationRuntimeException(ex.getMessage(), HiveMind
 69   
                     .getLocation(factoryParameters.getFirstParameter()), ex);
 70   
         }
 71   
 
 72   
     }
 73   
 
 74   
     // package private for testing purposes
 75   
 
 76  3
     void buildAdapterRegistry(ServiceImplementationFactoryParameters factoryParameters,
 77   
             AdapterRegistry ar)
 78   
     {
 79  3
         Class serviceInterface = factoryParameters.getServiceInterface();
 80   
 
 81  3
         AdapterRegistryParameter p = (AdapterRegistryParameter) factoryParameters
 82   
                 .getFirstParameter();
 83   
 
 84  3
         List contributions = p.getContributions();
 85   
 
 86  3
         Iterator i = contributions.iterator();
 87   
 
 88  3
         while (i.hasNext())
 89   
         {
 90  3
             AdapterRegistryContribution c = (AdapterRegistryContribution) i.next();
 91   
 
 92  3
             try
 93   
             {
 94  3
                 Object adapter = c.getAdapter();
 95   
 
 96  3
                 if (!serviceInterface.isAssignableFrom(adapter.getClass()))
 97  1
                     throw new ClassCastException(AdapterMessages.adapterWrongInterface(adapter, c
 98   
                             .getRegisterClass(), serviceInterface));
 99   
 
 100  2
                 ar.register(c.getRegisterClass(), adapter);
 101   
             }
 102   
             catch (Exception ex)
 103   
             {
 104  1
                 factoryParameters.getErrorLog().error(ex.getMessage(), c.getLocation(), ex);
 105   
             }
 106   
 
 107   
         }
 108   
 
 109   
     }
 110   
 
 111   
     // package private for testing purposes
 112   
 
 113  1
     private Class buildImplementationClass(ServiceImplementationFactoryParameters factoryParameters)
 114   
     {
 115  1
         String name = ClassFabUtils.generateClassName(factoryParameters.getServiceInterface());
 116   
 
 117  1
         return buildImplementationClass(factoryParameters, name);
 118   
     }
 119   
 
 120   
     // package private for testing purposes
 121   
 
 122  3
     Class buildImplementationClass(ServiceImplementationFactoryParameters factoryParameters,
 123   
             String name)
 124   
     {
 125  3
         Class serviceInterface = factoryParameters.getServiceInterface();
 126   
 
 127  3
         ClassFab cf = _classFactory.newClass(name, Object.class);
 128   
 
 129  3
         cf.addInterface(serviceInterface);
 130   
 
 131  3
         cf.addField("_adapterRegistry", AdapterRegistry.class);
 132   
 
 133  3
         cf.addConstructor(new Class[]
 134   
         { AdapterRegistry.class }, null, "_adapterRegistry = $1;");
 135   
 
 136   
         // TODO: Should we add a check for $1 == null?
 137   
 
 138  3
         cf.addMethod(Modifier.PRIVATE, new MethodSignature(serviceInterface, "_getAdapter",
 139   
                 new Class[]
 140   
                 { Object.class }, null), "return (" + serviceInterface.getName()
 141   
                 + ") _adapterRegistry.getAdapter($1.getClass());");
 142   
 
 143  3
         MethodIterator i = new MethodIterator(serviceInterface);
 144   
 
 145  3
         while (i.hasNext())
 146   
         {
 147  3
             MethodSignature sig = i.next();
 148   
 
 149  3
             if (proper(sig))
 150   
             {
 151  2
                 addAdaptedMethod(cf, sig);
 152   
             }
 153   
             else
 154   
             {
 155  1
                 ClassFabUtils.addNoOpMethod(cf, sig);
 156   
 
 157  1
                 factoryParameters.getErrorLog().error(
 158   
                         AdapterMessages.improperServiceMethod(sig),
 159   
                         HiveMind.getLocation(factoryParameters.getFirstParameter()),
 160   
                         null);
 161   
             }
 162   
 
 163   
         }
 164   
 
 165  3
         if (!i.getToString())
 166  3
             ClassFabUtils.addToStringMethod(cf, AdapterMessages.toString(factoryParameters
 167   
                     .getServiceId(), serviceInterface));
 168   
 
 169  3
         return cf.createClass();
 170   
     }
 171   
 
 172  2
     private void addAdaptedMethod(ClassFab cf, MethodSignature sig)
 173   
     {
 174  2
         String body = "return ($r) _getAdapter($1)." + sig.getName() + "($$);";
 175   
 
 176  2
         cf.addMethod(Modifier.PUBLIC, sig, body);
 177   
     }
 178   
 
 179   
     /**
 180   
      * A "proper" method is one with at least one parameter and whose first parameter is an object
 181   
      * (not primitive) type.
 182   
      */
 183   
 
 184  3
     private boolean proper(MethodSignature sig)
 185   
     {
 186  3
         Class[] parameterTypes = sig.getParameterTypes();
 187   
 
 188  3
         return parameterTypes != null && parameterTypes.length > 0
 189   
                 && !parameterTypes[0].isPrimitive();
 190   
     }
 191   
 
 192  3
     public void setClassFactory(ClassFactory classFactory)
 193   
     {
 194  3
         _classFactory = classFactory;
 195   
     }
 196   
 }