Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 221   Methods: 6
NCLOC: 145   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
BuilderFactory.java 100% 98.1% 100% 98.6%
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.service.impl;
 16   
 
 17   
 import java.lang.reflect.Method;
 18   
 import java.util.Iterator;
 19   
 import java.util.List;
 20   
 
 21   
 import org.apache.commons.logging.Log;
 22   
 import org.apache.commons.logging.LogFactory;
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.hivemind.ClassResolver;
 25   
 import org.apache.hivemind.ErrorHandler;
 26   
 import org.apache.hivemind.HiveMind;
 27   
 import org.apache.hivemind.Location;
 28   
 import org.apache.hivemind.ServiceImplementationFactory;
 29   
 import org.apache.hivemind.internal.Module;
 30   
 import org.apache.hivemind.service.EventLinker;
 31   
 import org.apache.hivemind.util.ConstructorUtils;
 32   
 import org.apache.hivemind.util.PropertyUtils;
 33   
 
 34   
 /**
 35   
  * Implementation of {@link org.apache.hivemind.ServiceImplementationFactory}
 36   
  * that can instantiate an object and then configure its properties.
 37   
  * 
 38   
  * <p>
 39   
  * Some thought has been given to using bytecode generation to create properties
 40   
  * for messages, extension point id, and so forth.  This is being avoided because it
 41   
  * undermines the ability to test service implemenations as POJOs, outside the
 42   
  * framework of HiveMind.
 43   
  * 
 44   
  * <p>
 45   
  * Instead the service is configured by means of the implementation's
 46   
  * constructor and setter methods.
 47   
  *
 48   
  * @author Howard Lewis Ship
 49   
  */
 50   
 public class BuilderFactory implements ServiceImplementationFactory
 51   
 {
 52   
     private static final String POINT_ID = "hivemind.BuilderFactory";
 53   
 
 54  215
     public Object createCoreServiceImplementation(
 55   
         String serviceId,
 56   
         Class serviceInterface,
 57   
         Module invokingModule,
 58   
         List parameters)
 59   
     {
 60  215
         HiveMind.checkFactoryParameterCount(POINT_ID, parameters, 1);
 61   
 
 62  215
         Log log = LogFactory.getLog(serviceId);
 63  215
         ErrorHandler errorHandler = invokingModule.getErrorHandler();
 64   
 
 65  215
         BuilderParameter parameter = (BuilderParameter) parameters.get(0);
 66   
 
 67  215
         Object result = instantiateCoreServiceInstance(serviceId, invokingModule, parameter);
 68   
 
 69  215
         setProperties(serviceId, invokingModule, parameter, result, log);
 70   
 
 71  214
         registerForEvents(log, errorHandler, parameter, result);
 72   
 
 73  214
         invokeInitializer(parameter, result, serviceId, log, invokingModule);
 74   
 
 75  214
         return result;
 76   
     }
 77   
 
 78  214
     private void registerForEvents(
 79   
         Log log,
 80   
         ErrorHandler eventHandler,
 81   
         BuilderParameter parameter,
 82   
         Object result)
 83   
     {
 84  214
         List eventRegistrations = parameter.getEventRegistrations();
 85   
 
 86  214
         if (eventRegistrations.isEmpty())
 87  211
             return;
 88   
 
 89  3
         EventLinker linker = new EventLinkerImpl(log, eventHandler);
 90  3
         Iterator i = eventRegistrations.iterator();
 91  3
         while (i.hasNext())
 92   
         {
 93  3
             EventRegistration er = (EventRegistration) i.next();
 94   
 
 95  3
             linker.addEventListener(
 96   
                 er.getProducer(),
 97   
                 er.getEventSetName(),
 98   
                 result,
 99   
                 er.getLocation());
 100   
         }
 101   
     }
 102   
 
 103  215
     private void setProperties(
 104   
         String serviceId,
 105   
         Module invokingModule,
 106   
         BuilderParameter parameter,
 107   
         Object target,
 108   
         Log log)
 109   
     {
 110  215
         List properties = parameter.getProperties();
 111  215
         int count = properties.size();
 112   
 
 113  215
         for (int i = 0; i < count; i++)
 114   
         {
 115  1259
             BuilderFacet facet = (BuilderFacet) properties.get(i);
 116  1259
             String propertyName = facet.getPropertyName();
 117   
 
 118   
             // Facets that can autowire, should.
 119   
             
 120  1259
             facet.autowire(target, serviceId, invokingModule);
 121   
 
 122   
             // There will be a facet for log, messages, service-id, etc. even if no
 123   
             // property name is specified, so we skip it here.  In many cases, those
 124   
             // facets will have just done an autowire.
 125   
 
 126  1259
             if (propertyName == null)
 127  1056
                 continue;
 128   
 
 129  203
             Class targetType = PropertyUtils.getPropertyType(target, propertyName);
 130   
 
 131  202
             Object value = facet.getFacetValue(serviceId, invokingModule, targetType);
 132   
 
 133  202
             Location location = HiveMind.getLocation(facet);
 134   
 
 135  202
             HiveMind.setLocation(value, location);
 136   
 
 137  202
             try
 138   
             {
 139  202
                 PropertyUtils.write(target, propertyName, value);
 140   
             }
 141   
             catch (Exception ex)
 142   
             {
 143  0
                 throw new ApplicationRuntimeException(ex.getMessage(), target, location, ex);
 144   
             }
 145   
         }
 146   
     }
 147   
 
 148  215
     private Object instantiateCoreServiceInstance(
 149   
         String serviceId,
 150   
         Module invokingModule,
 151   
         BuilderParameter parameter)
 152   
     {
 153   
 
 154  215
         ClassResolver resolver = invokingModule.getClassResolver();
 155  215
         Class serviceClass = resolver.findClass(parameter.getClassName());
 156   
 
 157  215
         Object[] constructorParameters =
 158   
             buildConstructorParameters(parameter, invokingModule, serviceId);
 159   
 
 160  215
         return ConstructorUtils.invokeConstructor(serviceClass, constructorParameters, parameter);
 161   
     }
 162   
 
 163  215
     private Object[] buildConstructorParameters(
 164   
         BuilderParameter parameter,
 165   
         Module invokingModule,
 166   
         String serviceId)
 167   
     {
 168  215
         List parameters = parameter.getParameters();
 169  215
         int count = parameters.size();
 170   
 
 171  215
         if (count == 0)
 172  199
             return null;
 173   
 
 174  16
         Object[] result = new Object[count];
 175   
 
 176  16
         for (int i = 0; i < count; i++)
 177   
         {
 178  18
             BuilderFacet facet = (BuilderFacet) parameters.get(i);
 179   
 
 180  18
             result[i] = facet.getFacetValue(serviceId, invokingModule, Object.class);
 181   
 
 182  18
             HiveMind.setLocation(result[i], HiveMind.getLocation(facet));
 183   
         }
 184   
 
 185  16
         return result;
 186   
     }
 187   
 
 188  214
     private void invokeInitializer(
 189   
         BuilderParameter parameters,
 190   
         Object service,
 191   
         String serviceId,
 192   
         Log log,
 193   
         Module module)
 194   
     {
 195  214
         String methodName = parameters.getInitializeMethod();
 196   
 
 197  214
         if (HiveMind.isBlank(methodName))
 198  206
             return;
 199   
 
 200  8
         try
 201   
         {
 202  8
             Class serviceClass = service.getClass();
 203  8
             Method m = serviceClass.getMethod(methodName, null);
 204   
 
 205  7
             m.invoke(service, null);
 206   
         }
 207   
         catch (Exception ex)
 208   
         {
 209  1
             module.getErrorHandler().error(
 210   
                 log,
 211   
                 ServiceMessages.unableToInitializeService(
 212   
                     serviceId,
 213   
                     methodName,
 214   
                     service.getClass(),
 215   
                     ex),
 216   
                 parameters.getLocation(),
 217   
                 ex);
 218   
         }
 219   
     }
 220   
 }
 221