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 sun.util.locale.provider;
27
28 import java.io.File;
29 import java.security.AccessController;
30 import java.security.PrivilegedAction;
31 import java.text.spi.BreakIteratorProvider;
32 import java.text.spi.CollatorProvider;
33 import java.text.spi.DateFormatProvider;
34 import java.text.spi.DateFormatSymbolsProvider;
35 import java.text.spi.DecimalFormatSymbolsProvider;
36 import java.text.spi.NumberFormatProvider;
37 import java.util.Collections;
38 import java.util.HashSet;
39 import java.util.Locale;
40 import java.util.Set;
41 import java.util.StringTokenizer;
42 import java.util.concurrent.ConcurrentHashMap;
43 import java.util.concurrent.ConcurrentMap;
44 import java.util.spi.CalendarDataProvider;
45 import java.util.spi.CalendarNameProvider;
46 import java.util.spi.CurrencyNameProvider;
47 import java.util.spi.LocaleNameProvider;
48 import java.util.spi.LocaleServiceProvider;
49 import java.util.spi.TimeZoneNameProvider;
50 import sun.util.resources.LocaleData;
51 import sun.util.spi.CalendarProvider;
52
53 /**
54 * LocaleProviderAdapter implementation for the legacy JRE locale data.
55 *
56 * @author Naoto Sato
57 * @author Masayoshi Okutsu
58 */
59 public class JRELocaleProviderAdapter extends LocaleProviderAdapter implements ResourceBundleBasedAdapter {
60
61 private static final String LOCALE_DATA_JAR_NAME = "localedata.jar";
62
63 private final ConcurrentMap<String, Set<String>> langtagSets
64 = new ConcurrentHashMap<>();
65
66 private final ConcurrentMap<Locale, LocaleResources> localeResourcesMap
67 = new ConcurrentHashMap<>();
68
69 // LocaleData specific to this LocaleProviderAdapter.
70 private volatile LocaleData localeData;
71
72 /**
73 * Returns the type of this LocaleProviderAdapter
74 */
75 @Override
76 public LocaleProviderAdapter.Type getAdapterType() {
77 return Type.JRE;
78 }
79
80 /**
81 * Getter method for Locale Service Providers
82 */
339 * locale coverage in the JRE.
340 */
341 @Override
342 public Locale[] getAvailableLocales() {
343 return AvailableJRELocales.localeList.clone();
344 }
345
346 public Set<String> getLanguageTagSet(String category) {
347 Set<String> tagset = langtagSets.get(category);
348 if (tagset == null) {
349 tagset = createLanguageTagSet(category);
350 Set<String> ts = langtagSets.putIfAbsent(category, tagset);
351 if (ts != null) {
352 tagset = ts;
353 }
354 }
355 return tagset;
356 }
357
358 protected Set<String> createLanguageTagSet(String category) {
359 String supportedLocaleString = LocaleDataMetaInfo.getSupportedLocaleString(category);
360 if (supportedLocaleString == null) {
361 return Collections.emptySet();
362 }
363 Set<String> tagset = new HashSet<>();
364 StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
365 while (tokens.hasMoreTokens()) {
366 String token = tokens.nextToken();
367 if (token.equals("|")) {
368 if (isNonENLangSupported()) {
369 continue;
370 }
371 break;
372 }
373 tagset.add(token);
374 }
375
376 return tagset;
377 }
378
379 /**
380 * Lazy load available locales.
381 */
382 private static class AvailableJRELocales {
383 private static final Locale[] localeList = createAvailableLocales();
384 private AvailableJRELocales() {
385 }
386 }
387
388 private static Locale[] createAvailableLocales() {
389 /*
390 * Gets the locale string list from LocaleDataMetaInfo class and then
391 * contructs the Locale array and a set of language tags based on the
392 * locale string returned above.
393 */
394 String supportedLocaleString = LocaleDataMetaInfo.getSupportedLocaleString("AvailableLocales");
395
396 if (supportedLocaleString.length() == 0) {
397 throw new InternalError("No available locales for JRE");
398 }
399
400 /*
401 * Look for "|" and construct a new locale string list.
402 */
403 int barIndex = supportedLocaleString.indexOf('|');
404 StringTokenizer localeStringTokenizer;
405 if (isNonENLangSupported()) {
406 localeStringTokenizer = new StringTokenizer(supportedLocaleString.substring(0, barIndex)
407 + supportedLocaleString.substring(barIndex + 1));
408 } else {
409 localeStringTokenizer = new StringTokenizer(supportedLocaleString.substring(0, barIndex));
410 }
411
412 int length = localeStringTokenizer.countTokens();
413 Locale[] locales = new Locale[length + 1];
414 locales[0] = Locale.ROOT;
415 for (int i = 1; i <= length; i++) {
416 String currentToken = localeStringTokenizer.nextToken();
417 switch (currentToken) {
418 case "ja-JP-JP":
419 locales[i] = JRELocaleConstants.JA_JP_JP;
420 break;
421 case "no-NO-NY":
422 locales[i] = JRELocaleConstants.NO_NO_NY;
423 break;
424 case "th-TH-TH":
425 locales[i] = JRELocaleConstants.TH_TH_TH;
426 break;
427 default:
428 locales[i] = Locale.forLanguageTag(currentToken);
429 }
430 }
431 return locales;
432 }
433
434 private static volatile Boolean isNonENSupported = null;
435
436 /*
437 * Returns true if the non EN resources jar file exists in jre
438 * extension directory. @returns true if the jar file is there. Otherwise,
439 * returns false.
440 */
441 private static boolean isNonENLangSupported() {
442 if (isNonENSupported == null) {
443 synchronized (JRELocaleProviderAdapter.class) {
444 if (isNonENSupported == null) {
445 final String sep = File.separator;
446 String localeDataJar =
447 java.security.AccessController.doPrivileged(
448 new sun.security.action.GetPropertyAction("java.home"))
449 + sep + "lib" + sep + "ext" + sep + LOCALE_DATA_JAR_NAME;
450
451 /*
452 * Peek at the installed extension directory to see if
453 * localedata.jar is installed or not.
454 */
455 final File f = new File(localeDataJar);
456 isNonENSupported =
457 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
458 @Override
459 public Boolean run() {
460 return f.exists();
461 }
462 });
463 }
464 }
465 }
466 return isNonENSupported;
467 }
468 }
|
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 sun.util.locale.provider;
27
28 import java.security.AccessController;
29 import java.security.PrivilegedActionException;
30 import java.security.PrivilegedExceptionAction;
31 import java.text.spi.BreakIteratorProvider;
32 import java.text.spi.CollatorProvider;
33 import java.text.spi.DateFormatProvider;
34 import java.text.spi.DateFormatSymbolsProvider;
35 import java.text.spi.DecimalFormatSymbolsProvider;
36 import java.text.spi.NumberFormatProvider;
37 import java.util.Collections;
38 import java.util.HashSet;
39 import java.util.Locale;
40 import java.util.ServiceLoader;
41 import java.util.Set;
42 import java.util.StringTokenizer;
43 import java.util.concurrent.ConcurrentHashMap;
44 import java.util.concurrent.ConcurrentMap;
45 import java.util.spi.CalendarDataProvider;
46 import java.util.spi.CalendarNameProvider;
47 import java.util.spi.CurrencyNameProvider;
48 import java.util.spi.LocaleNameProvider;
49 import java.util.spi.LocaleServiceProvider;
50 import java.util.spi.TimeZoneNameProvider;
51 import sun.util.resources.LocaleData;
52 import sun.util.spi.CalendarProvider;
53
54 /**
55 * LocaleProviderAdapter implementation for the legacy JRE locale data.
56 *
57 * @author Naoto Sato
58 * @author Masayoshi Okutsu
59 */
60 public class JRELocaleProviderAdapter extends LocaleProviderAdapter implements ResourceBundleBasedAdapter {
61
62 private final ConcurrentMap<String, Set<String>> langtagSets
63 = new ConcurrentHashMap<>();
64
65 private final ConcurrentMap<Locale, LocaleResources> localeResourcesMap
66 = new ConcurrentHashMap<>();
67
68 // LocaleData specific to this LocaleProviderAdapter.
69 private volatile LocaleData localeData;
70
71 /**
72 * Returns the type of this LocaleProviderAdapter
73 */
74 @Override
75 public LocaleProviderAdapter.Type getAdapterType() {
76 return Type.JRE;
77 }
78
79 /**
80 * Getter method for Locale Service Providers
81 */
338 * locale coverage in the JRE.
339 */
340 @Override
341 public Locale[] getAvailableLocales() {
342 return AvailableJRELocales.localeList.clone();
343 }
344
345 public Set<String> getLanguageTagSet(String category) {
346 Set<String> tagset = langtagSets.get(category);
347 if (tagset == null) {
348 tagset = createLanguageTagSet(category);
349 Set<String> ts = langtagSets.putIfAbsent(category, tagset);
350 if (ts != null) {
351 tagset = ts;
352 }
353 }
354 return tagset;
355 }
356
357 protected Set<String> createLanguageTagSet(String category) {
358 String supportedLocaleString = createSupportedLocaleString(category);
359 if (supportedLocaleString == null) {
360 return Collections.emptySet();
361 }
362 Set<String> tagset = new HashSet<>();
363 StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
364 while (tokens.hasMoreTokens()) {
365 tagset.add(tokens.nextToken());
366 }
367
368 return tagset;
369 }
370
371 private static String createSupportedLocaleString(String category) {
372 // Directly call English tags, as we know it's in the base module.
373 String supportedLocaleString = EnLocaleDataMetaInfo.getSupportedLocaleString(category);
374
375 // Use ServiceLoader to dynamically acquire installede locales' tags.
376 try {
377 String nonENTags = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
378 @Override
379 public String run() {
380 String tags = null;
381 for (LocaleDataMetaInfo ldmi :
382 ServiceLoader.loadInstalled(LocaleDataMetaInfo.class)) {
383 if (ldmi.getType() == LocaleProviderAdapter.Type.JRE) {
384 String t = ldmi.availableLanguageTags(category);
385 if (t != null) {
386 if (tags == null) {
387 tags = t;
388 } else {
389 tags += " " + t;
390 }
391 }
392 }
393 }
394 return tags;
395 }
396 });
397
398 if (nonENTags != null) {
399 supportedLocaleString += " " + nonENTags;
400 }
401 } catch (Exception e) {
402 // catch any exception, and ignore them as if non-EN locales do not exist.
403 }
404
405 return supportedLocaleString;
406 }
407
408 /**
409 * Lazy load available locales.
410 */
411 private static class AvailableJRELocales {
412 private static final Locale[] localeList = createAvailableLocales();
413 private AvailableJRELocales() {
414 }
415 }
416
417 private static Locale[] createAvailableLocales() {
418 /*
419 * Gets the locale string list from LocaleDataMetaInfo classes and then
420 * contructs the Locale array and a set of language tags based on the
421 * locale string returned above.
422 */
423 String supportedLocaleString = createSupportedLocaleString("AvailableLocales");
424
425 if (supportedLocaleString.length() == 0) {
426 throw new InternalError("No available locales for JRE");
427 }
428
429 StringTokenizer localeStringTokenizer = new StringTokenizer(supportedLocaleString);
430
431 int length = localeStringTokenizer.countTokens();
432 Locale[] locales = new Locale[length + 1];
433 locales[0] = Locale.ROOT;
434 for (int i = 1; i <= length; i++) {
435 String currentToken = localeStringTokenizer.nextToken();
436 switch (currentToken) {
437 case "ja-JP-JP":
438 locales[i] = JRELocaleConstants.JA_JP_JP;
439 break;
440 case "no-NO-NY":
441 locales[i] = JRELocaleConstants.NO_NO_NY;
442 break;
443 case "th-TH-TH":
444 locales[i] = JRELocaleConstants.TH_TH_TH;
445 break;
446 default:
447 locales[i] = Locale.forLanguageTag(currentToken);
448 }
449 }
450 return locales;
451 }
452 }
|