Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
naotoj committed Dec 4, 2023
1 parent bd04f91 commit 26214cb
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/java.base/share/classes/java/util/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.spi.LocaleNameProvider;
import java.util.stream.Stream;

import jdk.internal.util.StaticProperty;
import jdk.internal.vm.annotation.Stable;

import sun.security.action.GetPropertyAction;
Expand Down Expand Up @@ -1053,11 +1054,10 @@ private static synchronized Locale getFormatLocale() {

private static Locale initDefault() {
String language, region, script, country, variant;
Properties props = GetPropertyAction.privilegedGetProperties();
language = props.getProperty("user.language", "en");
language = StaticProperty.userLanguage(0);
// for compatibility, check for old user.region property
region = props.getProperty("user.region");
if (region != null) {
region = StaticProperty.userRegion();
if (!region.isEmpty()) {
// region can be of form country, country_variant, or _variant
int i = region.indexOf('_');
if (i >= 0) {
Expand All @@ -1069,30 +1069,24 @@ private static Locale initDefault() {
}
script = "";
} else {
script = props.getProperty("user.script", "");
country = props.getProperty("user.country", "");
variant = props.getProperty("user.variant", "");
script = StaticProperty.userScript(0);
country = StaticProperty.userCountry(0);
variant = StaticProperty.userVariant(0);
}

return getInstance(language, script, country, variant,
getDefaultExtensions(props.getProperty("user.extensions", ""))
getDefaultExtensions(StaticProperty.userExtensions(0))
.orElse(null));
}

private static Locale initDefault(Locale.Category category) {
Properties props = GetPropertyAction.privilegedGetProperties();

Locale locale = Locale.defaultLocale;
return getInstance(
props.getProperty(category.languageKey,
locale.getLanguage()),
props.getProperty(category.scriptKey,
locale.getScript()),
props.getProperty(category.countryKey,
locale.getCountry()),
props.getProperty(category.variantKey,
locale.getVariant()),
getDefaultExtensions(props.getProperty(category.extensionsKey, ""))
StaticProperty.userLanguage(category.ordinal() + 1),
StaticProperty.userScript(category.ordinal() + 1),
StaticProperty.userCountry(category.ordinal() + 1),
StaticProperty.userVariant(category.ordinal() + 1),
getDefaultExtensions(StaticProperty.userExtensions(category.ordinal() + 1))
.orElse(locale.getLocaleExtensions()));
}

Expand Down
137 changes: 137 additions & 0 deletions src/java.base/share/classes/jdk/internal/util/StaticProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ public final class StaticProperty {
private static final String OS_NAME;
private static final String OS_ARCH;
private static final String OS_VERSION;
private static final String USER_LANGUAGE;
private static final String USER_LANGUAGE_DISPLAY;
private static final String USER_LANGUAGE_FORMAT;
private static final String USER_SCRIPT;
private static final String USER_SCRIPT_DISPLAY;
private static final String USER_SCRIPT_FORMAT;
private static final String USER_COUNTRY;
private static final String USER_COUNTRY_DISPLAY;
private static final String USER_COUNTRY_FORMAT;
private static final String USER_REGION;
private static final String USER_VARIANT;
private static final String USER_VARIANT_DISPLAY;
private static final String USER_VARIANT_FORMAT;
private static final String USER_EXTENSIONS;
private static final String USER_EXTENSIONS_DISPLAY;
private static final String USER_EXTENSIONS_FORMAT;

private StaticProperty() {}

Expand All @@ -79,6 +95,22 @@ private StaticProperty() {}
OS_NAME = getProperty(props, "os.name");
OS_ARCH = getProperty(props, "os.arch");
OS_VERSION = getProperty(props, "os.version");
USER_LANGUAGE = getProperty(props, "user.language", "en");
USER_LANGUAGE_DISPLAY = getProperty(props, "user.language.display", USER_LANGUAGE);
USER_LANGUAGE_FORMAT = getProperty(props, "user.language.format", USER_LANGUAGE);
USER_SCRIPT = getProperty(props, "user.script", "");
USER_SCRIPT_DISPLAY = getProperty(props, "user.script.display", USER_SCRIPT);
USER_SCRIPT_FORMAT = getProperty(props, "user.script.format", USER_SCRIPT);
USER_COUNTRY = getProperty(props, "user.country", "");
USER_COUNTRY_DISPLAY = getProperty(props, "user.country.display", USER_COUNTRY);
USER_COUNTRY_FORMAT = getProperty(props, "user.country.format", USER_COUNTRY);
USER_REGION = getProperty(props, "user.region", "");
USER_VARIANT = getProperty(props, "user.variant", "");
USER_VARIANT_DISPLAY = getProperty(props, "user.variant.display", USER_VARIANT);
USER_VARIANT_FORMAT = getProperty(props, "user.variant.format", USER_VARIANT);
USER_EXTENSIONS = getProperty(props, "user.extensions", "");
USER_EXTENSIONS_DISPLAY = getProperty(props, "user.extensions.display", USER_EXTENSIONS);
USER_EXTENSIONS_FORMAT = getProperty(props, "user.extensions.format", USER_EXTENSIONS);
}

private static String getProperty(Properties props, String key) {
Expand Down Expand Up @@ -276,4 +308,109 @@ public static String osArch() {
public static String osVersion() {
return OS_VERSION;
}

/**
* {@return the {@code user.language} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userLanguage(int category) {
return switch (category) {
case 0 -> USER_LANGUAGE;
case 1 -> USER_LANGUAGE_DISPLAY;
case 2 -> USER_LANGUAGE_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.script} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userScript(int category) {
return switch (category) {
case 0 -> USER_SCRIPT;
case 1 -> USER_SCRIPT_DISPLAY;
case 2 -> USER_SCRIPT_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.country} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userCountry(int category) {
return switch (category) {
case 0 -> USER_COUNTRY;
case 1 -> USER_COUNTRY_DISPLAY;
case 2 -> USER_COUNTRY_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.region} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*/
public static String userRegion() {
return USER_REGION;
}

/**
* {@return the {@code user.variant} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userVariant(int category) {
return switch (category) {
case 0 -> USER_VARIANT;
case 1 -> USER_VARIANT_DISPLAY;
case 2 -> USER_VARIANT_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.extensions} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userExtensions(int category) {
return switch (category) {
case 0 -> USER_EXTENSIONS;
case 1 -> USER_EXTENSIONS_DISPLAY;
case 2 -> USER_EXTENSIONS_FORMAT;
default -> throw new InternalError();
};
}
}

0 comments on commit 26214cb

Please sign in to comment.