Clover coverage report - Code Coverage for hivemind release 1.1-alpha-1
Coverage timestamp: Tue Jan 18 2005 07:55:08 EST
file stats: LOC: 733   Methods: 25
NCLOC: 476   Classes: 2
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
RegistryInfrastructureConstructor.java 92.5% 96.3% 100% 95.5%
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.util.Collection;
 18   
 import java.util.HashMap;
 19   
 import java.util.Iterator;
 20   
 import java.util.List;
 21   
 import java.util.Locale;
 22   
 import java.util.Map;
 23   
 
 24   
 import org.apache.commons.logging.Log;
 25   
 import org.apache.hivemind.ErrorHandler;
 26   
 import org.apache.hivemind.Location;
 27   
 import org.apache.hivemind.Occurances;
 28   
 import org.apache.hivemind.ShutdownCoordinator;
 29   
 import org.apache.hivemind.conditional.EvaluationContextImpl;
 30   
 import org.apache.hivemind.conditional.Node;
 31   
 import org.apache.hivemind.conditional.Parser;
 32   
 import org.apache.hivemind.internal.ConfigurationPoint;
 33   
 import org.apache.hivemind.internal.Module;
 34   
 import org.apache.hivemind.internal.RegistryInfrastructure;
 35   
 import org.apache.hivemind.internal.ServicePoint;
 36   
 import org.apache.hivemind.parse.ConfigurationPointDescriptor;
 37   
 import org.apache.hivemind.parse.ContributionDescriptor;
 38   
 import org.apache.hivemind.parse.DependencyDescriptor;
 39   
 import org.apache.hivemind.parse.ImplementationDescriptor;
 40   
 import org.apache.hivemind.parse.InstanceBuilder;
 41   
 import org.apache.hivemind.parse.InterceptorDescriptor;
 42   
 import org.apache.hivemind.parse.ModuleDescriptor;
 43   
 import org.apache.hivemind.parse.ServicePointDescriptor;
 44   
 import org.apache.hivemind.schema.Schema;
 45   
 import org.apache.hivemind.util.IdUtils;
 46   
 
 47   
 /**
 48   
  * Fed a series of {@link org.apache.hivemind.parse.ModuleDescriptor}s, this class will assemble
 49   
  * them into a final {@link org.apache.hivemind.internal.RegistryInfrastructure}as well as perform
 50   
  * some validations.
 51   
  * <p>
 52   
  * This class was extracted from {@link org.apache.hivemind.impl.RegistryBuilder}.
 53   
  * 
 54   
  * @author Howard M. Lewis Ship
 55   
  * @since 1.1
 56   
  */
 57   
 public class RegistryInfrastructureConstructor
 58   
 {
 59   
     private ErrorHandler _errorHandler;
 60   
 
 61   
     private Log _log;
 62   
 
 63   
     private RegistryAssembly _assembly;
 64   
 
 65   
     /** @since 1.1 */
 66   
 
 67   
     private Parser _conditionalExpressionParser;
 68   
 
 69  109
     public RegistryInfrastructureConstructor(ErrorHandler errorHandler, Log log,
 70   
             RegistryAssembly assembly)
 71   
     {
 72  109
         _errorHandler = errorHandler;
 73  109
         _log = log;
 74  109
         _assembly = assembly;
 75   
     }
 76   
 
 77   
     /**
 78   
      * Map of {@link ModuleDescriptor}keyed on module id.
 79   
      */
 80   
 
 81   
     private Map _moduleDescriptors = new HashMap();
 82   
 
 83   
     /**
 84   
      * Map of {@link ModuleImpl}keyed on module id.
 85   
      */
 86   
     private Map _modules = new HashMap();
 87   
 
 88   
     /**
 89   
      * Map of {@link Schema}keyed on fully qualified module id.
 90   
      */
 91   
     private Map _schemas = new HashMap();
 92   
 
 93   
     /**
 94   
      * Map of {@link ServicePointImpl}keyed on fully qualified id.
 95   
      */
 96   
 
 97   
     private Map _servicePoints = new HashMap();
 98   
 
 99   
     /**
 100   
      * Map of {@link ConfigurationPointImpl}keyed on fully qualified id.
 101   
      */
 102   
 
 103   
     private Map _configurationPoints = new HashMap();
 104   
 
 105   
     /**
 106   
      * Shutdown coordinator shared by all objects.
 107   
      */
 108   
 
 109   
     private ShutdownCoordinator _shutdownCoordinator = new ShutdownCoordinatorImpl();
 110   
 
 111   
     /**
 112   
      * This class is used to check the dependencies of a ModuleDescriptor. As the checker is run it
 113   
      * will log errors to the ErrorHandler if dependencies don't resolve or the versions dont match.
 114   
      */
 115   
     private class ModuleDependencyChecker implements Runnable
 116   
     {
 117   
         private ModuleDescriptor _source;
 118   
 
 119  3
         public ModuleDependencyChecker(ModuleDescriptor source)
 120   
         {
 121  3
             _source = source;
 122   
         }
 123   
 
 124  3
         public void run()
 125   
         {
 126  3
             List dependencies = _source.getDependencies();
 127  3
             int count = size(dependencies);
 128   
 
 129  3
             for (int i = 0; i < count; i++)
 130   
             {
 131  3
                 DependencyDescriptor dependency = (DependencyDescriptor) dependencies.get(i);
 132  3
                 checkDependency(dependency);
 133   
             }
 134   
         }
 135   
 
 136  3
         private void checkDependency(DependencyDescriptor dependency)
 137   
         {
 138  3
             ModuleDescriptor requiredModule = (ModuleDescriptor) _moduleDescriptors.get(dependency
 139   
                     .getModuleId());
 140   
 
 141  3
             if (requiredModule == null)
 142   
             {
 143  1
                 _errorHandler.error(
 144   
                         _log,
 145   
                         ImplMessages.dependencyOnUnknownModule(dependency),
 146   
                         dependency.getLocation(),
 147   
                         null);
 148  1
                 return;
 149   
             }
 150   
 
 151  2
             if (dependency.getVersion() != null
 152   
                     && !dependency.getVersion().equals(requiredModule.getVersion()))
 153   
             {
 154  1
                 _errorHandler.error(
 155   
                         _log,
 156   
                         ImplMessages.dependencyVersionMismatch(dependency),
 157   
                         dependency.getLocation(),
 158   
                         null);
 159  1
                 return;
 160   
             }
 161   
         }
 162   
     }
 163   
 
 164   
     /**
 165   
      * Constructs the registry infrastructure, based on data collected during the prior calls to
 166   
      * {@link #addModuleDescriptor(ModuleDescriptor)}. Expects that all post-processing of the
 167   
      * {@link RegistryAssembly}has already occured.
 168   
      */
 169  109
     public RegistryInfrastructure constructRegistryInfrastructure(Locale locale)
 170   
     {
 171  109
         RegistryInfrastructureImpl result = new RegistryInfrastructureImpl(_errorHandler, locale);
 172   
 
 173  109
         addServiceAndConfigurationPoints(result);
 174   
 
 175  109
         addImplementationsAndContributions();
 176   
 
 177  109
         checkForMissingServices();
 178   
 
 179  109
         checkContributionCounts();
 180   
 
 181  109
         result.setShutdownCoordinator(_shutdownCoordinator);
 182   
 
 183  109
         addModulesToRegistry(result);
 184   
 
 185   
         // The caller is responsible for invoking startup().
 186   
 
 187  109
         return result;
 188   
     }
 189   
 
 190  220
     public void addModuleDescriptor(ModuleDescriptor md)
 191   
     {
 192  220
         String id = md.getModuleId();
 193   
 
 194  220
         if (_log.isDebugEnabled())
 195  28
             _log.debug("Processing module " + id);
 196   
 
 197  220
         if (_modules.containsKey(id))
 198   
         {
 199  1
             Module existing = (Module) _modules.get(id);
 200   
 
 201  1
             _errorHandler.error(_log, ImplMessages.duplicateModuleId(id, existing.getLocation(), md
 202   
                     .getLocation()), null, null);
 203   
 
 204   
             // Ignore the duplicate module descriptor.
 205  1
             return;
 206   
         }
 207   
 
 208  219
         ModuleImpl module = new ModuleImpl();
 209   
 
 210  219
         module.setLocation(md.getLocation());
 211  219
         module.setModuleId(id);
 212  219
         module.setClassResolver(md.getClassResolver());
 213   
 
 214  219
         if (size(md.getDependencies()) > 0)
 215  3
             _assembly.addPostProcessor(new ModuleDependencyChecker(md));
 216   
 
 217  219
         if (md.getSchemas() != null)
 218  144
             for (Iterator schemas = md.getSchemas().iterator(); schemas.hasNext();)
 219   
             {
 220  255
                 Schema schema = (Schema) schemas.next();
 221   
 
 222  255
                 _schemas.put(IdUtils.qualify(id, schema.getId()), schema);
 223   
             }
 224   
 
 225  219
         _modules.put(id, module);
 226   
 
 227  219
         _moduleDescriptors.put(id, md);
 228   
     }
 229   
 
 230  109
     private void addServiceAndConfigurationPoints(RegistryInfrastructureImpl infrastructure)
 231   
     {
 232  109
         for (Iterator i = _moduleDescriptors.values().iterator(); i.hasNext();)
 233   
         {
 234  219
             ModuleDescriptor md = (ModuleDescriptor) i.next();
 235   
 
 236  219
             String id = md.getModuleId();
 237   
 
 238  219
             ModuleImpl module = (ModuleImpl) _modules.get(id);
 239   
 
 240  219
             addServicePoints(infrastructure, module, md);
 241   
 
 242  219
             addConfigurationPoints(infrastructure, module, md);
 243   
         }
 244   
     }
 245   
 
 246  219
     private void addServicePoints(RegistryInfrastructureImpl infrastructure, Module module,
 247   
             ModuleDescriptor md)
 248   
     {
 249  219
         String moduleId = md.getModuleId();
 250  219
         List services = md.getServicePoints();
 251  219
         int count = size(services);
 252   
 
 253  219
         for (int i = 0; i < count; i++)
 254   
         {
 255  1645
             ServicePointDescriptor sd = (ServicePointDescriptor) services.get(i);
 256   
 
 257  1645
             String pointId = moduleId + "." + sd.getId();
 258   
 
 259  1645
             ServicePoint existingPoint = (ServicePoint) _servicePoints.get(pointId);
 260   
 
 261  1645
             if (existingPoint != null)
 262   
             {
 263  1
                 _errorHandler.error(_log, ImplMessages.duplicateExtensionPointId(
 264   
                         pointId,
 265   
                         existingPoint), sd.getLocation(), null);
 266  1
                 continue;
 267   
             }
 268   
 
 269  1644
             if (_log.isDebugEnabled())
 270  196
                 _log.debug("Creating service point " + pointId);
 271   
 
 272   
             // Choose which class to instantiate based on
 273   
             // whether the service is create-on-first-reference
 274   
             // or create-on-first-use (deferred).
 275   
 
 276  1644
             ServicePointImpl point = new ServicePointImpl();
 277   
 
 278  1644
             point.setExtensionPointId(pointId);
 279  1644
             point.setLocation(sd.getLocation());
 280  1644
             point.setModule(module);
 281   
 
 282  1644
             point.setServiceInterfaceName(sd.getInterfaceClassName());
 283  1644
             setParametersSchema(sd, point);
 284  1644
             point.setParametersCount(sd.getParametersCount());
 285  1644
             point.setVisibility(sd.getVisibility());
 286   
 
 287  1644
             point.setShutdownCoordinator(_shutdownCoordinator);
 288   
 
 289  1644
             infrastructure.addServicePoint(point);
 290   
 
 291   
             // Save this for the second phase, where contributions
 292   
             // from other modules are applied.
 293   
 
 294  1644
             _servicePoints.put(pointId, point);
 295   
 
 296  1644
             addInternalImplementations(module, pointId, sd);
 297   
         }
 298   
     }
 299   
 
 300  219
     private void addConfigurationPoints(RegistryInfrastructureImpl registry, Module module,
 301   
             ModuleDescriptor md)
 302   
     {
 303  219
         String moduleId = md.getModuleId();
 304  219
         List points = md.getConfigurationPoints();
 305  219
         int count = size(points);
 306   
 
 307  219
         for (int i = 0; i < count; i++)
 308   
         {
 309  877
             ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(i);
 310   
 
 311  877
             String pointId = moduleId + "." + cpd.getId();
 312   
 
 313  877
             ConfigurationPoint existingPoint = (ConfigurationPoint) _configurationPoints
 314   
                     .get(pointId);
 315   
 
 316  877
             if (existingPoint != null)
 317   
             {
 318  1
                 _errorHandler.error(_log, ImplMessages.duplicateExtensionPointId(
 319   
                         pointId,
 320   
                         existingPoint), cpd.getLocation(), null);
 321  1
                 continue;
 322   
             }
 323   
 
 324  876
             if (_log.isDebugEnabled())
 325  112
                 _log.debug("Creating configuration point " + pointId);
 326   
 
 327  876
             ConfigurationPointImpl point = new ConfigurationPointImpl();
 328   
 
 329  876
             point.setExtensionPointId(pointId);
 330  876
             point.setLocation(cpd.getLocation());
 331  876
             point.setModule(module);
 332  876
             point.setExpectedCount(cpd.getCount());
 333  876
             setContributionsSchema(cpd, point);
 334  876
             point.setVisibility(cpd.getVisibility());
 335   
 
 336  876
             point.setShutdownCoordinator(_shutdownCoordinator);
 337   
 
 338  876
             registry.addConfigurationPoint(point);
 339   
 
 340   
             // Needed later when we reconcile the rest
 341   
             // of the configuration contributions.
 342   
 
 343  876
             _configurationPoints.put(pointId, point);
 344   
         }
 345   
     }
 346   
 
 347  578
     private void addContributionElements(Module sourceModule, ConfigurationPointImpl point,
 348   
             List elements)
 349   
     {
 350  578
         if (size(elements) == 0)
 351  0
             return;
 352   
 
 353  578
         if (_log.isDebugEnabled())
 354  73
             _log
 355   
                     .debug("Adding contributions to configuration point "
 356   
                             + point.getExtensionPointId());
 357   
 
 358  578
         ContributionImpl c = new ContributionImpl();
 359  578
         c.setContributingModule(sourceModule);
 360  578
         c.addElements(elements);
 361   
 
 362  578
         point.addContribution(c);
 363   
     }
 364   
 
 365  109
     private void addModulesToRegistry(RegistryInfrastructureImpl registry)
 366   
     {
 367   
         // Add each module to the registry.
 368   
 
 369  109
         Iterator i = _modules.values().iterator();
 370  109
         while (i.hasNext())
 371   
         {
 372  219
             ModuleImpl module = (ModuleImpl) i.next();
 373   
 
 374  219
             if (_log.isDebugEnabled())
 375  27
                 _log.debug("Adding module " + module.getModuleId() + " to registry");
 376   
 
 377  219
             module.setRegistry(registry);
 378   
         }
 379   
     }
 380   
 
 381  109
     private void addImplementationsAndContributions()
 382   
     {
 383  109
         for (Iterator i = _moduleDescriptors.values().iterator(); i.hasNext();)
 384   
         {
 385  219
             ModuleDescriptor md = (ModuleDescriptor) i.next();
 386   
 
 387  219
             if (_log.isDebugEnabled())
 388  27
                 _log.debug("Adding contributions from module " + md.getModuleId());
 389   
 
 390  219
             addImplementations(md);
 391  219
             addContributions(md);
 392   
         }
 393   
     }
 394   
 
 395  219
     private void addImplementations(ModuleDescriptor md)
 396   
     {
 397  219
         String moduleId = md.getModuleId();
 398  219
         Module sourceModule = (Module) _modules.get(moduleId);
 399   
 
 400  219
         List implementations = md.getImplementations();
 401  219
         int count = size(implementations);
 402   
 
 403  219
         for (int i = 0; i < count; i++)
 404   
         {
 405  4
             ImplementationDescriptor impl = (ImplementationDescriptor) implementations.get(i);
 406   
 
 407  4
             if (!includeContribution(impl.getConditionalExpression(), sourceModule, impl
 408   
                     .getLocation()))
 409  0
                 continue;
 410   
 
 411  4
             String pointId = impl.getServiceId();
 412  4
             String qualifiedId = IdUtils.qualify(moduleId, pointId);
 413   
 
 414  4
             addImplementations(sourceModule, qualifiedId, impl);
 415   
         }
 416   
 
 417   
     }
 418   
 
 419  219
     private void addContributions(ModuleDescriptor md)
 420   
     {
 421  219
         String moduleId = md.getModuleId();
 422  219
         Module sourceModule = (Module) _modules.get(moduleId);
 423   
 
 424  219
         List contributions = md.getContributions();
 425  219
         int count = size(contributions);
 426   
 
 427  219
         for (int i = 0; i < count; i++)
 428   
         {
 429  582
             ContributionDescriptor cd = (ContributionDescriptor) contributions.get(i);
 430   
 
 431  582
             if (!includeContribution(cd.getConditionalExpression(), sourceModule, cd.getLocation()))
 432  2
                 continue;
 433   
 
 434  580
             String pointId = cd.getConfigurationId();
 435  580
             String qualifiedId = IdUtils.qualify(moduleId, pointId);
 436   
 
 437  580
             ConfigurationPointImpl point = (ConfigurationPointImpl) _configurationPoints
 438   
                     .get(qualifiedId);
 439   
 
 440  580
             if (point == null)
 441   
             {
 442  1
                 _errorHandler.error(_log, ImplMessages.unknownConfigurationPoint(moduleId, cd), cd
 443   
                         .getLocation(), null);
 444   
 
 445  1
                 continue;
 446   
             }
 447   
 
 448  579
             if (!point.visibleToModule(sourceModule))
 449   
             {
 450  1
                 _errorHandler.error(_log, ImplMessages.configurationPointNotVisible(
 451   
                         point,
 452   
                         sourceModule), cd.getLocation(), null);
 453  1
                 continue;
 454   
             }
 455   
 
 456  578
             addContributionElements(sourceModule, point, cd.getElements());
 457   
         }
 458   
     }
 459   
 
 460  1644
     private void setParametersSchema(ServicePointDescriptor spd, ServicePointImpl point)
 461   
     {
 462  1644
         Schema schema = spd.getParametersSchema();
 463   
 
 464  1644
         if (schema != null)
 465  102
             point.setParametersSchema(schema);
 466  1542
         else if (spd.getParametersSchemaId() != null)
 467   
         {
 468  102
             String moduleId = point.getModule().getModuleId();
 469  102
             String schemaId = IdUtils.qualify(moduleId, spd.getParametersSchemaId());
 470   
 
 471  102
             schema = getSchema(schemaId, point.getModule().getModuleId(), spd.getLocation());
 472   
 
 473  102
             point.setParametersSchema(schema);
 474   
         }
 475   
     }
 476   
 
 477  876
     private void setContributionsSchema(ConfigurationPointDescriptor cpd,
 478   
             ConfigurationPointImpl point)
 479   
     {
 480  876
         Schema schema = cpd.getContributionsSchema();
 481   
 
 482  876
         if (schema != null)
 483  615
             point.setContributionsSchema(schema);
 484  261
         else if (cpd.getContributionsSchemaId() != null)
 485   
         {
 486  256
             String moduleId = point.getModule().getModuleId();
 487  256
             String schemaId = IdUtils.qualify(moduleId, cpd.getContributionsSchemaId());
 488   
 
 489  256
             schema = getSchema(schemaId, point.getModule().getModuleId(), cpd.getLocation());
 490   
 
 491  256
             point.setContributionsSchema(schema);
 492   
         }
 493   
     }
 494   
 
 495  358
     private Schema getSchema(String schemaId, String referencingModule, Location reference)
 496   
     {
 497  358
         Schema schema = (Schema) _schemas.get(schemaId);
 498   
 
 499  358
         if (schema == null)
 500  1
             _errorHandler
 501   
                     .error(_log, ImplMessages.unableToResolveSchema(schemaId), reference, null);
 502  357
         else if (!schema.visibleToModule(referencingModule))
 503   
         {
 504  1
             _errorHandler.error(
 505   
                     _log,
 506   
                     ImplMessages.schemaNotVisible(schemaId, referencingModule),
 507   
                     reference,
 508   
                     null);
 509  1
             schema = null;
 510   
         }
 511   
 
 512  358
         return schema;
 513   
     }
 514   
 
 515   
     /**
 516   
      * Adds internal service contributions; the contributions provided inplace with the service
 517   
      * definition.
 518   
      */
 519  1644
     private void addInternalImplementations(Module sourceModule, String pointId,
 520   
             ServicePointDescriptor spd)
 521   
     {
 522  1644
         InstanceBuilder builder = spd.getInstanceBuilder();
 523  1644
         List interceptors = spd.getInterceptors();
 524   
 
 525  1644
         if (builder == null && interceptors == null)
 526  3
             return;
 527   
 
 528  1641
         if (builder != null)
 529  1641
             addServiceInstanceBuilder(sourceModule, pointId, builder, true);
 530   
 
 531  1641
         if (interceptors == null)
 532  1614
             return;
 533   
 
 534  27
         int count = size(interceptors);
 535   
 
 536  27
         for (int i = 0; i < count; i++)
 537   
         {
 538  27
             InterceptorDescriptor id = (InterceptorDescriptor) interceptors.get(i);
 539  27
             addInterceptor(sourceModule, pointId, id);
 540   
         }
 541   
     }
 542   
 
 543   
     /**
 544   
      * Adds ordinary service contributions.
 545   
      */
 546   
 
 547  4
     private void addImplementations(Module sourceModule, String pointId, ImplementationDescriptor id)
 548   
     {
 549  4
         InstanceBuilder builder = id.getInstanceBuilder();
 550  4
         List interceptors = id.getInterceptors();
 551   
 
 552  4
         if (builder != null)
 553  2
             addServiceInstanceBuilder(sourceModule, pointId, builder, false);
 554   
 
 555  4
         int count = size(interceptors);
 556  4
         for (int i = 0; i < count; i++)
 557   
         {
 558  4
             InterceptorDescriptor ind = (InterceptorDescriptor) interceptors.get(i);
 559   
 
 560  4
             addInterceptor(sourceModule, pointId, ind);
 561   
         }
 562   
     }
 563   
 
 564   
     /**
 565   
      * Adds an {@link InstanceBuilder}to a service extension point.
 566   
      */
 567  1643
     private void addServiceInstanceBuilder(Module sourceModule, String pointId,
 568   
             InstanceBuilder builder, boolean isDefault)
 569   
     {
 570  1643
         if (_log.isDebugEnabled())
 571  195
             _log.debug("Adding " + builder + " to service extension point " + pointId);
 572   
 
 573  1643
         ServicePointImpl point = (ServicePointImpl) _servicePoints.get(pointId);
 574   
 
 575  1643
         if (point == null)
 576   
         {
 577  0
             _errorHandler.error(
 578   
                     _log,
 579   
                     ImplMessages.unknownServicePoint(sourceModule, pointId),
 580   
                     builder.getLocation(),
 581   
                     null);
 582  0
             return;
 583   
         }
 584   
 
 585  1643
         if (!point.visibleToModule(sourceModule))
 586   
         {
 587  1
             _errorHandler.error(
 588   
                     _log,
 589   
                     ImplMessages.servicePointNotVisible(point, sourceModule),
 590   
                     builder.getLocation(),
 591   
                     null);
 592  1
             return;
 593   
         }
 594   
 
 595  1642
         if (point.getServiceConstructor(isDefault) != null)
 596   
         {
 597  0
             _errorHandler.error(
 598   
                     _log,
 599   
                     ImplMessages.duplicateFactory(sourceModule, pointId, point),
 600   
                     builder.getLocation(),
 601   
                     null);
 602   
 
 603  0
             return;
 604   
         }
 605   
 
 606  1642
         point.setServiceModel(builder.getServiceModel());
 607  1642
         point.setServiceConstructor(builder.createConstructor(point, sourceModule), isDefault);
 608   
     }
 609   
 
 610  31
     private void addInterceptor(Module sourceModule, String pointId, InterceptorDescriptor id)
 611   
     {
 612  31
         if (_log.isDebugEnabled())
 613  0
             _log.debug("Adding " + id + " to service extension point " + pointId);
 614   
 
 615  31
         ServicePointImpl point = (ServicePointImpl) _servicePoints.get(pointId);
 616   
 
 617  31
         String sourceModuleId = sourceModule.getModuleId();
 618   
 
 619  31
         if (point == null)
 620   
         {
 621  0
             _errorHandler.error(_log, ImplMessages.unknownServicePoint(sourceModule, pointId), id
 622   
                     .getLocation(), null);
 623   
 
 624  0
             return;
 625   
         }
 626   
 
 627  31
         if (!point.visibleToModule(sourceModule))
 628   
         {
 629  1
             _errorHandler.error(_log, ImplMessages.servicePointNotVisible(point, sourceModule), id
 630   
                     .getLocation(), null);
 631  1
             return;
 632   
         }
 633   
 
 634  30
         ServiceInterceptorContributionImpl sic = new ServiceInterceptorContributionImpl();
 635   
 
 636   
         // Allow the factory id to be unqualified, to refer to an interceptor factory
 637   
         // service from within the same module.
 638   
 
 639  30
         sic.setFactoryServiceId(IdUtils.qualify(sourceModuleId, id.getFactoryServiceId()));
 640  30
         sic.setLocation(id.getLocation());
 641   
 
 642  30
         sic.setFollowingInterceptorIds(IdUtils.qualifyList(sourceModuleId, id.getBefore()));
 643  30
         sic.setPrecedingInterceptorIds(IdUtils.qualifyList(sourceModuleId, id.getAfter()));
 644   
 
 645  30
         sic.setContributingModule(sourceModule);
 646  30
         sic.setParameters(id.getParameters());
 647   
 
 648  30
         point.addInterceptorContribution(sic);
 649   
     }
 650   
 
 651   
     /**
 652   
      * Checks that each service has at service constructor.
 653   
      */
 654  109
     private void checkForMissingServices()
 655   
     {
 656  109
         Iterator i = _servicePoints.values().iterator();
 657  109
         while (i.hasNext())
 658   
         {
 659  1644
             ServicePointImpl point = (ServicePointImpl) i.next();
 660   
 
 661  1644
             if (point.getServiceConstructor() != null)
 662  1642
                 continue;
 663   
 
 664  2
             _errorHandler.error(_log, ImplMessages.missingService(point), null, null);
 665   
         }
 666   
     }
 667   
 
 668   
     /**
 669   
      * Checks that each configuration extension point has the right number of contributions.
 670   
      */
 671   
 
 672  109
     private void checkContributionCounts()
 673   
     {
 674  109
         Iterator i = _configurationPoints.values().iterator();
 675   
 
 676  109
         while (i.hasNext())
 677   
         {
 678  876
             ConfigurationPointImpl point = (ConfigurationPointImpl) i.next();
 679   
 
 680  876
             Occurances expected = point.getExpectedCount();
 681   
 
 682  876
             int actual = point.getContributionCount();
 683   
 
 684  876
             if (expected.inRange(actual))
 685  874
                 continue;
 686   
 
 687  2
             _errorHandler.error(_log, ImplMessages.wrongNumberOfContributions(
 688   
                     point,
 689   
                     actual,
 690   
                     expected), point.getLocation(), null);
 691   
         }
 692   
 
 693   
     }
 694   
 
 695   
     /**
 696   
      * Filters a contribution based on an expression. Returns true if the expression is null, or
 697   
      * evaluates to true. Returns false if the expression if non-null and evaluates to false, or an
 698   
      * exception occurs evaluating the expression.
 699   
      * 
 700   
      * @param expression
 701   
      *            to parse and evaluate
 702   
      * @param location
 703   
      *            of the expression (used if an error is reported)
 704   
      * @since 1.1
 705   
      */
 706   
 
 707  586
     private boolean includeContribution(String expression, Module module, Location location)
 708   
     {
 709  586
         if (expression == null)
 710  583
             return true;
 711   
 
 712  3
         if (_conditionalExpressionParser == null)
 713  3
             _conditionalExpressionParser = new Parser();
 714   
 
 715  3
         try
 716   
         {
 717  3
             Node node = _conditionalExpressionParser.parse(expression);
 718   
 
 719  2
             return node.evaluate(new EvaluationContextImpl(module.getClassResolver()));
 720   
         }
 721   
         catch (RuntimeException ex)
 722   
         {
 723  1
             _errorHandler.error(_log, ex.getMessage(), location, ex);
 724   
 
 725  1
             return false;
 726   
         }
 727   
     }
 728   
 
 729  1707
     private static int size(Collection c)
 730   
     {
 731  1707
         return c == null ? 0 : c.size();
 732   
     }
 733   
 }