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.TranslatorManager; 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 38 public class TranslatorManagerImpl implements TranslatorManager 39 { 40 static final Log LOG = LogFactory.getLog(TranslatorManagerImpl.class); 41 42 public static final String TRANSLATORS_CONFIGURATION_ID = "hivemind.Translators"; 43 44 private ErrorHandler _errorHandler; 45 46 51 private Map _translatorClasses = new HashMap (); 52 53 private Map _translatorsCache = new HashMap (); 54 55 private List _contributedTranslators; 56 57 private boolean _translatorsLoaded; 58 59 public TranslatorManagerImpl(List translators) 60 { 61 this(translators, new DefaultErrorHandler()); 62 } 63 64 public TranslatorManagerImpl(List translators, ErrorHandler errorHandler) 65 { 66 _contributedTranslators = translators; 67 _errorHandler = errorHandler; 68 69 72 _translatorsCache.put("class", new ClassTranslator()); 73 _translatorsCache.put("service", new ServiceTranslator()); 74 _translatorsCache.put("smart", new SmartTranslator()); 75 _translatorsCache.put("instance", new InstanceTranslator()); 76 77 80 _translatorClasses.put("smart", SmartTranslator.class); 81 82 } 83 84 87 public synchronized Translator getTranslator(String constructor) 88 { 89 91 if (!_translatorsLoaded && !_translatorsCache.containsKey(constructor)) 92 loadContributedTranslators(); 93 94 Translator result = (Translator) _translatorsCache.get(constructor); 95 96 if (result == null) 97 { 98 result = constructTranslator(constructor); 99 _translatorsCache.put(constructor, result); 100 } 101 102 return result; 103 } 104 105 private Translator constructTranslator(String constructor) 106 { 107 String name = constructor; 108 String initializer = null; 109 110 int commax = constructor.indexOf(','); 111 112 if (commax > 0) 113 { 114 name = constructor.substring(0, commax); 115 initializer = constructor.substring(commax + 1); 116 } 117 118 Class translatorClass = findTranslatorClass(name); 119 120 122 return createTranslator(translatorClass, initializer); 123 } 124 125 private Translator createTranslator(Class translatorClass, String initializer) 126 { 127 try 128 { 129 130 if (initializer == null) 131 return (Translator) translatorClass.newInstance(); 132 133 Constructor c = translatorClass.getConstructor(new Class [] 134 { String .class }); 135 136 return (Translator) c.newInstance(new Object [] 137 { initializer }); 138 } 139 catch (Exception ex) 140 { 141 throw new ApplicationRuntimeException(XmlImplMessages.translatorInstantiationFailure( 142 translatorClass, 143 ex), ex); 144 } 145 } 146 147 private Class findTranslatorClass(String translatorName) 148 { 149 Class result = (Class ) _translatorClasses.get(translatorName); 150 151 if (result == null) 152 throw new ApplicationRuntimeException(XmlImplMessages.unknownTranslatorName( 153 translatorName, 154 TRANSLATORS_CONFIGURATION_ID)); 155 156 return result; 157 } 158 159 164 private void loadContributedTranslators() 165 { 166 168 _translatorsLoaded = true; 169 170 Map locations = new HashMap (); 171 locations.put("class", null); 172 173 Iterator i = _contributedTranslators.iterator(); 174 while (i.hasNext()) 175 { 176 TranslatorContribution c = (TranslatorContribution) i.next(); 177 178 String name = c.getName(); 179 Location oldLocation = (Location) locations.get(name); 180 181 if (oldLocation != null) 182 { 183 _errorHandler.error(LOG, XmlImplMessages.duplicateTranslatorName(name, oldLocation), c 184 .getLocation(), null); 185 186 continue; 187 } 188 189 locations.put(name, c.getLocation()); 190 191 Translator t = c.getTranslator(); 192 193 if (t != null) 194 { 195 _translatorsCache.put(name, t); 196 continue; 197 } 198 199 Class tClass = c.getTranslatorClass(); 200 201 if (tClass == null) 202 { 203 _errorHandler.error( 204 LOG, 205 XmlImplMessages.incompleteTranslator(c), 206 c.getLocation(), 207 null); 208 continue; 209 } 210 211 _translatorClasses.put(name, tClass); 212 } 213 214 } 215 216 } | Popular Tags |