1 8 package org.apache.avalon.excalibur.i18n; 9 10 import java.util.Map ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.LinkedList ; 14 import java.util.Locale ; 15 import java.util.Iterator ; 16 17 import org.apache.avalon.excalibur.component.ExcaliburComponentSelector; 18 import org.apache.avalon.framework.component.Component; 19 import org.apache.avalon.framework.component.Composable; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.framework.component.ComponentException; 22 import org.apache.avalon.framework.component.ComponentSelector; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 import org.apache.avalon.framework.logger.LogEnabled; 28 import org.apache.avalon.framework.logger.LogKitLogger; 29 import org.apache.avalon.framework.activity.Initializable; 30 31 38 39 public class BundleSelector extends ExcaliburComponentSelector { 40 41 44 public static String ROLE = "org.apache.avalon.excalibur.i18n.BundleSelector"; 45 46 47 protected ComponentManager manager = null; 48 49 public void compose(ComponentManager manager) { 50 this.manager = manager; 51 } 52 53 54 protected Map cacheNotFound = new HashMap (); 55 protected Map cache = new HashMap (); 56 57 58 private Configuration[] matchers = null; 59 60 61 private String defaultType = null; 62 63 68 public void configure(Configuration configuration) throws ConfigurationException { 69 if (matchers == null) matchers = configuration.getChildren("bundle"); 70 if (defaultType == null) defaultType = configuration.getAttribute("default"); 71 } 72 73 82 public Component select(Object hint) throws ComponentException { 83 Component bundle = select((BundleInfo) hint); 84 if (bundle == null) 85 throw new ComponentException("Unable to locate bundle: " + hint); 86 return bundle; 87 } 88 89 97 public Component select(String name, String localeName) throws ComponentException { 98 return select(new ConfigurableBundleInfo(name, new Locale (localeName, localeName))); 99 } 100 101 110 private Component select(BundleInfo bundleInfo) { 111 if (getLogger().isDebugEnabled()) getLogger().debug("_getBundle: " + bundleInfo); 112 Bundle bundle = (Bundle) selectCached(bundleInfo); 113 if (bundle == null && !isNotFoundBundle(bundleInfo)) { 114 if (getLogger().isDebugEnabled()) getLogger().debug("not found in cache, loading: " + bundleInfo); 115 synchronized(this) { 116 bundle = (Bundle) selectCached(bundleInfo); 117 if (bundle == null && !isNotFoundBundle(bundleInfo)) { 118 if (getLogger().isDebugEnabled()) getLogger().debug("synchronized: not found in cache, loading: " + bundleInfo); 119 bundle = loadBundle(bundleInfo); 120 BundleInfo parentBundleInfo = bundleInfo.getParent(); 121 while (bundle == null && parentBundleInfo != null) { 122 if (getLogger().isDebugEnabled()) getLogger().debug("synchronized: still not found, trying parent: " + parentBundleInfo); 123 bundle = loadBundle(parentBundleInfo); 124 updateCache(parentBundleInfo, bundle); 125 parentBundleInfo = parentBundleInfo.getParent(); 126 } 127 updateCache(bundleInfo, bundle); 128 } 129 } 130 } 131 return (Component) bundle; 132 } 133 134 private Bundle loadBundle(BundleInfo bundleInfo) { 135 ComponentSelector matcherSelector = null; 136 ComponentSelector bundleFactorySelector = null; 137 BundleFactory bundleFactory = null; 138 Bundle bundle = null; 139 try { 140 matcherSelector = (ComponentSelector) manager.lookup(BundleMatcher.ROLE + "Selector"); 141 String type = null; 142 for (int i = 0; i < matchers.length; i++) { 143 BundleMatcher matcher = null; 144 try { 145 matcher = (BundleMatcher) matcherSelector.select(matchers[i].getAttribute("matcher")); 146 type = matcher.getType(bundleInfo); 147 matcherSelector.release((Component) matcher); 148 } catch (ComponentException e) { 149 getLogger().error("Error while matching bundle!", e); 150 } finally { 151 if (matcher != null) matcherSelector.release((Component) matcher); 152 } 153 if (type != null) break; 154 } 155 if (type == null) type = defaultType; 156 bundleFactorySelector = (ComponentSelector) manager.lookup(BundleFactory.ROLE + "Selector"); 157 bundleFactory = (BundleFactory) bundleFactorySelector.select(type); 158 bundle = bundleFactory.createInstance(bundleInfo); 159 } catch (ComponentException e) { 160 getLogger().error("Error while matching bundle!", e); 161 } catch (ConfigurationException e) { 162 getLogger().error("Error while matching bundle!", e); 163 } finally { 164 if (matcherSelector != null) manager.release(matcherSelector); 165 if (bundleFactorySelector != null) { 166 if (bundleFactory != null) bundleFactorySelector.release((Component) bundleFactory); 167 manager.release(bundleFactorySelector); 168 } 169 } 170 return bundle; 171 } 172 173 179 protected Component selectCached(BundleInfo bundleInfo) { 180 return (Component) cache.get(bundleInfo); 181 192 } 193 194 201 protected boolean isNotFoundBundle(BundleInfo bundleInfo) { 202 BundleInfo result = (BundleInfo)(cacheNotFound.get(bundleInfo)); 203 if (result != null) { 204 if (getLogger().isDebugEnabled()) getLogger().debug("returning from not_found_cache: " + bundleInfo); 205 } 206 else { 207 if (getLogger().isDebugEnabled()) getLogger().debug("not found in not_found_cache: " + bundleInfo); 208 } 209 return result != null; 210 } 211 212 219 protected void updateCache(BundleInfo bundleInfo, Bundle bundle) { 220 if (bundle == null) { 221 if (getLogger().isDebugEnabled()) getLogger().debug("updating not_found_cache: " + bundleInfo); 222 cacheNotFound.put(bundleInfo, bundleInfo); 223 } 224 else { 225 if (getLogger().isDebugEnabled()) getLogger().debug("updating cache: " + bundleInfo); 226 cache.put(bundleInfo, (Component) bundle); 228 } 229 } 230 231 } 232 | Popular Tags |