11/*
2- * Copyright (c) 2012, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2012, 2020 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525
2626package sun .util .locale .provider ;
2727
28+ import java .lang .reflect .InvocationTargetException ;
2829import java .text .spi .BreakIteratorProvider ;
2930import java .text .spi .CollatorProvider ;
3031import java .text .spi .DateFormatProvider ;
3738import java .util .Locale ;
3839import java .util .Map ;
3940import java .util .ResourceBundle ;
41+ import java .util .ServiceConfigurationError ;
4042import java .util .Set ;
4143import java .util .concurrent .ConcurrentHashMap ;
4244import java .util .concurrent .ConcurrentMap ;
5052import sun .text .spi .JavaTimeDateTimePatternProvider ;
5153import sun .util .spi .CalendarProvider ;
5254
55+ import static java .lang .System .*;
56+
5357/**
5458 * The LocaleProviderAdapter abstract class.
5559 *
@@ -60,7 +64,7 @@ public abstract class LocaleProviderAdapter {
6064 /**
6165 * Adapter type.
6266 */
63- public static enum Type {
67+ public enum Type {
6468 JRE ("sun.util.locale.provider.JRELocaleProviderAdapter" , "sun.util.resources" , "sun.text.resources" ),
6569 CLDR ("sun.util.cldr.CLDRLocaleProviderAdapter" , "sun.util.resources.cldr" , "sun.text.resources.cldr" ),
6670 SPI ("sun.util.locale.provider.SPILocaleProviderAdapter" ),
@@ -71,11 +75,11 @@ public static enum Type {
7175 private final String UTIL_RESOURCES_PACKAGE ;
7276 private final String TEXT_RESOURCES_PACKAGE ;
7377
74- private Type (String className ) {
78+ Type (String className ) {
7579 this (className , null , null );
7680 }
7781
78- private Type (String className , String util , String text ) {
82+ Type (String className , String util , String text ) {
7983 CLASSNAME = className ;
8084 UTIL_RESOURCES_PACKAGE = util ;
8185 TEXT_RESOURCES_PACKAGE = text ;
@@ -113,12 +117,13 @@ public String getTextResourcesPackage() {
113117 /**
114118 * Adapter lookup cache.
115119 */
116- private static ConcurrentMap <Class <? extends LocaleServiceProvider >, ConcurrentMap <Locale , LocaleProviderAdapter >>
120+ private static final ConcurrentMap <Class <? extends LocaleServiceProvider >, ConcurrentMap <Locale , LocaleProviderAdapter >>
117121 adapterCache = new ConcurrentHashMap <>();
118122
119123 static {
120124 String order = GetPropertyAction .privilegedGetProperty ("java.locale.providers" );
121- List <Type > typeList = new ArrayList <>();
125+ ArrayList <Type > typeList = new ArrayList <>();
126+ String invalidTypeMessage = null ;
122127
123128 // Check user specified adapter preference
124129 if (order != null && !order .isEmpty ()) {
@@ -133,18 +138,17 @@ public String getTextResourcesPackage() {
133138 if (!typeList .contains (aType )) {
134139 typeList .add (aType );
135140 }
136- } catch (IllegalArgumentException | UnsupportedOperationException e ) {
137- // could be caused by the user specifying wrong
138- // provider name or format in the system property
139- LocaleServiceProviderPool .config (LocaleProviderAdapter .class , e .toString ());
141+ } catch (IllegalArgumentException e ) {
142+ // construct a log message.
143+ invalidTypeMessage = "Invalid locale provider adapter \" " + type + "\" ignored." ;
140144 }
141145 }
142146 }
143147
144148 defaultLocaleProviderAdapter = Type .CLDR ;
145149 if (!typeList .isEmpty ()) {
146150 // bona fide preference exists
147- if (!(typeList .contains (Type .CLDR ) || ( typeList .contains (Type .JRE ) ))) {
151+ if (!(typeList .contains (Type .CLDR ) || typeList .contains (Type .JRE ))) {
148152 // Append FALLBACK as the last resort when no ResourceBundleBasedAdapter is available.
149153 typeList .add (Type .FALLBACK );
150154 defaultLocaleProviderAdapter = Type .FALLBACK ;
@@ -155,6 +159,15 @@ public String getTextResourcesPackage() {
155159 typeList .add (Type .JRE );
156160 }
157161 adapterPreference = Collections .unmodifiableList (typeList );
162+
163+ // Emit logs, if any, after 'adapterPreference' is initialized which is needed
164+ // for logging.
165+ if (invalidTypeMessage != null ) {
166+ // could be caused by the user specifying wrong
167+ // provider name or format in the system property
168+ getLogger (LocaleProviderAdapter .class .getCanonicalName ())
169+ .log (Logger .Level .INFO , invalidTypeMessage );
170+ }
158171 }
159172
160173 /**
@@ -167,30 +180,25 @@ public static LocaleProviderAdapter forType(Type type) {
167180 case SPI :
168181 case HOST :
169182 case FALLBACK :
170- LocaleProviderAdapter adapter = null ;
171- LocaleProviderAdapter cached = adapterInstances .get (type );
172- if (cached == null ) {
183+ LocaleProviderAdapter adapter = adapterInstances .get (type );
184+ if (adapter == null ) {
173185 try {
174186 // lazily load adapters here
175- @ SuppressWarnings ("deprecation" )
176- Object tmp = Class .forName (type .getAdapterClassName ()).newInstance ();
177- adapter = (LocaleProviderAdapter )tmp ;
178- cached = adapterInstances .putIfAbsent (type , adapter );
187+ adapter = (LocaleProviderAdapter )Class .forName (type .getAdapterClassName ())
188+ .getDeclaredConstructor ().newInstance ();
189+ LocaleProviderAdapter cached = adapterInstances .putIfAbsent (type , adapter );
179190 if (cached != null ) {
180191 adapter = cached ;
181192 }
182- } catch (ClassNotFoundException |
193+ } catch (NoSuchMethodException |
194+ InvocationTargetException |
195+ ClassNotFoundException |
183196 IllegalAccessException |
184197 InstantiationException |
185198 UnsupportedOperationException e ) {
186- LocaleServiceProviderPool .config (LocaleProviderAdapter .class , e .toString ());
187- adapterInstances .putIfAbsent (type , NONEXISTENT_ADAPTER );
188- if (defaultLocaleProviderAdapter == type ) {
189- defaultLocaleProviderAdapter = Type .FALLBACK ;
190- }
199+ throw new ServiceConfigurationError ("Locale provider adapter \" " +
200+ type + "\" cannot be instantiated." , e );
191201 }
192- } else if (cached != NONEXISTENT_ADAPTER ) {
193- adapter = cached ;
194202 }
195203 return adapter ;
196204 default :
@@ -440,14 +448,4 @@ public static Locale[] toLocaleArray(Set<String> tags) {
440448 public abstract LocaleResources getLocaleResources (Locale locale );
441449
442450 public abstract Locale [] getAvailableLocales ();
443-
444- private static final LocaleProviderAdapter NONEXISTENT_ADAPTER = new NonExistentAdapter ();
445- private static final class NonExistentAdapter extends FallbackLocaleProviderAdapter {
446- @ Override
447- public LocaleProviderAdapter .Type getAdapterType () {
448- return null ;
449- }
450-
451- private NonExistentAdapter () {};
452- }
453451}
0 commit comments