View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.om.impl;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Locale;
26  import java.util.MissingResourceException;
27  import java.util.ResourceBundle;
28  
29  import org.apache.jetspeed.om.common.MutableLanguage;
30  import org.apache.jetspeed.om.common.Support;
31  import org.apache.jetspeed.util.JetspeedLocale;
32  import org.apache.pluto.om.common.Language;
33  import org.apache.pluto.om.common.LanguageSet;
34  
35  /***
36   * 
37   * LanguageSetImpl
38   * 
39   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
40   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
41   * @version $Id: LanguageSetImpl.java 553014 2007-07-03 23:10:53Z ate $
42   *  
43   */
44  public class LanguageSetImpl implements LanguageSet, Serializable, Support
45  {
46      private transient ClassLoader classLoader = null;
47  
48      private String resources;
49      protected Collection innerCollection;
50  
51      /***
52       * 
53       * @param wrappedSet
54       */
55      public LanguageSetImpl( Collection collection )
56      {
57          super();
58          this.innerCollection = collection;
59      }
60  
61      public LanguageSetImpl()
62      {        
63          this(Collections.synchronizedList(new ArrayList()));
64      }
65  
66      /***
67       * @see org.apache.pluto.om.common.LanguageSet#iterator()
68       */
69      public Iterator iterator()
70      {
71          return innerCollection.iterator();
72      }
73  
74      /***
75       * @see org.apache.pluto.om.common.LanguageSet#getLocales()
76       */
77      public Iterator getLocales()
78      {
79          HashSet localSet = new HashSet();
80          synchronized (innerCollection)
81          {
82              Iterator itr = innerCollection.iterator();
83              while (itr.hasNext())
84              {
85                  Language lang = (Language) itr.next();
86                  localSet.add(lang.getLocale());
87              }
88          }
89          return localSet.iterator();
90      }
91  
92      /***
93       * @see org.apache.pluto.om.common.LanguageSet#get(java.util.Locale)
94       */
95      public Language get( Locale locale )
96      {
97          LanguageImpl fallback = null;
98          synchronized(innerCollection)
99          {
100             Iterator searchItr = innerCollection.iterator();
101             while (searchItr.hasNext())
102             {
103                 LanguageImpl lang = (LanguageImpl)searchItr.next();
104     
105                 if (lang.getLocale().equals(locale))
106                 {
107                     if (resources != null && lang.getParentResourceBundle() == null)
108                     {
109                         lang.setResourceBundle(loadResourceBundle(lang.getLocale()));
110                     }
111                     return lang;
112                 }
113                 else if (lang.getLocale().getLanguage().equals(locale.getLanguage()))
114                 {
115                     fallback = lang;
116                 }
117     
118             }
119         }
120         if ( fallback == null )
121         {
122             if ( getDefaultLocale().equals(locale) )
123             {
124                 // no default language stored yet
125                 LanguageImpl defaultLanguage = new LanguageImpl();
126                 defaultLanguage.setLocale(locale);
127                 
128                 if ( resources != null )
129                 {
130                     defaultLanguage.setResourceBundle(loadResourceBundle(locale));
131                     defaultLanguage.loadDefaults();
132                     innerCollection.add(defaultLanguage);
133                     return defaultLanguage;
134                 }
135             }
136             else
137             {
138                 fallback = (LanguageImpl)get(getDefaultLocale());
139             }
140         }
141         
142         LanguageImpl language = new LanguageImpl();
143         language.setLocale(locale);
144         language.setTitle(fallback.getTitle());
145         language.setShortTitle(fallback.getShortTitle());
146         language.setKeywords(fallback.getKeywordStr());
147         if ( resources != null )
148         {
149           language.setResourceBundle(loadResourceBundle(locale));
150         }
151         language.loadDefaults();
152         innerCollection.add(language);
153         return language;
154     }
155 
156     /***
157      * @see org.apache.pluto.om.common.LanguageSet#getDefaultLocale()
158      */
159     public Locale getDefaultLocale()
160     {        
161         return JetspeedLocale.getDefaultLocale();
162     }
163 
164     public boolean add( Object o )
165     {
166         if (o instanceof Language)
167         {
168             Language language = (Language) o;
169             if (language.getLocale() == null)
170             {
171                 ((MutableLanguage) o).setLocale(getDefaultLocale());
172             }
173 
174             synchronized (innerCollection)
175             {
176                 Iterator ite = innerCollection.iterator();
177                 while (ite.hasNext())
178                 {
179                     Language lang = (Language) ite.next();
180                     if (lang.equals(language))
181                     {
182                         innerCollection.remove(lang);
183                         return innerCollection.add(o);
184                     }
185                 }
186             }
187             return innerCollection.add(o);
188         }
189         return false;
190     }
191 
192     /***
193      * @return
194      */
195     public Collection getInnerCollection()
196     {
197         return innerCollection;
198     }
199 
200     /***
201      * @param collection
202      */
203     public void setInnerCollection( Collection collection )
204     {
205         innerCollection = collection;
206     }
207 
208     public int size()
209     {
210         return innerCollection.size();
211     }
212 
213     /***
214      * @param string
215      */
216     public void setResources( String string )
217     {
218         resources = string;
219     }
220 
221     /*
222      * (non-Javadoc)
223      * 
224      * @see org.apache.jetspeed.om.common.Support#postLoad(java.lang.Object)
225      */
226     public void postLoad( Object parameter ) throws Exception
227     {
228         Iterator iter = ((Collection) parameter).iterator();
229         LanguageImpl language;
230         while (iter.hasNext())
231         {
232             language = (LanguageImpl)get((Locale)iter.next());
233             // load available resource bundle values
234             language.loadDefaults();
235         }
236         // ensure default locale language is created and available resource bundle values are loaded
237         language = (LanguageImpl)get(getDefaultLocale());
238         language.loadDefaults();
239     }
240 
241     protected ResourceBundle loadResourceBundle( Locale locale )
242     {
243         ResourceBundle resourceBundle = null;
244         try
245         {
246             if (resources != null)
247             {
248                 if (classLoader != null)
249                 {
250                     resourceBundle = ResourceBundle.getBundle(resources, locale, classLoader);
251                 }
252                 else
253                 {
254                     resourceBundle = ResourceBundle.getBundle(resources, locale, Thread.currentThread()
255                             .getContextClassLoader());
256                 }
257             }
258         }
259         catch (MissingResourceException x)
260         {
261             return null;
262         }
263         return resourceBundle;
264     }
265 
266     /***
267      * 
268      * Sets Portlet Class Loader
269      * 
270      * @param loader
271      */
272     public void setClassLoader( ClassLoader loader )
273     {
274         classLoader = loader;
275     }
276 }