Skip to content

Commit 6e68c4c

Browse files
beikovlukasj
authored andcommitted
[#1996] Make XmlFactory#transformerFactoryCache ClassLoader aware
1 parent c5528d7 commit 6e68c4c

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

jaxb-ri/core/src/main/java/org/glassfish/jaxb/core/v2/util/XmlFactory.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import org.glassfish.jaxb.core.v2.Messages;
1515

1616
import java.lang.ref.SoftReference;
17+
import java.util.Collections;
18+
import java.util.Map;
19+
import java.util.WeakHashMap;
1720
import java.util.logging.Level;
1821
import java.util.logging.Logger;
1922
import javax.xml.XMLConstants;
@@ -39,8 +42,8 @@
3942
public class XmlFactory {
4043

4144
private static final Logger LOGGER = Logger.getLogger(XmlFactory.class.getName());
42-
private static volatile SoftReference<TransformerFactory> tfCacheSecure;
43-
private static volatile SoftReference<TransformerFactory> tfCacheInsecure;
45+
private static volatile Map<ClassLoader, SoftReference<TransformerFactory>> tfCacheSecure = Collections.synchronizedMap( new WeakHashMap<>() );
46+
private static volatile Map<ClassLoader, SoftReference<TransformerFactory>> tfCacheInsecure = Collections.synchronizedMap( new WeakHashMap<>() );
4447

4548
/**
4649
* If true XML security features when parsing XML documents will be disabled.
@@ -150,15 +153,13 @@ public static TransformerFactory createTransformerFactory(boolean disableSecureP
150153
if (!useCache) {
151154
return _createTransformerFactory(disableSecureProcessing);
152155
}
153-
SoftReference<TransformerFactory> ref = disableSecureProcessing ? tfCacheInsecure : tfCacheSecure;
156+
Map<ClassLoader, SoftReference<TransformerFactory>> cache = isXMLSecurityDisabled(disableSecureProcessing) ? tfCacheInsecure : tfCacheSecure;
157+
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
158+
SoftReference<TransformerFactory> ref = cache.get(contextClassLoader);
154159
TransformerFactory tf = ref != null ? ref.get() : null;
155160
if (tf == null) {
156161
tf = _createTransformerFactory(disableSecureProcessing);
157-
if (disableSecureProcessing) {
158-
tfCacheInsecure = new SoftReference<>(tf);
159-
} else {
160-
tfCacheSecure = new SoftReference<>(tf);
161-
}
162+
cache.put(contextClassLoader, new SoftReference<>(tf));
162163
}
163164
return tf;
164165
}

0 commit comments

Comments
 (0)