author | naoto |
Tue, 17 Jan 2017 11:34:47 -0800 | |
changeset 43182 | 66e6655abfde |
parent 42930 | b878b7b2788e |
child 43189 | d37bfddf7c38 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36511 | 2 |
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved |
|
29 |
* |
|
30 |
* The original version of this source code and documentation |
|
31 |
* is copyrighted and owned by Taligent, Inc., a wholly-owned |
|
32 |
* subsidiary of IBM. These materials are provided under terms |
|
33 |
* of a License Agreement between Taligent and Sun. This technology |
|
34 |
* is protected by multiple US and International patents. |
|
35 |
* |
|
36 |
* This notice and attribution to Taligent may not be removed. |
|
37 |
* Taligent is a registered trademark of Taligent, Inc. |
|
38 |
* |
|
39 |
*/ |
|
40 |
||
41 |
package java.util; |
|
42 |
||
43 |
import java.io.IOException; |
|
44 |
import java.io.InputStream; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
45 |
import java.io.UncheckedIOException; |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
46 |
import java.lang.ref.Reference; |
2 | 47 |
import java.lang.ref.ReferenceQueue; |
48 |
import java.lang.ref.SoftReference; |
|
49 |
import java.lang.ref.WeakReference; |
|
36511 | 50 |
import java.lang.reflect.Constructor; |
51 |
import java.lang.reflect.InvocationTargetException; |
|
52 |
import java.lang.reflect.Modifier; |
|
53 |
import java.lang.reflect.Module; |
|
2 | 54 |
import java.net.JarURLConnection; |
55 |
import java.net.URL; |
|
56 |
import java.net.URLConnection; |
|
57 |
import java.security.AccessController; |
|
58 |
import java.security.PrivilegedAction; |
|
59 |
import java.security.PrivilegedActionException; |
|
60 |
import java.security.PrivilegedExceptionAction; |
|
61 |
import java.util.concurrent.ConcurrentHashMap; |
|
62 |
import java.util.concurrent.ConcurrentMap; |
|
63 |
import java.util.jar.JarEntry; |
|
36511 | 64 |
import java.util.spi.ResourceBundleProvider; |
2 | 65 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
66 |
import jdk.internal.loader.BootLoader; |
36511 | 67 |
import jdk.internal.misc.JavaUtilResourceBundleAccess; |
68 |
import jdk.internal.misc.SharedSecrets; |
|
37363
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36511
diff
changeset
|
69 |
import jdk.internal.reflect.CallerSensitive; |
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36511
diff
changeset
|
70 |
import jdk.internal.reflect.Reflection; |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
71 |
import sun.security.action.GetPropertyAction; |
6501 | 72 |
import sun.util.locale.BaseLocale; |
73 |
import sun.util.locale.LocaleObjectCache; |
|
36511 | 74 |
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION; |
6501 | 75 |
|
2 | 76 |
|
77 |
/** |
|
78 |
* |
|
6501 | 79 |
* Resource bundles contain locale-specific objects. When your program needs a |
80 |
* locale-specific resource, a <code>String</code> for example, your program can |
|
81 |
* load it from the resource bundle that is appropriate for the current user's |
|
82 |
* locale. In this way, you can write program code that is largely independent |
|
83 |
* of the user's locale isolating most, if not all, of the locale-specific |
|
2 | 84 |
* information in resource bundles. |
85 |
* |
|
86 |
* <p> |
|
87 |
* This allows you to write programs that can: |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
88 |
* <UL> |
2 | 89 |
* <LI> be easily localized, or translated, into different languages |
90 |
* <LI> handle multiple locales at once |
|
91 |
* <LI> be easily modified later to support even more locales |
|
92 |
* </UL> |
|
93 |
* |
|
94 |
* <P> |
|
95 |
* Resource bundles belong to families whose members share a common base |
|
96 |
* name, but whose names also have additional components that identify |
|
97 |
* their locales. For example, the base name of a family of resource |
|
98 |
* bundles might be "MyResources". The family should have a default |
|
99 |
* resource bundle which simply has the same name as its family - |
|
100 |
* "MyResources" - and will be used as the bundle of last resort if a |
|
101 |
* specific locale is not supported. The family can then provide as |
|
102 |
* many locale-specific members as needed, for example a German one |
|
103 |
* named "MyResources_de". |
|
104 |
* |
|
105 |
* <P> |
|
106 |
* Each resource bundle in a family contains the same items, but the items have |
|
107 |
* been translated for the locale represented by that resource bundle. |
|
108 |
* For example, both "MyResources" and "MyResources_de" may have a |
|
109 |
* <code>String</code> that's used on a button for canceling operations. |
|
110 |
* In "MyResources" the <code>String</code> may contain "Cancel" and in |
|
111 |
* "MyResources_de" it may contain "Abbrechen". |
|
112 |
* |
|
113 |
* <P> |
|
114 |
* If there are different resources for different countries, you |
|
115 |
* can make specializations: for example, "MyResources_de_CH" contains objects for |
|
116 |
* the German language (de) in Switzerland (CH). If you want to only |
|
117 |
* modify some of the resources |
|
118 |
* in the specialization, you can do so. |
|
119 |
* |
|
120 |
* <P> |
|
121 |
* When your program needs a locale-specific object, it loads |
|
122 |
* the <code>ResourceBundle</code> class using the |
|
123 |
* {@link #getBundle(java.lang.String, java.util.Locale) getBundle} |
|
124 |
* method: |
|
125 |
* <blockquote> |
|
126 |
* <pre> |
|
127 |
* ResourceBundle myResources = |
|
128 |
* ResourceBundle.getBundle("MyResources", currentLocale); |
|
129 |
* </pre> |
|
130 |
* </blockquote> |
|
131 |
* |
|
132 |
* <P> |
|
133 |
* Resource bundles contain key/value pairs. The keys uniquely |
|
134 |
* identify a locale-specific object in the bundle. Here's an |
|
135 |
* example of a <code>ListResourceBundle</code> that contains |
|
136 |
* two key/value pairs: |
|
137 |
* <blockquote> |
|
138 |
* <pre> |
|
139 |
* public class MyResources extends ListResourceBundle { |
|
140 |
* protected Object[][] getContents() { |
|
141 |
* return new Object[][] { |
|
142 |
* // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK") |
|
143 |
* {"OkKey", "OK"}, |
|
144 |
* {"CancelKey", "Cancel"}, |
|
145 |
* // END OF MATERIAL TO LOCALIZE |
|
146 |
* }; |
|
147 |
* } |
|
148 |
* } |
|
149 |
* </pre> |
|
150 |
* </blockquote> |
|
151 |
* Keys are always <code>String</code>s. |
|
152 |
* In this example, the keys are "OkKey" and "CancelKey". |
|
153 |
* In the above example, the values |
|
154 |
* are also <code>String</code>s--"OK" and "Cancel"--but |
|
155 |
* they don't have to be. The values can be any type of object. |
|
156 |
* |
|
157 |
* <P> |
|
158 |
* You retrieve an object from resource bundle using the appropriate |
|
159 |
* getter method. Because "OkKey" and "CancelKey" |
|
160 |
* are both strings, you would use <code>getString</code> to retrieve them: |
|
161 |
* <blockquote> |
|
162 |
* <pre> |
|
163 |
* button1 = new Button(myResources.getString("OkKey")); |
|
164 |
* button2 = new Button(myResources.getString("CancelKey")); |
|
165 |
* </pre> |
|
166 |
* </blockquote> |
|
167 |
* The getter methods all require the key as an argument and return |
|
168 |
* the object if found. If the object is not found, the getter method |
|
169 |
* throws a <code>MissingResourceException</code>. |
|
170 |
* |
|
171 |
* <P> |
|
172 |
* Besides <code>getString</code>, <code>ResourceBundle</code> also provides |
|
173 |
* a method for getting string arrays, <code>getStringArray</code>, |
|
174 |
* as well as a generic <code>getObject</code> method for any other |
|
175 |
* type of object. When using <code>getObject</code>, you'll |
|
176 |
* have to cast the result to the appropriate type. For example: |
|
177 |
* <blockquote> |
|
178 |
* <pre> |
|
179 |
* int[] myIntegers = (int[]) myResources.getObject("intList"); |
|
180 |
* </pre> |
|
181 |
* </blockquote> |
|
182 |
* |
|
183 |
* <P> |
|
184 |
* The Java Platform provides two subclasses of <code>ResourceBundle</code>, |
|
185 |
* <code>ListResourceBundle</code> and <code>PropertyResourceBundle</code>, |
|
186 |
* that provide a fairly simple way to create resources. |
|
187 |
* As you saw briefly in a previous example, <code>ListResourceBundle</code> |
|
188 |
* manages its resource as a list of key/value pairs. |
|
189 |
* <code>PropertyResourceBundle</code> uses a properties file to manage |
|
190 |
* its resources. |
|
191 |
* |
|
192 |
* <p> |
|
193 |
* If <code>ListResourceBundle</code> or <code>PropertyResourceBundle</code> |
|
194 |
* do not suit your needs, you can write your own <code>ResourceBundle</code> |
|
195 |
* subclass. Your subclasses must override two methods: <code>handleGetObject</code> |
|
196 |
* and <code>getKeys()</code>. |
|
197 |
* |
|
21949
d97fd8123cbe
8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents:
21428
diff
changeset
|
198 |
* <p> |
d97fd8123cbe
8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents:
21428
diff
changeset
|
199 |
* The implementation of a {@code ResourceBundle} subclass must be thread-safe |
d97fd8123cbe
8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents:
21428
diff
changeset
|
200 |
* if it's simultaneously used by multiple threads. The default implementations |
d97fd8123cbe
8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents:
21428
diff
changeset
|
201 |
* of the non-abstract methods in this class, and the methods in the direct |
d97fd8123cbe
8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents:
21428
diff
changeset
|
202 |
* known concrete subclasses {@code ListResourceBundle} and |
d97fd8123cbe
8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents:
21428
diff
changeset
|
203 |
* {@code PropertyResourceBundle} are thread-safe. |
d97fd8123cbe
8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents:
21428
diff
changeset
|
204 |
* |
36511 | 205 |
* <h3><a name="bundleprovider">Resource Bundles in Named Modules</a></h3> |
206 |
* |
|
207 |
* When resource bundles are deployed in named modules, the following |
|
208 |
* module-specific requirements and restrictions are applied. |
|
209 |
* |
|
210 |
* <ul> |
|
211 |
* <li>Code in a named module that calls {@link #getBundle(String, Locale)} |
|
212 |
* will locate resource bundles in the caller's module (<em>caller module</em>).</li> |
|
213 |
* <li>If resource bundles are deployed in named modules separate from |
|
214 |
* the caller module, those resource bundles need to be loaded from service |
|
215 |
* providers of {@link ResourceBundleProvider}. The caller module must declare |
|
216 |
* "{@code uses}" and the service interface name is the concatenation of the |
|
217 |
* base name of the bundles and the string "{@code Provider}". The |
|
218 |
* <em>bundle provider modules</em> containing resource bundles must |
|
219 |
* declare "{@code provides}" with the service interface name and |
|
220 |
* its implementation class name. For example, if the base name is |
|
221 |
* "{@code com.example.app.MyResources}", the caller module must declare |
|
222 |
* "{@code uses com.example.app.MyResourcesProvider;}" and a module containing resource |
|
223 |
* bundles must declare "{@code provides com.example.app.MyResourcesProvider |
|
224 |
* with com.example.app.internal.MyResourcesProviderImpl;}" |
|
225 |
* where {@code com.example.app.internal.MyResourcesProviderImpl} is an |
|
226 |
* implementation class of {@code com.example.app.MyResourcesProvider}.</li> |
|
227 |
* <li>If you want to use non-standard formats in named modules, such as XML, |
|
228 |
* {@link ResourceBundleProvider} needs to be used.</li> |
|
229 |
* <li>The {@code getBundle} method with a {@code ClassLoader} may not be able to |
|
230 |
* find resource bundles using the given {@code ClassLoader} in named modules. |
|
231 |
* The {@code getBundle} method with a {@code Module} can be used, instead.</li> |
|
232 |
* <li>{@code ResourceBundle.Control} is <em>not</em> supported in named modules. |
|
233 |
* If the {@code getBundle} method with a {@code ResourceBundle.Control} is called |
|
234 |
* in a named module, the method will throw an {@code UnsupportedOperationException}. |
|
235 |
* </li> |
|
236 |
* </ul> |
|
237 |
* |
|
238 |
* <h3><a name="RBP_support">ResourceBundleProvider Service Providers</a></h3> |
|
239 |
* |
|
240 |
* The {@code getBundle} factory methods load service providers of |
|
241 |
* {@link ResourceBundleProvider}, if available, using {@link ServiceLoader}. |
|
242 |
* The service type is designated by {@code basename+"Provider"}. For |
|
243 |
* example, if the base name is "{@code com.example.app.MyResources}", the service |
|
244 |
* type is {@code com.example.app.MyResourcesProvider}. |
|
245 |
* <p> |
|
246 |
* In named modules, the loaded service providers for the given base name are |
|
247 |
* used to load resource bundles. If no service provider is available, or if |
|
248 |
* none of the service providers returns a resource bundle and the caller module |
|
249 |
* doesn't have its own service provider, the {@code getBundle} factory method |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
250 |
* searches for resource bundles that are local in the caller module and that |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
251 |
* are visible to the class loader of the caller module. The resource bundle |
36511 | 252 |
* formats for local module searching are "java.class" and "java.properties". |
253 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
254 |
* <h3>ResourceBundle.Control</h3> |
2 | 255 |
* |
256 |
* The {@link ResourceBundle.Control} class provides information necessary |
|
257 |
* to perform the bundle loading process by the <code>getBundle</code> |
|
258 |
* factory methods that take a <code>ResourceBundle.Control</code> |
|
259 |
* instance. You can implement your own subclass in order to enable |
|
260 |
* non-standard resource bundle formats, change the search strategy, or |
|
261 |
* define caching parameters. Refer to the descriptions of the class and the |
|
262 |
* {@link #getBundle(String, Locale, ClassLoader, Control) getBundle} |
|
263 |
* factory method for details. |
|
264 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
265 |
* <h3>Cache Management</h3> |
2 | 266 |
* |
267 |
* Resource bundle instances created by the <code>getBundle</code> factory |
|
268 |
* methods are cached by default, and the factory methods return the same |
|
269 |
* resource bundle instance multiple times if it has been |
|
270 |
* cached. <code>getBundle</code> clients may clear the cache, manage the |
|
271 |
* lifetime of cached resource bundle instances using time-to-live values, |
|
272 |
* or specify not to cache resource bundle instances. Refer to the |
|
273 |
* descriptions of the {@linkplain #getBundle(String, Locale, ClassLoader, |
|
274 |
* Control) <code>getBundle</code> factory method}, {@link |
|
275 |
* #clearCache(ClassLoader) clearCache}, {@link |
|
276 |
* Control#getTimeToLive(String, Locale) |
|
277 |
* ResourceBundle.Control.getTimeToLive}, and {@link |
|
278 |
* Control#needsReload(String, Locale, String, ClassLoader, ResourceBundle, |
|
279 |
* long) ResourceBundle.Control.needsReload} for details. |
|
280 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
281 |
* <h3>Example</h3> |
2 | 282 |
* |
283 |
* The following is a very simple example of a <code>ResourceBundle</code> |
|
284 |
* subclass, <code>MyResources</code>, that manages two resources (for a larger number of |
|
285 |
* resources you would probably use a <code>Map</code>). |
|
286 |
* Notice that you don't need to supply a value if |
|
287 |
* a "parent-level" <code>ResourceBundle</code> handles the same |
|
288 |
* key with the same value (as for the okKey below). |
|
289 |
* <blockquote> |
|
290 |
* <pre> |
|
291 |
* // default (English language, United States) |
|
292 |
* public class MyResources extends ResourceBundle { |
|
293 |
* public Object handleGetObject(String key) { |
|
294 |
* if (key.equals("okKey")) return "Ok"; |
|
295 |
* if (key.equals("cancelKey")) return "Cancel"; |
|
296 |
* return null; |
|
297 |
* } |
|
298 |
* |
|
299 |
* public Enumeration<String> getKeys() { |
|
300 |
* return Collections.enumeration(keySet()); |
|
301 |
* } |
|
302 |
* |
|
303 |
* // Overrides handleKeySet() so that the getKeys() implementation |
|
304 |
* // can rely on the keySet() value. |
|
305 |
* protected Set<String> handleKeySet() { |
|
306 |
* return new HashSet<String>(Arrays.asList("okKey", "cancelKey")); |
|
307 |
* } |
|
308 |
* } |
|
309 |
* |
|
310 |
* // German language |
|
311 |
* public class MyResources_de extends MyResources { |
|
312 |
* public Object handleGetObject(String key) { |
|
313 |
* // don't need okKey, since parent level handles it. |
|
314 |
* if (key.equals("cancelKey")) return "Abbrechen"; |
|
315 |
* return null; |
|
316 |
* } |
|
317 |
* |
|
318 |
* protected Set<String> handleKeySet() { |
|
319 |
* return new HashSet<String>(Arrays.asList("cancelKey")); |
|
320 |
* } |
|
321 |
* } |
|
322 |
* </pre> |
|
323 |
* </blockquote> |
|
324 |
* You do not have to restrict yourself to using a single family of |
|
325 |
* <code>ResourceBundle</code>s. For example, you could have a set of bundles for |
|
326 |
* exception messages, <code>ExceptionResources</code> |
|
327 |
* (<code>ExceptionResources_fr</code>, <code>ExceptionResources_de</code>, ...), |
|
328 |
* and one for widgets, <code>WidgetResource</code> (<code>WidgetResources_fr</code>, |
|
329 |
* <code>WidgetResources_de</code>, ...); breaking up the resources however you like. |
|
330 |
* |
|
331 |
* @see ListResourceBundle |
|
332 |
* @see PropertyResourceBundle |
|
333 |
* @see MissingResourceException |
|
36511 | 334 |
* @see ResourceBundleProvider |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
335 |
* @since 1.1 |
2 | 336 |
*/ |
337 |
public abstract class ResourceBundle { |
|
338 |
||
339 |
/** initial size of the bundle cache */ |
|
340 |
private static final int INITIAL_CACHE_SIZE = 32; |
|
341 |
||
36511 | 342 |
static { |
343 |
SharedSecrets.setJavaUtilResourceBundleAccess( |
|
344 |
new JavaUtilResourceBundleAccess() { |
|
345 |
@Override |
|
346 |
public void setParent(ResourceBundle bundle, |
|
347 |
ResourceBundle parent) { |
|
348 |
bundle.setParent(parent); |
|
349 |
} |
|
350 |
||
351 |
@Override |
|
352 |
public ResourceBundle getParent(ResourceBundle bundle) { |
|
353 |
return bundle.parent; |
|
354 |
} |
|
355 |
||
356 |
@Override |
|
357 |
public void setLocale(ResourceBundle bundle, Locale locale) { |
|
358 |
bundle.locale = locale; |
|
359 |
} |
|
360 |
||
361 |
@Override |
|
362 |
public void setName(ResourceBundle bundle, String name) { |
|
363 |
bundle.name = name; |
|
364 |
} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
365 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
366 |
@Override |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
367 |
public ResourceBundle getBundle(String baseName, Locale locale, Module module) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
368 |
// use the given module as the caller to bypass the access check |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
369 |
return getBundleImpl(module, module, |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
370 |
baseName, locale, Control.INSTANCE); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
371 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
372 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
373 |
@Override |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
374 |
public ResourceBundle newResourceBundle(Class<? extends ResourceBundle> bundleClass) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
375 |
return ResourceBundleProviderHelper.newResourceBundle(bundleClass); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
376 |
} |
36511 | 377 |
}); |
378 |
} |
|
379 |
||
2 | 380 |
/** constant indicating that no resource bundle exists */ |
381 |
private static final ResourceBundle NONEXISTENT_BUNDLE = new ResourceBundle() { |
|
382 |
public Enumeration<String> getKeys() { return null; } |
|
383 |
protected Object handleGetObject(String key) { return null; } |
|
384 |
public String toString() { return "NONEXISTENT_BUNDLE"; } |
|
385 |
}; |
|
386 |
||
387 |
||
388 |
/** |
|
389 |
* The cache is a map from cache keys (with bundle base name, locale, and |
|
390 |
* class loader) to either a resource bundle or NONEXISTENT_BUNDLE wrapped by a |
|
391 |
* BundleReference. |
|
392 |
* |
|
393 |
* The cache is a ConcurrentMap, allowing the cache to be searched |
|
394 |
* concurrently by multiple threads. This will also allow the cache keys |
|
395 |
* to be reclaimed along with the ClassLoaders they reference. |
|
396 |
* |
|
397 |
* This variable would be better named "cache", but we keep the old |
|
398 |
* name for compatibility with some workarounds for bug 4212439. |
|
399 |
*/ |
|
400 |
private static final ConcurrentMap<CacheKey, BundleReference> cacheList |
|
9224 | 401 |
= new ConcurrentHashMap<>(INITIAL_CACHE_SIZE); |
2 | 402 |
|
403 |
/** |
|
404 |
* Queue for reference objects referring to class loaders or bundles. |
|
405 |
*/ |
|
13029
dfd934f86768
6380549: (rb) ResourceBundle.Control global binding support
okutsu
parents:
12448
diff
changeset
|
406 |
private static final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>(); |
2 | 407 |
|
408 |
/** |
|
20865
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
409 |
* Returns the base name of this bundle, if known, or {@code null} if unknown. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
410 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
411 |
* If not null, then this is the value of the {@code baseName} parameter |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
412 |
* that was passed to the {@code ResourceBundle.getBundle(...)} method |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
413 |
* when the resource bundle was loaded. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
414 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
415 |
* @return The base name of the resource bundle, as provided to and expected |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
416 |
* by the {@code ResourceBundle.getBundle(...)} methods. |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
417 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
418 |
* @see #getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
419 |
* |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
420 |
* @since 1.8 |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
421 |
*/ |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
422 |
public String getBaseBundleName() { |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
423 |
return name; |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
424 |
} |
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
425 |
|
164ba3f3484b
8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents:
18829
diff
changeset
|
426 |
/** |
2 | 427 |
* The parent bundle of this bundle. |
428 |
* The parent bundle is searched by {@link #getObject getObject} |
|
429 |
* when this bundle does not contain a particular resource. |
|
430 |
*/ |
|
431 |
protected ResourceBundle parent = null; |
|
432 |
||
433 |
/** |
|
434 |
* The locale for this bundle. |
|
435 |
*/ |
|
436 |
private Locale locale = null; |
|
437 |
||
438 |
/** |
|
439 |
* The base bundle name for this bundle. |
|
440 |
*/ |
|
441 |
private String name; |
|
442 |
||
443 |
/** |
|
444 |
* The flag indicating this bundle has expired in the cache. |
|
445 |
*/ |
|
446 |
private volatile boolean expired; |
|
447 |
||
448 |
/** |
|
449 |
* The back link to the cache key. null if this bundle isn't in |
|
450 |
* the cache (yet) or has expired. |
|
451 |
*/ |
|
452 |
private volatile CacheKey cacheKey; |
|
453 |
||
454 |
/** |
|
455 |
* A Set of the keys contained only in this ResourceBundle. |
|
456 |
*/ |
|
457 |
private volatile Set<String> keySet; |
|
458 |
||
459 |
/** |
|
460 |
* Sole constructor. (For invocation by subclass constructors, typically |
|
461 |
* implicit.) |
|
462 |
*/ |
|
463 |
public ResourceBundle() { |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Gets a string for the given key from this resource bundle or one of its parents. |
|
468 |
* Calling this method is equivalent to calling |
|
469 |
* <blockquote> |
|
470 |
* <code>(String) {@link #getObject(java.lang.String) getObject}(key)</code>. |
|
471 |
* </blockquote> |
|
472 |
* |
|
473 |
* @param key the key for the desired string |
|
474 |
* @exception NullPointerException if <code>key</code> is <code>null</code> |
|
475 |
* @exception MissingResourceException if no object for the given key can be found |
|
476 |
* @exception ClassCastException if the object found for the given key is not a string |
|
477 |
* @return the string for the given key |
|
478 |
*/ |
|
479 |
public final String getString(String key) { |
|
480 |
return (String) getObject(key); |
|
481 |
} |
|
482 |
||
483 |
/** |
|
484 |
* Gets a string array for the given key from this resource bundle or one of its parents. |
|
485 |
* Calling this method is equivalent to calling |
|
486 |
* <blockquote> |
|
487 |
* <code>(String[]) {@link #getObject(java.lang.String) getObject}(key)</code>. |
|
488 |
* </blockquote> |
|
489 |
* |
|
490 |
* @param key the key for the desired string array |
|
491 |
* @exception NullPointerException if <code>key</code> is <code>null</code> |
|
492 |
* @exception MissingResourceException if no object for the given key can be found |
|
493 |
* @exception ClassCastException if the object found for the given key is not a string array |
|
494 |
* @return the string array for the given key |
|
495 |
*/ |
|
496 |
public final String[] getStringArray(String key) { |
|
497 |
return (String[]) getObject(key); |
|
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* Gets an object for the given key from this resource bundle or one of its parents. |
|
502 |
* This method first tries to obtain the object from this resource bundle using |
|
503 |
* {@link #handleGetObject(java.lang.String) handleGetObject}. |
|
504 |
* If not successful, and the parent resource bundle is not null, |
|
505 |
* it calls the parent's <code>getObject</code> method. |
|
506 |
* If still not successful, it throws a MissingResourceException. |
|
507 |
* |
|
508 |
* @param key the key for the desired object |
|
509 |
* @exception NullPointerException if <code>key</code> is <code>null</code> |
|
510 |
* @exception MissingResourceException if no object for the given key can be found |
|
511 |
* @return the object for the given key |
|
512 |
*/ |
|
513 |
public final Object getObject(String key) { |
|
514 |
Object obj = handleGetObject(key); |
|
515 |
if (obj == null) { |
|
516 |
if (parent != null) { |
|
517 |
obj = parent.getObject(key); |
|
518 |
} |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
519 |
if (obj == null) { |
2 | 520 |
throw new MissingResourceException("Can't find resource for bundle " |
521 |
+this.getClass().getName() |
|
522 |
+", key "+key, |
|
523 |
this.getClass().getName(), |
|
524 |
key); |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
525 |
} |
2 | 526 |
} |
527 |
return obj; |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* Returns the locale of this resource bundle. This method can be used after a |
|
532 |
* call to getBundle() to determine whether the resource bundle returned really |
|
533 |
* corresponds to the requested locale or is a fallback. |
|
534 |
* |
|
535 |
* @return the locale of this resource bundle |
|
536 |
*/ |
|
537 |
public Locale getLocale() { |
|
538 |
return locale; |
|
539 |
} |
|
540 |
||
36511 | 541 |
private static ClassLoader getLoader(Module module) { |
542 |
PrivilegedAction<ClassLoader> pa = module::getClassLoader; |
|
543 |
return AccessController.doPrivileged(pa); |
|
544 |
} |
|
545 |
||
2 | 546 |
/** |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
547 |
* @param module a non-null-screened module form the {@link CacheKey#getModule()}. |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
548 |
* @return the ClassLoader to use in {@link Control#needsReload} |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
549 |
* and {@link Control#newBundle} |
2 | 550 |
*/ |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
551 |
private static ClassLoader getLoaderForControl(Module module) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
552 |
ClassLoader loader = getLoader(module); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
553 |
return loader == null ? ClassLoader.getSystemClassLoader() : loader; |
2 | 554 |
} |
555 |
||
556 |
/** |
|
557 |
* Sets the parent bundle of this bundle. |
|
558 |
* The parent bundle is searched by {@link #getObject getObject} |
|
559 |
* when this bundle does not contain a particular resource. |
|
560 |
* |
|
561 |
* @param parent this bundle's parent bundle. |
|
562 |
*/ |
|
563 |
protected void setParent(ResourceBundle parent) { |
|
564 |
assert parent != NONEXISTENT_BUNDLE; |
|
565 |
this.parent = parent; |
|
566 |
} |
|
567 |
||
568 |
/** |
|
569 |
* Key used for cached resource bundles. The key checks the base |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
570 |
* name, the locale, the bundle module, and the caller module |
36511 | 571 |
* to determine if the resource is a match to the requested one. |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
572 |
* The base name, the locale and both modules must have a non-null value. |
2 | 573 |
*/ |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
574 |
private static final class CacheKey { |
36511 | 575 |
// These four are the actual keys for lookup in Map. |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
576 |
private final String name; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
577 |
private volatile Locale locale; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
578 |
private final KeyElementReference<Module> moduleRef; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
579 |
private final KeyElementReference<Module> callerRef; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
580 |
// this is the part of hashCode that pertains to module and callerModule |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
581 |
// which can be GCed.. |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
582 |
private final int modulesHash; |
2 | 583 |
|
584 |
// bundle format which is necessary for calling |
|
585 |
// Control.needsReload(). |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
586 |
private volatile String format; |
2 | 587 |
|
588 |
// These time values are in CacheKey so that NONEXISTENT_BUNDLE |
|
589 |
// doesn't need to be cloned for caching. |
|
590 |
||
591 |
// The time when the bundle has been loaded |
|
592 |
private volatile long loadTime; |
|
593 |
||
594 |
// The time when the bundle expires in the cache, or either |
|
595 |
// Control.TTL_DONT_CACHE or Control.TTL_NO_EXPIRATION_CONTROL. |
|
596 |
private volatile long expirationTime; |
|
597 |
||
598 |
// Placeholder for an error report by a Throwable |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
599 |
private volatile Throwable cause; |
2 | 600 |
|
36511 | 601 |
// ResourceBundleProviders for loading ResourceBundles |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
602 |
private volatile ServiceLoader<ResourceBundleProvider> providers; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
603 |
private volatile boolean providersChecked; |
36511 | 604 |
|
605 |
// Boolean.TRUE if the factory method caller provides a ResourceBundleProvier. |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
606 |
private volatile Boolean callerHasProvider; |
36511 | 607 |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
608 |
CacheKey(String baseName, Locale locale, Module module, Module caller) { |
36511 | 609 |
Objects.requireNonNull(module); |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
610 |
Objects.requireNonNull(caller); |
36511 | 611 |
|
2 | 612 |
this.name = baseName; |
613 |
this.locale = locale; |
|
36511 | 614 |
this.moduleRef = new KeyElementReference<>(module, referenceQueue, this); |
42467
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
615 |
this.callerRef = new KeyElementReference<>(caller, referenceQueue, this); |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
616 |
this.modulesHash = module.hashCode() ^ caller.hashCode(); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
617 |
} |
42467
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
618 |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
619 |
CacheKey(CacheKey src) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
620 |
// Create References to src's modules |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
621 |
this.moduleRef = new KeyElementReference<>( |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
622 |
Objects.requireNonNull(src.getModule()), referenceQueue, this); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
623 |
this.callerRef = new KeyElementReference<>( |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
624 |
Objects.requireNonNull(src.getCallerModule()), referenceQueue, this); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
625 |
// Copy fields from src. ResourceBundleProviders related fields |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
626 |
// and "cause" should not be copied. |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
627 |
this.name = src.name; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
628 |
this.locale = src.locale; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
629 |
this.modulesHash = src.modulesHash; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
630 |
this.format = src.format; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
631 |
this.loadTime = src.loadTime; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
632 |
this.expirationTime = src.expirationTime; |
2 | 633 |
} |
634 |
||
635 |
String getName() { |
|
636 |
return name; |
|
637 |
} |
|
638 |
||
639 |
Locale getLocale() { |
|
640 |
return locale; |
|
641 |
} |
|
642 |
||
643 |
CacheKey setLocale(Locale locale) { |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
644 |
this.locale = locale; |
2 | 645 |
return this; |
646 |
} |
|
647 |
||
36511 | 648 |
Module getModule() { |
649 |
return moduleRef.get(); |
|
650 |
} |
|
651 |
||
42467
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
652 |
Module getCallerModule() { |
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
653 |
return callerRef.get(); |
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
654 |
} |
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
655 |
|
36511 | 656 |
ServiceLoader<ResourceBundleProvider> getProviders() { |
39768
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
657 |
if (!providersChecked) { |
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
658 |
providers = getServiceLoader(getModule(), name); |
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
659 |
providersChecked = true; |
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
660 |
} |
36511 | 661 |
return providers; |
662 |
} |
|
663 |
||
664 |
boolean hasProviders() { |
|
39768
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
665 |
return getProviders() != null; |
36511 | 666 |
} |
667 |
||
668 |
boolean callerHasProvider() { |
|
669 |
return callerHasProvider == Boolean.TRUE; |
|
670 |
} |
|
671 |
||
672 |
@Override |
|
2 | 673 |
public boolean equals(Object other) { |
674 |
if (this == other) { |
|
675 |
return true; |
|
676 |
} |
|
677 |
try { |
|
678 |
final CacheKey otherEntry = (CacheKey)other; |
|
679 |
//quick check to see if they are not equal |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
680 |
if (modulesHash != otherEntry.modulesHash) { |
2 | 681 |
return false; |
682 |
} |
|
683 |
//are the names the same? |
|
684 |
if (!name.equals(otherEntry.name)) { |
|
685 |
return false; |
|
686 |
} |
|
687 |
// are the locales the same? |
|
688 |
if (!locale.equals(otherEntry.locale)) { |
|
689 |
return false; |
|
690 |
} |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
691 |
// are modules and callerModules the same and non-null? |
36511 | 692 |
Module module = getModule(); |
42467
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
693 |
Module caller = getCallerModule(); |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
694 |
return ((module != null) && (module.equals(otherEntry.getModule())) && |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
695 |
(caller != null) && (caller.equals(otherEntry.getCallerModule()))); |
36511 | 696 |
} catch (NullPointerException | ClassCastException e) { |
2 | 697 |
} |
698 |
return false; |
|
699 |
} |
|
700 |
||
36511 | 701 |
@Override |
2 | 702 |
public int hashCode() { |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
703 |
return (name.hashCode() << 3) ^ locale.hashCode() ^ modulesHash; |
2 | 704 |
} |
705 |
||
706 |
String getFormat() { |
|
707 |
return format; |
|
708 |
} |
|
709 |
||
710 |
void setFormat(String format) { |
|
711 |
this.format = format; |
|
712 |
} |
|
713 |
||
714 |
private void setCause(Throwable cause) { |
|
715 |
if (this.cause == null) { |
|
716 |
this.cause = cause; |
|
717 |
} else { |
|
718 |
// Override the cause if the previous one is |
|
719 |
// ClassNotFoundException. |
|
720 |
if (this.cause instanceof ClassNotFoundException) { |
|
721 |
this.cause = cause; |
|
722 |
} |
|
723 |
} |
|
724 |
} |
|
725 |
||
726 |
private Throwable getCause() { |
|
727 |
return cause; |
|
728 |
} |
|
729 |
||
36511 | 730 |
@Override |
2 | 731 |
public String toString() { |
732 |
String l = locale.toString(); |
|
733 |
if (l.length() == 0) { |
|
734 |
if (locale.getVariant().length() != 0) { |
|
735 |
l = "__" + locale.getVariant(); |
|
736 |
} else { |
|
737 |
l = "\"\""; |
|
738 |
} |
|
739 |
} |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
740 |
return "CacheKey[" + name + |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
741 |
", locale=" + l + |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
742 |
", module=" + getModule() + |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
743 |
", callerModule=" + getCallerModule() + |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
744 |
", format=" + format + |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
745 |
"]"; |
2 | 746 |
} |
747 |
} |
|
748 |
||
749 |
/** |
|
750 |
* The common interface to get a CacheKey in LoaderReference and |
|
751 |
* BundleReference. |
|
752 |
*/ |
|
753 |
private static interface CacheKeyReference { |
|
754 |
public CacheKey getCacheKey(); |
|
755 |
} |
|
756 |
||
757 |
/** |
|
36511 | 758 |
* References to a CacheKey element as a WeakReference so that it can be |
759 |
* garbage collected when nobody else is using it. |
|
2 | 760 |
*/ |
36511 | 761 |
private static class KeyElementReference<T> extends WeakReference<T> |
762 |
implements CacheKeyReference { |
|
763 |
private final CacheKey cacheKey; |
|
2 | 764 |
|
36511 | 765 |
KeyElementReference(T referent, ReferenceQueue<Object> q, CacheKey key) { |
2 | 766 |
super(referent, q); |
767 |
cacheKey = key; |
|
768 |
} |
|
769 |
||
36511 | 770 |
@Override |
2 | 771 |
public CacheKey getCacheKey() { |
772 |
return cacheKey; |
|
773 |
} |
|
774 |
} |
|
775 |
||
776 |
/** |
|
777 |
* References to bundles are soft references so that they can be garbage |
|
778 |
* collected when they have no hard references. |
|
779 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
780 |
private static class BundleReference extends SoftReference<ResourceBundle> |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
781 |
implements CacheKeyReference { |
36511 | 782 |
private final CacheKey cacheKey; |
2 | 783 |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
784 |
BundleReference(ResourceBundle referent, ReferenceQueue<Object> q, CacheKey key) { |
2 | 785 |
super(referent, q); |
786 |
cacheKey = key; |
|
787 |
} |
|
788 |
||
36511 | 789 |
@Override |
2 | 790 |
public CacheKey getCacheKey() { |
791 |
return cacheKey; |
|
792 |
} |
|
793 |
} |
|
794 |
||
795 |
/** |
|
796 |
* Gets a resource bundle using the specified base name, the default locale, |
|
797 |
* and the caller's class loader. Calling this method is equivalent to calling |
|
798 |
* <blockquote> |
|
799 |
* <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>, |
|
800 |
* </blockquote> |
|
801 |
* except that <code>getClassLoader()</code> is run with the security |
|
802 |
* privileges of <code>ResourceBundle</code>. |
|
803 |
* See {@link #getBundle(String, Locale, ClassLoader) getBundle} |
|
804 |
* for a complete description of the search and instantiation strategy. |
|
805 |
* |
|
806 |
* @param baseName the base name of the resource bundle, a fully qualified class name |
|
807 |
* @exception java.lang.NullPointerException |
|
808 |
* if <code>baseName</code> is <code>null</code> |
|
809 |
* @exception MissingResourceException |
|
810 |
* if no resource bundle for the specified base name can be found |
|
811 |
* @return a resource bundle for the given base name and the default locale |
|
812 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
813 |
@CallerSensitive |
2 | 814 |
public static final ResourceBundle getBundle(String baseName) |
815 |
{ |
|
36511 | 816 |
Class<?> caller = Reflection.getCallerClass(); |
2 | 817 |
return getBundleImpl(baseName, Locale.getDefault(), |
42769
c6ee006baceb
8171189: Deprecate ResourceBundleControlProvider for removal
naoto
parents:
42467
diff
changeset
|
818 |
caller, Control.INSTANCE); |
2 | 819 |
} |
820 |
||
821 |
/** |
|
822 |
* Returns a resource bundle using the specified base name, the |
|
823 |
* default locale and the specified control. Calling this method |
|
824 |
* is equivalent to calling |
|
825 |
* <pre> |
|
826 |
* getBundle(baseName, Locale.getDefault(), |
|
827 |
* this.getClass().getClassLoader(), control), |
|
828 |
* </pre> |
|
829 |
* except that <code>getClassLoader()</code> is run with the security |
|
830 |
* privileges of <code>ResourceBundle</code>. See {@link |
|
831 |
* #getBundle(String, Locale, ClassLoader, Control) getBundle} for the |
|
832 |
* complete description of the resource bundle loading process with a |
|
833 |
* <code>ResourceBundle.Control</code>. |
|
834 |
* |
|
835 |
* @param baseName |
|
836 |
* the base name of the resource bundle, a fully qualified class |
|
837 |
* name |
|
838 |
* @param control |
|
839 |
* the control which gives information for the resource bundle |
|
840 |
* loading process |
|
36511 | 841 |
* @return a resource bundle for the given base name and the default locale |
842 |
* @throws NullPointerException |
|
843 |
* if <code>baseName</code> or <code>control</code> is |
|
844 |
* <code>null</code> |
|
845 |
* @throws MissingResourceException |
|
846 |
* if no resource bundle for the specified base name can be found |
|
847 |
* @throws IllegalArgumentException |
|
848 |
* if the given <code>control</code> doesn't perform properly |
|
849 |
* (e.g., <code>control.getCandidateLocales</code> returns null.) |
|
850 |
* Note that validation of <code>control</code> is performed as |
|
851 |
* needed. |
|
852 |
* @throws UnsupportedOperationException |
|
853 |
* if this method is called in a named module |
|
2 | 854 |
* @since 1.6 |
855 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
856 |
@CallerSensitive |
2 | 857 |
public static final ResourceBundle getBundle(String baseName, |
858 |
Control control) { |
|
36511 | 859 |
Class<?> caller = Reflection.getCallerClass(); |
860 |
Locale targetLocale = Locale.getDefault(); |
|
861 |
checkNamedModule(caller); |
|
862 |
return getBundleImpl(baseName, targetLocale, caller, control); |
|
2 | 863 |
} |
864 |
||
865 |
/** |
|
866 |
* Gets a resource bundle using the specified base name and locale, |
|
867 |
* and the caller's class loader. Calling this method is equivalent to calling |
|
868 |
* <blockquote> |
|
869 |
* <code>getBundle(baseName, locale, this.getClass().getClassLoader())</code>, |
|
870 |
* </blockquote> |
|
871 |
* except that <code>getClassLoader()</code> is run with the security |
|
872 |
* privileges of <code>ResourceBundle</code>. |
|
873 |
* See {@link #getBundle(String, Locale, ClassLoader) getBundle} |
|
874 |
* for a complete description of the search and instantiation strategy. |
|
875 |
* |
|
876 |
* @param baseName |
|
877 |
* the base name of the resource bundle, a fully qualified class name |
|
878 |
* @param locale |
|
879 |
* the locale for which a resource bundle is desired |
|
880 |
* @exception NullPointerException |
|
881 |
* if <code>baseName</code> or <code>locale</code> is <code>null</code> |
|
882 |
* @exception MissingResourceException |
|
883 |
* if no resource bundle for the specified base name can be found |
|
884 |
* @return a resource bundle for the given base name and locale |
|
885 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
886 |
@CallerSensitive |
2 | 887 |
public static final ResourceBundle getBundle(String baseName, |
888 |
Locale locale) |
|
889 |
{ |
|
36511 | 890 |
Class<?> caller = Reflection.getCallerClass(); |
2 | 891 |
return getBundleImpl(baseName, locale, |
42769
c6ee006baceb
8171189: Deprecate ResourceBundleControlProvider for removal
naoto
parents:
42467
diff
changeset
|
892 |
caller, Control.INSTANCE); |
36511 | 893 |
} |
894 |
||
895 |
/** |
|
896 |
* Gets a resource bundle using the specified base name and the default locale |
|
897 |
* on behalf of the specified module. This method is equivalent to calling |
|
898 |
* <blockquote> |
|
899 |
* <code>getBundle(baseName, Locale.getDefault(), module)</code> |
|
900 |
* </blockquote> |
|
901 |
* |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
902 |
* <p> Resource bundles in named modules may be encapsulated. When |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
903 |
* the resource bundle is loaded from a provider, the caller module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
904 |
* must have an appropriate <i>uses</i> clause in its <i>module descriptor</i> |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
905 |
* to declare that the module uses implementations of {@code "baseName"Provider}. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
906 |
* When the resource bundle is loaded from the specified module, it is |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
907 |
* subject to the encapsulation rules specified by |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
908 |
* {@link Module#getResourceAsStream Module.getResourceAsStream}. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
909 |
* |
36511 | 910 |
* @param baseName the base name of the resource bundle, |
911 |
* a fully qualified class name |
|
912 |
* @param module the module for which the resource bundle is searched |
|
913 |
* @throws NullPointerException |
|
914 |
* if {@code baseName} or {@code module} is {@code null} |
|
915 |
* @throws SecurityException |
|
916 |
* if a security manager exists and the caller is not the specified |
|
917 |
* module and doesn't have {@code RuntimePermission("getClassLoader")} |
|
918 |
* @throws MissingResourceException |
|
919 |
* if no resource bundle for the specified base name can be found in the |
|
920 |
* specified module |
|
921 |
* @return a resource bundle for the given base name and the default locale |
|
922 |
* @since 9 |
|
923 |
* @see ResourceBundleProvider |
|
924 |
*/ |
|
925 |
@CallerSensitive |
|
926 |
public static ResourceBundle getBundle(String baseName, Module module) { |
|
927 |
return getBundleFromModule(Reflection.getCallerClass(), module, baseName, |
|
928 |
Locale.getDefault(), Control.INSTANCE); |
|
929 |
} |
|
930 |
||
931 |
/** |
|
932 |
* Gets a resource bundle using the specified base name and locale |
|
933 |
* on behalf of the specified module. |
|
934 |
* |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
935 |
* <p> Resource bundles in named modules may be encapsulated. When |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
936 |
* the resource bundle is loaded from a provider, the caller module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
937 |
* must have an appropriate <i>uses</i> clause in its <i>module descriptor</i> |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
938 |
* to declare that the module uses implementations of {@code "baseName"Provider}. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
939 |
* When the resource bundle is loaded from the specified module, it is |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
940 |
* subject to the encapsulation rules specified by |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
941 |
* {@link Module#getResourceAsStream Module.getResourceAsStream}. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
942 |
* |
36511 | 943 |
* <p> |
944 |
* If the given {@code module} is a named module, this method will |
|
945 |
* load the service providers for {@link java.util.spi.ResourceBundleProvider} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
946 |
* and also resource bundles that are local in the given module or that |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
947 |
* are visible to the class loader of the given module (refer to the |
36511 | 948 |
* <a href="#bundleprovider">Resource Bundles in Named Modules</a> section |
949 |
* for details). |
|
950 |
* |
|
951 |
* <p> |
|
952 |
* If the given {@code module} is an unnamed module, then this method is |
|
953 |
* equivalent to calling {@link #getBundle(String, Locale, ClassLoader) |
|
954 |
* getBundle(baseName, targetLocale, module.getClassLoader()} to load |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
955 |
* resource bundles that are visible to the class loader of the given |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
956 |
* unnamed module. |
36511 | 957 |
* |
958 |
* @param baseName the base name of the resource bundle, |
|
959 |
* a fully qualified class name |
|
960 |
* @param targetLocale the locale for which a resource bundle is desired |
|
961 |
* @param module the module for which the resource bundle is searched |
|
962 |
* @throws NullPointerException |
|
963 |
* if {@code baseName}, {@code targetLocale}, or {@code module} is |
|
964 |
* {@code null} |
|
965 |
* @throws SecurityException |
|
966 |
* if a security manager exists and the caller is not the specified |
|
967 |
* module and doesn't have {@code RuntimePermission("getClassLoader")} |
|
968 |
* @throws MissingResourceException |
|
969 |
* if no resource bundle for the specified base name and locale can |
|
970 |
* be found in the specified {@code module} |
|
971 |
* @return a resource bundle for the given base name and locale in the module |
|
972 |
* @since 9 |
|
973 |
*/ |
|
974 |
@CallerSensitive |
|
975 |
public static ResourceBundle getBundle(String baseName, Locale targetLocale, Module module) { |
|
976 |
return getBundleFromModule(Reflection.getCallerClass(), module, baseName, targetLocale, |
|
977 |
Control.INSTANCE); |
|
2 | 978 |
} |
979 |
||
980 |
/** |
|
981 |
* Returns a resource bundle using the specified base name, target |
|
982 |
* locale and control, and the caller's class loader. Calling this |
|
983 |
* method is equivalent to calling |
|
984 |
* <pre> |
|
985 |
* getBundle(baseName, targetLocale, this.getClass().getClassLoader(), |
|
986 |
* control), |
|
987 |
* </pre> |
|
988 |
* except that <code>getClassLoader()</code> is run with the security |
|
989 |
* privileges of <code>ResourceBundle</code>. See {@link |
|
990 |
* #getBundle(String, Locale, ClassLoader, Control) getBundle} for the |
|
991 |
* complete description of the resource bundle loading process with a |
|
992 |
* <code>ResourceBundle.Control</code>. |
|
993 |
* |
|
994 |
* @param baseName |
|
995 |
* the base name of the resource bundle, a fully qualified |
|
996 |
* class name |
|
997 |
* @param targetLocale |
|
998 |
* the locale for which a resource bundle is desired |
|
999 |
* @param control |
|
1000 |
* the control which gives information for the resource |
|
1001 |
* bundle loading process |
|
1002 |
* @return a resource bundle for the given base name and a |
|
36511 | 1003 |
* <code>Locale</code> in <code>locales</code> |
1004 |
* @throws NullPointerException |
|
1005 |
* if <code>baseName</code>, <code>locales</code> or |
|
1006 |
* <code>control</code> is <code>null</code> |
|
1007 |
* @throws MissingResourceException |
|
1008 |
* if no resource bundle for the specified base name in any |
|
1009 |
* of the <code>locales</code> can be found. |
|
1010 |
* @throws IllegalArgumentException |
|
1011 |
* if the given <code>control</code> doesn't perform properly |
|
1012 |
* (e.g., <code>control.getCandidateLocales</code> returns null.) |
|
1013 |
* Note that validation of <code>control</code> is performed as |
|
1014 |
* needed. |
|
1015 |
* @throws UnsupportedOperationException |
|
1016 |
* if this method is called in a named module |
|
2 | 1017 |
* @since 1.6 |
1018 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
1019 |
@CallerSensitive |
2 | 1020 |
public static final ResourceBundle getBundle(String baseName, Locale targetLocale, |
1021 |
Control control) { |
|
36511 | 1022 |
Class<?> caller = Reflection.getCallerClass(); |
1023 |
checkNamedModule(caller); |
|
1024 |
return getBundleImpl(baseName, targetLocale, caller, control); |
|
2 | 1025 |
} |
1026 |
||
1027 |
/** |
|
6501 | 1028 |
* Gets a resource bundle using the specified base name, locale, and class |
1029 |
* loader. |
|
1030 |
* |
|
13029
dfd934f86768
6380549: (rb) ResourceBundle.Control global binding support
okutsu
parents:
12448
diff
changeset
|
1031 |
* <p>This method behaves the same as calling |
6501 | 1032 |
* {@link #getBundle(String, Locale, ClassLoader, Control)} passing a |
42769
c6ee006baceb
8171189: Deprecate ResourceBundleControlProvider for removal
naoto
parents:
42467
diff
changeset
|
1033 |
* default instance of {@link Control}. |
13029
dfd934f86768
6380549: (rb) ResourceBundle.Control global binding support
okutsu
parents:
12448
diff
changeset
|
1034 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1035 |
* <p><a name="default_behavior">The following describes the default |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1036 |
* behavior</a>. |
6501 | 1037 |
* |
36511 | 1038 |
* <p> |
1039 |
* Resource bundles in a named module are private to that module. If |
|
1040 |
* the caller is in a named module, this method will find resource bundles |
|
1041 |
* from the service providers of {@link java.util.spi.ResourceBundleProvider} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1042 |
* and also find resource bundles that are in the caller's module or |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1043 |
* that are visible to the given class loader. |
36511 | 1044 |
* If the caller is in a named module and the given {@code loader} is |
1045 |
* different than the caller's class loader, or if the caller is not in |
|
1046 |
* a named module, this method will not find resource bundles from named |
|
1047 |
* modules. |
|
1048 |
* |
|
6501 | 1049 |
* <p><code>getBundle</code> uses the base name, the specified locale, and |
1050 |
* the default locale (obtained from {@link java.util.Locale#getDefault() |
|
1051 |
* Locale.getDefault}) to generate a sequence of <a |
|
1052 |
* name="candidates"><em>candidate bundle names</em></a>. If the specified |
|
1053 |
* locale's language, script, country, and variant are all empty strings, |
|
1054 |
* then the base name is the only candidate bundle name. Otherwise, a list |
|
1055 |
* of candidate locales is generated from the attribute values of the |
|
1056 |
* specified locale (language, script, country and variant) and appended to |
|
1057 |
* the base name. Typically, this will look like the following: |
|
1058 |
* |
|
1059 |
* <pre> |
|
1060 |
* baseName + "_" + language + "_" + script + "_" + country + "_" + variant |
|
1061 |
* baseName + "_" + language + "_" + script + "_" + country |
|
1062 |
* baseName + "_" + language + "_" + script |
|
1063 |
* baseName + "_" + language + "_" + country + "_" + variant |
|
1064 |
* baseName + "_" + language + "_" + country |
|
1065 |
* baseName + "_" + language |
|
1066 |
* </pre> |
|
2 | 1067 |
* |
6501 | 1068 |
* <p>Candidate bundle names where the final component is an empty string |
1069 |
* are omitted, along with the underscore. For example, if country is an |
|
1070 |
* empty string, the second and the fifth candidate bundle names above |
|
1071 |
* would be omitted. Also, if script is an empty string, the candidate names |
|
1072 |
* including script are omitted. For example, a locale with language "de" |
|
1073 |
* and variant "JAVA" will produce candidate names with base name |
|
1074 |
* "MyResource" below. |
|
1075 |
* |
|
1076 |
* <pre> |
|
1077 |
* MyResource_de__JAVA |
|
1078 |
* MyResource_de |
|
1079 |
* </pre> |
|
1080 |
* |
|
1081 |
* In the case that the variant contains one or more underscores ('_'), a |
|
1082 |
* sequence of bundle names generated by truncating the last underscore and |
|
1083 |
* the part following it is inserted after a candidate bundle name with the |
|
1084 |
* original variant. For example, for a locale with language "en", script |
|
1085 |
* "Latn, country "US" and variant "WINDOWS_VISTA", and bundle base name |
|
1086 |
* "MyResource", the list of candidate bundle names below is generated: |
|
1087 |
* |
|
1088 |
* <pre> |
|
1089 |
* MyResource_en_Latn_US_WINDOWS_VISTA |
|
1090 |
* MyResource_en_Latn_US_WINDOWS |
|
1091 |
* MyResource_en_Latn_US |
|
1092 |
* MyResource_en_Latn |
|
1093 |
* MyResource_en_US_WINDOWS_VISTA |
|
1094 |
* MyResource_en_US_WINDOWS |
|
1095 |
* MyResource_en_US |
|
1096 |
* MyResource_en |
|
1097 |
* </pre> |
|
1098 |
* |
|
1099 |
* <blockquote><b>Note:</b> For some <code>Locale</code>s, the list of |
|
1100 |
* candidate bundle names contains extra names, or the order of bundle names |
|
1101 |
* is slightly modified. See the description of the default implementation |
|
1102 |
* of {@link Control#getCandidateLocales(String, Locale) |
|
1103 |
* getCandidateLocales} for details.</blockquote> |
|
2 | 1104 |
* |
6501 | 1105 |
* <p><code>getBundle</code> then iterates over the candidate bundle names |
1106 |
* to find the first one for which it can <em>instantiate</em> an actual |
|
1107 |
* resource bundle. It uses the default controls' {@link Control#getFormats |
|
1108 |
* getFormats} method, which generates two bundle names for each generated |
|
1109 |
* name, the first a class name and the second a properties file name. For |
|
1110 |
* each candidate bundle name, it attempts to create a resource bundle: |
|
1111 |
* |
|
1112 |
* <ul><li>First, it attempts to load a class using the generated class name. |
|
1113 |
* If such a class can be found and loaded using the specified class |
|
1114 |
* loader, is assignment compatible with ResourceBundle, is accessible from |
|
1115 |
* ResourceBundle, and can be instantiated, <code>getBundle</code> creates a |
|
1116 |
* new instance of this class and uses it as the <em>result resource |
|
1117 |
* bundle</em>. |
|
1118 |
* |
|
1119 |
* <li>Otherwise, <code>getBundle</code> attempts to locate a property |
|
1120 |
* resource file using the generated properties file name. It generates a |
|
1121 |
* path name from the candidate bundle name by replacing all "." characters |
|
1122 |
* with "/" and appending the string ".properties". It attempts to find a |
|
1123 |
* "resource" with this name using {@link |
|
1124 |
* java.lang.ClassLoader#getResource(java.lang.String) |
|
1125 |
* ClassLoader.getResource}. (Note that a "resource" in the sense of |
|
1126 |
* <code>getResource</code> has nothing to do with the contents of a |
|
1127 |
* resource bundle, it is just a container of data, such as a file.) If it |
|
1128 |
* finds a "resource", it attempts to create a new {@link |
|
1129 |
* PropertyResourceBundle} instance from its contents. If successful, this |
|
1130 |
* instance becomes the <em>result resource bundle</em>. </ul> |
|
2 | 1131 |
* |
6501 | 1132 |
* <p>This continues until a result resource bundle is instantiated or the |
1133 |
* list of candidate bundle names is exhausted. If no matching resource |
|
1134 |
* bundle is found, the default control's {@link Control#getFallbackLocale |
|
1135 |
* getFallbackLocale} method is called, which returns the current default |
|
1136 |
* locale. A new sequence of candidate locale names is generated using this |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
27083
diff
changeset
|
1137 |
* locale and searched again, as above. |
6501 | 1138 |
* |
1139 |
* <p>If still no result bundle is found, the base name alone is looked up. If |
|
1140 |
* this still fails, a <code>MissingResourceException</code> is thrown. |
|
2 | 1141 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1142 |
* <p><a name="parent_chain"> Once a result resource bundle has been found, |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1143 |
* its <em>parent chain</em> is instantiated</a>. If the result bundle already |
6501 | 1144 |
* has a parent (perhaps because it was returned from a cache) the chain is |
1145 |
* complete. |
|
1146 |
* |
|
1147 |
* <p>Otherwise, <code>getBundle</code> examines the remainder of the |
|
1148 |
* candidate locale list that was used during the pass that generated the |
|
1149 |
* result resource bundle. (As before, candidate bundle names where the |
|
1150 |
* final component is an empty string are omitted.) When it comes to the |
|
1151 |
* end of the candidate list, it tries the plain bundle name. With each of the |
|
1152 |
* candidate bundle names it attempts to instantiate a resource bundle (first |
|
1153 |
* looking for a class and then a properties file, as described above). |
|
1154 |
* |
|
1155 |
* <p>Whenever it succeeds, it calls the previously instantiated resource |
|
2 | 1156 |
* bundle's {@link #setParent(java.util.ResourceBundle) setParent} method |
6501 | 1157 |
* with the new resource bundle. This continues until the list of names |
1158 |
* is exhausted or the current bundle already has a non-null parent. |
|
1159 |
* |
|
1160 |
* <p>Once the parent chain is complete, the bundle is returned. |
|
2 | 1161 |
* |
6501 | 1162 |
* <p><b>Note:</b> <code>getBundle</code> caches instantiated resource |
1163 |
* bundles and might return the same resource bundle instance multiple times. |
|
2 | 1164 |
* |
6501 | 1165 |
* <p><b>Note:</b>The <code>baseName</code> argument should be a fully |
1166 |
* qualified class name. However, for compatibility with earlier versions, |
|
1167 |
* Sun's Java SE Runtime Environments do not verify this, and so it is |
|
1168 |
* possible to access <code>PropertyResourceBundle</code>s by specifying a |
|
1169 |
* path name (using "/") instead of a fully qualified class name (using |
|
1170 |
* "."). |
|
2 | 1171 |
* |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1172 |
* <p><a name="default_behavior_example"> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1173 |
* <strong>Example:</strong></a> |
6501 | 1174 |
* <p> |
1175 |
* The following class and property files are provided: |
|
2 | 1176 |
* <pre> |
1177 |
* MyResources.class |
|
1178 |
* MyResources.properties |
|
1179 |
* MyResources_fr.properties |
|
1180 |
* MyResources_fr_CH.class |
|
1181 |
* MyResources_fr_CH.properties |
|
1182 |
* MyResources_en.properties |
|
1183 |
* MyResources_es_ES.class |
|
1184 |
* </pre> |
|
6501 | 1185 |
* |
1186 |
* The contents of all files are valid (that is, public non-abstract |
|
1187 |
* subclasses of <code>ResourceBundle</code> for the ".class" files, |
|
1188 |
* syntactically correct ".properties" files). The default locale is |
|
1189 |
* <code>Locale("en", "GB")</code>. |
|
1190 |
* |
|
1191 |
* <p>Calling <code>getBundle</code> with the locale arguments below will |
|
1192 |
* instantiate resource bundles as follows: |
|
1193 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1194 |
* <table summary="getBundle() locale to resource bundle mapping"> |
6501 | 1195 |
* <tr><td>Locale("fr", "CH")</td><td>MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class</td></tr> |
1196 |
* <tr><td>Locale("fr", "FR")</td><td>MyResources_fr.properties, parent MyResources.class</td></tr> |
|
1197 |
* <tr><td>Locale("de", "DE")</td><td>MyResources_en.properties, parent MyResources.class</td></tr> |
|
1198 |
* <tr><td>Locale("en", "US")</td><td>MyResources_en.properties, parent MyResources.class</td></tr> |
|
1199 |
* <tr><td>Locale("es", "ES")</td><td>MyResources_es_ES.class, parent MyResources.class</td></tr> |
|
1200 |
* </table> |
|
1201 |
* |
|
1202 |
* <p>The file MyResources_fr_CH.properties is never used because it is |
|
1203 |
* hidden by the MyResources_fr_CH.class. Likewise, MyResources.properties |
|
1204 |
* is also hidden by MyResources.class. |
|
2 | 1205 |
* |
36511 | 1206 |
* @apiNote If the caller module is a named module and the given |
1207 |
* {@code loader} is the caller module's class loader, this method is |
|
1208 |
* equivalent to {@code getBundle(baseName, locale)}; otherwise, it will not |
|
1209 |
* find resource bundles from named modules. |
|
1210 |
* Use {@link #getBundle(String, Locale, Module)} to load resource bundles |
|
1211 |
* on behalf on a specific module instead. |
|
1212 |
* |
|
2 | 1213 |
* @param baseName the base name of the resource bundle, a fully qualified class name |
1214 |
* @param locale the locale for which a resource bundle is desired |
|
1215 |
* @param loader the class loader from which to load the resource bundle |
|
1216 |
* @return a resource bundle for the given base name and locale |
|
1217 |
* @exception java.lang.NullPointerException |
|
1218 |
* if <code>baseName</code>, <code>locale</code>, or <code>loader</code> is <code>null</code> |
|
1219 |
* @exception MissingResourceException |
|
1220 |
* if no resource bundle for the specified base name can be found |
|
1221 |
* @since 1.2 |
|
1222 |
*/ |
|
36511 | 1223 |
@CallerSensitive |
2 | 1224 |
public static ResourceBundle getBundle(String baseName, Locale locale, |
1225 |
ClassLoader loader) |
|
1226 |
{ |
|
1227 |
if (loader == null) { |
|
1228 |
throw new NullPointerException(); |
|
1229 |
} |
|
36511 | 1230 |
Class<?> caller = Reflection.getCallerClass(); |
42769
c6ee006baceb
8171189: Deprecate ResourceBundleControlProvider for removal
naoto
parents:
42467
diff
changeset
|
1231 |
return getBundleImpl(baseName, locale, caller, loader, Control.INSTANCE); |
2 | 1232 |
} |
1233 |
||
1234 |
/** |
|
1235 |
* Returns a resource bundle using the specified base name, target |
|
1236 |
* locale, class loader and control. Unlike the {@linkplain |
|
1237 |
* #getBundle(String, Locale, ClassLoader) <code>getBundle</code> |
|
1238 |
* factory methods with no <code>control</code> argument}, the given |
|
1239 |
* <code>control</code> specifies how to locate and instantiate resource |
|
1240 |
* bundles. Conceptually, the bundle loading process with the given |
|
1241 |
* <code>control</code> is performed in the following steps. |
|
1242 |
* |
|
1243 |
* <ol> |
|
1244 |
* <li>This factory method looks up the resource bundle in the cache for |
|
1245 |
* the specified <code>baseName</code>, <code>targetLocale</code> and |
|
1246 |
* <code>loader</code>. If the requested resource bundle instance is |
|
1247 |
* found in the cache and the time-to-live periods of the instance and |
|
1248 |
* all of its parent instances have not expired, the instance is returned |
|
1249 |
* to the caller. Otherwise, this factory method proceeds with the |
|
1250 |
* loading process below.</li> |
|
1251 |
* |
|
1252 |
* <li>The {@link ResourceBundle.Control#getFormats(String) |
|
1253 |
* control.getFormats} method is called to get resource bundle formats |
|
1254 |
* to produce bundle or resource names. The strings |
|
1255 |
* <code>"java.class"</code> and <code>"java.properties"</code> |
|
1256 |
* designate class-based and {@linkplain PropertyResourceBundle |
|
1257 |
* property}-based resource bundles, respectively. Other strings |
|
1258 |
* starting with <code>"java."</code> are reserved for future extensions |
|
1259 |
* and must not be used for application-defined formats. Other strings |
|
1260 |
* designate application-defined formats.</li> |
|
1261 |
* |
|
1262 |
* <li>The {@link ResourceBundle.Control#getCandidateLocales(String, |
|
1263 |
* Locale) control.getCandidateLocales} method is called with the target |
|
1264 |
* locale to get a list of <em>candidate <code>Locale</code>s</em> for |
|
1265 |
* which resource bundles are searched.</li> |
|
1266 |
* |
|
1267 |
* <li>The {@link ResourceBundle.Control#newBundle(String, Locale, |
|
1268 |
* String, ClassLoader, boolean) control.newBundle} method is called to |
|
1269 |
* instantiate a <code>ResourceBundle</code> for the base bundle name, a |
|
1270 |
* candidate locale, and a format. (Refer to the note on the cache |
|
1271 |
* lookup below.) This step is iterated over all combinations of the |
|
1272 |
* candidate locales and formats until the <code>newBundle</code> method |
|
1273 |
* returns a <code>ResourceBundle</code> instance or the iteration has |
|
1274 |
* used up all the combinations. For example, if the candidate locales |
|
1275 |
* are <code>Locale("de", "DE")</code>, <code>Locale("de")</code> and |
|
1276 |
* <code>Locale("")</code> and the formats are <code>"java.class"</code> |
|
1277 |
* and <code>"java.properties"</code>, then the following is the |
|
1278 |
* sequence of locale-format combinations to be used to call |
|
1279 |
* <code>control.newBundle</code>. |
|
1280 |
* |
|
1281 |
* <table style="width: 50%; text-align: left; margin-left: 40px;" |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1282 |
* border="0" cellpadding="2" cellspacing="2" summary="locale-format combinations for newBundle"> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1283 |
* <tbody> |
2 | 1284 |
* <tr> |
1285 |
* <td |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1286 |
* style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;"><code>Locale</code><br> |
2 | 1287 |
* </td> |
1288 |
* <td |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1289 |
* style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;"><code>format</code><br> |
2 | 1290 |
* </td> |
1291 |
* </tr> |
|
1292 |
* <tr> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1293 |
* <td style="vertical-align: top; width: 50%;"><code>Locale("de", "DE")</code><br> |
2 | 1294 |
* </td> |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1295 |
* <td style="vertical-align: top; width: 50%;"><code>java.class</code><br> |
2 | 1296 |
* </td> |
1297 |
* </tr> |
|
1298 |
* <tr> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1299 |
* <td style="vertical-align: top; width: 50%;"><code>Locale("de", "DE")</code></td> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1300 |
* <td style="vertical-align: top; width: 50%;"><code>java.properties</code><br> |
2 | 1301 |
* </td> |
1302 |
* </tr> |
|
1303 |
* <tr> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1304 |
* <td style="vertical-align: top; width: 50%;"><code>Locale("de")</code></td> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1305 |
* <td style="vertical-align: top; width: 50%;"><code>java.class</code></td> |
2 | 1306 |
* </tr> |
1307 |
* <tr> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1308 |
* <td style="vertical-align: top; width: 50%;"><code>Locale("de")</code></td> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1309 |
* <td style="vertical-align: top; width: 50%;"><code>java.properties</code></td> |
2 | 1310 |
* </tr> |
1311 |
* <tr> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1312 |
* <td style="vertical-align: top; width: 50%;"><code>Locale("")</code><br> |
2 | 1313 |
* </td> |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1314 |
* <td style="vertical-align: top; width: 50%;"><code>java.class</code></td> |
2 | 1315 |
* </tr> |
1316 |
* <tr> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1317 |
* <td style="vertical-align: top; width: 50%;"><code>Locale("")</code></td> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1318 |
* <td style="vertical-align: top; width: 50%;"><code>java.properties</code></td> |
2 | 1319 |
* </tr> |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1320 |
* </tbody> |
2 | 1321 |
* </table> |
1322 |
* </li> |
|
1323 |
* |
|
1324 |
* <li>If the previous step has found no resource bundle, proceed to |
|
1325 |
* Step 6. If a bundle has been found that is a base bundle (a bundle |
|
1326 |
* for <code>Locale("")</code>), and the candidate locale list only contained |
|
1327 |
* <code>Locale("")</code>, return the bundle to the caller. If a bundle |
|
1328 |
* has been found that is a base bundle, but the candidate locale list |
|
1329 |
* contained locales other than Locale(""), put the bundle on hold and |
|
1330 |
* proceed to Step 6. If a bundle has been found that is not a base |
|
1331 |
* bundle, proceed to Step 7.</li> |
|
1332 |
* |
|
1333 |
* <li>The {@link ResourceBundle.Control#getFallbackLocale(String, |
|
1334 |
* Locale) control.getFallbackLocale} method is called to get a fallback |
|
1335 |
* locale (alternative to the current target locale) to try further |
|
1336 |
* finding a resource bundle. If the method returns a non-null locale, |
|
1337 |
* it becomes the next target locale and the loading process starts over |
|
1338 |
* from Step 3. Otherwise, if a base bundle was found and put on hold in |
|
1339 |
* a previous Step 5, it is returned to the caller now. Otherwise, a |
|
1340 |
* MissingResourceException is thrown.</li> |
|
1341 |
* |
|
1342 |
* <li>At this point, we have found a resource bundle that's not the |
|
1343 |
* base bundle. If this bundle set its parent during its instantiation, |
|
1344 |
* it is returned to the caller. Otherwise, its <a |
|
1345 |
* href="./ResourceBundle.html#parent_chain">parent chain</a> is |
|
1346 |
* instantiated based on the list of candidate locales from which it was |
|
1347 |
* found. Finally, the bundle is returned to the caller.</li> |
|
1348 |
* </ol> |
|
1349 |
* |
|
1350 |
* <p>During the resource bundle loading process above, this factory |
|
1351 |
* method looks up the cache before calling the {@link |
|
1352 |
* Control#newBundle(String, Locale, String, ClassLoader, boolean) |
|
1353 |
* control.newBundle} method. If the time-to-live period of the |
|
1354 |
* resource bundle found in the cache has expired, the factory method |
|
1355 |
* calls the {@link ResourceBundle.Control#needsReload(String, Locale, |
|
1356 |
* String, ClassLoader, ResourceBundle, long) control.needsReload} |
|
1357 |
* method to determine whether the resource bundle needs to be reloaded. |
|
1358 |
* If reloading is required, the factory method calls |
|
1359 |
* <code>control.newBundle</code> to reload the resource bundle. If |
|
1360 |
* <code>control.newBundle</code> returns <code>null</code>, the factory |
|
1361 |
* method puts a dummy resource bundle in the cache as a mark of |
|
1362 |
* nonexistent resource bundles in order to avoid lookup overhead for |
|
1363 |
* subsequent requests. Such dummy resource bundles are under the same |
|
1364 |
* expiration control as specified by <code>control</code>. |
|
1365 |
* |
|
1366 |
* <p>All resource bundles loaded are cached by default. Refer to |
|
1367 |
* {@link Control#getTimeToLive(String,Locale) |
|
1368 |
* control.getTimeToLive} for details. |
|
1369 |
* |
|
1370 |
* <p>The following is an example of the bundle loading process with the |
|
1371 |
* default <code>ResourceBundle.Control</code> implementation. |
|
1372 |
* |
|
1373 |
* <p>Conditions: |
|
1374 |
* <ul> |
|
1375 |
* <li>Base bundle name: <code>foo.bar.Messages</code> |
|
1376 |
* <li>Requested <code>Locale</code>: {@link Locale#ITALY}</li> |
|
1377 |
* <li>Default <code>Locale</code>: {@link Locale#FRENCH}</li> |
|
1378 |
* <li>Available resource bundles: |
|
1379 |
* <code>foo/bar/Messages_fr.properties</code> and |
|
1380 |
* <code>foo/bar/Messages.properties</code></li> |
|
1381 |
* </ul> |
|
1382 |
* |
|
1383 |
* <p>First, <code>getBundle</code> tries loading a resource bundle in |
|
1384 |
* the following sequence. |
|
1385 |
* |
|
1386 |
* <ul> |
|
1387 |
* <li>class <code>foo.bar.Messages_it_IT</code> |
|
1388 |
* <li>file <code>foo/bar/Messages_it_IT.properties</code> |
|
1389 |
* <li>class <code>foo.bar.Messages_it</code></li> |
|
1390 |
* <li>file <code>foo/bar/Messages_it.properties</code></li> |
|
1391 |
* <li>class <code>foo.bar.Messages</code></li> |
|
1392 |
* <li>file <code>foo/bar/Messages.properties</code></li> |
|
1393 |
* </ul> |
|
1394 |
* |
|
1395 |
* <p>At this point, <code>getBundle</code> finds |
|
1396 |
* <code>foo/bar/Messages.properties</code>, which is put on hold |
|
1397 |
* because it's the base bundle. <code>getBundle</code> calls {@link |
|
1398 |
* Control#getFallbackLocale(String, Locale) |
|
1399 |
* control.getFallbackLocale("foo.bar.Messages", Locale.ITALY)} which |
|
1400 |
* returns <code>Locale.FRENCH</code>. Next, <code>getBundle</code> |
|
1401 |
* tries loading a bundle in the following sequence. |
|
1402 |
* |
|
1403 |
* <ul> |
|
1404 |
* <li>class <code>foo.bar.Messages_fr</code></li> |
|
1405 |
* <li>file <code>foo/bar/Messages_fr.properties</code></li> |
|
1406 |
* <li>class <code>foo.bar.Messages</code></li> |
|
1407 |
* <li>file <code>foo/bar/Messages.properties</code></li> |
|
1408 |
* </ul> |
|
1409 |
* |
|
1410 |
* <p><code>getBundle</code> finds |
|
1411 |
* <code>foo/bar/Messages_fr.properties</code> and creates a |
|
1412 |
* <code>ResourceBundle</code> instance. Then, <code>getBundle</code> |
|
21278 | 1413 |
* sets up its parent chain from the list of the candidate locales. Only |
2 | 1414 |
* <code>foo/bar/Messages.properties</code> is found in the list and |
1415 |
* <code>getBundle</code> creates a <code>ResourceBundle</code> instance |
|
1416 |
* that becomes the parent of the instance for |
|
1417 |
* <code>foo/bar/Messages_fr.properties</code>. |
|
1418 |
* |
|
1419 |
* @param baseName |
|
1420 |
* the base name of the resource bundle, a fully qualified |
|
1421 |
* class name |
|
1422 |
* @param targetLocale |
|
1423 |
* the locale for which a resource bundle is desired |
|
1424 |
* @param loader |
|
1425 |
* the class loader from which to load the resource bundle |
|
1426 |
* @param control |
|
1427 |
* the control which gives information for the resource |
|
1428 |
* bundle loading process |
|
1429 |
* @return a resource bundle for the given base name and locale |
|
36511 | 1430 |
* @throws NullPointerException |
1431 |
* if <code>baseName</code>, <code>targetLocale</code>, |
|
1432 |
* <code>loader</code>, or <code>control</code> is |
|
1433 |
* <code>null</code> |
|
1434 |
* @throws MissingResourceException |
|
1435 |
* if no resource bundle for the specified base name can be found |
|
1436 |
* @throws IllegalArgumentException |
|
1437 |
* if the given <code>control</code> doesn't perform properly |
|
1438 |
* (e.g., <code>control.getCandidateLocales</code> returns null.) |
|
1439 |
* Note that validation of <code>control</code> is performed as |
|
1440 |
* needed. |
|
1441 |
* @throws UnsupportedOperationException |
|
1442 |
* if this method is called in a named module |
|
2 | 1443 |
* @since 1.6 |
1444 |
*/ |
|
36511 | 1445 |
@CallerSensitive |
2 | 1446 |
public static ResourceBundle getBundle(String baseName, Locale targetLocale, |
1447 |
ClassLoader loader, Control control) { |
|
1448 |
if (loader == null || control == null) { |
|
1449 |
throw new NullPointerException(); |
|
1450 |
} |
|
36511 | 1451 |
Class<?> caller = Reflection.getCallerClass(); |
1452 |
checkNamedModule(caller); |
|
1453 |
return getBundleImpl(baseName, targetLocale, caller, loader, control); |
|
2 | 1454 |
} |
1455 |
||
36511 | 1456 |
private static void checkNamedModule(Class<?> caller) { |
1457 |
if (caller.getModule().isNamed()) { |
|
1458 |
throw new UnsupportedOperationException( |
|
1459 |
"ResourceBundle.Control not supported in named modules"); |
|
1460 |
} |
|
1461 |
} |
|
1462 |
||
1463 |
private static ResourceBundle getBundleImpl(String baseName, |
|
1464 |
Locale locale, |
|
1465 |
Class<?> caller, |
|
1466 |
Control control) { |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1467 |
return getBundleImpl(baseName, locale, caller, caller.getClassLoader(), control); |
36511 | 1468 |
} |
1469 |
||
1470 |
/** |
|
1471 |
* This method will find resource bundles using the legacy mechanism |
|
1472 |
* if the caller is unnamed module or the given class loader is |
|
1473 |
* not the class loader of the caller module getting the resource |
|
1474 |
* bundle, i.e. find the class that is visible to the class loader |
|
1475 |
* and properties from unnamed module. |
|
1476 |
* |
|
1477 |
* The module-aware resource bundle lookup mechanism will load |
|
1478 |
* the service providers using the service loader mechanism |
|
1479 |
* as well as properties local in the caller module. |
|
1480 |
*/ |
|
1481 |
private static ResourceBundle getBundleImpl(String baseName, |
|
1482 |
Locale locale, |
|
1483 |
Class<?> caller, |
|
1484 |
ClassLoader loader, |
|
1485 |
Control control) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1486 |
if (caller == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1487 |
throw new InternalError("null caller"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1488 |
} |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1489 |
Module callerModule = caller.getModule(); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1490 |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1491 |
// get resource bundles for a named module only if loader is the module's class loader |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1492 |
if (callerModule.isNamed() && loader == getLoader(callerModule)) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1493 |
return getBundleImpl(callerModule, callerModule, baseName, locale, control); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1494 |
} |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1495 |
|
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1496 |
// find resource bundles from unnamed module of given class loader |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1497 |
// Java agent can add to the bootclasspath e.g. via |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1498 |
// java.lang.instrument.Instrumentation and load classes in unnamed module. |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1499 |
// It may call RB::getBundle that will end up here with loader == null. |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1500 |
Module unnamedModule = loader != null |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1501 |
? loader.getUnnamedModule() |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1502 |
: BootLoader.getUnnamedModule(); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1503 |
|
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1504 |
return getBundleImpl(callerModule, unnamedModule, baseName, locale, control); |
36511 | 1505 |
} |
1506 |
||
1507 |
private static ResourceBundle getBundleFromModule(Class<?> caller, |
|
1508 |
Module module, |
|
1509 |
String baseName, |
|
1510 |
Locale locale, |
|
1511 |
Control control) { |
|
1512 |
Objects.requireNonNull(module); |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1513 |
Module callerModule = caller.getModule(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1514 |
if (callerModule != module) { |
36511 | 1515 |
SecurityManager sm = System.getSecurityManager(); |
1516 |
if (sm != null) { |
|
1517 |
sm.checkPermission(GET_CLASSLOADER_PERMISSION); |
|
1518 |
} |
|
1519 |
} |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1520 |
return getBundleImpl(callerModule, module, baseName, locale, control); |
36511 | 1521 |
} |
1522 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1523 |
private static ResourceBundle getBundleImpl(Module callerModule, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1524 |
Module module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1525 |
String baseName, |
36511 | 1526 |
Locale locale, |
1527 |
Control control) { |
|
2 | 1528 |
if (locale == null || control == null) { |
1529 |
throw new NullPointerException(); |
|
1530 |
} |
|
1531 |
||
36511 | 1532 |
// We create a CacheKey here for use by this call. The base name |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1533 |
// and modules will never change during the bundle loading |
2 | 1534 |
// process. We have to make sure that the locale is set before |
1535 |
// using it as a cache key. |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1536 |
CacheKey cacheKey = new CacheKey(baseName, locale, module, callerModule); |
2 | 1537 |
ResourceBundle bundle = null; |
1538 |
||
1539 |
// Quick lookup of the cache. |
|
1540 |
BundleReference bundleRef = cacheList.get(cacheKey); |
|
1541 |
if (bundleRef != null) { |
|
1542 |
bundle = bundleRef.get(); |
|
1543 |
bundleRef = null; |
|
1544 |
} |
|
1545 |
||
1546 |
// If this bundle and all of its parents are valid (not expired), |
|
1547 |
// then return this bundle. If any of the bundles is expired, we |
|
1548 |
// don't call control.needsReload here but instead drop into the |
|
1549 |
// complete loading process below. |
|
1550 |
if (isValidBundle(bundle) && hasValidParentChain(bundle)) { |
|
1551 |
return bundle; |
|
1552 |
} |
|
1553 |
||
1554 |
// No valid bundle was found in the cache, so we need to load the |
|
1555 |
// resource bundle and its parents. |
|
1556 |
||
1557 |
boolean isKnownControl = (control == Control.INSTANCE) || |
|
1558 |
(control instanceof SingleFormatControl); |
|
1559 |
List<String> formats = control.getFormats(baseName); |
|
1560 |
if (!isKnownControl && !checkList(formats)) { |
|
1561 |
throw new IllegalArgumentException("Invalid Control: getFormats"); |
|
1562 |
} |
|
1563 |
||
1564 |
ResourceBundle baseBundle = null; |
|
1565 |
for (Locale targetLocale = locale; |
|
1566 |
targetLocale != null; |
|
1567 |
targetLocale = control.getFallbackLocale(baseName, targetLocale)) { |
|
1568 |
List<Locale> candidateLocales = control.getCandidateLocales(baseName, targetLocale); |
|
1569 |
if (!isKnownControl && !checkList(candidateLocales)) { |
|
1570 |
throw new IllegalArgumentException("Invalid Control: getCandidateLocales"); |
|
1571 |
} |
|
1572 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1573 |
bundle = findBundle(callerModule, module, cacheKey, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1574 |
candidateLocales, formats, 0, control, baseBundle); |
2 | 1575 |
|
1576 |
// If the loaded bundle is the base bundle and exactly for the |
|
1577 |
// requested locale or the only candidate locale, then take the |
|
1578 |
// bundle as the resulting one. If the loaded bundle is the base |
|
1579 |
// bundle, it's put on hold until we finish processing all |
|
1580 |
// fallback locales. |
|
1581 |
if (isValidBundle(bundle)) { |
|
1582 |
boolean isBaseBundle = Locale.ROOT.equals(bundle.locale); |
|
1583 |
if (!isBaseBundle || bundle.locale.equals(locale) |
|
1584 |
|| (candidateLocales.size() == 1 |
|
1585 |
&& bundle.locale.equals(candidateLocales.get(0)))) { |
|
1586 |
break; |
|
1587 |
} |
|
1588 |
||
1589 |
// If the base bundle has been loaded, keep the reference in |
|
1590 |
// baseBundle so that we can avoid any redundant loading in case |
|
1591 |
// the control specify not to cache bundles. |
|
1592 |
if (isBaseBundle && baseBundle == null) { |
|
1593 |
baseBundle = bundle; |
|
1594 |
} |
|
1595 |
} |
|
1596 |
} |
|
1597 |
||
1598 |
if (bundle == null) { |
|
1599 |
if (baseBundle == null) { |
|
1600 |
throwMissingResourceException(baseName, locale, cacheKey.getCause()); |
|
1601 |
} |
|
1602 |
bundle = baseBundle; |
|
1603 |
} |
|
1604 |
||
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1605 |
// keep callerModule and module reachable for as long as we are operating |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1606 |
// with WeakReference(s) to them (in CacheKey)... |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1607 |
Reference.reachabilityFence(callerModule); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1608 |
Reference.reachabilityFence(module); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1609 |
|
2 | 1610 |
return bundle; |
1611 |
} |
|
1612 |
||
1613 |
/** |
|
1614 |
* Checks if the given <code>List</code> is not null, not empty, |
|
1615 |
* not having null in its elements. |
|
1616 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1617 |
private static boolean checkList(List<?> a) { |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1618 |
boolean valid = (a != null && !a.isEmpty()); |
2 | 1619 |
if (valid) { |
1620 |
int size = a.size(); |
|
1621 |
for (int i = 0; valid && i < size; i++) { |
|
1622 |
valid = (a.get(i) != null); |
|
1623 |
} |
|
1624 |
} |
|
1625 |
return valid; |
|
1626 |
} |
|
1627 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1628 |
private static ResourceBundle findBundle(Module callerModule, |
36511 | 1629 |
Module module, |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1630 |
CacheKey cacheKey, |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1631 |
List<Locale> candidateLocales, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1632 |
List<String> formats, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1633 |
int index, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1634 |
Control control, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1635 |
ResourceBundle baseBundle) { |
2 | 1636 |
Locale targetLocale = candidateLocales.get(index); |
1637 |
ResourceBundle parent = null; |
|
1638 |
if (index != candidateLocales.size() - 1) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1639 |
parent = findBundle(callerModule, module, cacheKey, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1640 |
candidateLocales, formats, index + 1, |
2 | 1641 |
control, baseBundle); |
1642 |
} else if (baseBundle != null && Locale.ROOT.equals(targetLocale)) { |
|
1643 |
return baseBundle; |
|
1644 |
} |
|
1645 |
||
1646 |
// Before we do the real loading work, see whether we need to |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1647 |
// do some housekeeping: If references to modules or |
2 | 1648 |
// resource bundles have been nulled out, remove all related |
1649 |
// information from the cache. |
|
1650 |
Object ref; |
|
1651 |
while ((ref = referenceQueue.poll()) != null) { |
|
1652 |
cacheList.remove(((CacheKeyReference)ref).getCacheKey()); |
|
1653 |
} |
|
1654 |
||
1655 |
// flag indicating the resource bundle has expired in the cache |
|
1656 |
boolean expiredBundle = false; |
|
1657 |
||
1658 |
// First, look up the cache to see if it's in the cache, without |
|
6660
7bc303389b4f
6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents:
6501
diff
changeset
|
1659 |
// attempting to load bundle. |
2 | 1660 |
cacheKey.setLocale(targetLocale); |
1661 |
ResourceBundle bundle = findBundleInCache(cacheKey, control); |
|
1662 |
if (isValidBundle(bundle)) { |
|
1663 |
expiredBundle = bundle.expired; |
|
1664 |
if (!expiredBundle) { |
|
1665 |
// If its parent is the one asked for by the candidate |
|
1666 |
// locales (the runtime lookup path), we can take the cached |
|
1667 |
// one. (If it's not identical, then we'd have to check the |
|
1668 |
// parent's parents to be consistent with what's been |
|
1669 |
// requested.) |
|
1670 |
if (bundle.parent == parent) { |
|
1671 |
return bundle; |
|
1672 |
} |
|
1673 |
// Otherwise, remove the cached one since we can't keep |
|
1674 |
// the same bundles having different parents. |
|
1675 |
BundleReference bundleRef = cacheList.get(cacheKey); |
|
1676 |
if (bundleRef != null && bundleRef.get() == bundle) { |
|
1677 |
cacheList.remove(cacheKey, bundleRef); |
|
1678 |
} |
|
1679 |
} |
|
1680 |
} |
|
1681 |
||
1682 |
if (bundle != NONEXISTENT_BUNDLE) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1683 |
trace("findBundle: %d %s %s formats: %s%n", index, candidateLocales, cacheKey, formats); |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1684 |
if (module.isNamed()) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1685 |
bundle = loadBundle(cacheKey, formats, control, module, callerModule); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1686 |
} else { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1687 |
bundle = loadBundle(cacheKey, formats, control, expiredBundle); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1688 |
} |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1689 |
if (bundle != null) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1690 |
if (bundle.parent == null) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1691 |
bundle.setParent(parent); |
36511 | 1692 |
} |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1693 |
bundle.locale = targetLocale; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1694 |
bundle = putBundleInCache(cacheKey, bundle, control); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1695 |
return bundle; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1696 |
} |
2 | 1697 |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1698 |
// Put NONEXISTENT_BUNDLE in the cache as a mark that there's no bundle |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1699 |
// instance for the locale. |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1700 |
putBundleInCache(cacheKey, NONEXISTENT_BUNDLE, control); |
2 | 1701 |
} |
1702 |
return parent; |
|
1703 |
} |
|
1704 |
||
36511 | 1705 |
private static final String UNKNOWN_FORMAT = ""; |
1706 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1707 |
|
36511 | 1708 |
/* |
1709 |
* Loads a ResourceBundle in named modules |
|
1710 |
*/ |
|
1711 |
private static ResourceBundle loadBundle(CacheKey cacheKey, |
|
1712 |
List<String> formats, |
|
1713 |
Control control, |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1714 |
Module module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1715 |
Module callerModule) { |
36511 | 1716 |
String baseName = cacheKey.getName(); |
1717 |
Locale targetLocale = cacheKey.getLocale(); |
|
1718 |
||
1719 |
ResourceBundle bundle = null; |
|
1720 |
if (cacheKey.hasProviders()) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1721 |
if (callerModule == module) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1722 |
bundle = loadBundleFromProviders(baseName, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1723 |
targetLocale, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1724 |
cacheKey.getProviders(), |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1725 |
cacheKey); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1726 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1727 |
// load from provider if the caller module has access to the |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1728 |
// service type and also declares `uses` |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1729 |
ClassLoader loader = getLoader(module); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1730 |
Class<ResourceBundleProvider> svc = |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1731 |
getResourceBundleProviderType(baseName, loader); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1732 |
if (svc != null |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1733 |
&& Reflection.verifyModuleAccess(callerModule, svc) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1734 |
&& callerModule.canUse(svc)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1735 |
bundle = loadBundleFromProviders(baseName, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1736 |
targetLocale, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1737 |
cacheKey.getProviders(), |
42467
46018567e1a6
8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents:
42338
diff
changeset
|
1738 |
cacheKey); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1739 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1740 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1741 |
|
36511 | 1742 |
if (bundle != null) { |
1743 |
cacheKey.setFormat(UNKNOWN_FORMAT); |
|
1744 |
} |
|
1745 |
} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1746 |
|
36511 | 1747 |
// If none of providers returned a bundle and the caller has no provider, |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1748 |
// look up module-local bundles or from the class path |
36511 | 1749 |
if (bundle == null && !cacheKey.callerHasProvider()) { |
1750 |
for (String format : formats) { |
|
1751 |
try { |
|
1752 |
switch (format) { |
|
1753 |
case "java.class": |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1754 |
bundle = ResourceBundleProviderHelper |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1755 |
.loadResourceBundle(callerModule, module, baseName, targetLocale); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1756 |
|
36511 | 1757 |
break; |
1758 |
case "java.properties": |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1759 |
bundle = ResourceBundleProviderHelper |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1760 |
.loadPropertyResourceBundle(callerModule, module, baseName, targetLocale); |
36511 | 1761 |
break; |
1762 |
default: |
|
1763 |
throw new InternalError("unexpected format: " + format); |
|
1764 |
} |
|
1765 |
||
1766 |
if (bundle != null) { |
|
1767 |
cacheKey.setFormat(format); |
|
1768 |
break; |
|
1769 |
} |
|
1770 |
} catch (Exception e) { |
|
1771 |
cacheKey.setCause(e); |
|
1772 |
} |
|
1773 |
} |
|
1774 |
} |
|
1775 |
return bundle; |
|
1776 |
} |
|
1777 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1778 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1779 |
* Returns a ServiceLoader that will find providers that are bound to |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1780 |
* a given named module. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1781 |
*/ |
36511 | 1782 |
private static ServiceLoader<ResourceBundleProvider> getServiceLoader(Module module, |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1783 |
String baseName) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1784 |
{ |
39768
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
1785 |
if (!module.isNamed()) { |
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
1786 |
return null; |
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
1787 |
} |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1788 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1789 |
ClassLoader loader = getLoader(module); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1790 |
Class<ResourceBundleProvider> service = |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1791 |
getResourceBundleProviderType(baseName, loader); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1792 |
if (service != null && Reflection.verifyModuleAccess(module, service)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1793 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1794 |
// locate providers that are visible to the class loader |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1795 |
// ServiceConfigurationError will be thrown if the module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1796 |
// does not declare `uses` the service type |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1797 |
return ServiceLoader.load(service, loader, module); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1798 |
} catch (ServiceConfigurationError e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1799 |
// "uses" not declared |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1800 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1801 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1802 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1803 |
return null; |
36511 | 1804 |
} |
1805 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1806 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1807 |
* Returns the service type of the given baseName that is visible |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1808 |
* to the given class loader |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1809 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1810 |
private static Class<ResourceBundleProvider> |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1811 |
getResourceBundleProviderType(String baseName, ClassLoader loader) |
36511 | 1812 |
{ |
1813 |
// Look up <baseName> + "Provider" |
|
1814 |
String providerName = baseName + "Provider"; |
|
1815 |
// Use the class loader of the getBundle caller so that the caller's |
|
1816 |
// visibility of the provider type is checked. |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1817 |
return AccessController.doPrivileged( |
36511 | 1818 |
new PrivilegedAction<>() { |
1819 |
@Override |
|
1820 |
public Class<ResourceBundleProvider> run() { |
|
1821 |
try { |
|
1822 |
Class<?> c = Class.forName(providerName, false, loader); |
|
1823 |
if (ResourceBundleProvider.class.isAssignableFrom(c)) { |
|
1824 |
@SuppressWarnings("unchecked") |
|
1825 |
Class<ResourceBundleProvider> s = (Class<ResourceBundleProvider>) c; |
|
1826 |
return s; |
|
1827 |
} |
|
1828 |
} catch (ClassNotFoundException e) {} |
|
1829 |
return null; |
|
1830 |
} |
|
1831 |
}); |
|
1832 |
} |
|
1833 |
||
1834 |
/** |
|
1835 |
* Loads ResourceBundle from service providers. |
|
1836 |
*/ |
|
1837 |
private static ResourceBundle loadBundleFromProviders(String baseName, |
|
1838 |
Locale locale, |
|
1839 |
ServiceLoader<ResourceBundleProvider> providers, |
|
1840 |
CacheKey cacheKey) |
|
1841 |
{ |
|
1842 |
if (providers == null) return null; |
|
1843 |
||
1844 |
return AccessController.doPrivileged( |
|
1845 |
new PrivilegedAction<>() { |
|
1846 |
public ResourceBundle run() { |
|
1847 |
for (Iterator<ResourceBundleProvider> itr = providers.iterator(); itr.hasNext(); ) { |
|
1848 |
try { |
|
1849 |
ResourceBundleProvider provider = itr.next(); |
|
1850 |
if (cacheKey != null && cacheKey.callerHasProvider == null |
|
1851 |
&& cacheKey.getModule() == provider.getClass().getModule()) { |
|
1852 |
cacheKey.callerHasProvider = Boolean.TRUE; |
|
1853 |
} |
|
1854 |
ResourceBundle bundle = provider.getBundle(baseName, locale); |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1855 |
trace("provider %s %s locale: %s bundle: %s%n", provider, baseName, locale, bundle); |
36511 | 1856 |
if (bundle != null) { |
1857 |
return bundle; |
|
1858 |
} |
|
1859 |
} catch (ServiceConfigurationError | SecurityException e) { |
|
1860 |
if (cacheKey != null) { |
|
1861 |
cacheKey.setCause(e); |
|
1862 |
} |
|
1863 |
} |
|
1864 |
} |
|
1865 |
if (cacheKey != null && cacheKey.callerHasProvider == null) { |
|
1866 |
cacheKey.callerHasProvider = Boolean.FALSE; |
|
1867 |
} |
|
1868 |
return null; |
|
1869 |
} |
|
1870 |
}); |
|
1871 |
||
1872 |
} |
|
1873 |
||
1874 |
/* |
|
1875 |
* Legacy mechanism to load resource bundles |
|
1876 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1877 |
private static ResourceBundle loadBundle(CacheKey cacheKey, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1878 |
List<String> formats, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1879 |
Control control, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1880 |
boolean reload) { |
2 | 1881 |
|
1882 |
// Here we actually load the bundle in the order of formats |
|
1883 |
// specified by the getFormats() value. |
|
1884 |
Locale targetLocale = cacheKey.getLocale(); |
|
1885 |
||
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1886 |
Module module = cacheKey.getModule(); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1887 |
if (module == null) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1888 |
// should not happen |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1889 |
throw new InternalError( |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1890 |
"Module for cache key: " + cacheKey + " has been GCed."); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1891 |
} |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1892 |
ClassLoader loader = getLoaderForControl(module); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1893 |
|
2 | 1894 |
ResourceBundle bundle = null; |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21949
diff
changeset
|
1895 |
for (String format : formats) { |
2 | 1896 |
try { |
36511 | 1897 |
// ResourceBundle.Control.newBundle may be overridden |
2 | 1898 |
bundle = control.newBundle(cacheKey.getName(), targetLocale, format, |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
1899 |
loader, reload); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21949
diff
changeset
|
1900 |
} catch (LinkageError | Exception error) { |
2 | 1901 |
// We need to handle the LinkageError case due to |
1902 |
// inconsistent case-sensitivity in ClassLoader. |
|
1903 |
// See 6572242 for details. |
|
1904 |
cacheKey.setCause(error); |
|
1905 |
} |
|
1906 |
if (bundle != null) { |
|
1907 |
// Set the format in the cache key so that it can be |
|
1908 |
// used when calling needsReload later. |
|
1909 |
cacheKey.setFormat(format); |
|
1910 |
bundle.name = cacheKey.getName(); |
|
1911 |
bundle.locale = targetLocale; |
|
1912 |
// Bundle provider might reuse instances. So we should make |
|
1913 |
// sure to clear the expired flag here. |
|
1914 |
bundle.expired = false; |
|
1915 |
break; |
|
1916 |
} |
|
1917 |
} |
|
1918 |
||
1919 |
return bundle; |
|
1920 |
} |
|
1921 |
||
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1922 |
private static boolean isValidBundle(ResourceBundle bundle) { |
2 | 1923 |
return bundle != null && bundle != NONEXISTENT_BUNDLE; |
1924 |
} |
|
1925 |
||
1926 |
/** |
|
1927 |
* Determines whether any of resource bundles in the parent chain, |
|
1928 |
* including the leaf, have expired. |
|
1929 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1930 |
private static boolean hasValidParentChain(ResourceBundle bundle) { |
2 | 1931 |
long now = System.currentTimeMillis(); |
1932 |
while (bundle != null) { |
|
1933 |
if (bundle.expired) { |
|
1934 |
return false; |
|
1935 |
} |
|
1936 |
CacheKey key = bundle.cacheKey; |
|
1937 |
if (key != null) { |
|
1938 |
long expirationTime = key.expirationTime; |
|
1939 |
if (expirationTime >= 0 && expirationTime <= now) { |
|
1940 |
return false; |
|
1941 |
} |
|
1942 |
} |
|
1943 |
bundle = bundle.parent; |
|
1944 |
} |
|
1945 |
return true; |
|
1946 |
} |
|
1947 |
||
1948 |
/** |
|
1949 |
* Throw a MissingResourceException with proper message |
|
1950 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1951 |
private static void throwMissingResourceException(String baseName, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1952 |
Locale locale, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1953 |
Throwable cause) { |
2 | 1954 |
// If the cause is a MissingResourceException, avoid creating |
1955 |
// a long chain. (6355009) |
|
1956 |
if (cause instanceof MissingResourceException) { |
|
1957 |
cause = null; |
|
1958 |
} |
|
1959 |
throw new MissingResourceException("Can't find bundle for base name " |
|
1960 |
+ baseName + ", locale " + locale, |
|
1961 |
baseName + "_" + locale, // className |
|
1962 |
"", // key |
|
1963 |
cause); |
|
1964 |
} |
|
1965 |
||
1966 |
/** |
|
1967 |
* Finds a bundle in the cache. Any expired bundles are marked as |
|
1968 |
* `expired' and removed from the cache upon return. |
|
1969 |
* |
|
1970 |
* @param cacheKey the key to look up the cache |
|
1971 |
* @param control the Control to be used for the expiration control |
|
1972 |
* @return the cached bundle, or null if the bundle is not found in the |
|
1973 |
* cache or its parent has expired. <code>bundle.expire</code> is true |
|
1974 |
* upon return if the bundle in the cache has expired. |
|
1975 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1976 |
private static ResourceBundle findBundleInCache(CacheKey cacheKey, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
1977 |
Control control) { |
2 | 1978 |
BundleReference bundleRef = cacheList.get(cacheKey); |
1979 |
if (bundleRef == null) { |
|
1980 |
return null; |
|
1981 |
} |
|
1982 |
ResourceBundle bundle = bundleRef.get(); |
|
1983 |
if (bundle == null) { |
|
1984 |
return null; |
|
1985 |
} |
|
1986 |
ResourceBundle p = bundle.parent; |
|
1987 |
assert p != NONEXISTENT_BUNDLE; |
|
1988 |
// If the parent has expired, then this one must also expire. We |
|
1989 |
// check only the immediate parent because the actual loading is |
|
1990 |
// done from the root (base) to leaf (child) and the purpose of |
|
1991 |
// checking is to propagate expiration towards the leaf. For |
|
1992 |
// example, if the requested locale is ja_JP_JP and there are |
|
1993 |
// bundles for all of the candidates in the cache, we have a list, |
|
1994 |
// |
|
1995 |
// base <- ja <- ja_JP <- ja_JP_JP |
|
1996 |
// |
|
1997 |
// If ja has expired, then it will reload ja and the list becomes a |
|
1998 |
// tree. |
|
1999 |
// |
|
2000 |
// base <- ja (new) |
|
2001 |
// " <- ja (expired) <- ja_JP <- ja_JP_JP |
|
2002 |
// |
|
2003 |
// When looking up ja_JP in the cache, it finds ja_JP in the cache |
|
2004 |
// which references to the expired ja. Then, ja_JP is marked as |
|
2005 |
// expired and removed from the cache. This will be propagated to |
|
2006 |
// ja_JP_JP. |
|
2007 |
// |
|
2008 |
// Now, it's possible, for example, that while loading new ja_JP, |
|
2009 |
// someone else has started loading the same bundle and finds the |
|
2010 |
// base bundle has expired. Then, what we get from the first |
|
2011 |
// getBundle call includes the expired base bundle. However, if |
|
2012 |
// someone else didn't start its loading, we wouldn't know if the |
|
2013 |
// base bundle has expired at the end of the loading process. The |
|
2014 |
// expiration control doesn't guarantee that the returned bundle and |
|
2015 |
// its parents haven't expired. |
|
2016 |
// |
|
2017 |
// We could check the entire parent chain to see if there's any in |
|
2018 |
// the chain that has expired. But this process may never end. An |
|
2019 |
// extreme case would be that getTimeToLive returns 0 and |
|
2020 |
// needsReload always returns true. |
|
2021 |
if (p != null && p.expired) { |
|
2022 |
assert bundle != NONEXISTENT_BUNDLE; |
|
2023 |
bundle.expired = true; |
|
2024 |
bundle.cacheKey = null; |
|
2025 |
cacheList.remove(cacheKey, bundleRef); |
|
2026 |
bundle = null; |
|
2027 |
} else { |
|
2028 |
CacheKey key = bundleRef.getCacheKey(); |
|
2029 |
long expirationTime = key.expirationTime; |
|
2030 |
if (!bundle.expired && expirationTime >= 0 && |
|
2031 |
expirationTime <= System.currentTimeMillis()) { |
|
2032 |
// its TTL period has expired. |
|
2033 |
if (bundle != NONEXISTENT_BUNDLE) { |
|
2034 |
// Synchronize here to call needsReload to avoid |
|
2035 |
// redundant concurrent calls for the same bundle. |
|
2036 |
synchronized (bundle) { |
|
2037 |
expirationTime = key.expirationTime; |
|
2038 |
if (!bundle.expired && expirationTime >= 0 && |
|
2039 |
expirationTime <= System.currentTimeMillis()) { |
|
2040 |
try { |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2041 |
Module module = cacheKey.getModule(); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2042 |
bundle.expired = |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2043 |
module == null || // already GCed |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2044 |
control.needsReload(key.getName(), |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2045 |
key.getLocale(), |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2046 |
key.getFormat(), |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2047 |
getLoaderForControl(module), |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2048 |
bundle, |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2049 |
key.loadTime); |
2 | 2050 |
} catch (Exception e) { |
2051 |
cacheKey.setCause(e); |
|
2052 |
} |
|
2053 |
if (bundle.expired) { |
|
2054 |
// If the bundle needs to be reloaded, then |
|
2055 |
// remove the bundle from the cache, but |
|
2056 |
// return the bundle with the expired flag |
|
2057 |
// on. |
|
2058 |
bundle.cacheKey = null; |
|
2059 |
cacheList.remove(cacheKey, bundleRef); |
|
2060 |
} else { |
|
2061 |
// Update the expiration control info. and reuse |
|
2062 |
// the same bundle instance |
|
2063 |
setExpirationTime(key, control); |
|
2064 |
} |
|
2065 |
} |
|
2066 |
} |
|
2067 |
} else { |
|
2068 |
// We just remove NONEXISTENT_BUNDLE from the cache. |
|
2069 |
cacheList.remove(cacheKey, bundleRef); |
|
2070 |
bundle = null; |
|
2071 |
} |
|
2072 |
} |
|
2073 |
} |
|
2074 |
return bundle; |
|
2075 |
} |
|
2076 |
||
2077 |
/** |
|
2078 |
* Put a new bundle in the cache. |
|
2079 |
* |
|
2080 |
* @param cacheKey the key for the resource bundle |
|
2081 |
* @param bundle the resource bundle to be put in the cache |
|
2082 |
* @return the ResourceBundle for the cacheKey; if someone has put |
|
2083 |
* the bundle before this call, the one found in the cache is |
|
2084 |
* returned. |
|
2085 |
*/ |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2086 |
private static ResourceBundle putBundleInCache(CacheKey cacheKey, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2087 |
ResourceBundle bundle, |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2088 |
Control control) { |
2 | 2089 |
setExpirationTime(cacheKey, control); |
2090 |
if (cacheKey.expirationTime != Control.TTL_DONT_CACHE) { |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2091 |
CacheKey key = new CacheKey(cacheKey); |
2 | 2092 |
BundleReference bundleRef = new BundleReference(bundle, referenceQueue, key); |
2093 |
bundle.cacheKey = key; |
|
2094 |
||
2095 |
// Put the bundle in the cache if it's not been in the cache. |
|
2096 |
BundleReference result = cacheList.putIfAbsent(key, bundleRef); |
|
2097 |
||
2098 |
// If someone else has put the same bundle in the cache before |
|
2099 |
// us and it has not expired, we should use the one in the cache. |
|
2100 |
if (result != null) { |
|
2101 |
ResourceBundle rb = result.get(); |
|
2102 |
if (rb != null && !rb.expired) { |
|
2103 |
// Clear the back link to the cache key |
|
2104 |
bundle.cacheKey = null; |
|
2105 |
bundle = rb; |
|
2106 |
// Clear the reference in the BundleReference so that |
|
2107 |
// it won't be enqueued. |
|
2108 |
bundleRef.clear(); |
|
2109 |
} else { |
|
2110 |
// Replace the invalid (garbage collected or expired) |
|
2111 |
// instance with the valid one. |
|
2112 |
cacheList.put(key, bundleRef); |
|
2113 |
} |
|
2114 |
} |
|
2115 |
} |
|
2116 |
return bundle; |
|
2117 |
} |
|
2118 |
||
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2119 |
private static void setExpirationTime(CacheKey cacheKey, Control control) { |
2 | 2120 |
long ttl = control.getTimeToLive(cacheKey.getName(), |
2121 |
cacheKey.getLocale()); |
|
2122 |
if (ttl >= 0) { |
|
2123 |
// If any expiration time is specified, set the time to be |
|
2124 |
// expired in the cache. |
|
2125 |
long now = System.currentTimeMillis(); |
|
2126 |
cacheKey.loadTime = now; |
|
2127 |
cacheKey.expirationTime = now + ttl; |
|
2128 |
} else if (ttl >= Control.TTL_NO_EXPIRATION_CONTROL) { |
|
2129 |
cacheKey.expirationTime = ttl; |
|
2130 |
} else { |
|
2131 |
throw new IllegalArgumentException("Invalid Control: TTL=" + ttl); |
|
2132 |
} |
|
2133 |
} |
|
2134 |
||
2135 |
/** |
|
2136 |
* Removes all resource bundles from the cache that have been loaded |
|
36511 | 2137 |
* by the caller's module using the caller's class loader. |
2 | 2138 |
* |
2139 |
* @since 1.6 |
|
2140 |
* @see ResourceBundle.Control#getTimeToLive(String,Locale) |
|
2141 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
2142 |
@CallerSensitive |
2 | 2143 |
public static final void clearCache() { |
36511 | 2144 |
Class<?> caller = Reflection.getCallerClass(); |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2145 |
clearCacheImpl(caller.getModule(), caller.getClassLoader()); |
2 | 2146 |
} |
2147 |
||
2148 |
/** |
|
2149 |
* Removes all resource bundles from the cache that have been loaded |
|
36511 | 2150 |
* by the caller's module using the given class loader. |
2 | 2151 |
* |
2152 |
* @param loader the class loader |
|
2153 |
* @exception NullPointerException if <code>loader</code> is null |
|
2154 |
* @since 1.6 |
|
2155 |
* @see ResourceBundle.Control#getTimeToLive(String,Locale) |
|
2156 |
*/ |
|
36511 | 2157 |
@CallerSensitive |
2 | 2158 |
public static final void clearCache(ClassLoader loader) { |
36511 | 2159 |
Objects.requireNonNull(loader); |
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2160 |
Class<?> caller = Reflection.getCallerClass(); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2161 |
clearCacheImpl(caller.getModule(), loader); |
36511 | 2162 |
} |
2163 |
||
2164 |
/** |
|
2165 |
* Removes all resource bundles from the cache that have been loaded by the |
|
2166 |
* given {@code module}. |
|
2167 |
* |
|
2168 |
* @param module the module |
|
2169 |
* @throws NullPointerException |
|
2170 |
* if {@code module} is {@code null} |
|
2171 |
* @throws SecurityException |
|
2172 |
* if the caller doesn't have the permission to |
|
2173 |
* {@linkplain Module#getClassLoader() get the class loader} |
|
2174 |
* of the given {@code module} |
|
2175 |
* @since 9 |
|
2176 |
* @see ResourceBundle.Control#getTimeToLive(String,Locale) |
|
2177 |
*/ |
|
2178 |
public static final void clearCache(Module module) { |
|
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2179 |
Objects.requireNonNull(module); |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2180 |
clearCacheImpl(module, module.getClassLoader()); |
36511 | 2181 |
} |
2182 |
||
43182
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2183 |
private static void clearCacheImpl(Module callerModule, ClassLoader loader) { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2184 |
cacheList.keySet().removeIf( |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2185 |
key -> { |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2186 |
Module m; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2187 |
return key.getCallerModule() == callerModule && |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2188 |
(m = key.getModule()) != null && |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2189 |
getLoader(m) == loader; |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2190 |
} |
66e6655abfde
8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents:
42930
diff
changeset
|
2191 |
); |
2 | 2192 |
} |
2193 |
||
2194 |
/** |
|
2195 |
* Gets an object for the given key from this resource bundle. |
|
2196 |
* Returns null if this resource bundle does not contain an |
|
2197 |
* object for the given key. |
|
2198 |
* |
|
2199 |
* @param key the key for the desired object |
|
2200 |
* @exception NullPointerException if <code>key</code> is <code>null</code> |
|
2201 |
* @return the object for the given key, or null |
|
2202 |
*/ |
|
2203 |
protected abstract Object handleGetObject(String key); |
|
2204 |
||
2205 |
/** |
|
2206 |
* Returns an enumeration of the keys. |
|
2207 |
* |
|
2208 |
* @return an <code>Enumeration</code> of the keys contained in |
|
2209 |
* this <code>ResourceBundle</code> and its parent bundles. |
|
2210 |
*/ |
|
2211 |
public abstract Enumeration<String> getKeys(); |
|
2212 |
||
2213 |
/** |
|
2214 |
* Determines whether the given <code>key</code> is contained in |
|
2215 |
* this <code>ResourceBundle</code> or its parent bundles. |
|
2216 |
* |
|
2217 |
* @param key |
|
2218 |
* the resource <code>key</code> |
|
2219 |
* @return <code>true</code> if the given <code>key</code> is |
|
2220 |
* contained in this <code>ResourceBundle</code> or its |
|
2221 |
* parent bundles; <code>false</code> otherwise. |
|
2222 |
* @exception NullPointerException |
|
2223 |
* if <code>key</code> is <code>null</code> |
|
2224 |
* @since 1.6 |
|
2225 |
*/ |
|
2226 |
public boolean containsKey(String key) { |
|
2227 |
if (key == null) { |
|
2228 |
throw new NullPointerException(); |
|
2229 |
} |
|
2230 |
for (ResourceBundle rb = this; rb != null; rb = rb.parent) { |
|
2231 |
if (rb.handleKeySet().contains(key)) { |
|
2232 |
return true; |
|
2233 |
} |
|
2234 |
} |
|
2235 |
return false; |
|
2236 |
} |
|
2237 |
||
2238 |
/** |
|
2239 |
* Returns a <code>Set</code> of all keys contained in this |
|
2240 |
* <code>ResourceBundle</code> and its parent bundles. |
|
2241 |
* |
|
2242 |
* @return a <code>Set</code> of all keys contained in this |
|
2243 |
* <code>ResourceBundle</code> and its parent bundles. |
|
2244 |
* @since 1.6 |
|
2245 |
*/ |
|
2246 |
public Set<String> keySet() { |
|
9224 | 2247 |
Set<String> keys = new HashSet<>(); |
2 | 2248 |
for (ResourceBundle rb = this; rb != null; rb = rb.parent) { |
2249 |
keys.addAll(rb.handleKeySet()); |
|
2250 |
} |
|
2251 |
return keys; |
|
2252 |
} |
|
2253 |
||
2254 |
/** |
|
2255 |
* Returns a <code>Set</code> of the keys contained <em>only</em> |
|
2256 |
* in this <code>ResourceBundle</code>. |
|
2257 |
* |
|
2258 |
* <p>The default implementation returns a <code>Set</code> of the |
|
2259 |
* keys returned by the {@link #getKeys() getKeys} method except |
|
2260 |
* for the ones for which the {@link #handleGetObject(String) |
|
2261 |
* handleGetObject} method returns <code>null</code>. Once the |
|
2262 |
* <code>Set</code> has been created, the value is kept in this |
|
2263 |
* <code>ResourceBundle</code> in order to avoid producing the |
|
6501 | 2264 |
* same <code>Set</code> in subsequent calls. Subclasses can |
2265 |
* override this method for faster handling. |
|
2 | 2266 |
* |
2267 |
* @return a <code>Set</code> of the keys contained only in this |
|
2268 |
* <code>ResourceBundle</code> |
|
2269 |
* @since 1.6 |
|
2270 |
*/ |
|
2271 |
protected Set<String> handleKeySet() { |
|
2272 |
if (keySet == null) { |
|
2273 |
synchronized (this) { |
|
2274 |
if (keySet == null) { |
|
9224 | 2275 |
Set<String> keys = new HashSet<>(); |
2 | 2276 |
Enumeration<String> enumKeys = getKeys(); |
2277 |
while (enumKeys.hasMoreElements()) { |
|
2278 |
String key = enumKeys.nextElement(); |
|
2279 |
if (handleGetObject(key) != null) { |
|
2280 |
keys.add(key); |
|
2281 |
} |
|
2282 |
} |
|
2283 |
keySet = keys; |
|
2284 |
} |
|
2285 |
} |
|
2286 |
} |
|
2287 |
return keySet; |
|
2288 |
} |
|
2289 |
||
2290 |
||
2291 |
||
2292 |
/** |
|
2293 |
* <code>ResourceBundle.Control</code> defines a set of callback methods |
|
2294 |
* that are invoked by the {@link ResourceBundle#getBundle(String, |
|
2295 |
* Locale, ClassLoader, Control) ResourceBundle.getBundle} factory |
|
2296 |
* methods during the bundle loading process. In other words, a |
|
2297 |
* <code>ResourceBundle.Control</code> collaborates with the factory |
|
2298 |
* methods for loading resource bundles. The default implementation of |
|
2299 |
* the callback methods provides the information necessary for the |
|
2300 |
* factory methods to perform the <a |
|
2301 |
* href="./ResourceBundle.html#default_behavior">default behavior</a>. |
|
36511 | 2302 |
* <a href="#note">Note that this class is not supported in named modules.</a> |
2 | 2303 |
* |
2304 |
* <p>In addition to the callback methods, the {@link |
|
2305 |
* #toBundleName(String, Locale) toBundleName} and {@link |
|
2306 |
* #toResourceName(String, String) toResourceName} methods are defined |
|
2307 |
* primarily for convenience in implementing the callback |
|
2308 |
* methods. However, the <code>toBundleName</code> method could be |
|
2309 |
* overridden to provide different conventions in the organization and |
|
2310 |
* packaging of localized resources. The <code>toResourceName</code> |
|
2311 |
* method is <code>final</code> to avoid use of wrong resource and class |
|
2312 |
* name separators. |
|
2313 |
* |
|
2314 |
* <p>Two factory methods, {@link #getControl(List)} and {@link |
|
2315 |
* #getNoFallbackControl(List)}, provide |
|
2316 |
* <code>ResourceBundle.Control</code> instances that implement common |
|
2317 |
* variations of the default bundle loading process. |
|
2318 |
* |
|
2319 |
* <p>The formats returned by the {@link Control#getFormats(String) |
|
2320 |
* getFormats} method and candidate locales returned by the {@link |
|
2321 |
* ResourceBundle.Control#getCandidateLocales(String, Locale) |
|
2322 |
* getCandidateLocales} method must be consistent in all |
|
2323 |
* <code>ResourceBundle.getBundle</code> invocations for the same base |
|
2324 |
* bundle. Otherwise, the <code>ResourceBundle.getBundle</code> methods |
|
2325 |
* may return unintended bundles. For example, if only |
|
2326 |
* <code>"java.class"</code> is returned by the <code>getFormats</code> |
|
2327 |
* method for the first call to <code>ResourceBundle.getBundle</code> |
|
2328 |
* and only <code>"java.properties"</code> for the second call, then the |
|
2329 |
* second call will return the class-based one that has been cached |
|
2330 |
* during the first call. |
|
2331 |
* |
|
2332 |
* <p>A <code>ResourceBundle.Control</code> instance must be thread-safe |
|
2333 |
* if it's simultaneously used by multiple threads. |
|
2334 |
* <code>ResourceBundle.getBundle</code> does not synchronize to call |
|
2335 |
* the <code>ResourceBundle.Control</code> methods. The default |
|
2336 |
* implementations of the methods are thread-safe. |
|
2337 |
* |
|
2338 |
* <p>Applications can specify <code>ResourceBundle.Control</code> |
|
2339 |
* instances returned by the <code>getControl</code> factory methods or |
|
2340 |
* created from a subclass of <code>ResourceBundle.Control</code> to |
|
2341 |
* customize the bundle loading process. The following are examples of |
|
2342 |
* changing the default bundle loading process. |
|
2343 |
* |
|
2344 |
* <p><b>Example 1</b> |
|
2345 |
* |
|
2346 |
* <p>The following code lets <code>ResourceBundle.getBundle</code> look |
|
2347 |
* up only properties-based resources. |
|
2348 |
* |
|
2349 |
* <pre> |
|
2350 |
* import java.util.*; |
|
2351 |
* import static java.util.ResourceBundle.Control.*; |
|
2352 |
* ... |
|
2353 |
* ResourceBundle bundle = |
|
2354 |
* ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"), |
|
2355 |
* ResourceBundle.Control.getControl(FORMAT_PROPERTIES)); |
|
2356 |
* </pre> |
|
2357 |
* |
|
2358 |
* Given the resource bundles in the <a |
|
2359 |
* href="./ResourceBundle.html#default_behavior_example">example</a> in |
|
2360 |
* the <code>ResourceBundle.getBundle</code> description, this |
|
2361 |
* <code>ResourceBundle.getBundle</code> call loads |
|
2362 |
* <code>MyResources_fr_CH.properties</code> whose parent is |
|
2363 |
* <code>MyResources_fr.properties</code> whose parent is |
|
2364 |
* <code>MyResources.properties</code>. (<code>MyResources_fr_CH.properties</code> |
|
2365 |
* is not hidden, but <code>MyResources_fr_CH.class</code> is.) |
|
2366 |
* |
|
2367 |
* <p><b>Example 2</b> |
|
2368 |
* |
|
2369 |
* <p>The following is an example of loading XML-based bundles |
|
2370 |
* using {@link Properties#loadFromXML(java.io.InputStream) |
|
2371 |
* Properties.loadFromXML}. |
|
2372 |
* |
|
2373 |
* <pre> |
|
2374 |
* ResourceBundle rb = ResourceBundle.getBundle("Messages", |
|
2375 |
* new ResourceBundle.Control() { |
|
2376 |
* public List<String> getFormats(String baseName) { |
|
2377 |
* if (baseName == null) |
|
2378 |
* throw new NullPointerException(); |
|
2379 |
* return Arrays.asList("xml"); |
|
2380 |
* } |
|
2381 |
* public ResourceBundle newBundle(String baseName, |
|
2382 |
* Locale locale, |
|
2383 |
* String format, |
|
2384 |
* ClassLoader loader, |
|
2385 |
* boolean reload) |
|
2386 |
* throws IllegalAccessException, |
|
2387 |
* InstantiationException, |
|
2388 |
* IOException { |
|
2389 |
* if (baseName == null || locale == null |
|
2390 |
* || format == null || loader == null) |
|
2391 |
* throw new NullPointerException(); |
|
2392 |
* ResourceBundle bundle = null; |
|
2393 |
* if (format.equals("xml")) { |
|
2394 |
* String bundleName = toBundleName(baseName, locale); |
|
2395 |
* String resourceName = toResourceName(bundleName, format); |
|
2396 |
* InputStream stream = null; |
|
2397 |
* if (reload) { |
|
2398 |
* URL url = loader.getResource(resourceName); |
|
2399 |
* if (url != null) { |
|
2400 |
* URLConnection connection = url.openConnection(); |
|
2401 |
* if (connection != null) { |
|
2402 |
* // Disable caches to get fresh data for |
|
2403 |
* // reloading. |
|
2404 |
* connection.setUseCaches(false); |
|
2405 |
* stream = connection.getInputStream(); |
|
2406 |
* } |
|
2407 |
* } |
|
2408 |
* } else { |
|
2409 |
* stream = loader.getResourceAsStream(resourceName); |
|
2410 |
* } |
|
2411 |
* if (stream != null) { |
|
2412 |
* BufferedInputStream bis = new BufferedInputStream(stream); |
|
2413 |
* bundle = new XMLResourceBundle(bis); |
|
2414 |
* bis.close(); |
|
2415 |
* } |
|
2416 |
* } |
|
2417 |
* return bundle; |
|
2418 |
* } |
|
2419 |
* }); |
|
2420 |
* |
|
2421 |
* ... |
|
2422 |
* |
|
2423 |
* private static class XMLResourceBundle extends ResourceBundle { |
|
2424 |
* private Properties props; |
|
2425 |
* XMLResourceBundle(InputStream stream) throws IOException { |
|
2426 |
* props = new Properties(); |
|
2427 |
* props.loadFromXML(stream); |
|
2428 |
* } |
|
2429 |
* protected Object handleGetObject(String key) { |
|
2430 |
* return props.getProperty(key); |
|
2431 |
* } |
|
2432 |
* public Enumeration<String> getKeys() { |
|
2433 |
* ... |
|
2434 |
* } |
|
2435 |
* } |
|
2436 |
* </pre> |
|
2437 |
* |
|
36511 | 2438 |
* @apiNote <a name="note">{@code ResourceBundle.Control} is not supported |
2439 |
* in named modules.</a> If the {@code ResourceBundle.getBundle} method with |
|
2440 |
* a {@code ResourceBundle.Control} is called in a named module, the method |
|
42769
c6ee006baceb
8171189: Deprecate ResourceBundleControlProvider for removal
naoto
parents:
42467
diff
changeset
|
2441 |
* will throw an {@link UnsupportedOperationException}. |
36511 | 2442 |
* |
2 | 2443 |
* @since 1.6 |
36511 | 2444 |
* @see java.util.spi.ResourceBundleProvider |
2 | 2445 |
*/ |
2446 |
public static class Control { |
|
2447 |
/** |
|
2448 |
* The default format <code>List</code>, which contains the strings |
|
2449 |
* <code>"java.class"</code> and <code>"java.properties"</code>, in |
|
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39768
diff
changeset
|
2450 |
* this order. This <code>List</code> is unmodifiable. |
2 | 2451 |
* |
2452 |
* @see #getFormats(String) |
|
2453 |
*/ |
|
2454 |
public static final List<String> FORMAT_DEFAULT |
|
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39768
diff
changeset
|
2455 |
= List.of("java.class", "java.properties"); |
2 | 2456 |
|
2457 |
/** |
|
2458 |
* The class-only format <code>List</code> containing |
|
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39768
diff
changeset
|
2459 |
* <code>"java.class"</code>. This <code>List</code> is unmodifiable. |
2 | 2460 |
* |
2461 |
* @see #getFormats(String) |
|
2462 |
*/ |
|
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39768
diff
changeset
|
2463 |
public static final List<String> FORMAT_CLASS = List.of("java.class"); |
2 | 2464 |
|
2465 |
/** |
|
2466 |
* The properties-only format <code>List</code> containing |
|
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39768
diff
changeset
|
2467 |
* <code>"java.properties"</code>. This <code>List</code> is unmodifiable. |
2 | 2468 |
* |
2469 |
* @see #getFormats(String) |
|
2470 |
*/ |
|
2471 |
public static final List<String> FORMAT_PROPERTIES |
|
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39768
diff
changeset
|
2472 |
= List.of("java.properties"); |
2 | 2473 |
|
2474 |
/** |
|
2475 |
* The time-to-live constant for not caching loaded resource bundle |
|
2476 |
* instances. |
|
2477 |
* |
|
2478 |
* @see #getTimeToLive(String, Locale) |
|
2479 |
*/ |
|
2480 |
public static final long TTL_DONT_CACHE = -1; |
|
2481 |
||
2482 |
/** |
|
2483 |
* The time-to-live constant for disabling the expiration control |
|
2484 |
* for loaded resource bundle instances in the cache. |
|
2485 |
* |
|
2486 |
* @see #getTimeToLive(String, Locale) |
|
2487 |
*/ |
|
2488 |
public static final long TTL_NO_EXPIRATION_CONTROL = -2; |
|
2489 |
||
2490 |
private static final Control INSTANCE = new Control(); |
|
2491 |
||
2492 |
/** |
|
2493 |
* Sole constructor. (For invocation by subclass constructors, |
|
2494 |
* typically implicit.) |
|
2495 |
*/ |
|
2496 |
protected Control() { |
|
2497 |
} |
|
2498 |
||
2499 |
/** |
|
2500 |
* Returns a <code>ResourceBundle.Control</code> in which the {@link |
|
2501 |
* #getFormats(String) getFormats} method returns the specified |
|
2502 |
* <code>formats</code>. The <code>formats</code> must be equal to |
|
2503 |
* one of {@link Control#FORMAT_PROPERTIES}, {@link |
|
2504 |
* Control#FORMAT_CLASS} or {@link |
|
2505 |
* Control#FORMAT_DEFAULT}. <code>ResourceBundle.Control</code> |
|
2506 |
* instances returned by this method are singletons and thread-safe. |
|
2507 |
* |
|
2508 |
* <p>Specifying {@link Control#FORMAT_DEFAULT} is equivalent to |
|
2509 |
* instantiating the <code>ResourceBundle.Control</code> class, |
|
2510 |
* except that this method returns a singleton. |
|
2511 |
* |
|
2512 |
* @param formats |
|
2513 |
* the formats to be returned by the |
|
2514 |
* <code>ResourceBundle.Control.getFormats</code> method |
|
2515 |
* @return a <code>ResourceBundle.Control</code> supporting the |
|
2516 |
* specified <code>formats</code> |
|
2517 |
* @exception NullPointerException |
|
2518 |
* if <code>formats</code> is <code>null</code> |
|
2519 |
* @exception IllegalArgumentException |
|
2520 |
* if <code>formats</code> is unknown |
|
2521 |
*/ |
|
2522 |
public static final Control getControl(List<String> formats) { |
|
2523 |
if (formats.equals(Control.FORMAT_PROPERTIES)) { |
|
2524 |
return SingleFormatControl.PROPERTIES_ONLY; |
|
2525 |
} |
|
2526 |
if (formats.equals(Control.FORMAT_CLASS)) { |
|
2527 |
return SingleFormatControl.CLASS_ONLY; |
|
2528 |
} |
|
2529 |
if (formats.equals(Control.FORMAT_DEFAULT)) { |
|
2530 |
return Control.INSTANCE; |
|
2531 |
} |
|
2532 |
throw new IllegalArgumentException(); |
|
2533 |
} |
|
2534 |
||
2535 |
/** |
|
2536 |
* Returns a <code>ResourceBundle.Control</code> in which the {@link |
|
2537 |
* #getFormats(String) getFormats} method returns the specified |
|
2538 |
* <code>formats</code> and the {@link |
|
2539 |
* Control#getFallbackLocale(String, Locale) getFallbackLocale} |
|
2540 |
* method returns <code>null</code>. The <code>formats</code> must |
|
2541 |
* be equal to one of {@link Control#FORMAT_PROPERTIES}, {@link |
|
2542 |
* Control#FORMAT_CLASS} or {@link Control#FORMAT_DEFAULT}. |
|
2543 |
* <code>ResourceBundle.Control</code> instances returned by this |
|
2544 |
* method are singletons and thread-safe. |
|
2545 |
* |
|
2546 |
* @param formats |
|
2547 |
* the formats to be returned by the |
|
2548 |
* <code>ResourceBundle.Control.getFormats</code> method |
|
2549 |
* @return a <code>ResourceBundle.Control</code> supporting the |
|
2550 |
* specified <code>formats</code> with no fallback |
|
2551 |
* <code>Locale</code> support |
|
2552 |
* @exception NullPointerException |
|
2553 |
* if <code>formats</code> is <code>null</code> |
|
2554 |
* @exception IllegalArgumentException |
|
2555 |
* if <code>formats</code> is unknown |
|
2556 |
*/ |
|
2557 |
public static final Control getNoFallbackControl(List<String> formats) { |
|
2558 |
if (formats.equals(Control.FORMAT_DEFAULT)) { |
|
2559 |
return NoFallbackControl.NO_FALLBACK; |
|
2560 |
} |
|
2561 |
if (formats.equals(Control.FORMAT_PROPERTIES)) { |
|
2562 |
return NoFallbackControl.PROPERTIES_ONLY_NO_FALLBACK; |
|
2563 |
} |
|
2564 |
if (formats.equals(Control.FORMAT_CLASS)) { |
|
2565 |
return NoFallbackControl.CLASS_ONLY_NO_FALLBACK; |
|
2566 |
} |
|
2567 |
throw new IllegalArgumentException(); |
|
2568 |
} |
|
2569 |
||
2570 |
/** |
|
2571 |
* Returns a <code>List</code> of <code>String</code>s containing |
|
2572 |
* formats to be used to load resource bundles for the given |
|
2573 |
* <code>baseName</code>. The <code>ResourceBundle.getBundle</code> |
|
2574 |
* factory method tries to load resource bundles with formats in the |
|
2575 |
* order specified by the list. The list returned by this method |
|
2576 |
* must have at least one <code>String</code>. The predefined |
|
2577 |
* formats are <code>"java.class"</code> for class-based resource |
|
2578 |
* bundles and <code>"java.properties"</code> for {@linkplain |
|
2579 |
* PropertyResourceBundle properties-based} ones. Strings starting |
|
2580 |
* with <code>"java."</code> are reserved for future extensions and |
|
2581 |
* must not be used by application-defined formats. |
|
2582 |
* |
|
2583 |
* <p>It is not a requirement to return an immutable (unmodifiable) |
|
2584 |
* <code>List</code>. However, the returned <code>List</code> must |
|
2585 |
* not be mutated after it has been returned by |
|
2586 |
* <code>getFormats</code>. |
|
2587 |
* |
|
2588 |
* <p>The default implementation returns {@link #FORMAT_DEFAULT} so |
|
2589 |
* that the <code>ResourceBundle.getBundle</code> factory method |
|
2590 |
* looks up first class-based resource bundles, then |
|
2591 |
* properties-based ones. |
|
2592 |
* |
|
2593 |
* @param baseName |
|
2594 |
* the base name of the resource bundle, a fully qualified class |
|
2595 |
* name |
|
2596 |
* @return a <code>List</code> of <code>String</code>s containing |
|
2597 |
* formats for loading resource bundles. |
|
2598 |
* @exception NullPointerException |
|
2599 |
* if <code>baseName</code> is null |
|
2600 |
* @see #FORMAT_DEFAULT |
|
2601 |
* @see #FORMAT_CLASS |
|
2602 |
* @see #FORMAT_PROPERTIES |
|
2603 |
*/ |
|
2604 |
public List<String> getFormats(String baseName) { |
|
2605 |
if (baseName == null) { |
|
2606 |
throw new NullPointerException(); |
|
2607 |
} |
|
2608 |
return FORMAT_DEFAULT; |
|
2609 |
} |
|
2610 |
||
2611 |
/** |
|
2612 |
* Returns a <code>List</code> of <code>Locale</code>s as candidate |
|
2613 |
* locales for <code>baseName</code> and <code>locale</code>. This |
|
2614 |
* method is called by the <code>ResourceBundle.getBundle</code> |
|
2615 |
* factory method each time the factory method tries finding a |
|
2616 |
* resource bundle for a target <code>Locale</code>. |
|
2617 |
* |
|
2618 |
* <p>The sequence of the candidate locales also corresponds to the |
|
2619 |
* runtime resource lookup path (also known as the <I>parent |
|
2620 |
* chain</I>), if the corresponding resource bundles for the |
|
2621 |
* candidate locales exist and their parents are not defined by |
|
2622 |
* loaded resource bundles themselves. The last element of the list |
|
2623 |
* must be a {@linkplain Locale#ROOT root locale} if it is desired to |
|
2624 |
* have the base bundle as the terminal of the parent chain. |
|
2625 |
* |
|
2626 |
* <p>If the given locale is equal to <code>Locale.ROOT</code> (the |
|
2627 |
* root locale), a <code>List</code> containing only the root |
|
2628 |
* <code>Locale</code> must be returned. In this case, the |
|
2629 |
* <code>ResourceBundle.getBundle</code> factory method loads only |
|
2630 |
* the base bundle as the resulting resource bundle. |
|
2631 |
* |
|
6501 | 2632 |
* <p>It is not a requirement to return an immutable (unmodifiable) |
2633 |
* <code>List</code>. However, the returned <code>List</code> must not |
|
2634 |
* be mutated after it has been returned by |
|
2635 |
* <code>getCandidateLocales</code>. |
|
2 | 2636 |
* |
2637 |
* <p>The default implementation returns a <code>List</code> containing |
|
6501 | 2638 |
* <code>Locale</code>s using the rules described below. In the |
2639 |
* description below, <em>L</em>, <em>S</em>, <em>C</em> and <em>V</em> |
|
2640 |
* respectively represent non-empty language, script, country, and |
|
2641 |
* variant. For example, [<em>L</em>, <em>C</em>] represents a |
|
2642 |
* <code>Locale</code> that has non-empty values only for language and |
|
2643 |
* country. The form <em>L</em>("xx") represents the (non-empty) |
|
2644 |
* language value is "xx". For all cases, <code>Locale</code>s whose |
|
2 | 2645 |
* final component values are empty strings are omitted. |
2646 |
* |
|
6501 | 2647 |
* <ol><li>For an input <code>Locale</code> with an empty script value, |
2648 |
* append candidate <code>Locale</code>s by omitting the final component |
|
2649 |
* one by one as below: |
|
2650 |
* |
|
2651 |
* <ul> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2652 |
* <li> [<em>L</em>, <em>C</em>, <em>V</em>] </li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2653 |
* <li> [<em>L</em>, <em>C</em>] </li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2654 |
* <li> [<em>L</em>] </li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2655 |
* <li> <code>Locale.ROOT</code> </li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2656 |
* </ul></li> |
6501 | 2657 |
* |
2658 |
* <li>For an input <code>Locale</code> with a non-empty script value, |
|
2659 |
* append candidate <code>Locale</code>s by omitting the final component |
|
2660 |
* up to language, then append candidates generated from the |
|
2661 |
* <code>Locale</code> with country and variant restored: |
|
2662 |
* |
|
2663 |
* <ul> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2664 |
* <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2665 |
* <li> [<em>L</em>, <em>S</em>, <em>C</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2666 |
* <li> [<em>L</em>, <em>S</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2667 |
* <li> [<em>L</em>, <em>C</em>, <em>V</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2668 |
* <li> [<em>L</em>, <em>C</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2669 |
* <li> [<em>L</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2670 |
* <li> <code>Locale.ROOT</code></li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2671 |
* </ul></li> |
6501 | 2672 |
* |
2673 |
* <li>For an input <code>Locale</code> with a variant value consisting |
|
2674 |
* of multiple subtags separated by underscore, generate candidate |
|
2675 |
* <code>Locale</code>s by omitting the variant subtags one by one, then |
|
21278 | 2676 |
* insert them after every occurrence of <code> Locale</code>s with the |
6501 | 2677 |
* full variant value in the original list. For example, if the |
2678 |
* the variant consists of two subtags <em>V1</em> and <em>V2</em>: |
|
2679 |
* |
|
2680 |
* <ul> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2681 |
* <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2682 |
* <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2683 |
* <li> [<em>L</em>, <em>S</em>, <em>C</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2684 |
* <li> [<em>L</em>, <em>S</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2685 |
* <li> [<em>L</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2686 |
* <li> [<em>L</em>, <em>C</em>, <em>V1</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2687 |
* <li> [<em>L</em>, <em>C</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2688 |
* <li> [<em>L</em>]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2689 |
* <li> <code>Locale.ROOT</code></li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2690 |
* </ul></li> |
6501 | 2691 |
* |
2692 |
* <li>Special cases for Chinese. When an input <code>Locale</code> has the |
|
2693 |
* language "zh" (Chinese) and an empty script value, either "Hans" (Simplified) or |
|
2694 |
* "Hant" (Traditional) might be supplied, depending on the country. |
|
2695 |
* When the country is "CN" (China) or "SG" (Singapore), "Hans" is supplied. |
|
2696 |
* When the country is "HK" (Hong Kong SAR China), "MO" (Macau SAR China), |
|
2697 |
* or "TW" (Taiwan), "Hant" is supplied. For all other countries or when the country |
|
2698 |
* is empty, no script is supplied. For example, for <code>Locale("zh", "CN") |
|
2699 |
* </code>, the candidate list will be: |
|
2700 |
* <ul> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2701 |
* <li> [<em>L</em>("zh"), <em>S</em>("Hans"), <em>C</em>("CN")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2702 |
* <li> [<em>L</em>("zh"), <em>S</em>("Hans")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2703 |
* <li> [<em>L</em>("zh"), <em>C</em>("CN")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2704 |
* <li> [<em>L</em>("zh")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2705 |
* <li> <code>Locale.ROOT</code></li> |
6501 | 2706 |
* </ul> |
2707 |
* |
|
2708 |
* For <code>Locale("zh", "TW")</code>, the candidate list will be: |
|
2709 |
* <ul> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2710 |
* <li> [<em>L</em>("zh"), <em>S</em>("Hant"), <em>C</em>("TW")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2711 |
* <li> [<em>L</em>("zh"), <em>S</em>("Hant")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2712 |
* <li> [<em>L</em>("zh"), <em>C</em>("TW")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2713 |
* <li> [<em>L</em>("zh")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2714 |
* <li> <code>Locale.ROOT</code></li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2715 |
* </ul></li> |
6501 | 2716 |
* |
2717 |
* <li>Special cases for Norwegian. Both <code>Locale("no", "NO", |
|
2718 |
* "NY")</code> and <code>Locale("nn", "NO")</code> represent Norwegian |
|
2719 |
* Nynorsk. When a locale's language is "nn", the standard candidate |
|
2720 |
* list is generated up to [<em>L</em>("nn")], and then the following |
|
2721 |
* candidates are added: |
|
2722 |
* |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2723 |
* <ul><li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("NY")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2724 |
* <li> [<em>L</em>("no"), <em>C</em>("NO")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2725 |
* <li> [<em>L</em>("no")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2726 |
* <li> <code>Locale.ROOT</code></li> |
6501 | 2727 |
* </ul> |
2728 |
* |
|
2729 |
* If the locale is exactly <code>Locale("no", "NO", "NY")</code>, it is first |
|
2730 |
* converted to <code>Locale("nn", "NO")</code> and then the above procedure is |
|
2731 |
* followed. |
|
2732 |
* |
|
2733 |
* <p>Also, Java treats the language "no" as a synonym of Norwegian |
|
2734 |
* Bokmål "nb". Except for the single case <code>Locale("no", |
|
2735 |
* "NO", "NY")</code> (handled above), when an input <code>Locale</code> |
|
2736 |
* has language "no" or "nb", candidate <code>Locale</code>s with |
|
2737 |
* language code "no" and "nb" are interleaved, first using the |
|
2738 |
* requested language, then using its synonym. For example, |
|
2739 |
* <code>Locale("nb", "NO", "POSIX")</code> generates the following |
|
2740 |
* candidate list: |
|
2741 |
* |
|
2742 |
* <ul> |
|
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2743 |
* <li> [<em>L</em>("nb"), <em>C</em>("NO"), <em>V</em>("POSIX")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2744 |
* <li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("POSIX")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2745 |
* <li> [<em>L</em>("nb"), <em>C</em>("NO")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2746 |
* <li> [<em>L</em>("no"), <em>C</em>("NO")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2747 |
* <li> [<em>L</em>("nb")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2748 |
* <li> [<em>L</em>("no")]</li> |
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
2749 |
* <li> <code>Locale.ROOT</code></li> |
6501 | 2750 |
* </ul> |
2751 |
* |
|
2752 |
* <code>Locale("no", "NO", "POSIX")</code> would generate the same list |
|
2753 |
* except that locales with "no" would appear before the corresponding |
|
2754 |
* locales with "nb".</li> |
|
2755 |
* </ol> |
|
2756 |
* |
|
2 | 2757 |
* <p>The default implementation uses an {@link ArrayList} that |
2758 |
* overriding implementations may modify before returning it to the |
|
2759 |
* caller. However, a subclass must not modify it after it has |
|
2760 |
* been returned by <code>getCandidateLocales</code>. |
|
2761 |
* |
|
2762 |
* <p>For example, if the given <code>baseName</code> is "Messages" |
|
2763 |
* and the given <code>locale</code> is |
|
2764 |
* <code>Locale("ja", "", "XX")</code>, then a |
|
2765 |
* <code>List</code> of <code>Locale</code>s: |
|
2766 |
* <pre> |
|
2767 |
* Locale("ja", "", "XX") |
|
2768 |
* Locale("ja") |
|
2769 |
* Locale.ROOT |
|
2770 |
* </pre> |
|
2771 |
* is returned. And if the resource bundles for the "ja" and |
|
2772 |
* "" <code>Locale</code>s are found, then the runtime resource |
|
2773 |
* lookup path (parent chain) is: |
|
18156 | 2774 |
* <pre>{@code |
2 | 2775 |
* Messages_ja -> Messages |
18156 | 2776 |
* }</pre> |
2 | 2777 |
* |
2778 |
* @param baseName |
|
2779 |
* the base name of the resource bundle, a fully |
|
2780 |
* qualified class name |
|
2781 |
* @param locale |
|
2782 |
* the locale for which a resource bundle is desired |
|
2783 |
* @return a <code>List</code> of candidate |
|
2784 |
* <code>Locale</code>s for the given <code>locale</code> |
|
2785 |
* @exception NullPointerException |
|
2786 |
* if <code>baseName</code> or <code>locale</code> is |
|
2787 |
* <code>null</code> |
|
2788 |
*/ |
|
2789 |
public List<Locale> getCandidateLocales(String baseName, Locale locale) { |
|
2790 |
if (baseName == null) { |
|
2791 |
throw new NullPointerException(); |
|
2792 |
} |
|
9224 | 2793 |
return new ArrayList<>(CANDIDATES_CACHE.get(locale.getBaseLocale())); |
6501 | 2794 |
} |
2795 |
||
2796 |
private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache(); |
|
2797 |
||
2798 |
private static class CandidateListCache extends LocaleObjectCache<BaseLocale, List<Locale>> { |
|
2799 |
protected List<Locale> createObject(BaseLocale base) { |
|
2800 |
String language = base.getLanguage(); |
|
2801 |
String script = base.getScript(); |
|
2802 |
String region = base.getRegion(); |
|
2803 |
String variant = base.getVariant(); |
|
2 | 2804 |
|
6501 | 2805 |
// Special handling for Norwegian |
2806 |
boolean isNorwegianBokmal = false; |
|
2807 |
boolean isNorwegianNynorsk = false; |
|
2808 |
if (language.equals("no")) { |
|
2809 |
if (region.equals("NO") && variant.equals("NY")) { |
|
2810 |
variant = ""; |
|
2811 |
isNorwegianNynorsk = true; |
|
2812 |
} else { |
|
2813 |
isNorwegianBokmal = true; |
|
2814 |
} |
|
2815 |
} |
|
2816 |
if (language.equals("nb") || isNorwegianBokmal) { |
|
2817 |
List<Locale> tmpList = getDefaultList("nb", script, region, variant); |
|
2818 |
// Insert a locale replacing "nb" with "no" for every list entry |
|
9224 | 2819 |
List<Locale> bokmalList = new LinkedList<>(); |
6501 | 2820 |
for (Locale l : tmpList) { |
2821 |
bokmalList.add(l); |
|
2822 |
if (l.getLanguage().length() == 0) { |
|
2823 |
break; |
|
2824 |
} |
|
2825 |
bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(), |
|
9224 | 2826 |
l.getVariant(), null)); |
6501 | 2827 |
} |
2828 |
return bokmalList; |
|
2829 |
} else if (language.equals("nn") || isNorwegianNynorsk) { |
|
2830 |
// Insert no_NO_NY, no_NO, no after nn |
|
2831 |
List<Locale> nynorskList = getDefaultList("nn", script, region, variant); |
|
2832 |
int idx = nynorskList.size() - 1; |
|
2833 |
nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY")); |
|
2834 |
nynorskList.add(idx++, Locale.getInstance("no", "NO", "")); |
|
2835 |
nynorskList.add(idx++, Locale.getInstance("no", "", "")); |
|
2836 |
return nynorskList; |
|
2837 |
} |
|
2838 |
// Special handling for Chinese |
|
2839 |
else if (language.equals("zh")) { |
|
2840 |
if (script.length() == 0 && region.length() > 0) { |
|
2841 |
// Supply script for users who want to use zh_Hans/zh_Hant |
|
2842 |
// as bundle names (recommended for Java7+) |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2843 |
switch (region) { |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2844 |
case "TW": |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2845 |
case "HK": |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2846 |
case "MO": |
6501 | 2847 |
script = "Hant"; |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2848 |
break; |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2849 |
case "CN": |
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2850 |
case "SG": |
6501 | 2851 |
script = "Hans"; |
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
2852 |
break; |
6501 | 2853 |
} |
2854 |
} |
|
2855 |
} |
|
2856 |
||
2857 |
return getDefaultList(language, script, region, variant); |
|
2 | 2858 |
} |
6501 | 2859 |
|
2860 |
private static List<Locale> getDefaultList(String language, String script, String region, String variant) { |
|
2861 |
List<String> variants = null; |
|
2862 |
||
2863 |
if (variant.length() > 0) { |
|
9224 | 2864 |
variants = new LinkedList<>(); |
6501 | 2865 |
int idx = variant.length(); |
2866 |
while (idx != -1) { |
|
2867 |
variants.add(variant.substring(0, idx)); |
|
2868 |
idx = variant.lastIndexOf('_', --idx); |
|
2869 |
} |
|
2870 |
} |
|
2871 |
||
9224 | 2872 |
List<Locale> list = new LinkedList<>(); |
6501 | 2873 |
|
2874 |
if (variants != null) { |
|
2875 |
for (String v : variants) { |
|
9224 | 2876 |
list.add(Locale.getInstance(language, script, region, v, null)); |
6501 | 2877 |
} |
2878 |
} |
|
2879 |
if (region.length() > 0) { |
|
9224 | 2880 |
list.add(Locale.getInstance(language, script, region, "", null)); |
6501 | 2881 |
} |
2882 |
if (script.length() > 0) { |
|
9224 | 2883 |
list.add(Locale.getInstance(language, script, "", "", null)); |
42930
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2884 |
// Special handling for Chinese |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2885 |
if (language.equals("zh")) { |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2886 |
if (region.length() == 0) { |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2887 |
// Supply region(country) for users who still package Chinese |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2888 |
// bundles using old convension. |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2889 |
switch (script) { |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2890 |
case "Hans": |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2891 |
region = "CN"; |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2892 |
break; |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2893 |
case "Hant": |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2894 |
region = "TW"; |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2895 |
break; |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2896 |
} |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2897 |
} |
b878b7b2788e
8167143: CLDR timezone parsing does not work for all locales
rgoel
parents:
42769
diff
changeset
|
2898 |
} |
6501 | 2899 |
|
2900 |
// With script, after truncating variant, region and script, |
|
2901 |
// start over without script. |
|
2902 |
if (variants != null) { |
|
2903 |
for (String v : variants) { |
|
9224 | 2904 |
list.add(Locale.getInstance(language, "", region, v, null)); |
6501 | 2905 |
} |
2906 |
} |
|
2907 |
if (region.length() > 0) { |
|
9224 | 2908 |
list.add(Locale.getInstance(language, "", region, "", null)); |
6501 | 2909 |
} |
2910 |
} |
|
2911 |
if (language.length() > 0) { |
|
9224 | 2912 |
list.add(Locale.getInstance(language, "", "", "", null)); |
6501 | 2913 |
} |
2914 |
// Add root locale at the end |
|
2915 |
list.add(Locale.ROOT); |
|
2916 |
||
2917 |
return list; |
|
2 | 2918 |
} |
2919 |
} |
|
2920 |
||
2921 |
/** |
|
2922 |
* Returns a <code>Locale</code> to be used as a fallback locale for |
|
2923 |
* further resource bundle searches by the |
|
2924 |
* <code>ResourceBundle.getBundle</code> factory method. This method |
|
2925 |
* is called from the factory method every time when no resulting |
|
2926 |
* resource bundle has been found for <code>baseName</code> and |
|
2927 |
* <code>locale</code>, where locale is either the parameter for |
|
2928 |
* <code>ResourceBundle.getBundle</code> or the previous fallback |
|
2929 |
* locale returned by this method. |
|
2930 |
* |
|
2931 |
* <p>The method returns <code>null</code> if no further fallback |
|
2932 |
* search is desired. |
|
2933 |
* |
|
2934 |
* <p>The default implementation returns the {@linkplain |
|
2935 |
* Locale#getDefault() default <code>Locale</code>} if the given |
|
2936 |
* <code>locale</code> isn't the default one. Otherwise, |
|
2937 |
* <code>null</code> is returned. |
|
2938 |
* |
|
2939 |
* @param baseName |
|
2940 |
* the base name of the resource bundle, a fully |
|
2941 |
* qualified class name for which |
|
2942 |
* <code>ResourceBundle.getBundle</code> has been |
|
2943 |
* unable to find any resource bundles (except for the |
|
2944 |
* base bundle) |
|
2945 |
* @param locale |
|
2946 |
* the <code>Locale</code> for which |
|
2947 |
* <code>ResourceBundle.getBundle</code> has been |
|
2948 |
* unable to find any resource bundles (except for the |
|
2949 |
* base bundle) |
|
2950 |
* @return a <code>Locale</code> for the fallback search, |
|
2951 |
* or <code>null</code> if no further fallback search |
|
2952 |
* is desired. |
|
2953 |
* @exception NullPointerException |
|
2954 |
* if <code>baseName</code> or <code>locale</code> |
|
2955 |
* is <code>null</code> |
|
2956 |
*/ |
|
2957 |
public Locale getFallbackLocale(String baseName, Locale locale) { |
|
2958 |
if (baseName == null) { |
|
2959 |
throw new NullPointerException(); |
|
2960 |
} |
|
2961 |
Locale defaultLocale = Locale.getDefault(); |
|
2962 |
return locale.equals(defaultLocale) ? null : defaultLocale; |
|
2963 |
} |
|
2964 |
||
2965 |
/** |
|
2966 |
* Instantiates a resource bundle for the given bundle name of the |
|
2967 |
* given format and locale, using the given class loader if |
|
2968 |
* necessary. This method returns <code>null</code> if there is no |
|
2969 |
* resource bundle available for the given parameters. If a resource |
|
2970 |
* bundle can't be instantiated due to an unexpected error, the |
|
2971 |
* error must be reported by throwing an <code>Error</code> or |
|
2972 |
* <code>Exception</code> rather than simply returning |
|
2973 |
* <code>null</code>. |
|
2974 |
* |
|
2975 |
* <p>If the <code>reload</code> flag is <code>true</code>, it |
|
2976 |
* indicates that this method is being called because the previously |
|
2977 |
* loaded resource bundle has expired. |
|
2978 |
* |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2979 |
* @implSpec |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2980 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2981 |
* Resource bundles in named modules are subject to the encapsulation |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2982 |
* rules specified by {@link Module#getResourceAsStream Module.getResourceAsStream}. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2983 |
* A resource bundle in a named module visible to the given class loader |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2984 |
* is accessible when the package of the resource file corresponding |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2985 |
* to the resource bundle is open unconditionally. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2986 |
* |
2 | 2987 |
* <p>The default implementation instantiates a |
2988 |
* <code>ResourceBundle</code> as follows. |
|
2989 |
* |
|
2990 |
* <ul> |
|
2991 |
* |
|
2992 |
* <li>The bundle name is obtained by calling {@link |
|
2993 |
* #toBundleName(String, Locale) toBundleName(baseName, |
|
2994 |
* locale)}.</li> |
|
2995 |
* |
|
2996 |
* <li>If <code>format</code> is <code>"java.class"</code>, the |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2997 |
* {@link Class} specified by the bundle name is loaded with the |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2998 |
* given class loader. If the {@code Class} is found and accessible |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
2999 |
* then the <code>ResourceBundle</code> is instantiated. The |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3000 |
* resource bundle is accessible if the package of the bundle class file |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3001 |
* is open unconditionally; otherwise, {@code IllegalAccessException} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3002 |
* will be thrown. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3003 |
* Note that the <code>reload</code> flag is ignored for loading |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3004 |
* class-based resource bundles in this default implementation. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3005 |
* </li> |
2 | 3006 |
* |
3007 |
* <li>If <code>format</code> is <code>"java.properties"</code>, |
|
3008 |
* {@link #toResourceName(String, String) toResourceName(bundlename, |
|
3009 |
* "properties")} is called to get the resource name. |
|
3010 |
* If <code>reload</code> is <code>true</code>, {@link |
|
3011 |
* ClassLoader#getResource(String) load.getResource} is called |
|
3012 |
* to get a {@link URL} for creating a {@link |
|
3013 |
* URLConnection}. This <code>URLConnection</code> is used to |
|
3014 |
* {@linkplain URLConnection#setUseCaches(boolean) disable the |
|
3015 |
* caches} of the underlying resource loading layers, |
|
3016 |
* and to {@linkplain URLConnection#getInputStream() get an |
|
3017 |
* <code>InputStream</code>}. |
|
3018 |
* Otherwise, {@link ClassLoader#getResourceAsStream(String) |
|
3019 |
* loader.getResourceAsStream} is called to get an {@link |
|
3020 |
* InputStream}. Then, a {@link |
|
3021 |
* PropertyResourceBundle} is constructed with the |
|
3022 |
* <code>InputStream</code>.</li> |
|
3023 |
* |
|
3024 |
* <li>If <code>format</code> is neither <code>"java.class"</code> |
|
3025 |
* nor <code>"java.properties"</code>, an |
|
3026 |
* <code>IllegalArgumentException</code> is thrown.</li> |
|
3027 |
* |
|
3028 |
* </ul> |
|
3029 |
* |
|
3030 |
* @param baseName |
|
3031 |
* the base bundle name of the resource bundle, a fully |
|
3032 |
* qualified class name |
|
3033 |
* @param locale |
|
3034 |
* the locale for which the resource bundle should be |
|
3035 |
* instantiated |
|
3036 |
* @param format |
|
3037 |
* the resource bundle format to be loaded |
|
3038 |
* @param loader |
|
3039 |
* the <code>ClassLoader</code> to use to load the bundle |
|
3040 |
* @param reload |
|
3041 |
* the flag to indicate bundle reloading; <code>true</code> |
|
3042 |
* if reloading an expired resource bundle, |
|
3043 |
* <code>false</code> otherwise |
|
3044 |
* @return the resource bundle instance, |
|
3045 |
* or <code>null</code> if none could be found. |
|
3046 |
* @exception NullPointerException |
|
3047 |
* if <code>bundleName</code>, <code>locale</code>, |
|
3048 |
* <code>format</code>, or <code>loader</code> is |
|
3049 |
* <code>null</code>, or if <code>null</code> is returned by |
|
3050 |
* {@link #toBundleName(String, Locale) toBundleName} |
|
3051 |
* @exception IllegalArgumentException |
|
3052 |
* if <code>format</code> is unknown, or if the resource |
|
3053 |
* found for the given parameters contains malformed data. |
|
3054 |
* @exception ClassCastException |
|
3055 |
* if the loaded class cannot be cast to <code>ResourceBundle</code> |
|
3056 |
* @exception IllegalAccessException |
|
3057 |
* if the class or its nullary constructor is not |
|
3058 |
* accessible. |
|
3059 |
* @exception InstantiationException |
|
3060 |
* if the instantiation of a class fails for some other |
|
3061 |
* reason. |
|
3062 |
* @exception ExceptionInInitializerError |
|
3063 |
* if the initialization provoked by this method fails. |
|
3064 |
* @exception SecurityException |
|
3065 |
* If a security manager is present and creation of new |
|
3066 |
* instances is denied. See {@link Class#newInstance()} |
|
3067 |
* for details. |
|
3068 |
* @exception IOException |
|
3069 |
* if an error occurred when reading resources using |
|
3070 |
* any I/O operations |
|
36511 | 3071 |
* @see java.util.spi.ResourceBundleProvider#getBundle(String, Locale) |
2 | 3072 |
*/ |
3073 |
public ResourceBundle newBundle(String baseName, Locale locale, String format, |
|
3074 |
ClassLoader loader, boolean reload) |
|
3075 |
throws IllegalAccessException, InstantiationException, IOException { |
|
36511 | 3076 |
/* |
3077 |
* Legacy mechanism to locate resource bundle in unnamed module only |
|
3078 |
* that is visible to the given loader and accessible to the given caller. |
|
3079 |
*/ |
|
2 | 3080 |
String bundleName = toBundleName(baseName, locale); |
3081 |
ResourceBundle bundle = null; |
|
3082 |
if (format.equals("java.class")) { |
|
3083 |
try { |
|
36511 | 3084 |
Class<?> c = loader.loadClass(bundleName); |
2 | 3085 |
// If the class isn't a ResourceBundle subclass, throw a |
3086 |
// ClassCastException. |
|
36511 | 3087 |
if (ResourceBundle.class.isAssignableFrom(c)) { |
3088 |
@SuppressWarnings("unchecked") |
|
3089 |
Class<ResourceBundle> bundleClass = (Class<ResourceBundle>)c; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3090 |
Module m = bundleClass.getModule(); |
36511 | 3091 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3092 |
// To access a resource bundle in a named module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3093 |
// either class-based or properties-based, the resource |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3094 |
// bundle must be opened unconditionally, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3095 |
// same rule as accessing a resource file. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3096 |
if (m.isNamed() && !m.isOpen(bundleClass.getPackageName())) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3097 |
throw new IllegalAccessException("unnamed module can't load " + |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3098 |
bundleClass.getName() + " in " + m.toString()); |
36511 | 3099 |
} |
3100 |
try { |
|
3101 |
// bundle in a unnamed module |
|
3102 |
Constructor<ResourceBundle> ctor = bundleClass.getConstructor(); |
|
3103 |
if (!Modifier.isPublic(ctor.getModifiers())) { |
|
3104 |
return null; |
|
3105 |
} |
|
3106 |
||
3107 |
// java.base may not be able to read the bundleClass's module. |
|
3108 |
PrivilegedAction<Void> pa1 = () -> { ctor.setAccessible(true); return null; }; |
|
3109 |
AccessController.doPrivileged(pa1); |
|
3110 |
bundle = ctor.newInstance((Object[]) null); |
|
3111 |
} catch (InvocationTargetException e) { |
|
3112 |
uncheckedThrow(e); |
|
3113 |
} |
|
2 | 3114 |
} else { |
36511 | 3115 |
throw new ClassCastException(c.getName() |
3116 |
+ " cannot be cast to ResourceBundle"); |
|
2 | 3117 |
} |
36511 | 3118 |
} catch (ClassNotFoundException|NoSuchMethodException e) { |
2 | 3119 |
} |
3120 |
} else if (format.equals("java.properties")) { |
|
27083 | 3121 |
final String resourceName = toResourceName0(bundleName, "properties"); |
3122 |
if (resourceName == null) { |
|
3123 |
return bundle; |
|
3124 |
} |
|
36511 | 3125 |
|
2 | 3126 |
final boolean reloadFlag = reload; |
3127 |
InputStream stream = null; |
|
3128 |
try { |
|
3129 |
stream = AccessController.doPrivileged( |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
28059
diff
changeset
|
3130 |
new PrivilegedExceptionAction<>() { |
2 | 3131 |
public InputStream run() throws IOException { |
36511 | 3132 |
URL url = loader.getResource(resourceName); |
3133 |
if (url == null) return null; |
|
3134 |
||
3135 |
URLConnection connection = url.openConnection(); |
|
2 | 3136 |
if (reloadFlag) { |
36511 | 3137 |
// Disable caches to get fresh data for |
3138 |
// reloading. |
|
3139 |
connection.setUseCaches(false); |
|
2 | 3140 |
} |
36511 | 3141 |
return connection.getInputStream(); |
2 | 3142 |
} |
3143 |
}); |
|
3144 |
} catch (PrivilegedActionException e) { |
|
3145 |
throw (IOException) e.getException(); |
|
3146 |
} |
|
3147 |
if (stream != null) { |
|
3148 |
try { |
|
3149 |
bundle = new PropertyResourceBundle(stream); |
|
3150 |
} finally { |
|
3151 |
stream.close(); |
|
3152 |
} |
|
3153 |
} |
|
3154 |
} else { |
|
3155 |
throw new IllegalArgumentException("unknown format: " + format); |
|
3156 |
} |
|
3157 |
return bundle; |
|
3158 |
} |
|
3159 |
||
3160 |
/** |
|
3161 |
* Returns the time-to-live (TTL) value for resource bundles that |
|
3162 |
* are loaded under this |
|
3163 |
* <code>ResourceBundle.Control</code>. Positive time-to-live values |
|
3164 |
* specify the number of milliseconds a bundle can remain in the |
|
3165 |
* cache without being validated against the source data from which |
|
3166 |
* it was constructed. The value 0 indicates that a bundle must be |
|
3167 |
* validated each time it is retrieved from the cache. {@link |
|
3168 |
* #TTL_DONT_CACHE} specifies that loaded resource bundles are not |
|
3169 |
* put in the cache. {@link #TTL_NO_EXPIRATION_CONTROL} specifies |
|
3170 |
* that loaded resource bundles are put in the cache with no |
|
3171 |
* expiration control. |
|
3172 |
* |
|
3173 |
* <p>The expiration affects only the bundle loading process by the |
|
3174 |
* <code>ResourceBundle.getBundle</code> factory method. That is, |
|
3175 |
* if the factory method finds a resource bundle in the cache that |
|
3176 |
* has expired, the factory method calls the {@link |
|
3177 |
* #needsReload(String, Locale, String, ClassLoader, ResourceBundle, |
|
3178 |
* long) needsReload} method to determine whether the resource |
|
3179 |
* bundle needs to be reloaded. If <code>needsReload</code> returns |
|
3180 |
* <code>true</code>, the cached resource bundle instance is removed |
|
3181 |
* from the cache. Otherwise, the instance stays in the cache, |
|
3182 |
* updated with the new TTL value returned by this method. |
|
3183 |
* |
|
3184 |
* <p>All cached resource bundles are subject to removal from the |
|
3185 |
* cache due to memory constraints of the runtime environment. |
|
3186 |
* Returning a large positive value doesn't mean to lock loaded |
|
3187 |
* resource bundles in the cache. |
|
3188 |
* |
|
3189 |
* <p>The default implementation returns {@link #TTL_NO_EXPIRATION_CONTROL}. |
|
3190 |
* |
|
3191 |
* @param baseName |
|
3192 |
* the base name of the resource bundle for which the |
|
3193 |
* expiration value is specified. |
|
3194 |
* @param locale |
|
3195 |
* the locale of the resource bundle for which the |
|
3196 |
* expiration value is specified. |
|
3197 |
* @return the time (0 or a positive millisecond offset from the |
|
3198 |
* cached time) to get loaded bundles expired in the cache, |
|
3199 |
* {@link #TTL_NO_EXPIRATION_CONTROL} to disable the |
|
3200 |
* expiration control, or {@link #TTL_DONT_CACHE} to disable |
|
3201 |
* caching. |
|
3202 |
* @exception NullPointerException |
|
3203 |
* if <code>baseName</code> or <code>locale</code> is |
|
3204 |
* <code>null</code> |
|
3205 |
*/ |
|
3206 |
public long getTimeToLive(String baseName, Locale locale) { |
|
3207 |
if (baseName == null || locale == null) { |
|
3208 |
throw new NullPointerException(); |
|
3209 |
} |
|
3210 |
return TTL_NO_EXPIRATION_CONTROL; |
|
3211 |
} |
|
3212 |
||
3213 |
/** |
|
3214 |
* Determines if the expired <code>bundle</code> in the cache needs |
|
3215 |
* to be reloaded based on the loading time given by |
|
3216 |
* <code>loadTime</code> or some other criteria. The method returns |
|
3217 |
* <code>true</code> if reloading is required; <code>false</code> |
|
3218 |
* otherwise. <code>loadTime</code> is a millisecond offset since |
|
3219 |
* the <a href="Calendar.html#Epoch"> <code>Calendar</code> |
|
3220 |
* Epoch</a>. |
|
3221 |
* |
|
36511 | 3222 |
* <p> |
2 | 3223 |
* The calling <code>ResourceBundle.getBundle</code> factory method |
3224 |
* calls this method on the <code>ResourceBundle.Control</code> |
|
3225 |
* instance used for its current invocation, not on the instance |
|
3226 |
* used in the invocation that originally loaded the resource |
|
3227 |
* bundle. |
|
3228 |
* |
|
3229 |
* <p>The default implementation compares <code>loadTime</code> and |
|
3230 |
* the last modified time of the source data of the resource |
|
3231 |
* bundle. If it's determined that the source data has been modified |
|
3232 |
* since <code>loadTime</code>, <code>true</code> is |
|
3233 |
* returned. Otherwise, <code>false</code> is returned. This |
|
3234 |
* implementation assumes that the given <code>format</code> is the |
|
3235 |
* same string as its file suffix if it's not one of the default |
|
3236 |
* formats, <code>"java.class"</code> or |
|
3237 |
* <code>"java.properties"</code>. |
|
3238 |
* |
|
3239 |
* @param baseName |
|
3240 |
* the base bundle name of the resource bundle, a |
|
3241 |
* fully qualified class name |
|
3242 |
* @param locale |
|
3243 |
* the locale for which the resource bundle |
|
3244 |
* should be instantiated |
|
3245 |
* @param format |
|
3246 |
* the resource bundle format to be loaded |
|
3247 |
* @param loader |
|
3248 |
* the <code>ClassLoader</code> to use to load the bundle |
|
3249 |
* @param bundle |
|
3250 |
* the resource bundle instance that has been expired |
|
3251 |
* in the cache |
|
3252 |
* @param loadTime |
|
3253 |
* the time when <code>bundle</code> was loaded and put |
|
3254 |
* in the cache |
|
3255 |
* @return <code>true</code> if the expired bundle needs to be |
|
3256 |
* reloaded; <code>false</code> otherwise. |
|
3257 |
* @exception NullPointerException |
|
3258 |
* if <code>baseName</code>, <code>locale</code>, |
|
3259 |
* <code>format</code>, <code>loader</code>, or |
|
3260 |
* <code>bundle</code> is <code>null</code> |
|
3261 |
*/ |
|
3262 |
public boolean needsReload(String baseName, Locale locale, |
|
3263 |
String format, ClassLoader loader, |
|
3264 |
ResourceBundle bundle, long loadTime) { |
|
3265 |
if (bundle == null) { |
|
3266 |
throw new NullPointerException(); |
|
3267 |
} |
|
3268 |
if (format.equals("java.class") || format.equals("java.properties")) { |
|
3269 |
format = format.substring(5); |
|
3270 |
} |
|
3271 |
boolean result = false; |
|
3272 |
try { |
|
27083 | 3273 |
String resourceName = toResourceName0(toBundleName(baseName, locale), format); |
3274 |
if (resourceName == null) { |
|
3275 |
return result; |
|
3276 |
} |
|
2 | 3277 |
URL url = loader.getResource(resourceName); |
3278 |
if (url != null) { |
|
3279 |
long lastModified = 0; |
|
3280 |
URLConnection connection = url.openConnection(); |
|
3281 |
if (connection != null) { |
|
3282 |
// disable caches to get the correct data |
|
3283 |
connection.setUseCaches(false); |
|
3284 |
if (connection instanceof JarURLConnection) { |
|
3285 |
JarEntry ent = ((JarURLConnection)connection).getJarEntry(); |
|
3286 |
if (ent != null) { |
|
3287 |
lastModified = ent.getTime(); |
|
3288 |
if (lastModified == -1) { |
|
3289 |
lastModified = 0; |
|
3290 |
} |
|
3291 |
} |
|
3292 |
} else { |
|
3293 |
lastModified = connection.getLastModified(); |
|
3294 |
} |
|
3295 |
} |
|
3296 |
result = lastModified >= loadTime; |
|
3297 |
} |
|
3298 |
} catch (NullPointerException npe) { |
|
3299 |
throw npe; |
|
3300 |
} catch (Exception e) { |
|
3301 |
// ignore other exceptions |
|
3302 |
} |
|
3303 |
return result; |
|
3304 |
} |
|
3305 |
||
3306 |
/** |
|
3307 |
* Converts the given <code>baseName</code> and <code>locale</code> |
|
3308 |
* to the bundle name. This method is called from the default |
|
3309 |
* implementation of the {@link #newBundle(String, Locale, String, |
|
3310 |
* ClassLoader, boolean) newBundle} and {@link #needsReload(String, |
|
3311 |
* Locale, String, ClassLoader, ResourceBundle, long) needsReload} |
|
3312 |
* methods. |
|
3313 |
* |
|
3314 |
* <p>This implementation returns the following value: |
|
3315 |
* <pre> |
|
6501 | 3316 |
* baseName + "_" + language + "_" + script + "_" + country + "_" + variant |
2 | 3317 |
* </pre> |
6501 | 3318 |
* where <code>language</code>, <code>script</code>, <code>country</code>, |
3319 |
* and <code>variant</code> are the language, script, country, and variant |
|
3320 |
* values of <code>locale</code>, respectively. Final component values that |
|
3321 |
* are empty Strings are omitted along with the preceding '_'. When the |
|
21278 | 3322 |
* script is empty, the script value is omitted along with the preceding '_'. |
6501 | 3323 |
* If all of the values are empty strings, then <code>baseName</code> |
2 | 3324 |
* is returned. |
3325 |
* |
|
3326 |
* <p>For example, if <code>baseName</code> is |
|
3327 |
* <code>"baseName"</code> and <code>locale</code> is |
|
3328 |
* <code>Locale("ja", "", "XX")</code>, then |
|
3329 |
* <code>"baseName_ja_ _XX"</code> is returned. If the given |
|
3330 |
* locale is <code>Locale("en")</code>, then |
|
3331 |
* <code>"baseName_en"</code> is returned. |
|
3332 |
* |
|
3333 |
* <p>Overriding this method allows applications to use different |
|
3334 |
* conventions in the organization and packaging of localized |
|
3335 |
* resources. |
|
3336 |
* |
|
3337 |
* @param baseName |
|
3338 |
* the base name of the resource bundle, a fully |
|
3339 |
* qualified class name |
|
3340 |
* @param locale |
|
3341 |
* the locale for which a resource bundle should be |
|
3342 |
* loaded |
|
3343 |
* @return the bundle name for the resource bundle |
|
3344 |
* @exception NullPointerException |
|
3345 |
* if <code>baseName</code> or <code>locale</code> |
|
3346 |
* is <code>null</code> |
|
36511 | 3347 |
* @see java.util.spi.AbstractResourceBundleProvider#toBundleName(String, Locale) |
2 | 3348 |
*/ |
3349 |
public String toBundleName(String baseName, Locale locale) { |
|
3350 |
if (locale == Locale.ROOT) { |
|
3351 |
return baseName; |
|
3352 |
} |
|
3353 |
||
3354 |
String language = locale.getLanguage(); |
|
6501 | 3355 |
String script = locale.getScript(); |
2 | 3356 |
String country = locale.getCountry(); |
3357 |
String variant = locale.getVariant(); |
|
3358 |
||
3359 |
if (language == "" && country == "" && variant == "") { |
|
3360 |
return baseName; |
|
3361 |
} |
|
3362 |
||
3363 |
StringBuilder sb = new StringBuilder(baseName); |
|
3364 |
sb.append('_'); |
|
6501 | 3365 |
if (script != "") { |
3366 |
if (variant != "") { |
|
3367 |
sb.append(language).append('_').append(script).append('_').append(country).append('_').append(variant); |
|
3368 |
} else if (country != "") { |
|
3369 |
sb.append(language).append('_').append(script).append('_').append(country); |
|
3370 |
} else { |
|
3371 |
sb.append(language).append('_').append(script); |
|
3372 |
} |
|
2 | 3373 |
} else { |
6501 | 3374 |
if (variant != "") { |
3375 |
sb.append(language).append('_').append(country).append('_').append(variant); |
|
3376 |
} else if (country != "") { |
|
3377 |
sb.append(language).append('_').append(country); |
|
3378 |
} else { |
|
3379 |
sb.append(language); |
|
3380 |
} |
|
2 | 3381 |
} |
3382 |
return sb.toString(); |
|
3383 |
||
3384 |
} |
|
3385 |
||
3386 |
/** |
|
3387 |
* Converts the given <code>bundleName</code> to the form required |
|
3388 |
* by the {@link ClassLoader#getResource ClassLoader.getResource} |
|
3389 |
* method by replacing all occurrences of <code>'.'</code> in |
|
3390 |
* <code>bundleName</code> with <code>'/'</code> and appending a |
|
3391 |
* <code>'.'</code> and the given file <code>suffix</code>. For |
|
3392 |
* example, if <code>bundleName</code> is |
|
3393 |
* <code>"foo.bar.MyResources_ja_JP"</code> and <code>suffix</code> |
|
3394 |
* is <code>"properties"</code>, then |
|
3395 |
* <code>"foo/bar/MyResources_ja_JP.properties"</code> is returned. |
|
3396 |
* |
|
3397 |
* @param bundleName |
|
3398 |
* the bundle name |
|
3399 |
* @param suffix |
|
3400 |
* the file type suffix |
|
3401 |
* @return the converted resource name |
|
3402 |
* @exception NullPointerException |
|
3403 |
* if <code>bundleName</code> or <code>suffix</code> |
|
3404 |
* is <code>null</code> |
|
3405 |
*/ |
|
3406 |
public final String toResourceName(String bundleName, String suffix) { |
|
3407 |
StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length()); |
|
3408 |
sb.append(bundleName.replace('.', '/')).append('.').append(suffix); |
|
3409 |
return sb.toString(); |
|
3410 |
} |
|
27083 | 3411 |
|
3412 |
private String toResourceName0(String bundleName, String suffix) { |
|
3413 |
// application protocol check |
|
3414 |
if (bundleName.contains("://")) { |
|
3415 |
return null; |
|
3416 |
} else { |
|
3417 |
return toResourceName(bundleName, suffix); |
|
3418 |
} |
|
3419 |
} |
|
2 | 3420 |
} |
3421 |
||
36511 | 3422 |
@SuppressWarnings("unchecked") |
3423 |
private static <T extends Throwable> void uncheckedThrow(Throwable t) throws T { |
|
3424 |
if (t != null) |
|
3425 |
throw (T)t; |
|
3426 |
else |
|
3427 |
throw new Error("Unknown Exception"); |
|
3428 |
} |
|
3429 |
||
2 | 3430 |
private static class SingleFormatControl extends Control { |
3431 |
private static final Control PROPERTIES_ONLY |
|
3432 |
= new SingleFormatControl(FORMAT_PROPERTIES); |
|
3433 |
||
3434 |
private static final Control CLASS_ONLY |
|
3435 |
= new SingleFormatControl(FORMAT_CLASS); |
|
3436 |
||
3437 |
private final List<String> formats; |
|
3438 |
||
3439 |
protected SingleFormatControl(List<String> formats) { |
|
3440 |
this.formats = formats; |
|
3441 |
} |
|
3442 |
||
3443 |
public List<String> getFormats(String baseName) { |
|
3444 |
if (baseName == null) { |
|
3445 |
throw new NullPointerException(); |
|
3446 |
} |
|
3447 |
return formats; |
|
3448 |
} |
|
3449 |
} |
|
3450 |
||
3451 |
private static final class NoFallbackControl extends SingleFormatControl { |
|
3452 |
private static final Control NO_FALLBACK |
|
3453 |
= new NoFallbackControl(FORMAT_DEFAULT); |
|
3454 |
||
3455 |
private static final Control PROPERTIES_ONLY_NO_FALLBACK |
|
3456 |
= new NoFallbackControl(FORMAT_PROPERTIES); |
|
3457 |
||
3458 |
private static final Control CLASS_ONLY_NO_FALLBACK |
|
3459 |
= new NoFallbackControl(FORMAT_CLASS); |
|
3460 |
||
3461 |
protected NoFallbackControl(List<String> formats) { |
|
3462 |
super(formats); |
|
3463 |
} |
|
3464 |
||
3465 |
public Locale getFallbackLocale(String baseName, Locale locale) { |
|
3466 |
if (baseName == null || locale == null) { |
|
3467 |
throw new NullPointerException(); |
|
3468 |
} |
|
3469 |
return null; |
|
3470 |
} |
|
3471 |
} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3472 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3473 |
private static class ResourceBundleProviderHelper { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3474 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3475 |
* Returns a new ResourceBundle instance of the given bundleClass |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3476 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3477 |
static ResourceBundle newResourceBundle(Class<? extends ResourceBundle> bundleClass) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3478 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3479 |
@SuppressWarnings("unchecked") |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3480 |
Constructor<? extends ResourceBundle> ctor = |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3481 |
bundleClass.getConstructor(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3482 |
if (!Modifier.isPublic(ctor.getModifiers())) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3483 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3484 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3485 |
// java.base may not be able to read the bundleClass's module. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3486 |
PrivilegedAction<Void> pa = () -> { ctor.setAccessible(true); return null;}; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3487 |
AccessController.doPrivileged(pa); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3488 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3489 |
return ctor.newInstance((Object[]) null); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3490 |
} catch (InvocationTargetException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3491 |
uncheckedThrow(e); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3492 |
} catch (InstantiationException | IllegalAccessException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3493 |
throw new InternalError(e); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3494 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3495 |
} catch (NoSuchMethodException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3496 |
throw new InternalError(e); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3497 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3498 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3499 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3500 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3501 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3502 |
* Loads a {@code ResourceBundle} of the given {@code bundleName} local to |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3503 |
* the given {@code module}. If not found, search the bundle class |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3504 |
* that is visible from the module's class loader. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3505 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3506 |
* The caller module is used for access check only. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3507 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3508 |
static ResourceBundle loadResourceBundle(Module callerModule, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3509 |
Module module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3510 |
String baseName, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3511 |
Locale locale) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3512 |
{ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3513 |
String bundleName = Control.INSTANCE.toBundleName(baseName, locale); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3514 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3515 |
PrivilegedAction<Class<?>> pa = () -> Class.forName(module, bundleName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3516 |
Class<?> c = AccessController.doPrivileged(pa, null, GET_CLASSLOADER_PERMISSION); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3517 |
trace("local in %s %s caller %s: %s%n", module, bundleName, callerModule, c); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3518 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3519 |
if (c == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3520 |
// if not found from the given module, locate resource bundle |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3521 |
// that is visible to the module's class loader |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3522 |
ClassLoader loader = getLoader(module); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3523 |
if (loader != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3524 |
c = Class.forName(bundleName, false, loader); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3525 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3526 |
c = BootLoader.loadClassOrNull(bundleName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3527 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3528 |
trace("loader for %s %s caller %s: %s%n", module, bundleName, callerModule, c); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3529 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3530 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3531 |
if (c != null && ResourceBundle.class.isAssignableFrom(c)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3532 |
@SuppressWarnings("unchecked") |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3533 |
Class<ResourceBundle> bundleClass = (Class<ResourceBundle>) c; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3534 |
Module m = bundleClass.getModule(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3535 |
if (!isAccessible(callerModule, m, bundleClass.getPackageName())) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3536 |
trace(" %s does not have access to %s/%s%n", callerModule, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3537 |
m.getName(), bundleClass.getPackageName()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3538 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3539 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3540 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3541 |
return newResourceBundle(bundleClass); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3542 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3543 |
} catch (ClassNotFoundException e) {} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3544 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3545 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3546 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3547 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3548 |
* Tests if resources of the given package name from the given module are |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3549 |
* open to the caller module. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3550 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3551 |
static boolean isAccessible(Module callerModule, Module module, String pn) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3552 |
if (!module.isNamed() || callerModule == module) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3553 |
return true; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3554 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3555 |
return module.isOpen(pn, callerModule); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3556 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3557 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3558 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3559 |
* Loads properties of the given {@code bundleName} local in the given |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3560 |
* {@code module}. If the .properties is not found or not open |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3561 |
* to the caller module to access, it will find the resource that |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3562 |
* is visible to the module's class loader. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3563 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3564 |
* The caller module is used for access check only. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3565 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3566 |
static ResourceBundle loadPropertyResourceBundle(Module callerModule, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3567 |
Module module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3568 |
String baseName, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3569 |
Locale locale) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3570 |
throws IOException |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3571 |
{ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3572 |
String bundleName = Control.INSTANCE.toBundleName(baseName, locale); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3573 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3574 |
PrivilegedAction<InputStream> pa = () -> { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3575 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3576 |
String resourceName = Control.INSTANCE |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3577 |
.toResourceName0(bundleName, "properties"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3578 |
if (resourceName == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3579 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3580 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3581 |
trace("local in %s %s caller %s%n", module, resourceName, callerModule); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3582 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3583 |
// if the package is in the given module but not opened |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3584 |
// locate it from the given module first. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3585 |
String pn = toPackageName(bundleName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3586 |
trace(" %s/%s is accessible to %s : %s%n", |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3587 |
module.getName(), pn, callerModule, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3588 |
isAccessible(callerModule, module, pn)); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3589 |
if (isAccessible(callerModule, module, pn)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3590 |
InputStream in = module.getResourceAsStream(resourceName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3591 |
if (in != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3592 |
return in; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3593 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3594 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3595 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3596 |
ClassLoader loader = module.getClassLoader(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3597 |
trace("loader for %s %s caller %s%n", module, resourceName, callerModule); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3598 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3599 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3600 |
if (loader != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3601 |
return loader.getResourceAsStream(resourceName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3602 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3603 |
URL url = BootLoader.findResource(resourceName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3604 |
if (url != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3605 |
return url.openStream(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3606 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3607 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3608 |
} catch (Exception e) {} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3609 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3610 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3611 |
} catch (IOException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3612 |
throw new UncheckedIOException(e); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3613 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3614 |
}; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3615 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3616 |
try (InputStream stream = AccessController.doPrivileged(pa)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3617 |
if (stream != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3618 |
return new PropertyResourceBundle(stream); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3619 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3620 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3621 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3622 |
} catch (UncheckedIOException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3623 |
throw e.getCause(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3624 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3625 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3626 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3627 |
private static String toPackageName(String bundleName) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3628 |
int i = bundleName.lastIndexOf('.'); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3629 |
return i != -1 ? bundleName.substring(0, i) : ""; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3630 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3631 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3632 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3633 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3634 |
private static final boolean TRACE_ON = Boolean.valueOf( |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3635 |
GetPropertyAction.privilegedGetProperty("resource.bundle.debug", "false")); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3636 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3637 |
private static void trace(String format, Object... params) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3638 |
if (TRACE_ON) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3639 |
System.out.format(format, params); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
3640 |
} |
2 | 3641 |
} |