001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.om.impl;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Locale;
026: import java.util.MissingResourceException;
027: import java.util.ResourceBundle;
028:
029: import org.apache.jetspeed.om.common.MutableLanguage;
030: import org.apache.jetspeed.om.common.Support;
031: import org.apache.jetspeed.util.JetspeedLocale;
032: import org.apache.pluto.om.common.Language;
033: import org.apache.pluto.om.common.LanguageSet;
034:
035: /**
036: *
037: * LanguageSetImpl
038: *
039: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
040: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
041: * @version $Id: LanguageSetImpl.java 553014 2007-07-03 23:10:53Z ate $
042: *
043: */
044: public class LanguageSetImpl implements LanguageSet, Serializable,
045: Support {
046: private transient ClassLoader classLoader = null;
047:
048: private String resources;
049: protected Collection innerCollection;
050:
051: /**
052: *
053: * @param wrappedSet
054: */
055: public LanguageSetImpl(Collection collection) {
056: super ();
057: this .innerCollection = collection;
058: }
059:
060: public LanguageSetImpl() {
061: this (Collections.synchronizedList(new ArrayList()));
062: }
063:
064: /**
065: * @see org.apache.pluto.om.common.LanguageSet#iterator()
066: */
067: public Iterator iterator() {
068: return innerCollection.iterator();
069: }
070:
071: /**
072: * @see org.apache.pluto.om.common.LanguageSet#getLocales()
073: */
074: public Iterator getLocales() {
075: HashSet localSet = new HashSet();
076: synchronized (innerCollection) {
077: Iterator itr = innerCollection.iterator();
078: while (itr.hasNext()) {
079: Language lang = (Language) itr.next();
080: localSet.add(lang.getLocale());
081: }
082: }
083: return localSet.iterator();
084: }
085:
086: /**
087: * @see org.apache.pluto.om.common.LanguageSet#get(java.util.Locale)
088: */
089: public Language get(Locale locale) {
090: LanguageImpl fallback = null;
091: synchronized (innerCollection) {
092: Iterator searchItr = innerCollection.iterator();
093: while (searchItr.hasNext()) {
094: LanguageImpl lang = (LanguageImpl) searchItr.next();
095:
096: if (lang.getLocale().equals(locale)) {
097: if (resources != null
098: && lang.getParentResourceBundle() == null) {
099: lang.setResourceBundle(loadResourceBundle(lang
100: .getLocale()));
101: }
102: return lang;
103: } else if (lang.getLocale().getLanguage().equals(
104: locale.getLanguage())) {
105: fallback = lang;
106: }
107:
108: }
109: }
110: if (fallback == null) {
111: if (getDefaultLocale().equals(locale)) {
112: // no default language stored yet
113: LanguageImpl defaultLanguage = new LanguageImpl();
114: defaultLanguage.setLocale(locale);
115:
116: if (resources != null) {
117: defaultLanguage
118: .setResourceBundle(loadResourceBundle(locale));
119: defaultLanguage.loadDefaults();
120: innerCollection.add(defaultLanguage);
121: return defaultLanguage;
122: }
123: } else {
124: fallback = (LanguageImpl) get(getDefaultLocale());
125: }
126: }
127:
128: LanguageImpl language = new LanguageImpl();
129: language.setLocale(locale);
130: language.setTitle(fallback.getTitle());
131: language.setShortTitle(fallback.getShortTitle());
132: language.setKeywords(fallback.getKeywordStr());
133: if (resources != null) {
134: language.setResourceBundle(loadResourceBundle(locale));
135: }
136: language.loadDefaults();
137: innerCollection.add(language);
138: return language;
139: }
140:
141: /**
142: * @see org.apache.pluto.om.common.LanguageSet#getDefaultLocale()
143: */
144: public Locale getDefaultLocale() {
145: return JetspeedLocale.getDefaultLocale();
146: }
147:
148: public boolean add(Object o) {
149: if (o instanceof Language) {
150: Language language = (Language) o;
151: if (language.getLocale() == null) {
152: ((MutableLanguage) o).setLocale(getDefaultLocale());
153: }
154:
155: synchronized (innerCollection) {
156: Iterator ite = innerCollection.iterator();
157: while (ite.hasNext()) {
158: Language lang = (Language) ite.next();
159: if (lang.equals(language)) {
160: innerCollection.remove(lang);
161: return innerCollection.add(o);
162: }
163: }
164: }
165: return innerCollection.add(o);
166: }
167: return false;
168: }
169:
170: /**
171: * @return
172: */
173: public Collection getInnerCollection() {
174: return innerCollection;
175: }
176:
177: /**
178: * @param collection
179: */
180: public void setInnerCollection(Collection collection) {
181: innerCollection = collection;
182: }
183:
184: public int size() {
185: return innerCollection.size();
186: }
187:
188: /**
189: * @param string
190: */
191: public void setResources(String string) {
192: resources = string;
193: }
194:
195: /*
196: * (non-Javadoc)
197: *
198: * @see org.apache.jetspeed.om.common.Support#postLoad(java.lang.Object)
199: */
200: public void postLoad(Object parameter) throws Exception {
201: Iterator iter = ((Collection) parameter).iterator();
202: LanguageImpl language;
203: while (iter.hasNext()) {
204: language = (LanguageImpl) get((Locale) iter.next());
205: // load available resource bundle values
206: language.loadDefaults();
207: }
208: // ensure default locale language is created and available resource bundle values are loaded
209: language = (LanguageImpl) get(getDefaultLocale());
210: language.loadDefaults();
211: }
212:
213: protected ResourceBundle loadResourceBundle(Locale locale) {
214: ResourceBundle resourceBundle = null;
215: try {
216: if (resources != null) {
217: if (classLoader != null) {
218: resourceBundle = ResourceBundle.getBundle(
219: resources, locale, classLoader);
220: } else {
221: resourceBundle = ResourceBundle.getBundle(
222: resources, locale, Thread.currentThread()
223: .getContextClassLoader());
224: }
225: }
226: } catch (MissingResourceException x) {
227: return null;
228: }
229: return resourceBundle;
230: }
231:
232: /**
233: *
234: * Sets Portlet Class Loader
235: *
236: * @param loader
237: */
238: public void setClassLoader(ClassLoader loader) {
239: classLoader = loader;
240: }
241: }
|