KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > resources > BundleHandler


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.resources;
33
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.MissingResourceException JavaDoc;
37 import java.util.ResourceBundle JavaDoc;
38
39 import org.hsqldb.lib.HashMap;
40 import org.hsqldb.lib.HsqlArrayList;
41
42 /**
43  * A ResourceBundle helper class. <p>
44  *
45  * Allows clients to get/set locale and get at localized resource bundle
46  * content in a resource path independent manner, without having to worry
47  * about handling exception states or deal directly with ResourceBundle
48  * object instances. Instead, clients recieve numeric handles to the
49  * underlying objects. Rather than causing exception states, missing or
50  * inaccessible resources and underlying MissingResource and NullPointer
51  * exceptions result in null return values when attempting to retrieve a
52  * resource. <p>
53  *
54  * @author boucherb@users
55  * @version 1.7.2
56  * @since 1.7.2
57  */

58 public final class BundleHandler {
59
60     /** Used to synchronize access */
61     private static final Object JavaDoc mutex = new Object JavaDoc();
62
63     /** The Locale used internally to fetch resource bundles. */
64     private static Locale JavaDoc locale = Locale.getDefault();
65
66     /** Map: Integer object handle => <code>ResourceBundle</code> object. */
67     private static HashMap bundleHandleMap = new HashMap();
68
69     /** List whose elements are <code>ResourceBundle</code> objects */
70     private static HsqlArrayList bundleList = new HsqlArrayList();
71
72     /**
73      * The resource path prefix of the <code>ResourceBundle</code> objects
74      * handled by this class.
75      */

76     private static final String JavaDoc prefix = "org/hsqldb/resources/";
77
78     /** JDK 1.1 compliance */
79     private static final Method JavaDoc newGetBundleMethod = getNewGetBundleMethod();
80
81     /** Pure utility class: external construction disabled. */
82     private BundleHandler() {}
83
84     /**
85      * Getter for property locale. <p>
86      *
87      * @return Value of property locale.
88      */

89     public static Locale JavaDoc getLocale() {
90
91         synchronized (mutex) {
92             return locale;
93         }
94     }
95
96     /**
97      * Setter for property locale. <p>
98      *
99      * @param l the new locale
100      * @throws IllegalArgumentException when the new locale is null
101      */

102     public static void setLocale(Locale JavaDoc l) throws IllegalArgumentException JavaDoc {
103
104         synchronized (mutex) {
105             if (l == null) {
106                 throw new IllegalArgumentException JavaDoc("null locale");
107             }
108
109             locale = l;
110         }
111     }
112
113     /**
114      * Retrieves an <code>int</code> handle to the <code>ResourceBundle</code>
115      * object corresponding to the specified name and current
116      * <code>Locale</code>, using the specified <code>ClassLoader</code>. <p>
117      *
118      * @return <code>int</code> handle to the <code>ResourceBundle</code>
119      * object corresponding to the specified name and
120      * current <code>Locale</code>, or -1 if no such bundle
121      * can be found
122      * @param cl The ClassLoader to use in the search
123      * @param name of the desired bundle
124      */

125     public static int getBundleHandle(String JavaDoc name, ClassLoader JavaDoc cl) {
126
127         Integer JavaDoc bundleHandle;
128         ResourceBundle JavaDoc bundle;
129         String JavaDoc bundleName;
130         String JavaDoc bundleKey;
131
132         bundleName = prefix + name;
133
134         synchronized (mutex) {
135             bundleKey = locale.toString() + bundleName;
136             bundleHandle = (Integer JavaDoc) bundleHandleMap.get(bundleKey);
137
138             if (bundleHandle == null) {
139                 try {
140                     bundle = getBundle(bundleName, locale, cl);
141
142                     bundleList.add(bundle);
143
144                     bundleHandle = new Integer JavaDoc(bundleList.size() - 1);
145
146                     bundleHandleMap.put(bundleKey, bundleHandle);
147                 } catch (Exception JavaDoc e) {
148
149                     //e.printStackTrace();
150
}
151             }
152         }
153
154         return bundleHandle == null ? -1
155                                     : bundleHandle.intValue();
156     }
157
158     /**
159      * Retrieves, from the <code>ResourceBundle</code> object corresponding
160      * to the specified handle, the <code>String</code> value corresponding
161      * to the specified key. <code>null</code> is retrieved if either there
162      * is no <code>ResourceBundle</code> object for the handle or there is no
163      * <code>String</code> value for the specified key. <p>
164      *
165      * @param handle an <code>int</code> handle to a
166      * <code>ResourceBundle</code> object
167      * @param key A <code>String</code> key to a <code>String</code> value
168      * @return The String value correspoding to the specified handle and key.
169      */

170     public static String JavaDoc getString(int handle, String JavaDoc key) {
171
172         ResourceBundle JavaDoc bundle;
173         String JavaDoc s;
174
175         synchronized (mutex) {
176             if (handle < 0 || handle >= bundleList.size() || key == null) {
177                 bundle = null;
178             } else {
179                 bundle = (ResourceBundle JavaDoc) bundleList.get(handle);
180             }
181         }
182
183         if (bundle == null) {
184             s = null;
185         } else {
186             try {
187                 s = bundle.getString(key);
188             } catch (Exception JavaDoc e) {
189                 s = null;
190             }
191         }
192
193         return s;
194     }
195
196     /**
197      * One-shot initialization of JDK 1.2+ ResourceBundle.getBundle() method
198      * having ClassLoader in the signature.
199      */

200     private static Method JavaDoc getNewGetBundleMethod() {
201
202         Class JavaDoc clazz;
203         Class JavaDoc[] args;
204
205         clazz = ResourceBundle JavaDoc.class;
206         args = new Class JavaDoc[] {
207             String JavaDoc.class, Locale JavaDoc.class, ClassLoader JavaDoc.class
208         };
209
210         try {
211             return clazz.getMethod("getBundle", args);
212         } catch (Exception JavaDoc e) {
213             return null;
214         }
215     }
216
217     /**
218      * Retrieves a resource bundle using the specified base name, locale, and
219      * class loader. This is a JDK 1.1 compliant substitution for the
220      * ResourceBundle method with the same name and signature. If there
221      * is a problem using the JDK 1.2 functionality (the class loader is
222      * specified non-null and the underlying method is not available or there
223      * is a security exception, etc.), then the behaviour reverts to that
224      * of JDK 1.1.
225      *
226      * @param name the base name of the resource bundle, a fully
227      * qualified class name
228      * @param locale the locale for which a resource bundle is desired
229      * @param cl the class loader from which to load the resource bundle
230      */

231     public static ResourceBundle JavaDoc getBundle(String JavaDoc name, Locale JavaDoc locale,
232                                            ClassLoader JavaDoc cl)
233                                            throws NullPointerException JavaDoc,
234                                                MissingResourceException JavaDoc {
235
236         if (cl == null) {
237             return ResourceBundle.getBundle(name, locale);
238         } else if (newGetBundleMethod == null) {
239             return ResourceBundle.getBundle(name, locale);
240         } else {
241             try {
242                 return (ResourceBundle JavaDoc) newGetBundleMethod.invoke(null,
243                         new Object JavaDoc[] {
244                     name, locale, cl
245                 });
246             } catch (Exception JavaDoc e) {
247                 return ResourceBundle.getBundle(name, locale);
248             }
249         }
250     }
251 }
252
Popular Tags