author | mchung |
Fri, 03 Jun 2016 12:26:45 -0700 | |
changeset 38782 | ff27bc5c278e |
parent 36511 | 9d0388c6b336 |
child 42338 | a60f280f803c |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package java.util.spi; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.UncheckedIOException; |
|
30 |
import java.lang.reflect.Module; |
|
31 |
import java.security.AccessController; |
|
32 |
import java.security.PrivilegedAction; |
|
33 |
import java.util.Locale; |
|
34 |
import java.util.ResourceBundle; |
|
35 |
import sun.util.locale.provider.ResourceBundleProviderSupport; |
|
36 |
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION; |
|
37 |
||
38 |
||
39 |
/** |
|
40 |
* {@code AbstractResourceBundleProvider} is an abstract class for helping |
|
41 |
* implement the {@link ResourceBundleProvider} interface. |
|
42 |
* |
|
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
43 |
* <p> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
44 |
* Resource bundles can be packaged in a named module separated from |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
45 |
* the <em>caller module</em> loading the resource bundle, i.e. the module |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
46 |
* calling {@link ResourceBundle#getBundle(String)}. For the caller module |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
47 |
* to load a resource bundle "{@code com.example.app.MyResources}" |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
48 |
* from another module and a service interface named |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
49 |
* "{@code com.example.app.MyResourcesProvider}", |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
50 |
* the <em>bundle provider module</em> can provide the implementation class |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
51 |
* as follows: |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
52 |
* |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
53 |
* <pre><code> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
54 |
* import com.example.app.MyResourcesProvider; |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
55 |
* class MyResourcesProviderImpl extends AbstractResourceBundleProvider |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
56 |
* implements MyResourcesProvider |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
57 |
* {</code> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
58 |
* {@code @Override |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
59 |
* public ResourceBundle getBundle(String baseName, Locale locale) { |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
60 |
* // this module only provides bundles in french |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
61 |
* if (locale.equals(Locale.FRENCH)) { |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
62 |
* return super.getBundle(baseName, locale); |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
63 |
* } |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
64 |
* return null; |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
65 |
* } |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
66 |
* }}</pre> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
67 |
* |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
68 |
* @see <a href="../ResourceBundle.html#bundleprovider"> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
69 |
* Resource Bundles in Named Modules</a> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
70 |
* @see <a href="../ResourceBundle.html#RBP_support"> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
71 |
* ResourceBundleProvider Service Providers</a> |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
72 |
* |
36511 | 73 |
* @since 9 |
74 |
*/ |
|
75 |
public abstract class AbstractResourceBundleProvider implements ResourceBundleProvider { |
|
76 |
private static final String FORMAT_CLASS = "java.class"; |
|
77 |
private static final String FORMAT_PROPERTIES = "java.properties"; |
|
78 |
||
79 |
private final String[] formats; |
|
80 |
||
81 |
/** |
|
82 |
* Constructs an {@code AbstractResourceBundleProvider} with the |
|
83 |
* "java.properties" format. This constructor is equivalent to |
|
84 |
* {@code AbstractResourceBundleProvider("java.properties")}. |
|
85 |
*/ |
|
86 |
protected AbstractResourceBundleProvider() { |
|
87 |
this(FORMAT_PROPERTIES); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Constructs an {@code AbstractResourceBundleProvider} with the specified |
|
92 |
* {@code formats}. The {@link #getBundle(String, Locale)} method looks up |
|
93 |
* resource bundles for the given {@code formats}. {@code formats} must |
|
94 |
* be "java.class" or "java.properties". |
|
95 |
* |
|
96 |
* @param formats the formats to be used for loading resource bundles |
|
97 |
* @throws NullPointerException if the given {@code formats} is null |
|
98 |
* @throws IllegalArgumentException if the given {@code formats} is not |
|
99 |
* "java.class" or "java.properties". |
|
100 |
*/ |
|
101 |
protected AbstractResourceBundleProvider(String... formats) { |
|
102 |
this.formats = formats.clone(); // defensive copy |
|
103 |
if (this.formats.length == 0) { |
|
104 |
throw new IllegalArgumentException("empty formats"); |
|
105 |
} |
|
106 |
for (String f : this.formats) { |
|
107 |
if (!FORMAT_CLASS.equals(f) && !FORMAT_PROPERTIES.equals(f)) { |
|
108 |
throw new IllegalArgumentException(f); |
|
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Returns the bundle name for the given {@code baseName} and {@code |
|
115 |
* locale}. This method is called from the default implementation of the |
|
116 |
* {@link #getBundle(String, Locale)} method. |
|
117 |
* |
|
118 |
* @implNote The default implementation of this method is the same as the |
|
119 |
* implementation of |
|
120 |
* {@link java.util.ResourceBundle.Control#toBundleName(String, Locale)}. |
|
121 |
* |
|
122 |
* @param baseName the base name of the resource bundle, a fully qualified |
|
123 |
* class name |
|
124 |
* @param locale the locale for which a resource bundle should be loaded |
|
125 |
* @return the bundle name for the resource bundle |
|
126 |
*/ |
|
127 |
protected String toBundleName(String baseName, Locale locale) { |
|
128 |
return ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT) |
|
129 |
.toBundleName(baseName, locale); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Returns a {@code ResourceBundle} for the given {@code baseName} and |
|
134 |
* {@code locale}. This method calls the |
|
135 |
* {@link #toBundleName(String, Locale) toBundleName} method to get the |
|
136 |
* bundle name for the {@code baseName} and {@code locale}. The formats |
|
137 |
* specified by the constructor will be searched to find the resource |
|
138 |
* bundle. |
|
139 |
* |
|
140 |
* @implNote |
|
141 |
* The default implementation of this method will find the resource bundle |
|
142 |
* local to the module of this provider. |
|
143 |
* |
|
144 |
* @param baseName the base bundle name of the resource bundle, a fully |
|
145 |
* qualified class name. |
|
146 |
* @param locale the locale for which the resource bundle should be instantiated |
|
147 |
* @return {@code ResourceBundle} of the given {@code baseName} and {@code locale}, |
|
148 |
* or null if no resource bundle is found |
|
149 |
* @throws NullPointerException if {@code baseName} or {@code locale} is null |
|
150 |
* @throws UncheckedIOException if any IO exception occurred during resource |
|
151 |
* bundle loading |
|
152 |
*/ |
|
153 |
@Override |
|
154 |
public ResourceBundle getBundle(String baseName, Locale locale) { |
|
155 |
Module module = this.getClass().getModule(); |
|
156 |
String bundleName = toBundleName(baseName, locale); |
|
157 |
ResourceBundle bundle = null; |
|
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
158 |
|
36511 | 159 |
for (String format : formats) { |
160 |
try { |
|
161 |
if (FORMAT_CLASS.equals(format)) { |
|
162 |
PrivilegedAction<ResourceBundle> pa = () -> |
|
163 |
ResourceBundleProviderSupport |
|
164 |
.loadResourceBundle(module, bundleName); |
|
165 |
bundle = AccessController.doPrivileged(pa, null, GET_CLASSLOADER_PERMISSION); |
|
166 |
} else if (FORMAT_PROPERTIES.equals(format)) { |
|
167 |
bundle = ResourceBundleProviderSupport |
|
168 |
.loadPropertyResourceBundle(module, bundleName); |
|
169 |
} |
|
170 |
if (bundle != null) { |
|
171 |
break; |
|
172 |
} |
|
173 |
} catch (IOException e) { |
|
174 |
throw new UncheckedIOException(e); |
|
175 |
} |
|
176 |
} |
|
177 |
return bundle; |
|
178 |
} |
|
179 |
} |