Skip to content

Commit

Permalink
8304911: Use OperatingSystem enum in some modules
Browse files Browse the repository at this point in the history
Reviewed-by: naoto, lancea, iris, jpai
  • Loading branch information
Roger Riggs committed Apr 10, 2023
1 parent 76975da commit ba90dc7
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 100 deletions.
7 changes: 6 additions & 1 deletion src/java.base/share/classes/module-info.java
Expand Up @@ -281,7 +281,12 @@
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;
Expand Down
4 changes: 4 additions & 0 deletions src/java.base/share/lib/security/default.policy
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
};

Expand Down
16 changes: 7 additions & 9 deletions src/java.prefs/share/classes/java/util/prefs/Preferences.java
Expand Up @@ -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;
Expand Down Expand Up @@ -292,15 +294,11 @@ private static PreferencesFactory factory1() {
}

// 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,
Expand Down
Expand Up @@ -30,6 +30,9 @@
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;
Expand Down Expand Up @@ -87,25 +90,22 @@ public HashMap<String, String> run() {
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 };
}
Expand Down
17 changes: 7 additions & 10 deletions src/java.security.jgss/share/classes/sun/security/krb5/Config.java
@@ -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
Expand Down Expand Up @@ -44,6 +44,7 @@
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;
Expand Down Expand Up @@ -159,8 +160,7 @@ public static void refresh() throws KrbException {

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;
}

Expand Down Expand Up @@ -892,8 +892,7 @@ private String getJavaFileName() {
*/
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) {
Expand Down Expand Up @@ -926,7 +925,7 @@ private String getNativeFileName() {
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";
Expand Down Expand Up @@ -1193,8 +1192,7 @@ public String getDefaultRealm() throws KrbException {
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;
Expand Down Expand Up @@ -1241,8 +1239,7 @@ public String getKDCList(String realm) throws KrbException {
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("\\\\")) {
Expand Down
@@ -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
Expand Down Expand Up @@ -31,15 +31,14 @@

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;
import sun.security.util.SecurityProperties;

import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.net.InetAddress;

/**
Expand Down Expand Up @@ -327,9 +326,8 @@ public static Credentials acquireTGTFromCache(PrincipalName princ,

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) {
Expand Down Expand Up @@ -530,7 +528,7 @@ static void ensureLoaded() {
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");
Expand Down
@@ -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
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
@@ -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
Expand Down Expand Up @@ -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.*;
Expand Down Expand Up @@ -465,9 +466,6 @@ public static String getDefaultCacheName() {
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
Expand All @@ -479,7 +477,7 @@ public static String getDefaultCacheName() {
* Windows.
*/

if (osname != null && !osname.startsWith("Windows")) {
if (!OperatingSystem.isWindows()) {
long uid = jdk.internal.misc.VM.getuid();
if (uid != -1) {
name = File.separator + "tmp" +
Expand Down
@@ -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
Expand All @@ -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.*;

Expand Down Expand Up @@ -62,16 +61,6 @@ private static enum State { OK, REMOVED, DISCONNECTED };
// 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;
Expand All @@ -88,7 +77,7 @@ private static enum State { OK, REMOVED, DISCONNECTED };
// 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 {
Expand Down
10 changes: 3 additions & 7 deletions src/jdk.charsets/share/classes/sun/nio/cs/ext/JISAutoDetect.java
@@ -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
Expand Down Expand Up @@ -35,11 +35,10 @@
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
Expand Down Expand Up @@ -93,9 +92,6 @@ private static boolean looksLikeJapanese(CharBuffer cb) {
}

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";
Expand Down Expand Up @@ -226,7 +222,7 @@ public Charset detectedCharset() {
* 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");
Expand Down

1 comment on commit ba90dc7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.