Skip to content

Commit

Permalink
logging: fix deprecated APIs usage
Browse files Browse the repository at this point in the history
- Remove unused method from Util class. This removes SecurityManager
  usage.

- Remove AccessController usage.
  • Loading branch information
smancill committed Apr 7, 2024
1 parent 5f8691a commit a05771f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.function.Supplier;

/**
* <p>Simple implementation of {@link Logger} that sends all enabled log messages,
Expand Down Expand Up @@ -168,15 +167,15 @@ private static OutputChoice computeOutputChoice(String logFile) {

@SuppressWarnings("removal")
private static void loadProperties() {
// Add props from the resource simplelogger.properties
InputStream in = AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
Supplier<InputStream> configStream = () -> {
ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
if (threadCL != null) {
return threadCL.getResourceAsStream(CONFIGURATION_FILE);
} else {
return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
}
});
};
InputStream in = configStream.get();
if (null != in) {
try {
SIMPLE_LOGGER_PROPS.load(in);
Expand Down
92 changes: 0 additions & 92 deletions clara-api/src/main/java/org/jlab/clara/util/logging/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,98 +35,9 @@
@SuppressWarnings("ALL")
public final class Util {


private Util() {
}

public static String safeGetSystemProperty(String key) {
if (key == null) {
throw new IllegalArgumentException("null input");
}

String result = null;
try {
result = System.getProperty(key);
} catch (java.lang.SecurityException sm) {
; // ignore
}
return result;
}

public static boolean safeGetBooleanSystemProperty(String key) {
String value = safeGetSystemProperty(key);
if (value == null) {
return false;
} else {
return value.equalsIgnoreCase("true");
}
}

/**
* In order to call {@link SecurityManager#getClassContext()}, which is a
* protected method, we add this wrapper which allows the method to be visible
* inside this package.
*/
@SuppressWarnings("removal")
private static final class ClassContextSecurityManager extends SecurityManager {
@Override
protected Class<?>[] getClassContext() {
return super.getClassContext();
}
}

private static ClassContextSecurityManager SECURITY_MANAGER;
private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false;

private static ClassContextSecurityManager getSecurityManager() {
if (SECURITY_MANAGER != null) {
return SECURITY_MANAGER;
} else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED) {
return null;
} else {
SECURITY_MANAGER = safeCreateSecurityManager();
SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true;
return SECURITY_MANAGER;
}
}

private static ClassContextSecurityManager safeCreateSecurityManager() {
try {
return new ClassContextSecurityManager();
} catch (java.lang.SecurityException sm) {
return null;
}
}

/**
* Returns the name of the class which called the invoking method.
*
* @return the name of the class which called the invoking method.
*/
public static Class<?> getCallingClass() {
ClassContextSecurityManager securityManager = getSecurityManager();
if (securityManager == null) {
return null;
}
Class<?>[] trace = securityManager.getClassContext();
String thisClassName = Util.class.getName();

// Advance until Util is found
int i;
for (i = 0; i < trace.length; i++) {
if (thisClassName.equals(trace[i].getName())) {
break;
}
}

// trace[i] = Util; trace[i+1] = caller; trace[i+2] = caller's caller
if (i >= trace.length || i + 2 >= trace.length) {
throw new IllegalStateException("Failed to find org.slf4j.helpers.Util or its caller in the stack; " + "this should not happen");
}

return trace[i + 2];
}

static final public void report(String msg, Throwable t) {
System.err.println(msg);
System.err.println("Reported exception:");
Expand All @@ -136,7 +47,4 @@ static final public void report(String msg, Throwable t) {
static final public void report(String msg) {
System.err.println("SLF4J: " + msg);
}



}

0 comments on commit a05771f

Please sign in to comment.