Clover coverage report - Code Coverage for hivemind release 1.1-alpha-2
Coverage timestamp: Wed Feb 23 2005 09:59:04 EST
file stats: LOC: 193   Methods: 6
NCLOC: 94   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
RegistryBuilder.java 83.3% 72.7% 100% 77.8%
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.HashSet;
 18   
 import java.util.Iterator;
 19   
 import java.util.List;
 20   
 import java.util.Locale;
 21   
 import java.util.Set;
 22   
 
 23   
 import org.apache.commons.logging.Log;
 24   
 import org.apache.commons.logging.LogFactory;
 25   
 import org.apache.hivemind.ClassResolver;
 26   
 import org.apache.hivemind.ErrorHandler;
 27   
 import org.apache.hivemind.ModuleDescriptorProvider;
 28   
 import org.apache.hivemind.Registry;
 29   
 import org.apache.hivemind.internal.ConfigurationPoint;
 30   
 import org.apache.hivemind.internal.Contribution;
 31   
 import org.apache.hivemind.internal.Module;
 32   
 import org.apache.hivemind.internal.RegistryInfrastructure;
 33   
 import org.apache.hivemind.parse.ModuleDescriptor;
 34   
 
 35   
 /**
 36   
  * Class used to build a {@link org.apache.hivemind.Registry}from individual
 37   
  * {@link org.apache.hivemind.parse.ModuleDescriptor}. The descriptors are provided by the
 38   
  * {@link ModuleDescriptorProvider}parameter passed to {@link #constructRegistry(Locale)}method.
 39   
  * <p>
 40   
  * A note about threadsafety: The assumption is that a single thread will access the RegistryBuilder
 41   
  * at one time (typically, a startup class within some form of server or application). Code here and
 42   
  * in many of the related classes is divided into construction-time logic and runtime logic. Runtime
 43   
  * logic is synchronized and threadsafe. Construction-time logic is not threadsafe. Once the
 44   
  * registry is fully constructed, it is not allowed to invoke those methods (though, at this time,
 45   
  * no checks occur).
 46   
  * <p>
 47   
  * Runtime methods, such as {@link org.apache.hivemind.impl.ModuleImpl#getService(String, Class)}
 48   
  * are fully threadsafe.
 49   
  * 
 50   
  * @author Howard Lewis Ship
 51   
  */
 52   
 public final class RegistryBuilder
 53   
 {
 54   
     private static final Log LOG = LogFactory.getLog(RegistryBuilder.class);
 55   
 
 56   
     static
 57   
     {
 58  1
         if (!LOG.isErrorEnabled())
 59   
         {
 60  0
             System.err
 61   
                     .println("********************************************************************************");
 62  0
             System.err
 63   
                     .println("* L O G G I N G   C O N F I G U R A T I O N   E R R O R                        *");
 64  0
             System.err
 65   
                     .println("* ---------------------------------------------------------------------------- *");
 66  0
             System.err
 67   
                     .println("* Logging is not enabled for org.apache.hivemind.impl.RegistryBuilder.         *");
 68  0
             System.err
 69   
                     .println("* Errors during HiveMind module descriptor parsing and validation may not be   *");
 70  0
             System.err
 71   
                     .println("* logged. This may result in difficult-to-trace runtime exceptions, if there   *");
 72  0
             System.err
 73   
                     .println("* are errors in any of your module descriptors. You should enable error        *");
 74  0
             System.err
 75   
                     .println("* logging for the org.apache.hivemind and hivemind loggers.                    *");
 76  0
             System.err
 77   
                     .println("********************************************************************************");
 78   
         }
 79   
     }
 80   
 
 81   
     /**
 82   
      * Delegate used for handling errors.
 83   
      */
 84   
 
 85   
     private ErrorHandler _errorHandler;
 86   
 
 87   
     /**
 88   
      * RegistryAssembly used by the module descriptor parser(s).
 89   
      */
 90   
 
 91   
     private RegistryAssemblyImpl _registryAssembly;
 92   
 
 93   
     /**
 94   
      * A set of all {@link ModuleDescriptorProvider}objects used to construct the Registry.
 95   
      * 
 96   
      * @since 1.1
 97   
      */
 98   
 
 99   
     private Set _moduleDescriptorProviders;
 100   
 
 101   
     /**
 102   
      * Contains most of the logic for actually creating the registry.
 103   
      * 
 104   
      * @since 1.1
 105   
      */
 106   
 
 107   
     private RegistryInfrastructureConstructor _constructor;
 108   
 
 109  108
     public RegistryBuilder()
 110   
     {
 111  108
         this(new DefaultErrorHandler());
 112   
     }
 113   
 
 114  108
     public RegistryBuilder(ErrorHandler handler)
 115   
     {
 116  108
         _errorHandler = handler;
 117   
 
 118  108
         _registryAssembly = new RegistryAssemblyImpl();
 119   
 
 120  108
         _moduleDescriptorProviders = new HashSet();
 121   
 
 122  108
         _constructor = new RegistryInfrastructureConstructor(handler, LOG, _registryAssembly);
 123   
     }
 124   
 
 125   
     /**
 126   
      * Adds a {@link ModuleDescriptorProvider}as a source for
 127   
      * {@link ModuleDescriptor module descriptors}to this RegistryBuilder. Adding the same provider
 128   
      * instance multiple times has no effect.
 129   
      * 
 130   
      * @since 1.1
 131   
      */
 132  208
     public void addModuleDescriptorProvider(ModuleDescriptorProvider provider)
 133   
     {
 134  208
         _moduleDescriptorProviders.add(provider);
 135   
     }
 136   
 
 137   
     /**
 138   
      * This first loads all modules provided by the ModuleDescriptorProvider, then resolves all the
 139   
      * contributions, then constructs and returns the Registry.
 140   
      */
 141  108
     public Registry constructRegistry(Locale locale)
 142   
     {
 143  108
         for (Iterator i = _moduleDescriptorProviders.iterator(); i.hasNext();)
 144   
         {
 145  208
             ModuleDescriptorProvider provider = (ModuleDescriptorProvider) i.next();
 146   
 
 147  208
             processModuleDescriptorProvider(provider);
 148   
         }
 149   
 
 150   
         // Process any deferred operations. Post processing is added by
 151   
         // both the parser and the registry constructor.
 152   
 
 153  108
         _registryAssembly.performPostProcessing();
 154   
 
 155  108
         RegistryInfrastructure infrastructure = _constructor
 156   
                 .constructRegistryInfrastructure(locale);
 157   
 
 158  108
         infrastructure.startup();
 159   
 
 160  108
         return new RegistryImpl(infrastructure);
 161   
     }
 162   
 
 163  208
     private void processModuleDescriptorProvider(ModuleDescriptorProvider provider)
 164   
     {
 165  208
         List descriptors = provider.getModuleDescriptors(_errorHandler);
 166   
 
 167  208
         Iterator i = descriptors.iterator();
 168  208
         while (i.hasNext())
 169   
         {
 170  220
             ModuleDescriptor md = (ModuleDescriptor) i.next();
 171   
 
 172  220
             _constructor.addModuleDescriptor(md);
 173   
         }
 174   
     }
 175   
 
 176   
     /**
 177   
      * Constructs a default registry based on just the modules visible to the thread context class
 178   
      * loader (this is sufficient is the majority of cases), and using the default locale. If you
 179   
      * have different error handling needs, or wish to pick up HiveMind module deployment
 180   
      * descriptors for non-standard locations, you must create a RegistryBuilder instance yourself.
 181   
      */
 182  4
     public static Registry constructDefaultRegistry()
 183   
     {
 184  4
         ClassResolver resolver = new DefaultClassResolver();
 185  4
         RegistryBuilder builder = new RegistryBuilder();
 186  4
         ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(resolver);
 187   
 
 188  4
         builder.addModuleDescriptorProvider(provider);
 189   
 
 190  4
         return builder.constructRegistry(Locale.getDefault());
 191   
     }
 192   
 
 193   
 }