Clover coverage report - Code Coverage for hivemind release 1.0
Coverage timestamp: Wed Sep 22 2004 08:05:25 EDT
file stats: LOC: 205   Methods: 6
NCLOC: 127   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
TranslatorManager.java 94.4% 94.4% 100% 94.9%
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.lang.reflect.Constructor;
 18   
 import java.util.HashMap;
 19   
 import java.util.Iterator;
 20   
 import java.util.List;
 21   
 import java.util.Map;
 22   
 
 23   
 import org.apache.commons.logging.Log;
 24   
 import org.apache.commons.logging.LogFactory;
 25   
 import org.apache.hivemind.ApplicationRuntimeException;
 26   
 import org.apache.hivemind.ErrorHandler;
 27   
 import org.apache.hivemind.Location;
 28   
 import org.apache.hivemind.Registry;
 29   
 import org.apache.hivemind.schema.Translator;
 30   
 import org.apache.hivemind.schema.rules.ClassTranslator;
 31   
 import org.apache.hivemind.schema.rules.InstanceTranslator;
 32   
 import org.apache.hivemind.schema.rules.ServiceTranslator;
 33   
 import org.apache.hivemind.schema.rules.SmartTranslator;
 34   
 
 35   
 /**
 36   
  * Manages translators for {@link org.apache.hivemind.impl.RegistryImpl}.
 37   
  *
 38   
  * @author Howard Lewis Ship
 39   
  */
 40   
 public class TranslatorManager
 41   
 {
 42   
     static final Log LOG = LogFactory.getLog(TranslatorManager.class);
 43   
 
 44   
     public static final String TRANSLATORS_CONFIGURATION_ID = "hivemind.Translators";
 45   
 
 46   
     private ErrorHandler _errorHandler;
 47   
     private Registry _registry;
 48   
 
 49   
     /**
 50   
      * Map of Class, keyed on translator name, used to instantiate
 51   
      * new {@link org.apache.hivemind.schema.Translator}s. Loaded
 52   
      * from the <code>hivemind.Translators</code> configuration point;
 53   
      */
 54   
     private Map _translatorClasses = new HashMap();
 55   
     private Map _translatorsCache = new HashMap();
 56   
     private boolean _translatorsLoaded;
 57   
 
 58  98
     public TranslatorManager(Registry registry, ErrorHandler errorHandler)
 59   
     {
 60  98
         _registry = registry;
 61  98
         _errorHandler = errorHandler;
 62   
 
 63   
         // Seed the basic translators used to "bootstrap" the
 64   
         // processing of the hivemind.Translators configuration point.
 65   
 
 66  98
         _translatorsCache.put("class", new ClassTranslator());
 67  98
         _translatorsCache.put("service", new ServiceTranslator());
 68  98
         _translatorsCache.put("smart", new SmartTranslator());
 69  98
         _translatorsCache.put("instance", new InstanceTranslator());
 70   
 
 71   
         // smart may take an initializer, so we need to put it into the classes as
 72   
         // well.
 73   
 
 74  98
         _translatorClasses.put("smart", SmartTranslator.class);
 75   
 
 76   
     }
 77   
 
 78  5376
     public synchronized Translator getTranslator(String constructor)
 79   
     {
 80   
         // The cache is preloaded with the hardcoded translators.
 81   
 
 82  5376
         if (!_translatorsLoaded && !_translatorsCache.containsKey(constructor))
 83  97
             loadTranslators();
 84   
 
 85  5376
         Translator result = (Translator) _translatorsCache.get(constructor);
 86   
 
 87  5376
         if (result == null)
 88   
         {
 89  201
             result = constructTranslator(constructor);
 90  200
             _translatorsCache.put(constructor, result);
 91   
         }
 92   
 
 93  5375
         return result;
 94   
     }
 95   
 
 96  201
     private Translator constructTranslator(String constructor)
 97   
     {
 98  201
         String name = constructor;
 99  201
         String initializer = null;
 100   
 
 101  201
         int commax = constructor.indexOf(',');
 102   
 
 103  201
         if (commax > 0)
 104   
         {
 105  97
             name = constructor.substring(0, commax);
 106  97
             initializer = constructor.substring(commax + 1);
 107   
         }
 108   
 
 109  201
         Class translatorClass = findTranslatorClass(name);
 110   
 
 111   
         // TODO: check for null class, meaning that the translator is a service.
 112   
 
 113  200
         return createTranslator(translatorClass, initializer);
 114   
     }
 115   
 
 116  200
     private Translator createTranslator(Class translatorClass, String initializer)
 117   
     {
 118  200
         try
 119   
         {
 120   
 
 121  200
             if (initializer == null)
 122  103
                 return (Translator) translatorClass.newInstance();
 123   
 
 124  97
             Constructor c = translatorClass.getConstructor(new Class[] { String.class });
 125   
 
 126  97
             return (Translator) c.newInstance(new Object[] { initializer });
 127   
         }
 128   
         catch (Exception ex)
 129   
         {
 130  0
             throw new ApplicationRuntimeException(
 131   
                 ImplMessages.translatorInstantiationFailure(translatorClass, ex),
 132   
                 ex);
 133   
         }
 134   
     }
 135   
 
 136  201
     private Class findTranslatorClass(String translatorName)
 137   
     {
 138  201
         Class result = (Class) _translatorClasses.get(translatorName);
 139   
 
 140  201
         if (result == null)
 141  1
             throw new ApplicationRuntimeException(
 142   
                 ImplMessages.unknownTranslatorName(translatorName, TRANSLATORS_CONFIGURATION_ID));
 143   
 
 144  200
         return result;
 145   
     }
 146   
 
 147  97
     private void loadTranslators()
 148   
     {
 149   
         // Prevent endless recursion!
 150   
 
 151  97
         _translatorsLoaded = true;
 152   
 
 153  97
         List contributions = _registry.getConfiguration(TRANSLATORS_CONFIGURATION_ID);
 154   
 
 155  97
         Map locations = new HashMap();
 156  97
         locations.put("class", null);
 157   
 
 158  97
         Iterator i = contributions.iterator();
 159  97
         while (i.hasNext())
 160   
         {
 161  1057
             TranslatorContribution c = (TranslatorContribution) i.next();
 162   
 
 163  1057
             String name = c.getName();
 164  1057
             Location oldLocation = (Location) locations.get(name);
 165   
 
 166  1057
             if (oldLocation != null)
 167   
             {
 168  0
                 _errorHandler.error(
 169   
                     LOG,
 170   
                     ImplMessages.duplicateTranslatorName(name, oldLocation),
 171   
                     c.getLocation(),
 172   
                     null);
 173   
 
 174  0
                 continue;
 175   
             }
 176   
 
 177  1057
             locations.put(name, c.getLocation());
 178   
 
 179  1057
             Translator t = c.getTranslator();
 180   
 
 181  1057
             if (t != null)
 182   
             {
 183  96
                 _translatorsCache.put(name, t);
 184  96
                 continue;
 185   
             }
 186   
 
 187  961
             Class tClass = c.getTranslatorClass();
 188   
 
 189  961
             if (tClass == null)
 190   
             {
 191  1
                 _errorHandler.error(
 192   
                     LOG,
 193   
                     ImplMessages.incompleteTranslator(c),
 194   
                     c.getLocation(),
 195   
                     null);
 196  1
                 continue;
 197   
             }
 198   
 
 199  960
             _translatorClasses.put(name, tClass);
 200   
         }
 201   
 
 202   
     }
 203   
 
 204   
 }
 205