001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.support;
016:
017: import java.io.Serializable;
018: import java.util.ArrayList;
019: import java.util.Enumeration;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Locale;
023: import java.util.MissingResourceException;
024: import java.util.ResourceBundle;
025:
026: /**
027: * ResourceBundle that which {@link FallbackResourceBundle#handleGetObject(String)} searches
028: * for <code>key</code> from all added <code>ResourceBundles</code>.
029: *
030: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
031: */
032: public class FallbackResourceBundle extends ResourceBundle implements
033: Serializable {
034: protected Locale locale;
035: protected List resourceBundles = new ArrayList();
036:
037: /**
038: * Adds a <code>ResourceBundle</code> from which resources are searched.
039: * @param resourceBundle
040: */
041: public void addResourceBundle(ResourceBundle resourceBundle) {
042: resourceBundles.add(resourceBundle);
043:
044: if (resourceBundle instanceof LocaleAwareResourceBundle)
045: ((LocaleAwareResourceBundle) resourceBundle)
046: .setLocale(locale);
047: }
048:
049: public void clearResourceBundles() {
050: resourceBundles.clear();
051: }
052:
053: public Enumeration getKeys() {
054: return null;
055: }
056:
057: /**
058: * Returns the resource under the <code>key</code>. Resource is searched
059: * among all <code>ResourceBundles</code> this {@link FallbackResourceBundle}
060: * knows about, in same order as <code>ResourceBundles</code> were added.
061: *
062: * @return the resource under the <code>key</code>
063: */
064: protected Object handleGetObject(String key) {
065: if (locale == null)
066: setLocale(getLocale());
067:
068: Object result = null;
069:
070: for (Iterator i = resourceBundles.iterator(); i.hasNext()
071: && result == null;) {
072: try {
073: ResourceBundle currentBundle = (ResourceBundle) i
074: .next();
075: result = currentBundle.getObject(key);
076: } catch (MissingResourceException e) {
077: //Totally normal result
078: }
079: }
080:
081: return result;
082: }
083:
084: public void setLocale(Locale locale) {
085: this .locale = locale;
086:
087: for (Iterator i = resourceBundles.iterator(); i.hasNext();) {
088: ResourceBundle currentBundle = (ResourceBundle) i.next();
089: if (currentBundle instanceof LocaleAwareResourceBundle)
090: ((LocaleAwareResourceBundle) currentBundle)
091: .setLocale(locale);
092: }
093: }
094:
095: public Locale getLocale() {
096: if (locale != null)
097: return locale;
098: return super.getLocale();
099: }
100:
101: }
|