1 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 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 55 private Map _translatorClasses = new HashMap (); 56 57 private Map _translatorsCache = new HashMap (); 58 59 private boolean _translatorsLoaded; 60 61 public TranslatorManager(RegistryInfrastructure registry, ErrorHandler errorHandler) 62 { 63 _registry = registry; 64 _errorHandler = errorHandler; 65 66 69 _translatorsCache.put("class", new ClassTranslator()); 70 _translatorsCache.put("service", new ServiceTranslator()); 71 _translatorsCache.put("smart", new SmartTranslator()); 72 _translatorsCache.put("instance", new InstanceTranslator()); 73 74 77 _translatorClasses.put("smart", SmartTranslator.class); 78 79 } 80 81 public synchronized Translator getTranslator(String constructor) 82 { 83 85 if (!_translatorsLoaded && !_translatorsCache.containsKey(constructor)) 86 loadTranslators(); 87 88 Translator result = (Translator) _translatorsCache.get(constructor); 89 90 if (result == null) 91 { 92 result = constructTranslator(constructor); 93 _translatorsCache.put(constructor, result); 94 } 95 96 return result; 97 } 98 99 private Translator constructTranslator(String constructor) 100 { 101 String name = constructor; 102 String initializer = null; 103 104 int commax = constructor.indexOf(','); 105 106 if (commax > 0) 107 { 108 name = constructor.substring(0, commax); 109 initializer = constructor.substring(commax + 1); 110 } 111 112 Class translatorClass = findTranslatorClass(name); 113 114 116 return createTranslator(translatorClass, initializer); 117 } 118 119 private Translator createTranslator(Class translatorClass, String initializer) 120 { 121 try 122 { 123 124 if (initializer == null) 125 return (Translator) translatorClass.newInstance(); 126 127 Constructor c = translatorClass.getConstructor(new Class [] 128 { String .class }); 129 130 return (Translator) c.newInstance(new Object [] 131 { initializer }); 132 } 133 catch (Exception ex) 134 { 135 throw new ApplicationRuntimeException(ImplMessages.translatorInstantiationFailure( 136 translatorClass, 137 ex), ex); 138 } 139 } 140 141 private Class findTranslatorClass(String translatorName) 142 { 143 Class result = (Class ) _translatorClasses.get(translatorName); 144 145 if (result == null) 146 throw new ApplicationRuntimeException(ImplMessages.unknownTranslatorName( 147 translatorName, 148 TRANSLATORS_CONFIGURATION_ID)); 149 150 return result; 151 } 152 153 private void loadTranslators() 154 { 155 157 _translatorsLoaded = true; 158 159 List contributions = _registry.getConfiguration(TRANSLATORS_CONFIGURATION_ID, null); 160 161 Map locations = new HashMap (); 162 locations.put("class", null); 163 164 Iterator i = contributions.iterator(); 165 while (i.hasNext()) 166 { 167 TranslatorContribution c = (TranslatorContribution) i.next(); 168 169 String name = c.getName(); 170 Location oldLocation = (Location) locations.get(name); 171 172 if (oldLocation != null) 173 { 174 _errorHandler.error(LOG, ImplMessages.duplicateTranslatorName(name, oldLocation), c 175 .getLocation(), null); 176 177 continue; 178 } 179 180 locations.put(name, c.getLocation()); 181 182 Translator t = c.getTranslator(); 183 184 if (t != null) 185 { 186 _translatorsCache.put(name, t); 187 continue; 188 } 189 190 Class tClass = c.getTranslatorClass(); 191 192 if (tClass == null) 193 { 194 _errorHandler.error( 195 LOG, 196 ImplMessages.incompleteTranslator(c), 197 c.getLocation(), 198 null); 199 continue; 200 } 201 202 _translatorClasses.put(name, tClass); 203 } 204 205 } 206 207 } | Popular Tags |