View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.resources.impl.bundle;
17  
18  import java.util.Enumeration;
19  import java.util.HashSet;
20  import java.util.Locale;
21  import java.util.MissingResourceException;
22  import java.util.ResourceBundle;
23  import java.util.Set;
24  
25  import net.sf.jguiraffe.resources.ResourceGroup;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * <p>
32   * A specialized implementation of the {@code ResourceGroup} interface that
33   * is backed by a {@code java.util.ResourceBundle}.
34   * </p>
35   * <p>
36   * The methods required by the {@code ResourceGroup} interface are
37   * delegated to the internally managed resource bundle. During construction this
38   * bundle is loaded for the specified base name and locale.
39   * </p>
40   *
41   * @author Oliver Heger
42   * @version $Id: BundleResourceGroup.java 211 2012-07-10 19:49:13Z oheger $
43   */
44  class BundleResourceGroup implements ResourceGroup
45  {
46      /** The logger. */
47      private final Log log = LogFactory.getLog(getClass());
48  
49      /** Stores the bundle this implementation is based on. */
50      private final ResourceBundle bundle;
51  
52      /** Stores the name of this group. */
53      private final String name;
54  
55      /** Stores the locale of this group. */
56      private final Locale locale;
57  
58      /**
59       * Creates a new instance of {@code BundleResourceGroup} and
60       * initializes it.
61       *
62       * @param name the group's name
63       * @param locale the locale
64       * @param cl the class loader for resolving the bundle
65       * @throws java.util.MissingResourceException if the bundle cannot be found
66       */
67      public BundleResourceGroup(String name, Locale locale, ClassLoader cl)
68      {
69          this.name = name;
70          this.locale = locale;
71          bundle = initBundle(name, locale, cl);
72      }
73  
74      /**
75       * Returns the bundle that is wrapped by this resource group.
76       *
77       * @return the underlying bundle
78       */
79      public ResourceBundle getBundle()
80      {
81          return bundle;
82      }
83  
84      /**
85       * Returns the name of this resource group.
86       *
87       * @return the name of this group
88       */
89      public Object getName()
90      {
91          return name;
92      }
93  
94      /**
95       * Returns a set with all keys contained in this resource group.
96       *
97       * @return a set with the defined keys
98       */
99      public Set<Object> getKeys()
100     {
101         Set<Object> result = new HashSet<Object>();
102         for (Enumeration<String> en = getBundle().getKeys(); en
103                 .hasMoreElements();)
104         {
105             result.add(en.nextElement());
106         }
107 
108         return result;
109     }
110 
111     /**
112      * Returns the locale of this group.
113      *
114      * @return the locale
115      */
116     public Locale getLocale()
117     {
118         return locale;
119     }
120 
121     /**
122      * Returns the resource for the specified key.
123      *
124      * @param key the key
125      * @return the resource for this key
126      * @throws java.util.MissingResourceException if this key is unknown
127      * @throws IllegalArgumentException for a <b>null</b> key
128      */
129     public Object getResource(Object key)
130     {
131         if (key == null)
132         {
133             throw new IllegalArgumentException("Resource key must not be null!");
134         }
135 
136         return getBundle().getObject(key.toString());
137     }
138 
139     /**
140      * Initializes the specified resource bundle. This method tries to load the
141      * underlying resource bundle. First the provided class loader is used. If
142      * this fails, a 2nd attempt is made with the class loader which loaded this
143      * class.
144      *
145      * @param name the bundle's base name
146      * @param locale the locale
147      * @param cl the class loader for resolving the bundle
148      * @return the bundle instance
149      * @throws java.util.MissingResourceException if the bundle cannot be found
150      */
151     private ResourceBundle initBundle(String name, Locale locale, ClassLoader cl)
152     {
153         try
154         {
155             return ResourceBundle.getBundle(name, locale, cl);
156         }
157         catch (MissingResourceException mrex)
158         {
159             if (log.isInfoEnabled())
160             {
161                 log.info("Could not load resource bundle '" + name
162                         + "' using default class loader.");
163             }
164             return ResourceBundle.getBundle(name, locale);
165         }
166     }
167 }