8245241: Incorrect locale provider preference is not logged

Reviewed-by: joehw, dfuchs
This commit is contained in:
Naoto Sato 2020-05-21 13:55:06 -07:00
parent e3be308329
commit b5b6ae32d2
7 changed files with 111 additions and 114 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,8 +26,8 @@
package sun.util.locale.provider;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ServiceConfigurationError;
import java.util.spi.LocaleServiceProvider;
/**
@ -50,16 +50,20 @@ public class HostLocaleProviderAdapter extends AuxLocaleProviderAdapter {
@SuppressWarnings("unchecked")
protected <P extends LocaleServiceProvider> P findInstalledProvider(final Class<P> c) {
try {
Method getter = HostLocaleProviderAdapterImpl.class.getMethod(
"get" + c.getSimpleName(), (Class<?>[]) null);
return (P)getter.invoke(null, (Object[]) null);
} catch (NoSuchMethodException |
IllegalAccessException |
IllegalArgumentException |
InvocationTargetException ex) {
LocaleServiceProviderPool.config(HostLocaleProviderAdapter.class, ex.toString());
return (P)Class.forName(
"sun.util.locale.provider.HostLocaleProviderAdapterImpl")
.getMethod("get" + c.getSimpleName(), (Class<?>[]) null)
.invoke(null, (Object[]) null);
} catch (ClassNotFoundException |
NoSuchMethodException ex) {
// permissible exceptions as platform may not support host adapter
return null;
} catch (IllegalAccessException |
IllegalArgumentException |
InvocationTargetException ex) {
throw new ServiceConfigurationError(
"Host locale provider cannot be located.", ex);
}
return null;
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,7 @@
package sun.util.locale.provider;
import java.lang.reflect.InvocationTargetException;
import java.text.spi.BreakIteratorProvider;
import java.text.spi.CollatorProvider;
import java.text.spi.DateFormatProvider;
@ -37,6 +38,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.ServiceConfigurationError;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -50,6 +52,8 @@ import sun.security.action.GetPropertyAction;
import sun.text.spi.JavaTimeDateTimePatternProvider;
import sun.util.spi.CalendarProvider;
import static java.lang.System.*;
/**
* The LocaleProviderAdapter abstract class.
*
@ -60,7 +64,7 @@ public abstract class LocaleProviderAdapter {
/**
* Adapter type.
*/
public static enum Type {
public enum Type {
JRE("sun.util.locale.provider.JRELocaleProviderAdapter", "sun.util.resources", "sun.text.resources"),
CLDR("sun.util.cldr.CLDRLocaleProviderAdapter", "sun.util.resources.cldr", "sun.text.resources.cldr"),
SPI("sun.util.locale.provider.SPILocaleProviderAdapter"),
@ -71,11 +75,11 @@ public abstract class LocaleProviderAdapter {
private final String UTIL_RESOURCES_PACKAGE;
private final String TEXT_RESOURCES_PACKAGE;
private Type(String className) {
Type(String className) {
this(className, null, null);
}
private Type(String className, String util, String text) {
Type(String className, String util, String text) {
CLASSNAME = className;
UTIL_RESOURCES_PACKAGE = util;
TEXT_RESOURCES_PACKAGE = text;
@ -113,12 +117,13 @@ public abstract class LocaleProviderAdapter {
/**
* Adapter lookup cache.
*/
private static ConcurrentMap<Class<? extends LocaleServiceProvider>, ConcurrentMap<Locale, LocaleProviderAdapter>>
private static final ConcurrentMap<Class<? extends LocaleServiceProvider>, ConcurrentMap<Locale, LocaleProviderAdapter>>
adapterCache = new ConcurrentHashMap<>();
static {
String order = GetPropertyAction.privilegedGetProperty("java.locale.providers");
List<Type> typeList = new ArrayList<>();
ArrayList<Type> typeList = new ArrayList<>();
String invalidTypeMessage = null;
// Check user specified adapter preference
if (order != null && !order.isEmpty()) {
@ -133,10 +138,9 @@ public abstract class LocaleProviderAdapter {
if (!typeList.contains(aType)) {
typeList.add(aType);
}
} catch (IllegalArgumentException | UnsupportedOperationException e) {
// could be caused by the user specifying wrong
// provider name or format in the system property
LocaleServiceProviderPool.config(LocaleProviderAdapter.class, e.toString());
} catch (IllegalArgumentException e) {
// construct a log message.
invalidTypeMessage = "Invalid locale provider adapter \"" + type + "\" ignored.";
}
}
}
@ -144,7 +148,7 @@ public abstract class LocaleProviderAdapter {
defaultLocaleProviderAdapter = Type.CLDR;
if (!typeList.isEmpty()) {
// bona fide preference exists
if (!(typeList.contains(Type.CLDR) || (typeList.contains(Type.JRE)))) {
if (!(typeList.contains(Type.CLDR) || typeList.contains(Type.JRE))) {
// Append FALLBACK as the last resort when no ResourceBundleBasedAdapter is available.
typeList.add(Type.FALLBACK);
defaultLocaleProviderAdapter = Type.FALLBACK;
@ -155,6 +159,15 @@ public abstract class LocaleProviderAdapter {
typeList.add(Type.JRE);
}
adapterPreference = Collections.unmodifiableList(typeList);
// Emit logs, if any, after 'adapterPreference' is initialized which is needed
// for logging.
if (invalidTypeMessage != null) {
// could be caused by the user specifying wrong
// provider name or format in the system property
getLogger(LocaleProviderAdapter.class.getCanonicalName())
.log(Logger.Level.INFO, invalidTypeMessage);
}
}
/**
@ -167,30 +180,25 @@ public abstract class LocaleProviderAdapter {
case SPI:
case HOST:
case FALLBACK:
LocaleProviderAdapter adapter = null;
LocaleProviderAdapter cached = adapterInstances.get(type);
if (cached == null) {
LocaleProviderAdapter adapter = adapterInstances.get(type);
if (adapter == null) {
try {
// lazily load adapters here
@SuppressWarnings("deprecation")
Object tmp = Class.forName(type.getAdapterClassName()).newInstance();
adapter = (LocaleProviderAdapter)tmp;
cached = adapterInstances.putIfAbsent(type, adapter);
adapter = (LocaleProviderAdapter)Class.forName(type.getAdapterClassName())
.getDeclaredConstructor().newInstance();
LocaleProviderAdapter cached = adapterInstances.putIfAbsent(type, adapter);
if (cached != null) {
adapter = cached;
}
} catch (ClassNotFoundException |
} catch (NoSuchMethodException |
InvocationTargetException |
ClassNotFoundException |
IllegalAccessException |
InstantiationException |
UnsupportedOperationException e) {
LocaleServiceProviderPool.config(LocaleProviderAdapter.class, e.toString());
adapterInstances.putIfAbsent(type, NONEXISTENT_ADAPTER);
if (defaultLocaleProviderAdapter == type) {
defaultLocaleProviderAdapter = Type.FALLBACK;
}
throw new ServiceConfigurationError("Locale provider adapter \"" +
type + "\"cannot be instantiated.", e);
}
} else if (cached != NONEXISTENT_ADAPTER) {
adapter = cached;
}
return adapter;
default:
@ -440,14 +448,4 @@ public abstract class LocaleProviderAdapter {
public abstract LocaleResources getLocaleResources(Locale locale);
public abstract Locale[] getAvailableLocales();
private static final LocaleProviderAdapter NONEXISTENT_ADAPTER = new NonExistentAdapter();
private static final class NonExistentAdapter extends FallbackLocaleProviderAdapter {
@Override
public LocaleProviderAdapter.Type getAdapterType() {
return null;
}
private NonExistentAdapter() {};
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -38,7 +38,6 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.spi.LocaleServiceProvider;
import sun.util.logging.PlatformLogger;
/**
* An instance of this class holds a set of the third party implementations of a particular
@ -121,11 +120,6 @@ public final class LocaleServiceProviderPool {
providerClass = c;
}
static void config(Class<? extends Object> caller, String message) {
PlatformLogger logger = PlatformLogger.getLogger(caller.getCanonicalName());
logger.config(message);
}
/**
* Lazy loaded set of available locales.
* Loading all locales is a very long operation.
@ -282,9 +276,10 @@ public final class LocaleServiceProviderPool {
if (providersObj != null) {
return providersObj;
} else if (isObjectProvider) {
config(LocaleServiceProviderPool.class,
"A locale sensitive service object provider returned null, " +
"which should not happen. Provider: " + lsp + " Locale: " + locale);
System.getLogger(LocaleServiceProviderPool.class.getCanonicalName())
.log(System.Logger.Level.INFO,
"A locale sensitive service object provider returned null, " +
"which should not happen. Provider: " + lsp + " Locale: " + locale);
}
}
}
@ -341,9 +336,8 @@ public final class LocaleServiceProviderPool {
// ResourceBundle.Control.getCandidateLocales. The result
// returned by getCandidateLocales are already normalized
// (no extensions) for service look up.
List<Locale> lookupLocales = Control.getNoFallbackControl(Control.FORMAT_DEFAULT)
return Control.getNoFallbackControl(Control.FORMAT_DEFAULT)
.getCandidateLocales("", locale);
return lookupLocales;
}
/**
@ -370,8 +364,9 @@ public final class LocaleServiceProviderPool {
// should have well-formed fields except
// for ja_JP_JP and th_TH_TH. Therefore,
// it should never enter in this catch clause.
config(LocaleServiceProviderPool.class,
"A locale(" + locale + ") has non-empty extensions, but has illformed fields.");
System.getLogger(LocaleServiceProviderPool.class.getCanonicalName())
.log(System.Logger.Level.INFO,
"A locale(" + locale + ") has non-empty extensions, but has illformed fields.");
// Fallback - script field will be lost.
lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant());
@ -402,9 +397,9 @@ public final class LocaleServiceProviderPool {
* @param params provider specific params
* @return localized object from the provider
*/
public S getObject(P lsp,
Locale locale,
String key,
Object... params);
S getObject(P lsp,
Locale locale,
String key,
Object... params);
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -43,9 +43,9 @@ import java.text.spi.NumberFormatProvider;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.spi.CalendarDataProvider;
import java.util.spi.CalendarNameProvider;
import java.util.spi.CurrencyNameProvider;
@ -72,7 +72,7 @@ public class SPILocaleProviderAdapter extends AuxLocaleProviderAdapter {
@Override
protected <P extends LocaleServiceProvider> P findInstalledProvider(final Class<P> c) {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<P>() {
return AccessController.doPrivileged(new PrivilegedExceptionAction<>() {
@Override
@SuppressWarnings(value={"unchecked", "deprecation"})
public P run() {
@ -91,8 +91,8 @@ public class SPILocaleProviderAdapter extends AuxLocaleProviderAdapter {
} catch (ClassNotFoundException |
InstantiationException |
IllegalAccessException e) {
LocaleServiceProviderPool.config(SPILocaleProviderAdapter.class, e.toString());
return null;
throw new ServiceConfigurationError(
"SPI locale provider cannot be instantiated.", e);
}
}
@ -102,9 +102,9 @@ public class SPILocaleProviderAdapter extends AuxLocaleProviderAdapter {
}
});
} catch (PrivilegedActionException e) {
LocaleServiceProviderPool.config(SPILocaleProviderAdapter.class, e.toString());
throw new ServiceConfigurationError(
"SPI locale provider cannot be instantiated.", e);
}
return null;
}
/*
@ -112,7 +112,7 @@ public class SPILocaleProviderAdapter extends AuxLocaleProviderAdapter {
* following "<provider class name>Delegate" convention.
*/
private interface Delegate<P extends LocaleServiceProvider> {
default public void addImpl(P impl) {
default void addImpl(P impl) {
for (Locale l : impl.getAvailableLocales()) {
getDelegateMap().putIfAbsent(l, impl);
}
@ -121,7 +121,7 @@ public class SPILocaleProviderAdapter extends AuxLocaleProviderAdapter {
/*
* Obtain the real SPI implementation, using locale fallback
*/
default public P getImpl(Locale locale) {
default P getImpl(Locale locale) {
for (Locale l : LocaleServiceProviderPool.getLookupLocales(locale.stripExtensions())) {
P ret = getDelegateMap().get(l);
if (ret != null) {
@ -131,13 +131,13 @@ public class SPILocaleProviderAdapter extends AuxLocaleProviderAdapter {
return null;
}
public Map<Locale, P> getDelegateMap();
Map<Locale, P> getDelegateMap();
default public Locale[] getAvailableLocalesDelegate() {
return getDelegateMap().keySet().stream().toArray(Locale[]::new);
default Locale[] getAvailableLocalesDelegate() {
return getDelegateMap().keySet().toArray(new Locale[0]);
}
default public boolean isSupportedLocaleDelegate(Locale locale) {
default boolean isSupportedLocaleDelegate(Locale locale) {
Map<Locale, P> map = getDelegateMap();
Locale override = CalendarDataUtility.findRegionOverride(locale);