2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

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