|
14 | 14 | import org.glassfish.jaxb.core.v2.Messages; |
15 | 15 |
|
16 | 16 | import java.lang.ref.SoftReference; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.WeakHashMap; |
17 | 20 | import java.util.logging.Level; |
18 | 21 | import java.util.logging.Logger; |
19 | 22 | import javax.xml.XMLConstants; |
|
39 | 42 | public class XmlFactory { |
40 | 43 |
|
41 | 44 | 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<>() ); |
44 | 47 |
|
45 | 48 | /** |
46 | 49 | * If true XML security features when parsing XML documents will be disabled. |
@@ -150,15 +153,13 @@ public static TransformerFactory createTransformerFactory(boolean disableSecureP |
150 | 153 | if (!useCache) { |
151 | 154 | return _createTransformerFactory(disableSecureProcessing); |
152 | 155 | } |
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); |
154 | 159 | TransformerFactory tf = ref != null ? ref.get() : null; |
155 | 160 | if (tf == null) { |
156 | 161 | 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)); |
162 | 163 | } |
163 | 164 | return tf; |
164 | 165 | } |
|
0 commit comments