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