fix bugs 203 and 224, adaptations for maven 3

This commit is contained in:
Ceki Gulcu 2011-10-31 23:17:02 +01:00
parent 01d36bb31a
commit f5865d3241
5 changed files with 66 additions and 56 deletions

View File

@ -216,8 +216,7 @@
<executions>
<execution>
<id>parse-version</id>
<goals>
<goal>parse-version</goal>
<goals> <goal>parse-version</goal>
</goals>
</execution>
</executions>

View File

@ -26,8 +26,8 @@ package org.slf4j;
import java.util.Map;
import org.slf4j.helpers.BasicMDCAdapter;
import org.slf4j.helpers.NOPMDCAdapter;
import org.slf4j.helpers.BasicMDCAdapter;
import org.slf4j.helpers.Util;
import org.slf4j.impl.StaticMDCBinder;
import org.slf4j.spi.MDCAdapter;
@ -40,9 +40,10 @@ import org.slf4j.spi.MDCAdapter;
* If the underlying logging system offers MDC functionality, then SLF4J's MDC,
* i.e. this class, will delegate to the underlying system's MDC. Note that at
* this time, only two logging systems, namely log4j and logback, offer MDC
* functionality. If the underlying system does not support MDC, e.g.
* java.util.logging, then SLF4J will use a {@link BasicMDCAdapter}.
*
* functionality. For java.util.logging which does not support MDC,
* {@link BasicMDCAdapter} will be used. For other systems, i.e slf4j-simple
* and slf4j-nop, {@link NOPMDCAdapter} will be used.
*
* <p>
* Thus, as a SLF4J user, you can take advantage of MDC in the presence of log4j
* logback, or java.util.logging, but without forcing these systems as

View File

@ -26,18 +26,16 @@ package org.slf4j.helpers;
import org.slf4j.spi.MDCAdapter;
import java.util.HashMap;
import java.util.*;
import java.util.Map;
import java.util.Set;
/**
* Basic MDC implementation, which can be used with logging systems that lack
* out-of-the-box MDC support.
*
* This code is largely based on logback's <a
* href="http://svn.qos.ch/viewvc/logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java">
* LogbackMDCAdapter</a>.
*
* This code was initially inspired by logback's LogbackMDCAdapter. However,
* LogbackMDCAdapter has evolved and is now considerably more sophisticated.
*
* @author Ceki Gulcu
* @author Maarten Bosteels
*
@ -47,6 +45,19 @@ public class BasicMDCAdapter implements MDCAdapter {
private InheritableThreadLocal inheritableThreadLocal = new InheritableThreadLocal();
static boolean isJDK14() {
try {
String javaVersion = System.getProperty("java.version");
return javaVersion.startsWith("1.4");
} catch(SecurityException se) {
// punt and assume JDK 1.5 or later
return false;
}
}
static boolean IS_JDK14 = isJDK14();
/**
* Put a context value (the <code>val</code> parameter) as identified with
* the <code>key</code> parameter into the current thread's context map.
@ -63,9 +74,9 @@ public class BasicMDCAdapter implements MDCAdapter {
if (key == null) {
throw new IllegalArgumentException("key cannot be null");
}
HashMap map = (HashMap) inheritableThreadLocal.get();
Map map = (Map) inheritableThreadLocal.get();
if (map == null) {
map = new HashMap();
map = Collections.synchronizedMap(new HashMap());
inheritableThreadLocal.set(map);
}
map.put(key, val);
@ -75,9 +86,9 @@ public class BasicMDCAdapter implements MDCAdapter {
* Get the context identified by the <code>key</code> parameter.
*/
public String get(String key) {
HashMap hashMap = (HashMap) inheritableThreadLocal.get();
if ((hashMap != null) && (key != null)) {
return (String) hashMap.get(key);
Map Map = (Map) inheritableThreadLocal.get();
if ((Map != null) && (key != null)) {
return (String) Map.get(key);
} else {
return null;
}
@ -87,7 +98,7 @@ public class BasicMDCAdapter implements MDCAdapter {
* Remove the the context identified by the <code>key</code> parameter.
*/
public void remove(String key) {
HashMap map = (HashMap) inheritableThreadLocal.get();
Map map = (Map) inheritableThreadLocal.get();
if (map != null) {
map.remove(key);
}
@ -97,12 +108,16 @@ public class BasicMDCAdapter implements MDCAdapter {
* Clear all entries in the MDC.
*/
public void clear() {
HashMap hashMap = (HashMap) inheritableThreadLocal.get();
if (hashMap != null) {
hashMap.clear();
Map map = (Map) inheritableThreadLocal.get();
if (map != null) {
map.clear();
// the InheritableThreadLocal.remove method was introduced in JDK 1.5
// Thus, invoking clear() on previous JDK's will fail
inheritableThreadLocal.remove();
// Thus, invoking clear() on previous JDK 1.4 will fail
if(isJDK14()) {
inheritableThreadLocal.set(null);
} else {
inheritableThreadLocal.remove();
}
}
}
@ -113,9 +128,9 @@ public class BasicMDCAdapter implements MDCAdapter {
* @return the keys in the MDC
*/
public Set getKeys() {
HashMap hashMap = (HashMap) inheritableThreadLocal.get();
if (hashMap != null) {
return hashMap.keySet();
Map map = (Map) inheritableThreadLocal.get();
if (map != null) {
return map.keySet();
} else {
return null;
}
@ -126,23 +141,21 @@ public class BasicMDCAdapter implements MDCAdapter {
*
*/
public Map getCopyOfContextMap() {
HashMap hashMap = (HashMap) inheritableThreadLocal.get();
if (hashMap != null) {
return new HashMap(hashMap);
Map oldMap = (Map) inheritableThreadLocal.get();
if (oldMap != null) {
Map newMap = Collections.synchronizedMap(new HashMap());
synchronized (oldMap) {
newMap.putAll(oldMap);
}
return newMap;
} else {
return null;
}
}
public void setContextMap(Map contextMap) {
HashMap hashMap = (HashMap) inheritableThreadLocal.get();
if (hashMap != null) {
hashMap.clear();
hashMap.putAll(contextMap);
} else {
hashMap = new HashMap(contextMap);
inheritableThreadLocal.set(hashMap);
}
Map map = Collections.synchronizedMap(new HashMap(contextMap));
inheritableThreadLocal.set(map);
}
}

View File

@ -29,11 +29,7 @@
<filtering>true</filtering>
</resource>
</resources>
</build>
<reporting>
<plugins>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
@ -41,19 +37,7 @@
<outputDirectory>${project.parent.basedir}/target/site</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>
maven-project-info-reports-plugin
</artifactId>
<reportSets>
<reportSet>
<reports>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</plugins>
</build>
</project>

View File

@ -29,6 +29,19 @@
<hr noshade="noshade" size="1"/>
<h3>October 31st, 2011 - Release of SLF4J 1.6.4</h3>
<p>Fixed in thread-safety issues in <code>BasicMDCAdapter</code>
fixing <a href="http://bugzilla.slf4j.org/show_bug.cgi?id=203">bug
#203</a> and <a
href="http://bugzilla.slf4j.org/show_bug.cgi?id=224">bug
#224</a>. Note that <code>BasicMDCAdapter</code> is only used with
the slf4j-jdk14.jar binding.
</p>
<p><code>BasicMDCAdapter</code> invoked a method introduced in JDK
1.5 preventing it from running under JDK 1.4. Interestingly enough,
this issue has never been reported by the user community.</p>
<h3>October 17th, 2011 - Release of SLF4J 1.6.3</h3>