Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

Commit

Permalink
[JENKINS-18775] It is necessary to synchronize access to converters, …
Browse files Browse the repository at this point in the history
…not just typeToConverterMap.
  • Loading branch information
jglick committed Aug 26, 2013
1 parent f669b43 commit f3cd870
Showing 1 changed file with 13 additions and 10 deletions.
Expand Up @@ -18,8 +18,6 @@
import com.thoughtworks.xstream.core.util.PrioritizedList;
import com.thoughtworks.xstream.mapper.Mapper;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
Expand All @@ -34,7 +32,7 @@
public class DefaultConverterLookup implements ConverterLookup, ConverterRegistry, Caching {

private final PrioritizedList converters = new PrioritizedList();
private transient Map typeToConverterMap = Collections.synchronizedMap(new WeakHashMap());
private transient Map typeToConverterMap;

public DefaultConverterLookup() {
}
Expand All @@ -45,7 +43,10 @@ public DefaultConverterLookup() {
public DefaultConverterLookup(Mapper mapper) {
}

public Converter lookupConverterForType(Class type) {
public synchronized Converter lookupConverterForType(Class type) {
if (typeToConverterMap == null) {
typeToConverterMap = new WeakHashMap();
}
Converter cachedConverter = (Converter) typeToConverterMap.get(type);
if (cachedConverter != null) {
return cachedConverter;
Expand All @@ -61,7 +62,10 @@ public Converter lookupConverterForType(Class type) {
throw new ConversionException("No converter specified for " + type);
}

public void registerConverter(Converter converter, int priority) {
public synchronized void registerConverter(Converter converter, int priority) {
if (typeToConverterMap == null) {
typeToConverterMap = new WeakHashMap();
}
converters.add(converter, priority);
for (Iterator iter = typeToConverterMap.keySet().iterator(); iter.hasNext();) {
Class type = (Class) iter.next();
Expand All @@ -71,7 +75,10 @@ public void registerConverter(Converter converter, int priority) {
}
}

public void flushCache() {
public synchronized void flushCache() {
if (typeToConverterMap == null) {
typeToConverterMap = new WeakHashMap();
}
typeToConverterMap.clear();
Iterator iterator = converters.iterator();
while (iterator.hasNext()) {
Expand All @@ -82,8 +89,4 @@ public void flushCache() {
}
}

private Object readResolve() {
typeToConverterMap = Collections.synchronizedMap(new HashMap());
return this;
}
}

0 comments on commit f3cd870

Please sign in to comment.