author | mchung |
Thu, 21 Dec 2017 15:18:49 -0800 | |
changeset 48524 | b6fc9a193661 |
parent 47216 | 71c04702a3d5 |
child 52427 | 3c6aa484536c |
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 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
28 |
import jdk.internal.misc.JavaUtilResourceBundleAccess; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
29 |
import jdk.internal.misc.SharedSecrets; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
30 |
|
36511 | 31 |
import java.io.IOException; |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
32 |
import java.io.InputStream; |
36511 | 33 |
import java.io.UncheckedIOException; |
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
import java.util.Locale; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
37 |
import java.util.PropertyResourceBundle; |
36511 | 38 |
import java.util.ResourceBundle; |
39 |
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION; |
|
40 |
||
41 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
42 |
* {@code AbstractResourceBundleProvider} is an abstract class that provides |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
43 |
* the basic support for a provider implementation class for |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
44 |
* {@link ResourceBundleProvider}. |
36511 | 45 |
* |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
46 |
* <p> |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
47 |
* Resource bundles can be packaged in one or more |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
48 |
* named modules, <em>service provider modules</em>. The <em>consumer</em> of the |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
49 |
* resource bundle is the one calling {@link ResourceBundle#getBundle(String)}. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
50 |
* In order for the consumer module to load a resource bundle |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
51 |
* "{@code com.example.app.MyResources}" provided by another module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
52 |
* it will use the {@linkplain java.util.ServiceLoader service loader} |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
53 |
* mechanism. A service interface named "{@code com.example.app.spi.MyResourcesProvider}" |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
54 |
* must be defined and a <em>service provider module</em> will provide an |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
55 |
* implementation class of "{@code com.example.app.spi.MyResourcesProvider}" |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
56 |
* as follows: |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
57 |
* |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
58 |
* <blockquote><pre> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
59 |
* {@code import com.example.app.spi.MyResourcesProvider; |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
60 |
* class MyResourcesProviderImpl extends AbstractResourceBundleProvider |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
61 |
* implements MyResourcesProvider |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
62 |
* { |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
63 |
* public MyResourcesProviderImpl() { |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
64 |
* super("java.properties"); |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
65 |
* } |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
66 |
* // this provider maps the resource bundle to per-language package |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
67 |
* protected String toBundleName(String baseName, Locale locale) { |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
68 |
* return "p." + locale.getLanguage() + "." + baseName; |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
69 |
* } |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
70 |
* |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
71 |
* public ResourceBundle getBundle(String baseName, Locale locale) { |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
72 |
* // this module only provides bundles in French |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
73 |
* if (locale.equals(Locale.FRENCH)) { |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
74 |
* return super.getBundle(baseName, locale); |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
75 |
* } |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
76 |
* // otherwise return null |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
77 |
* return null; |
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
78 |
* } |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
79 |
* }}</pre></blockquote> |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
80 |
* |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
81 |
* Refer to {@link ResourceBundleProvider} for details. |
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
82 |
* |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
83 |
* @see <a href="../ResourceBundle.html#resource-bundle-modules"> |
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
84 |
* Resource Bundles and Named Modules</a> |
36511 | 85 |
* @since 9 |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
86 |
* @spec JPMS |
48524
b6fc9a193661
8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents:
47216
diff
changeset
|
87 |
|
36511 | 88 |
*/ |
89 |
public abstract class AbstractResourceBundleProvider implements ResourceBundleProvider { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
90 |
private static final JavaUtilResourceBundleAccess RB_ACCESS = |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
91 |
SharedSecrets.getJavaUtilResourceBundleAccess(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
92 |
|
36511 | 93 |
private static final String FORMAT_CLASS = "java.class"; |
94 |
private static final String FORMAT_PROPERTIES = "java.properties"; |
|
95 |
||
96 |
private final String[] formats; |
|
97 |
||
98 |
/** |
|
99 |
* Constructs an {@code AbstractResourceBundleProvider} with the |
|
100 |
* "java.properties" format. This constructor is equivalent to |
|
101 |
* {@code AbstractResourceBundleProvider("java.properties")}. |
|
102 |
*/ |
|
103 |
protected AbstractResourceBundleProvider() { |
|
104 |
this(FORMAT_PROPERTIES); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Constructs an {@code AbstractResourceBundleProvider} with the specified |
|
109 |
* {@code formats}. The {@link #getBundle(String, Locale)} method looks up |
|
110 |
* resource bundles for the given {@code formats}. {@code formats} must |
|
111 |
* be "java.class" or "java.properties". |
|
112 |
* |
|
113 |
* @param formats the formats to be used for loading resource bundles |
|
114 |
* @throws NullPointerException if the given {@code formats} is null |
|
115 |
* @throws IllegalArgumentException if the given {@code formats} is not |
|
116 |
* "java.class" or "java.properties". |
|
117 |
*/ |
|
118 |
protected AbstractResourceBundleProvider(String... formats) { |
|
119 |
this.formats = formats.clone(); // defensive copy |
|
120 |
if (this.formats.length == 0) { |
|
121 |
throw new IllegalArgumentException("empty formats"); |
|
122 |
} |
|
123 |
for (String f : this.formats) { |
|
124 |
if (!FORMAT_CLASS.equals(f) && !FORMAT_PROPERTIES.equals(f)) { |
|
125 |
throw new IllegalArgumentException(f); |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Returns the bundle name for the given {@code baseName} and {@code |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
132 |
* locale} that this provider provides. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
133 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
134 |
* @apiNote |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
135 |
* A resource bundle provider may package its resource bundles in the |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
136 |
* same package as the base name of the resource bundle if the package |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
137 |
* is not split among other named modules. If there are more than one |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
138 |
* bundle providers providing the resource bundle of a given base name, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
139 |
* the resource bundles can be packaged with per-language grouping |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
140 |
* or per-region grouping to eliminate the split packages. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
141 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
142 |
* <p>For example, if {@code baseName} is {@code "p.resources.Bundle"} then |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
143 |
* the resource bundle name of {@code "p.resources.Bundle"} of |
47027
84b6099d24e5
8179246: /  are literally visible in javadoc
naoto
parents:
44545
diff
changeset
|
144 |
* <code style="white-space:nowrap">Locale("ja", "", "XX")</code> |
84b6099d24e5
8179246: /  are literally visible in javadoc
naoto
parents:
44545
diff
changeset
|
145 |
* and {@code Locale("en")} could be <code style="white-space:nowrap"> |
84b6099d24e5
8179246: /  are literally visible in javadoc
naoto
parents:
44545
diff
changeset
|
146 |
* "p.resources.ja.Bundle_ja_ _XX"</code> and |
84b6099d24e5
8179246: /  are literally visible in javadoc
naoto
parents:
44545
diff
changeset
|
147 |
* {@code "p.resources.Bundle_en"} respectively. |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
148 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
149 |
* <p> This method is called from the default implementation of the |
36511 | 150 |
* {@link #getBundle(String, Locale)} method. |
151 |
* |
|
152 |
* @implNote The default implementation of this method is the same as the |
|
153 |
* implementation of |
|
154 |
* {@link java.util.ResourceBundle.Control#toBundleName(String, Locale)}. |
|
155 |
* |
|
156 |
* @param baseName the base name of the resource bundle, a fully qualified |
|
157 |
* class name |
|
158 |
* @param locale the locale for which a resource bundle should be loaded |
|
159 |
* @return the bundle name for the resource bundle |
|
160 |
*/ |
|
161 |
protected String toBundleName(String baseName, Locale locale) { |
|
162 |
return ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT) |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
163 |
.toBundleName(baseName, locale); |
36511 | 164 |
} |
165 |
||
166 |
/** |
|
167 |
* Returns a {@code ResourceBundle} for the given {@code baseName} and |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
168 |
* {@code locale}. |
36511 | 169 |
* |
170 |
* @implNote |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
171 |
* The default implementation of this method calls the |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
172 |
* {@link #toBundleName(String, Locale) toBundleName} method to get the |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
173 |
* bundle name for the {@code baseName} and {@code locale} and finds the |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
174 |
* resource bundle of the bundle name local in the module of this provider. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
175 |
* It will only search the formats specified when this provider was |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
176 |
* constructed. |
36511 | 177 |
* |
178 |
* @param baseName the base bundle name of the resource bundle, a fully |
|
179 |
* qualified class name. |
|
180 |
* @param locale the locale for which the resource bundle should be instantiated |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
181 |
* @return {@code ResourceBundle} of the given {@code baseName} and |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
182 |
* {@code locale}, or {@code null} if no resource bundle is found |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
183 |
* @throws NullPointerException if {@code baseName} or {@code locale} is |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
184 |
* {@code null} |
36511 | 185 |
* @throws UncheckedIOException if any IO exception occurred during resource |
186 |
* bundle loading |
|
187 |
*/ |
|
188 |
@Override |
|
189 |
public ResourceBundle getBundle(String baseName, Locale locale) { |
|
190 |
Module module = this.getClass().getModule(); |
|
191 |
String bundleName = toBundleName(baseName, locale); |
|
192 |
ResourceBundle bundle = null; |
|
38782
ff27bc5c278e
8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents:
36511
diff
changeset
|
193 |
|
36511 | 194 |
for (String format : formats) { |
195 |
try { |
|
196 |
if (FORMAT_CLASS.equals(format)) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
197 |
bundle = loadResourceBundle(module, bundleName); |
36511 | 198 |
} else if (FORMAT_PROPERTIES.equals(format)) { |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
199 |
bundle = loadPropertyResourceBundle(module, bundleName); |
36511 | 200 |
} |
201 |
if (bundle != null) { |
|
202 |
break; |
|
203 |
} |
|
204 |
} catch (IOException e) { |
|
205 |
throw new UncheckedIOException(e); |
|
206 |
} |
|
207 |
} |
|
208 |
return bundle; |
|
209 |
} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
210 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
211 |
/* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
212 |
* Returns the ResourceBundle of .class format if found in the module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
213 |
* of this provider. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
214 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
215 |
private static ResourceBundle loadResourceBundle(Module module, String bundleName) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
216 |
{ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
217 |
PrivilegedAction<Class<?>> pa = () -> Class.forName(module, bundleName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
218 |
Class<?> c = AccessController.doPrivileged(pa, null, GET_CLASSLOADER_PERMISSION); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
219 |
if (c != null && ResourceBundle.class.isAssignableFrom(c)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
220 |
@SuppressWarnings("unchecked") |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
221 |
Class<ResourceBundle> bundleClass = (Class<ResourceBundle>) c; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
222 |
return RB_ACCESS.newResourceBundle(bundleClass); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
223 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
224 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
225 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
226 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
227 |
/* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
228 |
* Returns the ResourceBundle of .property format if found in the module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
229 |
* of this provider. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
230 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
231 |
private static ResourceBundle loadPropertyResourceBundle(Module module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
232 |
String bundleName) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
233 |
throws IOException |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
234 |
{ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
235 |
String resourceName = toResourceName(bundleName, "properties"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
236 |
if (resourceName == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
237 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
238 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
239 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
240 |
PrivilegedAction<InputStream> pa = () -> { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
241 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
242 |
return module.getResourceAsStream(resourceName); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
243 |
} catch (IOException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
244 |
throw new UncheckedIOException(e); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
245 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
246 |
}; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
247 |
try (InputStream stream = AccessController.doPrivileged(pa)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
248 |
if (stream != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
249 |
return new PropertyResourceBundle(stream); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
250 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
251 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
252 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
253 |
} catch (UncheckedIOException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
254 |
throw e.getCause(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
255 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
256 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
257 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
258 |
private static String toResourceName(String bundleName, String suffix) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
259 |
if (bundleName.contains("://")) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
260 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
261 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
262 |
StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
263 |
sb.append(bundleName.replace('.', '/')).append('.').append(suffix); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
264 |
return sb.toString(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
265 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38782
diff
changeset
|
266 |
|
36511 | 267 |
} |