KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > i18n > BundleSelector


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.i18n;
9
10 import java.util.Map JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.LinkedList JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.Iterator JavaDoc;
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 /**
32  * This is the method for getting instances of ResourceBundles.
33  *
34  * @author <a HREF="mailto:neeme@one.lv">Neeme Praks</a>
35  * @author <a HREF="mailto:mengelhart@earthtrip.com">Mike Engelhart</a>
36  * @version $Id: BundleSelector.java,v 1.10 2002/01/02 19:04:56 neeme Exp $
37  */

38
39 public class BundleSelector extends ExcaliburComponentSelector {
40
41     /**
42      * The role implemented by an <code>BundleSelector</code>.
43      */

44     public static String JavaDoc ROLE = "org.apache.avalon.excalibur.i18n.BundleSelector";
45
46     /** Component Manager */
47     protected ComponentManager manager = null;
48
49     public void compose(ComponentManager manager) {
50         this.manager = manager;
51     }
52
53     /** Cache for the names of the bundles that were not found */
54     protected Map JavaDoc cacheNotFound = new HashMap JavaDoc();
55     protected Map JavaDoc cache = new HashMap JavaDoc();
56
57     /** Matchers */
58     private Configuration[] matchers = null;
59
60     /** Default bundle type */
61     private String JavaDoc defaultType = null;
62
63     /**
64      * Configure the component.
65      *
66      * @param configuration the configuration
67      */

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     /**
74      * Select a bundle based on bundle name and locale.
75      *
76      * @param name bundle name
77      * @param locale locale
78      * @param cacheAtStartup cache all the keys when constructing?
79      * @return the bundle
80      * @exception ComponentException if a bundle is not found
81      */

82     public Component select(Object JavaDoc 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     /**
90      * Select a bundle based on bundle name and locale name.
91      *
92      * @param name bundle name
93      * @param localeName locale name
94      * @return the bundle
95      * @exception ComponentException if a bundle is not found
96      */

97     public Component select(String JavaDoc name, String JavaDoc localeName) throws ComponentException {
98         return select(new ConfigurableBundleInfo(name, new Locale JavaDoc(localeName, localeName)));
99     }
100
101     /**
102      * Select a bundle based on bundle name and locale.
103      *
104      * @param name bundle name
105      * @param locale locale
106      * @param cacheAtStartup cache all the keys when constructing?
107      * @return the bundle
108      * @exception ComponentException if a bundle is not found
109      */

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 JavaDoc 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     /**
174      * Selects a bundle from the cache.
175      *
176      * @param fileName file name of the bundle
177      * @return the cached bundle; null, if not found
178      */

179     protected Component selectCached(BundleInfo bundleInfo) {
180         return (Component) cache.get(bundleInfo);
181         /*
182         Component bundle = null;
183         try {
184             bundle = super.select(bundleInfo);
185             if (getLogger().isDebugEnabled()) getLogger().debug("returning from cache: " + bundleInfo);
186         }
187         catch (ComponentException e) {
188             if (getLogger().isDebugEnabled()) getLogger().debug("not found in cache: " + bundleInfo);
189         }
190         return bundle;
191         */

192     }
193
194     /**
195      * Checks if the bundle is in the &quot;not-found&quot; cache.
196      *
197      * @param fileName file name of the bundle
198      * @return true, if the bundle wasn't found already before;
199      * otherwise, false.
200      */

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     /**
213      * Checks if the bundle is in the &quot;not-found&quot; cache.
214      *
215      * @param fileName file name of the bundle
216      * @return true, if the bundle wasn't found already before;
217      * otherwise, false.
218      */

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             //super.addComponentInstance(bundleInfo, (Component) bundle);
227
cache.put(bundleInfo, (Component) bundle);
228         }
229     }
230
231 }
232
Popular Tags