Clover coverage report - Code Coverage for hivemind release 1.0
Coverage timestamp: Wed Sep 22 2004 08:05:25 EDT
file stats: LOC: 429   Methods: 29
NCLOC: 277   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
RegistryImpl.java 86.8% 94.9% 96.6% 93.5%
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.Collection;
 18   
 import java.util.HashMap;
 19   
 import java.util.Iterator;
 20   
 import java.util.LinkedList;
 21   
 import java.util.List;
 22   
 import java.util.Locale;
 23   
 import java.util.Map;
 24   
 
 25   
 import org.apache.commons.logging.Log;
 26   
 import org.apache.commons.logging.LogFactory;
 27   
 import org.apache.hivemind.ApplicationRuntimeException;
 28   
 import org.apache.hivemind.ErrorHandler;
 29   
 import org.apache.hivemind.HiveMind;
 30   
 import org.apache.hivemind.HiveMindMessages;
 31   
 import org.apache.hivemind.Location;
 32   
 import org.apache.hivemind.Registry;
 33   
 import org.apache.hivemind.ShutdownCoordinator;
 34   
 import org.apache.hivemind.SymbolSource;
 35   
 import org.apache.hivemind.SymbolSourceContribution;
 36   
 import org.apache.hivemind.internal.ConfigurationPoint;
 37   
 import org.apache.hivemind.internal.RegistryInfrastructure;
 38   
 import org.apache.hivemind.internal.ServiceModelFactory;
 39   
 import org.apache.hivemind.internal.ServicePoint;
 40   
 import org.apache.hivemind.order.Orderer;
 41   
 import org.apache.hivemind.schema.Translator;
 42   
 import org.apache.hivemind.service.ThreadEventNotifier;
 43   
 import org.apache.hivemind.util.ToStringBuilder;
 44   
 
 45   
 /**
 46   
  * Implementation of {@link org.apache.hivemind.Registry}.
 47   
  *
 48   
  * @author Howard Lewis Ship
 49   
  */
 50   
 public final class RegistryImpl implements Registry, RegistryInfrastructure
 51   
 {
 52   
     private static final String SYMBOL_SOURCES = "hivemind.SymbolSources";
 53   
     private static final Log LOG = LogFactory.getLog(RegistryImpl.class);
 54   
 
 55   
     /**
 56   
      * Map of {@link ServicePoint} keyed on fully qualified service id.
 57   
      */
 58   
     private Map _servicePoints = new HashMap();
 59   
 
 60   
     /**
 61   
      * Map of List (of {@link ServicePoint}, keyed on service interface.
 62   
      */
 63   
     private Map _servicePointsByInterface = new HashMap();
 64   
 
 65   
     /**
 66   
      * Map of {@link ConfigurationPoint} keyed on fully qualified configuration id.
 67   
      */
 68   
     private Map _configurationPoints = new HashMap();
 69   
 
 70   
     private SymbolSource[] _variableSources;
 71   
     private ErrorHandler _errorHandler;
 72   
     private Locale _locale;
 73   
     private ShutdownCoordinator _shutdownCoordinator;
 74   
 
 75   
     /**
 76   
      * Map of {@link ServiceModelFactory}, keyed on service model name,
 77   
      * loaded from <code>hivemind.ServiceModels</code> configuration point.
 78   
      */
 79   
     private Map _serviceModelFactories;
 80   
 
 81   
     private boolean _started = false;
 82   
     private boolean _shutdown = false;
 83   
 
 84   
     private ThreadEventNotifier _threadEventNotifier;
 85   
     private TranslatorManager _translatorManager;
 86   
 
 87   
     private SymbolExpander _expander;
 88   
 
 89  97
     public RegistryImpl(ErrorHandler errorHandler, Locale locale)
 90   
     {
 91  97
         _errorHandler = errorHandler;
 92  97
         _locale = locale;
 93   
 
 94  97
         _translatorManager = new TranslatorManager(this, errorHandler);
 95   
 
 96  97
         _expander = new SymbolExpander(_errorHandler, this);
 97   
     }
 98   
 
 99  11
     public Locale getLocale()
 100   
     {
 101  11
         return _locale;
 102   
     }
 103   
 
 104  1544
     public void addServicePoint(ServicePoint point)
 105   
     {
 106  1544
         checkStarted();
 107   
 
 108  1544
         _servicePoints.put(point.getExtensionPointId(), point);
 109   
 
 110  1544
         addServicePointByInterface(point);
 111   
     }
 112   
 
 113  1544
     private void addServicePointByInterface(ServicePoint point)
 114   
     {
 115  1544
         Class key = point.getServiceInterface();
 116   
 
 117  1544
         List l = (List) _servicePointsByInterface.get(key);
 118   
 
 119  1544
         if (l == null)
 120   
         {
 121  1017
             l = new LinkedList();
 122  1017
             _servicePointsByInterface.put(key, l);
 123   
         }
 124   
 
 125  1544
         l.add(point);
 126   
     }
 127   
 
 128  819
     public void addConfigurationPoint(ConfigurationPoint point)
 129   
     {
 130  819
         checkStarted();
 131   
 
 132  819
         _configurationPoints.put(point.getExtensionPointId(), point);
 133   
     }
 134   
 
 135  2529
     public ServicePoint getServicePoint(String serviceId)
 136   
     {
 137  2529
         checkShutdown();
 138   
 
 139  2529
         ServicePoint result = (ServicePoint) _servicePoints.get(serviceId);
 140   
 
 141  2529
         if (result == null)
 142  0
             throw new ApplicationRuntimeException(ImplMessages.noSuchServicePoint(serviceId));
 143   
 
 144  2529
         return result;
 145   
     }
 146   
 
 147  2141
     public Object getService(String serviceId, Class serviceInterface)
 148   
     {
 149  2141
         ServicePoint point = getServicePoint(serviceId);
 150   
 
 151  2141
         return point.getService(serviceInterface);
 152   
     }
 153   
 
 154  128
     public Object getService(Class serviceInterface)
 155   
     {
 156  128
         List servicePoints = (List) _servicePointsByInterface.get(serviceInterface);
 157   
 
 158  128
         if (servicePoints == null)
 159  1
             throw new ApplicationRuntimeException(
 160   
                 ImplMessages.noServicePointForInterface(serviceInterface));
 161   
 
 162  127
         if (servicePoints.size() > 1)
 163  1
             throw new ApplicationRuntimeException(
 164   
                 ImplMessages.multipleServicePointsForInterface(serviceInterface, servicePoints));
 165   
 
 166  126
         ServicePoint sp = (ServicePoint) servicePoints.get(0);
 167   
 
 168  126
         return sp.getService(serviceInterface);
 169   
     }
 170   
 
 171  528
     public ConfigurationPoint getConfigurationPoint(String configurationId)
 172   
     {
 173  528
         checkShutdown();
 174   
 
 175  527
         ConfigurationPoint result = (ConfigurationPoint) _configurationPoints.get(configurationId);
 176   
 
 177  527
         if (result == null)
 178  1
             throw new ApplicationRuntimeException(
 179   
                 ImplMessages.noSuchConfiguration(configurationId));
 180   
 
 181  526
         return result;
 182   
     }
 183   
 
 184  527
     public List getConfiguration(String configurationId)
 185   
     {
 186  527
         ConfigurationPoint point = getConfigurationPoint(configurationId);
 187   
 
 188  525
         return point.getElements();
 189   
     }
 190   
 
 191  4
     public String toString()
 192   
     {
 193  4
         ToStringBuilder builder = new ToStringBuilder(this);
 194   
 
 195  4
         builder.append("locale", _locale);
 196   
 
 197  4
         return builder.toString();
 198   
     }
 199   
 
 200  4915
     public String expandSymbols(String text, Location location)
 201   
     {
 202  4915
         return _expander.expandSymbols(text, location);
 203   
     }
 204   
 
 205  5
     public String valueForSymbol(String name)
 206   
     {
 207  5
         checkShutdown();
 208   
 
 209  5
         SymbolSource[] sources = getSymbolSources();
 210   
 
 211  5
         for (int i = 0; i < sources.length; i++)
 212   
         {
 213  7
             String value = sources[i].valueForSymbol(name);
 214   
 
 215  7
             if (value != null)
 216  4
                 return value;
 217   
         }
 218   
 
 219  1
         return null;
 220   
     }
 221   
 
 222  5
     private synchronized SymbolSource[] getSymbolSources()
 223   
     {
 224  5
         if (_variableSources != null)
 225  2
             return _variableSources;
 226   
 
 227  3
         List contributions = getConfiguration(SYMBOL_SOURCES);
 228   
 
 229  3
         Orderer o =
 230   
             new Orderer(
 231   
                 LogFactory.getLog(SYMBOL_SOURCES),
 232   
                 _errorHandler,
 233   
                 ImplMessages.symbolSourceContribution());
 234   
 
 235  3
         Iterator i = contributions.iterator();
 236  3
         while (i.hasNext())
 237   
         {
 238  8
             SymbolSourceContribution c = (SymbolSourceContribution) i.next();
 239   
 
 240  8
             o.add(c, c.getName(), c.getPrecedingNames(), c.getFollowingNames());
 241   
         }
 242   
 
 243  3
         List sources = o.getOrderedObjects();
 244   
 
 245  3
         int count = sources.size();
 246   
 
 247  3
         _variableSources = new SymbolSource[count];
 248   
 
 249  3
         for (int j = 0; j < count; j++)
 250   
         {
 251  8
             SymbolSourceContribution c = (SymbolSourceContribution) sources.get(j);
 252  8
             _variableSources[j] = c.getSource();
 253   
         }
 254   
 
 255  3
         return _variableSources;
 256   
     }
 257   
 
 258  96
     public void setShutdownCoordinator(ShutdownCoordinator coordinator)
 259   
     {
 260  96
         _shutdownCoordinator = coordinator;
 261   
     }
 262   
 
 263   
     /**
 264   
      * Invokes {@link ShutdownCoordinator#shutdown()}, then releases
 265   
      * the coordinator, modules and variable sources.
 266   
      */
 267  19
     public synchronized void shutdown()
 268   
     {
 269  19
         checkShutdown();
 270   
         // Allow service implementations and such to shutdown.
 271   
 
 272  18
         ShutdownCoordinator coordinatorService =
 273   
             (ShutdownCoordinator) getService("hivemind.ShutdownCoordinator",
 274   
                 ShutdownCoordinator.class);
 275   
 
 276  18
         coordinatorService.shutdown();
 277   
 
 278  18
         _shutdown = true;
 279   
 
 280   
         // Shutdown infrastructure items, such as proxies.
 281   
 
 282  18
         _shutdownCoordinator.shutdown();
 283   
 
 284  18
         _servicePoints = null;
 285  18
         _servicePointsByInterface = null;
 286  18
         _configurationPoints = null;
 287  18
         _shutdownCoordinator = null;
 288  18
         _variableSources = null;
 289  18
         _serviceModelFactories = null;
 290  18
         _threadEventNotifier = null;
 291   
     }
 292   
 
 293  3088
     private synchronized void checkShutdown()
 294   
     {
 295  3088
         if (_shutdown)
 296  2
             throw new ApplicationRuntimeException(HiveMindMessages.registryShutdown());
 297   
     }
 298   
 
 299  2459
     private void checkStarted()
 300   
     {
 301  2459
         if (_started)
 302  0
             throw new IllegalStateException(ImplMessages.registryAlreadyStarted());
 303   
     }
 304   
 
 305   
     /**
 306   
      * Starts up the Registry after all service and configuration points have been defined.
 307   
      * This locks down the Registry so that no further extension points may be added.
 308   
      * This method may only be invoked once.
 309   
      * 
 310   
      * <p>
 311   
      * In addition, the service <code>hivemind.Startup</code> is obtained and 
 312   
      * <code>run()</code> is invoked on it. This allows additional startup, provided
 313   
      * in the <code>hivemind.Startup</code> configuration point, to be executed.
 314   
      */
 315  96
     public void startup()
 316   
     {
 317  96
         checkStarted();
 318   
 
 319  96
         _started = true;
 320   
 
 321  96
         Runnable startup = (Runnable) getService("hivemind.Startup", Runnable.class);
 322   
 
 323  96
         startup.run();
 324   
     }
 325   
 
 326  981
     public synchronized ServiceModelFactory getServiceModelFactory(String name)
 327   
     {
 328  981
         if (_serviceModelFactories == null)
 329  96
             readServiceModelFactories();
 330   
 
 331  981
         ServiceModelFactory result = (ServiceModelFactory) _serviceModelFactories.get(name);
 332   
 
 333  981
         if (result == null)
 334  0
             throw new ApplicationRuntimeException(ImplMessages.unknownServiceModel(name));
 335   
 
 336  981
         return result;
 337   
     }
 338   
 
 339  96
     private void readServiceModelFactories()
 340   
     {
 341  96
         List l = getConfiguration("hivemind.ServiceModels");
 342   
 
 343  96
         _serviceModelFactories = new HashMap();
 344   
 
 345  96
         Iterator i = l.iterator();
 346   
 
 347  96
         while (i.hasNext())
 348   
         {
 349  384
             ServiceModelContribution smc = (ServiceModelContribution) i.next();
 350   
 
 351  384
             String name = smc.getName();
 352   
 
 353  384
             ServiceModelFactory old = (ServiceModelFactory) _serviceModelFactories.get(name);
 354   
 
 355  384
             if (old != null)
 356   
             {
 357  0
                 _errorHandler.error(
 358   
                     LOG,
 359   
                     ImplMessages.dupeServiceModelName(name, HiveMind.getLocation(old)),
 360   
                     HiveMind.getLocation(smc),
 361   
                     null);
 362   
 
 363  0
                 continue;
 364   
             }
 365   
 
 366  384
             _serviceModelFactories.put(name, smc.getFactory());
 367   
         }
 368   
     }
 369   
 
 370  802
     public synchronized void cleanupThread()
 371   
     {
 372  802
         if (_threadEventNotifier == null)
 373  6
             _threadEventNotifier =
 374   
                 (ThreadEventNotifier) getService("hivemind.ThreadEventNotifier",
 375   
                     ThreadEventNotifier.class);
 376   
 
 377  802
         _threadEventNotifier.fireThreadCleanup();
 378   
     }
 379   
 
 380  2
     public boolean containsConfiguration(String configurationId)
 381   
     {
 382  2
         checkShutdown();
 383   
 
 384  2
         return _configurationPoints.containsKey(configurationId);
 385   
     }
 386   
 
 387  2
     public boolean containsService(Class serviceInterface)
 388   
     {
 389  2
         checkShutdown();
 390   
 
 391  2
         List servicePoints = (List) _servicePointsByInterface.get(serviceInterface);
 392   
 
 393  2
         return size(servicePoints) == 1;
 394   
     }
 395   
 
 396  3
     public boolean containsService(String serviceId, Class serviceInterface)
 397   
     {
 398  3
         checkShutdown();
 399   
 
 400  3
         ServicePoint point = (ServicePoint) _servicePoints.get(serviceId);
 401   
 
 402  3
         if (point == null)
 403  1
             return false;
 404   
 
 405  2
         return point.getServiceInterface().equals(serviceInterface);
 406   
     }
 407   
 
 408  0
     public void setLocale(Locale locale)
 409   
     {
 410  0
         _locale = locale;
 411   
     }
 412   
 
 413  936
     public ErrorHandler getErrorHander()
 414   
     {
 415  936
         return _errorHandler;
 416   
     }
 417   
 
 418  5375
     public Translator getTranslator(String translator)
 419   
     {
 420  5375
         return _translatorManager.getTranslator(translator);
 421   
     }
 422   
 
 423  2
     private int size(Collection c)
 424   
     {
 425  2
         return c == null ? 0 : c.size();
 426   
     }
 427   
 
 428   
 }
 429