8304911: Use OperatingSystem enum in some modules

Reviewed-by: naoto, lancea, iris, jpai
This commit is contained in:
Roger Riggs 2023-04-10 15:51:01 +00:00
parent 76975da59e
commit ba90dc7795
12 changed files with 70 additions and 100 deletions

View File

@ -281,7 +281,12 @@ module java.base {
exports jdk.internal.util.random to
jdk.random;
exports jdk.internal.util to
java.desktop;
java.desktop,
java.prefs,
java.security.jgss,
java.smartcardio,
jdk.charsets,
jdk.net;
exports sun.net to
java.net.http,
jdk.naming.dns;

View File

@ -50,6 +50,8 @@ grant codeBase "jrt:/java.smartcardio" {
"accessClassInPackage.sun.security.jca";
permission java.lang.RuntimePermission
"accessClassInPackage.sun.security.util";
permission java.lang.RuntimePermission
"accessClassInPackage.jdk.internal.util";
permission java.util.PropertyPermission
"javax.smartcardio.TerminalFactory.DefaultType", "read";
permission java.util.PropertyPermission "os.name", "read";
@ -118,6 +120,8 @@ grant codeBase "jrt:/jdk.charsets" {
"accessClassInPackage.jdk.internal.access";
permission java.lang.RuntimePermission
"accessClassInPackage.jdk.internal.misc";
permission java.lang.RuntimePermission
"accessClassInPackage.jdk.internal.util";
permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.cs";
};

View File

@ -25,6 +25,8 @@
package java.util.prefs;
import jdk.internal.util.OperatingSystem;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
@ -292,15 +294,11 @@ public abstract class Preferences {
}
// 3. Use platform-specific system-wide default
String osName = System.getProperty("os.name");
String platformFactory;
if (osName.startsWith("Windows")) {
platformFactory = "java.util.prefs.WindowsPreferencesFactory";
} else if (osName.contains("OS X")) {
platformFactory = "java.util.prefs.MacOSXPreferencesFactory";
} else {
platformFactory = "java.util.prefs.FileSystemPreferencesFactory";
}
String platformFactory = switch (OperatingSystem.current()) {
case WINDOWS -> "java.util.prefs.WindowsPreferencesFactory";
case MACOS -> "java.util.prefs.MacOSXPreferencesFactory";
default -> "java.util.prefs.FileSystemPreferencesFactory";
};
try {
@SuppressWarnings("deprecation")
Object result = Class.forName(platformFactory, false,

View File

@ -30,6 +30,9 @@ import java.util.HashMap;
import java.security.Provider;
import java.security.AccessController;
import java.security.PrivilegedAction;
import jdk.internal.util.OperatingSystem;
import jdk.internal.util.StaticProperty;
import org.ietf.jgss.Oid;
import sun.security.action.GetBooleanAction;
import sun.security.action.PutAllAction;
@ -87,25 +90,22 @@ public final class SunNativeProvider extends Provider {
String defaultLib
= System.getProperty("sun.security.jgss.lib");
if (defaultLib == null || defaultLib.trim().equals("")) {
String osname = System.getProperty("os.name");
if (osname.startsWith("Linux")) {
gssLibs = new String[]{
"libgssapi.so",
"libgssapi_krb5.so",
"libgssapi_krb5.so.2",
gssLibs = switch (OperatingSystem.current()) {
case LINUX -> new String[]{
"libgssapi.so",
"libgssapi_krb5.so",
"libgssapi_krb5.so.2",
};
} else if (osname.contains("OS X")) {
gssLibs = new String[]{
"libgssapi_krb5.dylib",
"/usr/lib/sasl2/libgssapiv2.2.so",
};
} else if (osname.contains("Windows")) {
// Full path needed, DLL is in jre/bin
gssLibs = new String[]{ System.getProperty("java.home")
+ "\\bin\\sspi_bridge.dll" };
} else {
gssLibs = new String[0];
}
case MACOS -> new String[]{
"libgssapi_krb5.dylib",
"/usr/lib/sasl2/libgssapiv2.2.so",
};
case WINDOWS -> new String[]{
// Full path needed, DLL is in jre/bin
StaticProperty.javaHome() + "\\bin\\sspi_bridge.dll",
};
default -> new String[0];
};
} else {
gssLibs = new String[]{ defaultLib };
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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
@ -44,6 +44,7 @@ import java.security.PrivilegedExceptionAction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jdk.internal.util.OperatingSystem;
import sun.net.dns.ResolverConfiguration;
import sun.security.action.GetPropertyAction;
import sun.security.krb5.internal.crypto.EType;
@ -159,8 +160,7 @@ public class Config {
private static boolean isMacosLionOrBetter() {
// split the "10.x.y" version number
String osname = GetPropertyAction.privilegedGetProperty("os.name");
if (!osname.contains("OS X")) {
if (!OperatingSystem.isMacOS()) {
return false;
}
@ -892,8 +892,7 @@ public class Config {
*/
private String getNativeFileName() {
String name = null;
String osname = GetPropertyAction.privilegedGetProperty("os.name");
if (osname.startsWith("Windows")) {
if (OperatingSystem.isWindows()) {
try {
Credentials.ensureLoaded();
} catch (Exception e) {
@ -926,7 +925,7 @@ public class Config {
if (name == null) {
name = "c:\\winnt\\krb5.ini";
}
} else if (osname.contains("OS X")) {
} else if (OperatingSystem.isMacOS()) {
name = findMacosConfigFile();
} else {
name = "/etc/krb5.conf";
@ -1193,8 +1192,7 @@ public class Config {
new java.security.PrivilegedAction<String>() {
@Override
public String run() {
String osname = System.getProperty("os.name");
if (osname.startsWith("Windows")) {
if (OperatingSystem.isWindows()) {
return System.getenv("USERDNSDOMAIN");
}
return null;
@ -1241,8 +1239,7 @@ public class Config {
new java.security.PrivilegedAction<String>() {
@Override
public String run() {
String osname = System.getProperty("os.name");
if (osname.startsWith("Windows")) {
if (OperatingSystem.isWindows()) {
String logonServer = System.getenv("LOGONSERVER");
if (logonServer != null
&& logonServer.startsWith("\\\\")) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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
@ -31,7 +31,7 @@
package sun.security.krb5;
import sun.security.action.GetPropertyAction;
import jdk.internal.util.OperatingSystem;
import sun.security.krb5.internal.*;
import sun.security.krb5.internal.ccache.CredentialsCache;
import sun.security.krb5.internal.crypto.EType;
@ -39,7 +39,6 @@ import sun.security.util.SecurityProperties;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.net.InetAddress;
/**
@ -327,9 +326,8 @@ public class Credentials {
if (ticketCache == null) {
// The default ticket cache on Windows and Mac is not a file.
String os = GetPropertyAction.privilegedGetProperty("os.name");
if (os.toUpperCase(Locale.ENGLISH).startsWith("WINDOWS") ||
os.toUpperCase(Locale.ENGLISH).contains("OS X")) {
if (OperatingSystem.isWindows() ||
OperatingSystem.isMacOS()) {
Credentials creds = acquireDefaultCreds();
if (creds == null) {
if (DEBUG) {
@ -530,7 +528,7 @@ public class Credentials {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void> () {
public Void run() {
if (System.getProperty("os.name").contains("OS X")) {
if (OperatingSystem.isMacOS()) {
System.loadLibrary("osxkrb5");
} else {
System.loadLibrary("w2k_lsa_auth");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2023, 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,8 @@
package sun.security.krb5;
import jdk.internal.util.OperatingSystem;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
@ -47,8 +49,7 @@ public class SCDynamicStoreConfig {
boolean isMac = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Boolean>() {
public Boolean run() {
String osname = System.getProperty("os.name");
if (osname.contains("OS X")) {
if (OperatingSystem.isMacOS()) {
System.loadLibrary("osxkrb5");
return true;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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
@ -33,6 +33,7 @@
*/
package sun.security.krb5.internal.ccache;
import jdk.internal.util.OperatingSystem;
import sun.security.action.GetPropertyAction;
import sun.security.krb5.*;
import sun.security.krb5.internal.*;
@ -465,9 +466,6 @@ public class FileCredentialsCache extends CredentialsCache
return name;
}
// get cache name from system.property
String osname = GetPropertyAction.privilegedGetProperty("os.name");
/*
* For Unix platforms we use the default cache name to be
* /tmp/krb5cc_uid ; for all other platforms we use
@ -479,7 +477,7 @@ public class FileCredentialsCache extends CredentialsCache
* Windows.
*/
if (osname != null && !osname.startsWith("Windows")) {
if (!OperatingSystem.isWindows()) {
long uid = jdk.internal.misc.VM.getuid();
if (uid != -1) {
name = File.separator + "tmp" +

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, 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,9 +25,8 @@
package sun.security.smartcardio;
import java.nio.ByteBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import jdk.internal.util.OperatingSystem;
import javax.smartcardio.*;
import static sun.security.smartcardio.PCSC.*;
@ -62,16 +61,6 @@ final class CardImpl extends Card {
// thread holding exclusive access to the card, or null
private volatile Thread exclusiveThread;
// used for platform specific logic
private static final boolean isWindows;
static {
@SuppressWarnings("removal")
final String osName = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty("os.name"));
isWindows = osName.startsWith("Windows");
}
CardImpl(TerminalImpl terminal, String protocol) throws PCSCException {
this.terminal = terminal;
int sharingMode = SCARD_SHARE_SHARED;
@ -88,7 +77,7 @@ final class CardImpl extends Card {
// MSDN states that the preferred protocol can be zero, but doesn't
// specify whether other values are allowed.
// pcsc-lite implementation expects the preferred protocol to be non zero.
connectProtocol = isWindows ? 0 : SCARD_PROTOCOL_RAW;
connectProtocol = OperatingSystem.isWindows() ? 0 : SCARD_PROTOCOL_RAW;
sharingMode = SCARD_SHARE_DIRECT;
} else {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, 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
@ -35,11 +35,10 @@ import java.nio.charset.CharacterCodingException;
import java.nio.charset.MalformedInputException;
import sun.nio.cs.DelegatableDecoder;
import sun.nio.cs.HistoricallyNamedCharset;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.nio.cs.*;
import static java.lang.Character.UnicodeBlock;
import jdk.internal.util.OperatingSystem;
public class JISAutoDetect
extends Charset
@ -93,9 +92,6 @@ public class JISAutoDetect
}
private static class Decoder extends CharsetDecoder {
@SuppressWarnings("removal")
private static final String osName = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty("os.name"));
private static final String SJISName = getSJISName();
private static final String EUCJPName = "EUC_JP";
@ -226,7 +222,7 @@ public class JISAutoDetect
* Returned Shift_JIS Charset name is OS dependent
*/
private static String getSJISName() {
if (osName.startsWith("Windows"))
if (OperatingSystem.isWindows())
return("windows-31J");
else
return("Shift_JIS");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, 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
@ -29,13 +29,12 @@ import java.io.FileDescriptor;
import java.net.SocketException;
import java.net.SocketOption;
import java.net.StandardProtocolFamily;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import jdk.internal.access.JavaIOFileDescriptorAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.OperatingSystem;
/**
* Defines extended socket options, beyond those defined in
@ -400,22 +399,12 @@ public final class ExtendedSocketOptions {
}
private static PlatformSocketOptions create() {
@SuppressWarnings("removal")
String osname = AccessController.doPrivileged(
new PrivilegedAction<String>() {
public String run() {
return System.getProperty("os.name");
}
});
if ("Linux".equals(osname)) {
return newInstance("jdk.net.LinuxSocketOptions");
} else if (osname.startsWith("Mac")) {
return newInstance("jdk.net.MacOSXSocketOptions");
} else if (osname.startsWith("Windows")) {
return newInstance("jdk.net.WindowsSocketOptions");
} else {
return new PlatformSocketOptions();
}
return switch (OperatingSystem.current()) {
case LINUX -> newInstance("jdk.net.LinuxSocketOptions");
case MACOS -> newInstance("jdk.net.MacOSXSocketOptions");
case WINDOWS -> newInstance("jdk.net.WindowsSocketOptions");
default -> new PlatformSocketOptions();
};
}
private static final PlatformSocketOptions instance = create();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2023, 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
@ -41,10 +41,6 @@ import com.sun.nio.sctp.SctpSocketOption;
import static com.sun.nio.sctp.SctpStandardSocketOptions.*;
public class SctpNet {
@SuppressWarnings("removal")
private static final String osName = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty("os.name"));
/* -- Miscellaneous SCTP utilities -- */
private static boolean IPv4MappedAddresses() {
@ -350,4 +346,3 @@ public class SctpNet {
init();
}
}