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: 196   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. Methods such as
 44   
  * {@link org.apache.hivemind.impl.RegistryInfrastructureImpl#addModule(Module)},
 45   
  * {@link org.apache.hivemind.impl.ModuleImpl#addConfigurationPoint(ConfigurationPoint)},
 46   
  * {@link org.apache.hivemind.impl.ConfigurationPointImpl#addContribution(Contribution)}and the
 47   
  * like are construction-time. Once the registry is fully constructed, it is not allowed to invoke
 48   
  * those methods (though, at this time, no checks occur).
 49   
  * <p>
 50   
  * Runtime methods, such as {@link org.apache.hivemind.impl.ModuleImpl#getService(String, Class)}
 51   
  * are fully threadsafe.
 52   
  * 
 53   
  * @author Howard Lewis Ship
 54   
  */
 55   
 public final class RegistryBuilder
 56   
 {
 57   
     private static final Log LOG = LogFactory.getLog(RegistryBuilder.class);
 58   
 
 59   
     static
 60   
     {
 61  1
         if (!LOG.isErrorEnabled())
 62   
         {
 63  0
             System.err
 64   
                     .println("********************************************************************************");
 65  0
             System.err
 66   
                     .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                        *");
 67  0
             System.err
 68   
                     .println("* ---------------------------------------------------------------------------- *");
 69  0
             System.err
 70   
                     .println("* Logging is not enabled for org.apache.hivemind.impl.RegistryBuilder.         *");
 71  0
             System.err
 72   
                     .println("* Errors during HiveMind module descriptor parsing and validation may not be   *");
 73  0
             System.err
 74   
                     .println("* logged. This may result in difficult-to-trace runtime exceptions, if there   *");
 75  0
             System.err
 76   
                     .println("* are errors in any of your module descriptors. You should enable error        *");
 77  0
             System.err
 78   
                     .println("* logging for the org.apache.hivemind and hivemind loggers.                    *");
 79  0
             System.err
 80   
                     .println("********************************************************************************");
 81   
         }
 82   
     }
 83   
 
 84   
     /**
 85   
      * Delegate used for handling errors.
 86   
      */
 87   
 
 88   
     private ErrorHandler _errorHandler;
 89   
 
 90   
     /**
 91   
      * RegistryAssembly used by the module descriptor parser(s).
 92   
      */
 93   
 
 94   
     private RegistryAssemblyImpl _registryAssembly;
 95   
 
 96   
     /**
 97   
      * A set of all {@link ModuleDescriptorProvider}objects used to construct the Registry.
 98   
      * 
 99   
      * @since 1.1
 100   
      */
 101   
 
 102   
     private Set _moduleDescriptorProviders;
 103   
 
 104   
     /**
 105   
      * Contains most of the logic for actually creating the registry.
 106   
      * 
 107   
      * @since 1.1
 108   
      */
 109   
 
 110   
     private RegistryInfrastructureConstructor _constructor;
 111   
 
 112  102
     public RegistryBuilder()
 113   
     {
 114  102
         this(new DefaultErrorHandler());
 115   
     }
 116   
 
 117  102
     public RegistryBuilder(ErrorHandler handler)
 118   
     {
 119  102
         _errorHandler = handler;
 120   
 
 121  102
         _registryAssembly = new RegistryAssemblyImpl();
 122   
 
 123  102
         _moduleDescriptorProviders = new HashSet();
 124   
 
 125  102
         _constructor = new RegistryInfrastructureConstructor(handler, LOG, _registryAssembly);
 126   
     }
 127   
 
 128   
     /**
 129   
      * Adds a {@link ModuleDescriptorProvider}as a source for
 130   
      * {@link ModuleDescriptor module descriptors}to this RegistryBuilder. Adding the same provider
 131   
      * instance multiple times has no effect.
 132   
      * 
 133   
      * @since 1.1
 134   
      */
 135  198
     public void addModuleDescriptorProvider(ModuleDescriptorProvider provider)
 136   
     {
 137  198
         _moduleDescriptorProviders.add(provider);
 138   
     }
 139   
 
 140   
     /**
 141   
      * This first loads all modules provided by the ModuleDescriptorProvider, then resolves all the
 142   
      * contributions, then constructs and returns the Registry.
 143   
      */
 144  102
     public Registry constructRegistry(Locale locale)
 145   
     {
 146  102
         for (Iterator i = _moduleDescriptorProviders.iterator(); i.hasNext();)
 147   
         {
 148  198
             ModuleDescriptorProvider provider = (ModuleDescriptorProvider) i.next();
 149   
 
 150  198
             processModuleDescriptorProvider(provider);
 151   
         }
 152   
 
 153   
         // Process any deferred operations. Post processing is added by
 154   
         // both the parser and the registry constructor.
 155   
 
 156  102
         _registryAssembly.performPostProcessing();
 157   
 
 158  102
         RegistryInfrastructure infrastructure = _constructor
 159   
                 .constructRegistryInfrastructure(locale);
 160   
 
 161  102
         infrastructure.startup();
 162   
 
 163  102
         return new RegistryImpl(infrastructure);
 164   
     }
 165   
 
 166  198
     private void processModuleDescriptorProvider(ModuleDescriptorProvider provider)
 167   
     {
 168  198
         List descriptors = provider.getModuleDescriptors(_errorHandler);
 169   
 
 170  198
         Iterator i = descriptors.iterator();
 171  198
         while (i.hasNext())
 172   
         {
 173  210
             ModuleDescriptor md = (ModuleDescriptor) i.next();
 174   
 
 175  210
             _constructor.addModuleDescriptor(md);
 176   
         }
 177   
     }
 178   
 
 179   
     /**
 180   
      * Constructs a default registry based on just the modules visible to the thread context class
 181   
      * loader (this is sufficient is the majority of cases), and using the default locale. If you
 182   
      * have different error handling needs, or wish to pick up HiveMind module deployment
 183   
      * descriptors for non-standard locations, you must create a RegistryBuilder instance yourself.
 184   
      */
 185  2
     public static Registry constructDefaultRegistry()
 186   
     {
 187  2
         ClassResolver resolver = new DefaultClassResolver();
 188  2
         RegistryBuilder builder = new RegistryBuilder();
 189  2
         ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(resolver);
 190   
 
 191  2
         builder.addModuleDescriptorProvider(provider);
 192   
         
 193  2
         return builder.constructRegistry(Locale.getDefault());
 194   
     }
 195   
 
 196   
 }