8344039: Remove security manager dependency in java.time

Reviewed-by: naoto, mullan, lancea
This commit is contained in:
Roger Riggs 2024-11-13 20:03:26 +00:00
parent 1eb38c8eb7
commit 5ac330b1ac
2 changed files with 47 additions and 78 deletions
src/java.base/share/classes/java/time

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -59,18 +59,14 @@ package java.time.chrono;
import static java.time.temporal.ChronoField.EPOCH_DAY; import static java.time.temporal.ChronoField.EPOCH_DAY;
import java.io.FilePermission;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.Serializable; import java.io.Serializable;
import java.io.UncheckedIOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.Clock; import java.time.Clock;
import java.time.DateTimeException; import java.time.DateTimeException;
import java.time.Instant; import java.time.Instant;
@ -88,6 +84,7 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Stream; import java.util.stream.Stream;
import jdk.internal.util.StaticProperty;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
/** /**
@ -291,10 +288,7 @@ public final class HijrahChronology extends AbstractChronology implements Serial
AbstractChronology.registerChrono(INSTANCE, "islamic"); AbstractChronology.registerChrono(INSTANCE, "islamic");
// custom config chronologies // custom config chronologies
@SuppressWarnings("removal") CONF_PATH = Path.of(StaticProperty.javaHome(), "conf", "chronology");
String javaHome = AccessController.doPrivileged((PrivilegedAction<String>)
() -> System.getProperty("java.home"));
CONF_PATH = Path.of(javaHome, "conf", "chronology");
registerCustomChrono(); registerCustomChrono();
} }
@ -824,19 +818,9 @@ public final class HijrahChronology extends AbstractChronology implements Serial
*/ */
private static Properties readConfigProperties(final String chronologyId, final String calendarType) throws Exception { private static Properties readConfigProperties(final String chronologyId, final String calendarType) throws Exception {
String resourceName = RESOURCE_PREFIX + chronologyId + "_" + calendarType + RESOURCE_SUFFIX; String resourceName = RESOURCE_PREFIX + chronologyId + "_" + calendarType + RESOURCE_SUFFIX;
PrivilegedAction<InputStream> getResourceAction = calendarType.equals("islamic-umalqura") ? try (InputStream is = calendarType.equals("islamic-umalqura")
() -> HijrahChronology.class.getResourceAsStream(resourceName) : ? HijrahChronology.class.getResourceAsStream(resourceName)
() -> { : Files.newInputStream(CONF_PATH.resolve(resourceName), StandardOpenOption.READ)) {
try {
return Files.newInputStream(CONF_PATH.resolve(resourceName),
StandardOpenOption.READ);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
FilePermission perm1 = new FilePermission("<<ALL FILES>>", "read");
RuntimePermission perm2 = new RuntimePermission("accessSystemModules");
try (@SuppressWarnings("removal") InputStream is = AccessController.doPrivileged(getResourceAction, null, perm1, perm2)) {
if (is == null) { if (is == null) {
throw new RuntimeException("Hijrah calendar resource not found: " + resourceName); throw new RuntimeException("Hijrah calendar resource not found: " + resourceName);
} }
@ -1031,38 +1015,32 @@ public final class HijrahChronology extends AbstractChronology implements Serial
* Look for Hijrah chronology variant properties files in * Look for Hijrah chronology variant properties files in
* <JAVA_HOME>/conf/chronology directory. Then register its chronology, if any. * <JAVA_HOME>/conf/chronology directory. Then register its chronology, if any.
*/ */
@SuppressWarnings("removal")
private static void registerCustomChrono() { private static void registerCustomChrono() {
AccessController.doPrivileged(
(PrivilegedAction<Void>)() -> { if (Files.isDirectory(CONF_PATH)) {
if (Files.isDirectory(CONF_PATH)) { try (Stream<Path> stream = Files.list(CONF_PATH)) {
try (Stream<Path> stream = Files.list(CONF_PATH)) { stream.map(p -> p.getFileName().toString())
stream.map(p -> p.getFileName().toString()) .filter(fn -> fn.matches("hijrah-config-[^\\.]+\\.properties"))
.filter(fn -> fn.matches("hijrah-config-[^\\.]+\\.properties")) .map(fn -> fn.replaceAll("(hijrah-config-|\\.properties)", ""))
.map(fn -> fn.replaceAll("(hijrah-config-|\\.properties)", "")) .forEach(idtype -> {
.forEach(idtype -> { int delimiterPos = idtype.indexOf('_');
int delimiterPos = idtype.indexOf('_'); // '_' should be somewhere in the middle of idtype
// '_' should be somewhere in the middle of idtype if (delimiterPos > 1 && delimiterPos < idtype.length() - 1) {
if (delimiterPos > 1 && delimiterPos < idtype.length() - 1) { AbstractChronology.registerChrono(
AbstractChronology.registerChrono( new HijrahChronology(
new HijrahChronology( idtype.substring(0, delimiterPos),
idtype.substring(0, delimiterPos), idtype.substring(delimiterPos + 1)));
idtype.substring(delimiterPos + 1))); } else {
} else { PlatformLogger.getLogger("java.time.chrono")
PlatformLogger.getLogger("java.time.chrono") .warning("Hijrah custom config init failed." +
.warning("Hijrah custom config init failed." + "'<id>_<type>' name convention not followed: " + idtype);
"'<id>_<type>' name convention not followed: " + idtype); }
} });
}); } catch (IOException e) {
} catch (IOException e) { PlatformLogger.getLogger("java.time.chrono")
PlatformLogger.getLogger("java.time.chrono") .warning("Hijrah custom config init failed.", e);
.warning("Hijrah custom config init failed.", e); }
} }
}
return null;
},
null,
new FilePermission("<<ALL FILES>>", "read"));
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -61,8 +61,6 @@
*/ */
package java.time.zone; package java.time.zone;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -146,28 +144,21 @@ public abstract class ZoneRulesProvider {
static { static {
// if the property java.time.zone.DefaultZoneRulesProvider is // if the property java.time.zone.DefaultZoneRulesProvider is
// set then its value is the class name of the default provider // set then its value is the class name of the default provider
@SuppressWarnings("removal") final List<ZoneRulesProvider> loaded = new ArrayList<>();
final List<ZoneRulesProvider> loaded = String prop = System.getProperty("java.time.zone.DefaultZoneRulesProvider");
AccessController.doPrivileged(new PrivilegedAction<List<ZoneRulesProvider>>() { if (prop != null) {
public List<ZoneRulesProvider> run() { try {
List<ZoneRulesProvider> result = new ArrayList<>(); Class<?> c = Class.forName(prop, true, ClassLoader.getSystemClassLoader());
String prop = System.getProperty("java.time.zone.DefaultZoneRulesProvider"); @SuppressWarnings("deprecation")
if (prop != null) { ZoneRulesProvider provider = ZoneRulesProvider.class.cast(c.newInstance());
try { registerProvider(provider);
Class<?> c = Class.forName(prop, true, ClassLoader.getSystemClassLoader()); loaded.add(provider);
@SuppressWarnings("deprecation") } catch (Exception x) {
ZoneRulesProvider provider = ZoneRulesProvider.class.cast(c.newInstance()); throw new Error(x);
registerProvider(provider); }
result.add(provider); } else {
} catch (Exception x) { registerProvider(new TzdbZoneRulesProvider());
throw new Error(x); }
}
} else {
registerProvider(new TzdbZoneRulesProvider());
}
return result;
}
});
ServiceLoader<ZoneRulesProvider> sl = ServiceLoader.load(ZoneRulesProvider.class, ClassLoader.getSystemClassLoader()); ServiceLoader<ZoneRulesProvider> sl = ServiceLoader.load(ZoneRulesProvider.class, ClassLoader.getSystemClassLoader());
Iterator<ZoneRulesProvider> it = sl.iterator(); Iterator<ZoneRulesProvider> it = sl.iterator();