57 */ |
57 */ |
58 public static enum Type { |
58 public static enum Type { |
59 JRE("sun.util.resources", "sun.text.resources"), |
59 JRE("sun.util.resources", "sun.text.resources"), |
60 CLDR("sun.util.resources.cldr", "sun.text.resources.cldr"), |
60 CLDR("sun.util.resources.cldr", "sun.text.resources.cldr"), |
61 SPI, |
61 SPI, |
62 HOST; |
62 HOST, |
|
63 FALLBACK("sun.util.resources", "sun.text.resources"); |
63 |
64 |
64 private final String UTIL_RESOURCES_PACKAGE; |
65 private final String UTIL_RESOURCES_PACKAGE; |
65 private final String TEXT_RESOURCES_PACKAGE; |
66 private final String TEXT_RESOURCES_PACKAGE; |
66 |
67 |
67 private Type() { |
68 private Type() { |
109 /** |
110 /** |
110 * HOST Locale Data Adapter instance, if any. |
111 * HOST Locale Data Adapter instance, if any. |
111 */ |
112 */ |
112 private static LocaleProviderAdapter hostLocaleProviderAdapter = null; |
113 private static LocaleProviderAdapter hostLocaleProviderAdapter = null; |
113 |
114 |
|
115 /** |
|
116 * FALLBACK Locale Data Adapter instance. It's basically the same with JRE, but only kicks |
|
117 * in for the root locale. |
|
118 */ |
|
119 private static LocaleProviderAdapter fallbackLocaleProviderAdapter = null; |
|
120 |
114 static { |
121 static { |
115 String order = AccessController.doPrivileged( |
122 String order = AccessController.doPrivileged( |
116 new sun.security.action.GetPropertyAction("java.locale.providers")); |
123 new sun.security.action.GetPropertyAction("java.locale.providers")); |
117 // Override adapterPreference with the properties one |
124 // Override adapterPreference with the properties one |
118 if (order != null && order.length() != 0) { |
125 if (order != null && order.length() != 0) { |
119 String[] types = order.split(","); |
126 String[] types = order.split(","); |
120 List<Type> typeList = new ArrayList<>(); |
127 List<Type> typeList = new ArrayList<>(); |
121 for (String type : types) { |
128 for (String type : types) { |
122 try { |
129 try { |
123 Type aType = Type.valueOf(type.trim().toUpperCase(Locale.ROOT)); |
130 Type aType = Type.valueOf(type.trim().toUpperCase(Locale.ROOT)); |
124 |
131 |
125 // load adapter if necessary |
132 // load adapter if necessary |
126 switch (aType) { |
133 switch (aType) { |
127 case CLDR: |
134 case CLDR: |
128 cldrLocaleProviderAdapter = new CLDRLocaleProviderAdapter(); |
135 cldrLocaleProviderAdapter = new CLDRLocaleProviderAdapter(); |
129 break; |
136 break; |
130 case HOST: |
137 case HOST: |
131 hostLocaleProviderAdapter = new HostLocaleProviderAdapter(); |
138 hostLocaleProviderAdapter = new HostLocaleProviderAdapter(); |
132 break; |
139 break; |
133 } |
|
134 typeList.add(aType); |
|
135 } catch (// could be caused by the user specifying wrong |
|
136 // provider name or format in the system property |
|
137 IllegalArgumentException | |
|
138 UnsupportedOperationException e) { |
|
139 LocaleServiceProviderPool.config(LocaleProviderAdapter.class, e.toString()); |
|
140 } |
|
141 } |
|
142 |
|
143 if (!typeList.contains(Type.JRE)) { |
|
144 // Append JRE as the last resort. |
|
145 typeList.add(Type.JRE); |
|
146 } |
|
147 adapterPreference = typeList.toArray(new Type[0]); |
|
148 } |
140 } |
|
141 typeList.add(aType); |
|
142 } catch (IllegalArgumentException | UnsupportedOperationException e) { |
|
143 // could be caused by the user specifying wrong |
|
144 // provider name or format in the system property |
|
145 LocaleServiceProviderPool.config(LocaleProviderAdapter.class, e.toString()); |
|
146 } |
|
147 } |
|
148 |
|
149 if (!typeList.isEmpty()) { |
|
150 if (!typeList.contains(Type.JRE)) { |
|
151 // Append FALLBACK as the last resort. |
|
152 fallbackLocaleProviderAdapter = new FallbackLocaleProviderAdapter(); |
|
153 typeList.add(Type.FALLBACK); |
|
154 } |
|
155 adapterPreference = typeList.toArray(new Type[0]); |
|
156 } |
|
157 } |
149 } |
158 } |
150 |
159 |
151 |
160 |
152 /** |
161 /** |
153 * Returns the singleton instance for each adapter type |
162 * Returns the singleton instance for each adapter type |
171 return jreLocaleProviderAdapter; |
182 return jreLocaleProviderAdapter; |
172 } |
183 } |
173 |
184 |
174 public static LocaleProviderAdapter getResourceBundleBased() { |
185 public static LocaleProviderAdapter getResourceBundleBased() { |
175 for (Type type : getAdapterPreference()) { |
186 for (Type type : getAdapterPreference()) { |
176 if (type == Type.JRE || type == Type.CLDR) { |
187 if (type == Type.JRE || type == Type.CLDR || type == Type.FALLBACK) { |
177 return forType(type); |
188 return forType(type); |
178 } |
189 } |
179 } |
190 } |
180 // Shouldn't happen. |
191 // Shouldn't happen. |
181 throw new InternalError(); |
192 throw new InternalError(); |
216 if (adapter != null) { |
227 if (adapter != null) { |
217 return adapter; |
228 return adapter; |
218 } |
229 } |
219 } |
230 } |
220 |
231 |
221 // returns the adapter for JRE as the last resort |
232 // returns the adapter for FALLBACK as the last resort |
222 return jreLocaleProviderAdapter; |
233 return fallbackLocaleProviderAdapter; |
223 } |
234 } |
224 |
235 |
225 private static LocaleProviderAdapter findAdapter(Class<? extends LocaleServiceProvider> providerClass, |
236 private static LocaleProviderAdapter findAdapter(Class<? extends LocaleServiceProvider> providerClass, |
226 Locale locale) { |
237 Locale locale) { |
227 for (Type type : getAdapterPreference()) { |
238 for (Type type : getAdapterPreference()) { |
236 return null; |
247 return null; |
237 } |
248 } |
238 |
249 |
239 /** |
250 /** |
240 * A utility method for implementing the default LocaleServiceProvider.isSupportedLocale |
251 * A utility method for implementing the default LocaleServiceProvider.isSupportedLocale |
241 * for the JRE and CLDR adapters. |
252 * for the JRE, CLDR, and FALLBACK adapters. |
242 */ |
253 */ |
243 static boolean isSupportedLocale(Locale locale, LocaleProviderAdapter.Type type, Set<String> langtags) { |
254 static boolean isSupportedLocale(Locale locale, LocaleProviderAdapter.Type type, Set<String> langtags) { |
244 assert type == Type.JRE || type == Type.CLDR; |
255 assert type == Type.JRE || type == Type.CLDR || type == Type.FALLBACK; |
245 if (locale == Locale.ROOT) { |
256 if (Locale.ROOT.equals(locale)) { |
246 return true; |
257 return true; |
247 } |
258 } |
|
259 |
|
260 if (type == Type.FALLBACK) { |
|
261 // no other locales except ROOT are supported for FALLBACK |
|
262 return false; |
|
263 } |
|
264 |
248 locale = locale.stripExtensions(); |
265 locale = locale.stripExtensions(); |
249 if (langtags.contains(locale.toLanguageTag())) { |
266 if (langtags.contains(locale.toLanguageTag())) { |
250 return true; |
267 return true; |
251 } |
268 } |
252 if (type == LocaleProviderAdapter.Type.JRE) { |
269 if (type == Type.JRE) { |
253 String oldname = locale.toString().replace('_', '-'); |
270 String oldname = locale.toString().replace('_', '-'); |
254 return langtags.contains(oldname); |
271 return langtags.contains(oldname); |
255 } |
272 } |
256 return false; |
273 return false; |
257 } |
274 } |