Clover coverage report - Code Coverage for hivemind release 1.0-beta-2
Coverage timestamp: Sun Aug 1 2004 14:03:45 EDT
file stats: LOC: 280   Methods: 20
NCLOC: 189   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
ServicePointImpl.java 81.8% 92.2% 100% 91.5%
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.util.ArrayList;
 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.HiveMind;
 26   
 import org.apache.hivemind.ShutdownCoordinator;
 27   
 import org.apache.hivemind.internal.ServiceImplementationConstructor;
 28   
 import org.apache.hivemind.internal.ServiceInterceptorContribution;
 29   
 import org.apache.hivemind.internal.ServiceModel;
 30   
 import org.apache.hivemind.internal.ServiceModelFactory;
 31   
 import org.apache.hivemind.order.Orderer;
 32   
 import org.apache.hivemind.schema.Schema;
 33   
 import org.apache.hivemind.util.ToStringBuilder;
 34   
 
 35   
 /**
 36   
  * Abstract implementation of {@link org.apache.hivemind.internal.ServicePoint}.
 37   
  * Provides some of the machinery for creating new service instances, delegating
 38   
  * most of it to the {@link org.apache.hivemind.internal.ServiceModel} instace
 39   
  * for the service.
 40   
  *
 41   
  * @author Howard Lewis Ship
 42   
  */
 43   
 public final class ServicePointImpl
 44   
     extends AbstractExtensionPoint
 45   
     implements ConstructableServicePoint
 46   
 {
 47   
     private Object _service;
 48   
     private boolean _building;
 49   
     private String _serviceInterfaceName;
 50   
     private Class _serviceInterface;
 51   
     private ServiceImplementationConstructor _serviceConstructor;
 52   
     private List _interceptorContributions;
 53   
     private boolean _interceptorsOrdered;
 54   
     private Schema _parametersSchema;
 55   
     private String _serviceModel;
 56   
     private ShutdownCoordinator _shutdownCoordinator;
 57   
     private ServiceModel _serviceModelObject;
 58   
 
 59  1
     protected void extendDescription(ToStringBuilder builder)
 60   
     {
 61  1
         if (_service != null)
 62  0
             builder.append("service", _service);
 63   
 
 64  1
         builder.append("serviceInterfaceName", _serviceInterfaceName);
 65  1
         builder.append("factoryContribution", _serviceConstructor);
 66  1
         builder.append("interceptorContributions", _interceptorContributions);
 67  1
         builder.append("parametersSchema", _parametersSchema);
 68  1
         builder.append("serviceModel", _serviceModel);
 69   
 
 70  1
         if (_building)
 71  0
             builder.append("building", _building);
 72   
     }
 73   
 
 74  15
     public void addInterceptorContribution(ServiceInterceptorContribution contribution)
 75   
     {
 76  15
         if (_interceptorContributions == null)
 77  13
             _interceptorContributions = new ArrayList();
 78   
 
 79  15
         _interceptorContributions.add(contribution);
 80   
     }
 81   
 
 82  2970
     public synchronized Class getServiceInterface()
 83   
     {
 84  2970
         if (_serviceInterface == null)
 85  1317
             _serviceInterface = lookupServiceInterface();
 86   
 
 87  2970
         return _serviceInterface;
 88   
     }
 89   
 
 90  1317
     private Class lookupServiceInterface()
 91   
     {
 92  1317
         ClassResolver resolver = getModule().getClassResolver();
 93  1317
         Class result = null;
 94   
 
 95  1317
         try
 96   
         {
 97  1317
             result = resolver.findClass(_serviceInterfaceName);
 98   
         }
 99   
         catch (Exception ex)
 100   
         {
 101  0
             throw new ApplicationRuntimeException(
 102   
                 ImplMessages.badInterface(_serviceInterfaceName, getExtensionPointId()),
 103   
                 getLocation(),
 104   
                 ex);
 105   
         }
 106   
 
 107  1317
         if (!result.isInterface())
 108  0
             throw new ApplicationRuntimeException(
 109   
                 ImplMessages.interfaceRequired(_serviceInterfaceName, getExtensionPointId()),
 110   
                 getLocation(),
 111   
                 null);
 112   
 
 113  1317
         return result;
 114   
     }
 115   
 
 116  1317
     public void setServiceConstructor(ServiceImplementationConstructor contribution)
 117   
     {
 118  1317
         _serviceConstructor = contribution;
 119   
     }
 120   
 
 121  1317
     public void setServiceInterfaceName(String string)
 122   
     {
 123  1317
         _serviceInterfaceName = string;
 124   
     }
 125   
 
 126  1317
     public void setParametersSchema(Schema schema)
 127   
     {
 128  1317
         _parametersSchema = schema;
 129   
     }
 130   
 
 131  240
     public Schema getParametersSchema()
 132   
     {
 133  240
         return _parametersSchema;
 134   
     }
 135   
 
 136  3055
     public ServiceImplementationConstructor getServiceConstructor()
 137   
     {
 138  3055
         return _serviceConstructor;
 139   
     }
 140   
 
 141   
     /**
 142   
      * Invoked by {@link #getService(Class)} to get a service implementation 
 143   
      * from the {@link ServiceModel}.
 144   
      * 
 145   
      * <p>
 146   
      * TODO: I'm concerned that this synchronized method could cause a deadlock. It would take 
 147   
      * a LOT (mutually dependent services in multiple threads being realized at the same time).
 148   
      * 
 149   
      * 
 150   
      */
 151  1105
     private synchronized Object getService()
 152   
     {
 153  1105
         if (_service == null)
 154   
         {
 155   
 
 156  513
             if (_building)
 157  1
                 throw new ApplicationRuntimeException(ImplMessages.recursiveServiceBuild(this));
 158   
 
 159  512
             _building = true;
 160   
 
 161  512
             try
 162   
             {
 163   
 
 164  512
                 ServiceModelFactory factory = getModule().getServiceModelFactory(getServiceModel());
 165   
 
 166  512
                 _serviceModelObject = factory.createServiceModelForService(this);
 167   
 
 168  512
                 _service = _serviceModelObject.getService();
 169   
             }
 170   
             finally
 171   
             {
 172  512
                 _building = false;
 173   
             }
 174   
         }
 175   
 
 176  1101
         return _service;
 177   
     }
 178   
 
 179  1101
     public Object getService(Class serviceInterface)
 180   
     {
 181  1101
         Object result = getService();
 182   
 
 183  1097
         if (!serviceInterface.isAssignableFrom(result.getClass()))
 184   
         {
 185  0
             throw new ApplicationRuntimeException(
 186   
                 ImplMessages.serviceWrongInterface(this, serviceInterface),
 187   
                 getLocation(),
 188   
                 null);
 189   
         }
 190   
 
 191  1097
         return result;
 192   
     }
 193   
 
 194  512
     public String getServiceModel()
 195   
     {
 196  512
         return _serviceModel;
 197   
     }
 198   
 
 199  1317
     public void setServiceModel(String model)
 200   
     {
 201  1317
         _serviceModel = model;
 202   
     }
 203   
 
 204  408
     public void clearConstructorInformation()
 205   
     {
 206  408
         _serviceConstructor = null;
 207  408
         _interceptorContributions = null;
 208   
     }
 209   
 
 210   
     // Hm. Does this need to be synchronized?
 211   
 
 212  422
     public List getOrderedInterceptorContributions()
 213   
     {
 214  422
         if (!_interceptorsOrdered)
 215   
         {
 216  420
             _interceptorContributions = orderInterceptors();
 217  420
             _interceptorsOrdered = true;
 218   
         }
 219   
 
 220  422
         return _interceptorContributions;
 221   
     }
 222   
 
 223  420
     private List orderInterceptors()
 224   
     {
 225  420
         if (HiveMind.isEmpty(_interceptorContributions))
 226  407
             return null;
 227   
 
 228   
         // Any error logging should go to the extension point
 229   
         // we're constructing.
 230   
 
 231  13
         Log log = LogFactory.getLog(getExtensionPointId());
 232   
 
 233  13
         Orderer orderer =
 234   
             new Orderer(log, getModule().getErrorHandler(), ImplMessages.interceptorContribution());
 235   
 
 236  13
         Iterator i = _interceptorContributions.iterator();
 237  13
         while (i.hasNext())
 238   
         {
 239  15
             ServiceInterceptorContribution sic = (ServiceInterceptorContribution) i.next();
 240   
 
 241   
             // Sort them into runtime excecution order. When we build 
 242   
             // the interceptor stack we'll apply them in reverse order,
 243   
             // building outward from the core service implementation.
 244   
 
 245  15
             orderer.add(
 246   
                 sic,
 247   
                 sic.getFactoryServiceId(),
 248   
                 sic.getPrecedingInterceptorIds(),
 249   
                 sic.getFollowingInterceptorIds());
 250   
         }
 251   
 
 252  13
         return orderer.getOrderedObjects();
 253   
     }
 254   
 
 255  251
     public ShutdownCoordinator getShutdownCoordinator()
 256   
     {
 257  251
         return _shutdownCoordinator;
 258   
     }
 259   
 
 260  1317
     public void setShutdownCoordinator(ShutdownCoordinator coordinator)
 261   
     {
 262  1317
         _shutdownCoordinator = coordinator;
 263   
     }
 264   
 
 265   
     /**
 266   
      * Forces the service into existence.
 267   
      */
 268  4
     public void forceServiceInstantiation()
 269   
     {
 270  4
         getService();
 271   
 
 272  4
         _serviceModelObject.instantiateService();
 273   
     }
 274   
 
 275  240
     public Log getServiceLog()
 276   
     {
 277  240
         return LogFactory.getLog(getExtensionPointId());
 278   
     }
 279   
 }
 280