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: 207   Methods: 6
NCLOC: 126   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TranslatorManager.java 94.4% 94.4% 100% 94.9%
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.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.internal.RegistryInfrastructure;
 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.RegistryInfrastructureImpl}.
 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   
 48    private RegistryInfrastructure _registry;
 49   
 50    /**
 51    * Map of Class, keyed on translator name, used to instantiate new
 52    * {@link org.apache.hivemind.schema.Translator}s. Loaded from the
 53    * <code>hivemind.Translators</code> configuration point;
 54    */
 55    private Map _translatorClasses = new HashMap();
 56   
 57    private Map _translatorsCache = new HashMap();
 58   
 59    private boolean _translatorsLoaded;
 60   
 61  146 public TranslatorManager(RegistryInfrastructure registry, ErrorHandler errorHandler)
 62    {
 63  146 _registry = registry;
 64  146 _errorHandler = errorHandler;
 65   
 66    // Seed the basic translators used to "bootstrap" the
 67    // processing of the hivemind.Translators configuration point.
 68   
 69  146 _translatorsCache.put("class", new ClassTranslator());
 70  146 _translatorsCache.put("service", new ServiceTranslator());
 71  146 _translatorsCache.put("smart", new SmartTranslator());
 72  146 _translatorsCache.put("instance", new InstanceTranslator());
 73   
 74    // smart may take an initializer, so we need to put it into the classes as
 75    // well.
 76   
 77  146 _translatorClasses.put("smart", SmartTranslator.class);
 78   
 79    }
 80   
 81  8884 public synchronized Translator getTranslator(String constructor)
 82    {
 83    // The cache is preloaded with the hardcoded translators.
 84   
 85  8884 if (!_translatorsLoaded && !_translatorsCache.containsKey(constructor))
 86  123 loadTranslators();
 87   
 88  8884 Translator result = (Translator) _translatorsCache.get(constructor);
 89   
 90  8884 if (result == null)
 91    {
 92  254 result = constructTranslator(constructor);
 93  253 _translatorsCache.put(constructor, result);
 94    }
 95   
 96  8883 return result;
 97    }
 98   
 99  254 private Translator constructTranslator(String constructor)
 100    {
 101  254 String name = constructor;
 102  254 String initializer = null;
 103   
 104  254 int commax = constructor.indexOf(',');
 105   
 106  254 if (commax > 0)
 107    {
 108  123 name = constructor.substring(0, commax);
 109  123 initializer = constructor.substring(commax + 1);
 110    }
 111   
 112  254 Class translatorClass = findTranslatorClass(name);
 113   
 114    // TODO: check for null class, meaning that the translator is a service.
 115   
 116  253 return createTranslator(translatorClass, initializer);
 117    }
 118   
 119  253 private Translator createTranslator(Class translatorClass, String initializer)
 120    {
 121  253 try
 122    {
 123   
 124  253 if (initializer == null)
 125  130 return (Translator) translatorClass.newInstance();
 126   
 127  123 Constructor c = translatorClass.getConstructor(new Class[]
 128    { String.class });
 129   
 130  123 return (Translator) c.newInstance(new Object[]
 131    { initializer });
 132    }
 133    catch (Exception ex)
 134    {
 135  0 throw new ApplicationRuntimeException(ImplMessages.translatorInstantiationFailure(
 136    translatorClass,
 137    ex), ex);
 138    }
 139    }
 140   
 141  254 private Class findTranslatorClass(String translatorName)
 142    {
 143  254 Class result = (Class) _translatorClasses.get(translatorName);
 144   
 145  254 if (result == null)
 146  1 throw new ApplicationRuntimeException(ImplMessages.unknownTranslatorName(
 147    translatorName,
 148    TRANSLATORS_CONFIGURATION_ID));
 149   
 150  253 return result;
 151    }
 152   
 153  123 private void loadTranslators()
 154    {
 155    // Prevent endless recursion!
 156   
 157  123 _translatorsLoaded = true;
 158   
 159  123 List contributions = _registry.getConfiguration(TRANSLATORS_CONFIGURATION_ID, null);
 160   
 161  123 Map locations = new HashMap();
 162  123 locations.put("class", null);
 163   
 164  123 Iterator i = contributions.iterator();
 165  123 while (i.hasNext())
 166    {
 167  1465 TranslatorContribution c = (TranslatorContribution) i.next();
 168   
 169  1465 String name = c.getName();
 170  1465 Location oldLocation = (Location) locations.get(name);
 171   
 172  1465 if (oldLocation != null)
 173    {
 174  0 _errorHandler.error(LOG, ImplMessages.duplicateTranslatorName(name, oldLocation), c
 175    .getLocation(), null);
 176   
 177  0 continue;
 178    }
 179   
 180  1465 locations.put(name, c.getLocation());
 181   
 182  1465 Translator t = c.getTranslator();
 183   
 184  1465 if (t != null)
 185    {
 186  122 _translatorsCache.put(name, t);
 187  122 continue;
 188    }
 189   
 190  1343 Class tClass = c.getTranslatorClass();
 191   
 192  1343 if (tClass == null)
 193    {
 194  1 _errorHandler.error(
 195    LOG,
 196    ImplMessages.incompleteTranslator(c),
 197    c.getLocation(),
 198    null);
 199  1 continue;
 200    }
 201   
 202  1342 _translatorClasses.put(name, tClass);
 203    }
 204   
 205    }
 206   
 207    }