Skip to content

Commit

Permalink
Remove SecurityManager references from logging package #16
Browse files Browse the repository at this point in the history
Signed-off-by: jmehrens <jason_mehrens@hotmail.com>
  • Loading branch information
jmehrens committed Jan 14, 2024
1 parent 5f3407f commit 34c1259
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
16 changes: 4 additions & 12 deletions demos/logging/src/main/java/example/app/FileErrorManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2009, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2023 Jason Mehrens. All Rights Reserved.
* Copyright (c) 2009, 2024Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2024 Jason Mehrens. All Rights Reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -18,8 +18,6 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.ErrorManager;
import java.util.logging.Level;
import java.util.logging.LogManager;
Expand Down Expand Up @@ -269,13 +267,7 @@ private File getEmailStore() {
String dir = manager.getProperty(
getClass().getName().concat(".pattern"));
if (dir == null) {
dir = AccessController.doPrivileged(new PrivilegedAction<String>() {

@Override
public String run() {
return System.getProperty("java.io.tmpdir", ".");
}
});
dir = System.getProperty("java.io.tmpdir", ".");
}
return new File(dir);
}
Expand All @@ -291,7 +283,7 @@ private OutputStream wrap(OutputStream out) {
assert out != null;
Class<?> k;
try {
k = Class.forName("example.app.NewlineOutputStream");
k = Class.forName("example.app.internal.NewlineOutputStream");
if (OutputStream.class.isAssignableFrom(k)) {
Constructor<?> c = k.getConstructor(OutputStream.class);
return (OutputStream) c.newInstance(out);
Expand Down
15 changes: 0 additions & 15 deletions demos/logging/src/main/java/example/app/MailHandlerDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,6 @@ private static void checkConfig(String prefix, PrintStream err) {
err.println(prefix + ": LOGGER=" + LOGGER.getLevel());
err.println(prefix + ": JVM id "
+ ManagementFactory.getRuntimeMXBean().getName());
err.println(prefix + ": java.security.debug="
+ System.getProperty("java.security.debug"));
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
err.println(prefix + ": SecurityManager.class="
+ sm.getClass().getName());
err.println(prefix + ": SecurityManager classLoader="
+ toString(sm.getClass().getClassLoader()));
err.println(prefix + ": SecurityManager.toString=" + sm);
} else {
err.println(prefix + ": SecurityManager.class=null");
err.println(prefix + ": SecurityManager.toString=null");
err.println(prefix + ": SecurityManager classLoader=null");
}

String policy = System.getProperty("java.security.policy");
if (policy != null) {
File f = new File(policy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,8 @@ private static Properties readConfiguration() {
String n = System.getProperty("java.util.logging.config.file");
if (n != null) {
final File f = new File(n).getCanonicalFile();
final InputStream in = new FileInputStream(f);
try {
try (InputStream in = new FileInputStream(f)) {
props.load(in);
} finally {
in.close();
}
}
} catch (final LinkageError | Exception permissionsOrMalformed) {
Expand Down Expand Up @@ -249,6 +246,35 @@ static String fromLogManager(final String name) {
* @since JavaMail 1.5.3
*/
static void checkLogManagerAccess() {
boolean checked = false;
final Object m = LOG_MANAGER;
if (m != null) {
try {
if (m instanceof LogManager) {
try {
LogManager.class.getMethod("checkAccess").invoke(m);
checked = true;
} catch (InvocationTargetException ite) {
Throwable cause = ite.getCause();
if (cause instanceof SecurityException) {
checked = true;
throw (SecurityException) cause;
}

if (cause instanceof UnsupportedOperationException) {
checked = true;
}
} catch (ReflectiveOperationException ignore) {
}
}
} catch (final SecurityException notAllowed) {
if (checked) {
throw notAllowed;
}
} catch (final LinkageError | RuntimeException restricted) {
}
}

/**
* Some environments selectively enforce logging permissions by allowing
* access to loggers but not allowing access to handlers. This is an
Expand All @@ -257,10 +283,13 @@ static void checkLogManagerAccess() {
* logger is used instead as it is a known named logger with well
* defined behavior. Contractually, Logger::remove will check
* permission before checking if the argument is null.
* See JDK-8023168
*/
try {
Logger.getGlobal().removeHandler((Handler) null);
} catch (final NullPointerException unexpected) {
if (!checked) {
try {
Logger.getGlobal().removeHandler((Handler) null);
} catch (final NullPointerException unexpected) {
}
}
}

Expand Down

0 comments on commit 34c1259

Please sign in to comment.