From 5ac330b1ac81e932924e0ea10988f2536352be04 Mon Sep 17 00:00:00 2001 From: Roger Riggs Date: Wed, 13 Nov 2024 20:03:26 +0000 Subject: [PATCH] 8344039: Remove security manager dependency in java.time Reviewed-by: naoto, mullan, lancea --- .../java/time/chrono/HijrahChronology.java | 84 +++++++------------ .../java/time/zone/ZoneRulesProvider.java | 41 ++++----- 2 files changed, 47 insertions(+), 78 deletions(-) diff --git a/src/java.base/share/classes/java/time/chrono/HijrahChronology.java b/src/java.base/share/classes/java/time/chrono/HijrahChronology.java index 681333dfa9f..881dfb15fc7 100644 --- a/src/java.base/share/classes/java/time/chrono/HijrahChronology.java +++ b/src/java.base/share/classes/java/time/chrono/HijrahChronology.java @@ -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. * * 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 java.io.FilePermission; import java.io.IOException; import java.io.InputStream; import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.Serializable; -import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.time.Clock; import java.time.DateTimeException; import java.time.Instant; @@ -88,6 +84,7 @@ import java.util.Map; import java.util.Properties; import java.util.stream.Stream; +import jdk.internal.util.StaticProperty; import sun.util.logging.PlatformLogger; /** @@ -291,10 +288,7 @@ public final class HijrahChronology extends AbstractChronology implements Serial AbstractChronology.registerChrono(INSTANCE, "islamic"); // custom config chronologies - @SuppressWarnings("removal") - String javaHome = AccessController.doPrivileged((PrivilegedAction) - () -> System.getProperty("java.home")); - CONF_PATH = Path.of(javaHome, "conf", "chronology"); + CONF_PATH = Path.of(StaticProperty.javaHome(), "conf", "chronology"); 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 { String resourceName = RESOURCE_PREFIX + chronologyId + "_" + calendarType + RESOURCE_SUFFIX; - PrivilegedAction getResourceAction = calendarType.equals("islamic-umalqura") ? - () -> HijrahChronology.class.getResourceAsStream(resourceName) : - () -> { - try { - return Files.newInputStream(CONF_PATH.resolve(resourceName), - StandardOpenOption.READ); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }; - FilePermission perm1 = new FilePermission("<>", "read"); - RuntimePermission perm2 = new RuntimePermission("accessSystemModules"); - try (@SuppressWarnings("removal") InputStream is = AccessController.doPrivileged(getResourceAction, null, perm1, perm2)) { + try (InputStream is = calendarType.equals("islamic-umalqura") + ? HijrahChronology.class.getResourceAsStream(resourceName) + : Files.newInputStream(CONF_PATH.resolve(resourceName), StandardOpenOption.READ)) { if (is == null) { 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 * /conf/chronology directory. Then register its chronology, if any. */ - @SuppressWarnings("removal") private static void registerCustomChrono() { - AccessController.doPrivileged( - (PrivilegedAction)() -> { - if (Files.isDirectory(CONF_PATH)) { - try (Stream stream = Files.list(CONF_PATH)) { - stream.map(p -> p.getFileName().toString()) - .filter(fn -> fn.matches("hijrah-config-[^\\.]+\\.properties")) - .map(fn -> fn.replaceAll("(hijrah-config-|\\.properties)", "")) - .forEach(idtype -> { - int delimiterPos = idtype.indexOf('_'); - // '_' should be somewhere in the middle of idtype - if (delimiterPos > 1 && delimiterPos < idtype.length() - 1) { - AbstractChronology.registerChrono( - new HijrahChronology( - idtype.substring(0, delimiterPos), - idtype.substring(delimiterPos + 1))); - } else { - PlatformLogger.getLogger("java.time.chrono") - .warning("Hijrah custom config init failed." + - "'_' name convention not followed: " + idtype); - } - }); - } catch (IOException e) { - PlatformLogger.getLogger("java.time.chrono") - .warning("Hijrah custom config init failed.", e); - } - } - return null; - }, - null, - new FilePermission("<>", "read")); + + if (Files.isDirectory(CONF_PATH)) { + try (Stream stream = Files.list(CONF_PATH)) { + stream.map(p -> p.getFileName().toString()) + .filter(fn -> fn.matches("hijrah-config-[^\\.]+\\.properties")) + .map(fn -> fn.replaceAll("(hijrah-config-|\\.properties)", "")) + .forEach(idtype -> { + int delimiterPos = idtype.indexOf('_'); + // '_' should be somewhere in the middle of idtype + if (delimiterPos > 1 && delimiterPos < idtype.length() - 1) { + AbstractChronology.registerChrono( + new HijrahChronology( + idtype.substring(0, delimiterPos), + idtype.substring(delimiterPos + 1))); + } else { + PlatformLogger.getLogger("java.time.chrono") + .warning("Hijrah custom config init failed." + + "'_' name convention not followed: " + idtype); + } + }); + } catch (IOException e) { + PlatformLogger.getLogger("java.time.chrono") + .warning("Hijrah custom config init failed.", e); + } + } } //----------------------------------------------------------------------- diff --git a/src/java.base/share/classes/java/time/zone/ZoneRulesProvider.java b/src/java.base/share/classes/java/time/zone/ZoneRulesProvider.java index 375be682c0c..c79b6355595 100644 --- a/src/java.base/share/classes/java/time/zone/ZoneRulesProvider.java +++ b/src/java.base/share/classes/java/time/zone/ZoneRulesProvider.java @@ -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. * * This code is free software; you can redistribute it and/or modify it @@ -61,8 +61,6 @@ */ package java.time.zone; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; @@ -146,28 +144,21 @@ public abstract class ZoneRulesProvider { static { // if the property java.time.zone.DefaultZoneRulesProvider is // set then its value is the class name of the default provider - @SuppressWarnings("removal") - final List loaded = - AccessController.doPrivileged(new PrivilegedAction>() { - public List run() { - List result = new ArrayList<>(); - String prop = System.getProperty("java.time.zone.DefaultZoneRulesProvider"); - if (prop != null) { - try { - Class c = Class.forName(prop, true, ClassLoader.getSystemClassLoader()); - @SuppressWarnings("deprecation") - ZoneRulesProvider provider = ZoneRulesProvider.class.cast(c.newInstance()); - registerProvider(provider); - result.add(provider); - } catch (Exception x) { - throw new Error(x); - } - } else { - registerProvider(new TzdbZoneRulesProvider()); - } - return result; - } - }); + final List loaded = new ArrayList<>(); + String prop = System.getProperty("java.time.zone.DefaultZoneRulesProvider"); + if (prop != null) { + try { + Class c = Class.forName(prop, true, ClassLoader.getSystemClassLoader()); + @SuppressWarnings("deprecation") + ZoneRulesProvider provider = ZoneRulesProvider.class.cast(c.newInstance()); + registerProvider(provider); + loaded.add(provider); + } catch (Exception x) { + throw new Error(x); + } + } else { + registerProvider(new TzdbZoneRulesProvider()); + } ServiceLoader sl = ServiceLoader.load(ZoneRulesProvider.class, ClassLoader.getSystemClassLoader()); Iterator it = sl.iterator();