fix SLF4J-324

This commit is contained in:
Ceki Gulcu 2015-11-10 19:54:10 +01:00
parent c41df923f2
commit 37229d8d62
7 changed files with 110 additions and 4 deletions

View File

@ -13,6 +13,10 @@
<echo message="runtime classpath: ${runtime_classpath}" />
<echo message="test classpath: ${test_classpath}" />
<echo message="plugin classpath: ${plugin_classpath}" />
<echo message="basedir: ${basedir}" />
<!--<property name="path_to_policy" value="file:./integration/src/policy/java-under-ant.policy"/>-->
<path id="path142Binding">
@ -103,7 +107,8 @@
testMatch,
testMultiBinding,
testIncompatibleMultiBinding,
testFuture_16Series">
testFuture_16Series,
testActiveSecurityManager">
</target>
@ -197,4 +202,34 @@
</junit>
</target>
<condition property="runFromWithinIntegrationModule">
<contains string="${user.dir}" substring="integration" />
</condition>
<target name="setPathToPolicy_FromTop" unless="runFromWithinIntegrationModule">
<echo>setPathToPolicy_FromTop</echo>
<property name="path_to_policy" value="file:./integration/src/policy/java-under-ant.policy"/>
</target>
<target name="setPathToPolicy_FromIntegration" if="runFromWithinIntegrationModule">
<echo>setPathToPolicy_FromInegration </echo>
<property name="path_to_policy" value="file:./src/policy/java-under-ant.policy"/>
</target>
<target name="testActiveSecurityManager" depends="setPathToPolicy_FromTop, setPathToPolicy_FromIntegration">
<junit printsummary="yes" fork="no" haltonfailure="yes">
<jvmarg value="-Djava.security.manager"/>
<jvmarg value="-Djava.security.policy=${path_to_policy}"/>
<jvmarg value="-Dslf4j.detectLoggerNameMismatch=true"/>
<classpath refid="pathCurrent" />
<formatter type="plain" />
<test fork="yes" todir="target/unit-reports"
outfile="TEST-SecurityManager"
name="org.slf4j.issues.Issue324Test" />
</junit>
</target>
</project>

View File

@ -0,0 +1,10 @@
grant {
// note that java.lang.RuntimePermission createSecurityManager is NOT granted
permission java.util.PropertyPermission "user.dir", "read";
permission java.util.PropertyPermission "*", "read";
permission java.net.SocketPermission "*", "connect,resolve";
};

View File

@ -0,0 +1,13 @@
grant {
// note that java.lang.RuntimePermission createSecurityManager is NOT granted
permission java.util.PropertyPermission "user.dir", "read";
// permissions required for Ant's Junit runner
permission java.util.PropertyPermission "*", "read, write";
permission java.io.FilePermission "./-", "read";
permission java.io.FilePermission "./-", "write";
permission java.lang.RuntimePermission "setIO";
};

View File

@ -0,0 +1,17 @@
package org.slf4j.issues;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import junit.framework.TestCase;
public class Issue324Test extends TestCase {
public void testLoggerCreationInPresenseOfSecurityManager() {
String currentDir = System.getProperty("user.dir");
System.out.println("currentDir:"+currentDir);
Logger logger = LoggerFactory.getLogger(Issue324Test.class);
logger.debug("hello");
}
}

View File

@ -84,7 +84,7 @@ public final class LoggerFactory {
// Support for detecting mismatched logger names.
static final String DETECT_LOGGER_NAME_MISMATCH_PROPERTY = "slf4j.detectLoggerNameMismatch";
static boolean DETECT_LOGGER_NAME_MISMATCH = Boolean.getBoolean(DETECT_LOGGER_NAME_MISMATCH_PROPERTY);
static boolean DETECT_LOGGER_NAME_MISMATCH = Util.safeGetBooleanSystemProperty(DETECT_LOGGER_NAME_MISMATCH_PROPERTY);
/**
* It is LoggerFactory's responsibility to track version changes and manage
@ -301,7 +301,7 @@ public final class LoggerFactory {
Logger logger = getLogger(clazz.getName());
if (DETECT_LOGGER_NAME_MISMATCH) {
Class<?> autoComputedCallingClass = Util.getCallingClass();
if (nonMatchingClasses(clazz, autoComputedCallingClass)) {
if (autoComputedCallingClass != null && nonMatchingClasses(clazz, autoComputedCallingClass)) {
Util.report(String.format("Detected logger name mismatch. Given name: \"%s\"; computed name: \"%s\".", logger.getName(),
autoComputedCallingClass.getName()));
Util.report("See " + LOGGER_NAME_MISMATCH_URL + " for an explanation");

View File

@ -35,6 +35,27 @@ 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
@ -46,7 +67,15 @@ public final class Util {
}
}
private static final ClassContextSecurityManager SECURITY_MANAGER = new ClassContextSecurityManager();
private static final ClassContextSecurityManager SECURITY_MANAGER = safeCreateSecurityManager();
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.
@ -54,6 +83,8 @@ public final class Util {
* @return the name of the class which called the invoking method.
*/
public static Class<?> getCallingClass() {
if(SECURITY_MANAGER == null)
return null;
Class<?>[] trace = SECURITY_MANAGER.getClassContext();
String thisClassName = Util.class.getName();