This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch coheigea/StaxOutInterceptor
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 1a6593128f7f0906afd7e00fef82699ad81b6006
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Thu Nov 26 07:18:03 2026 +0000

    Make the StAX factory caches in StaxIn/StaxOutInterceptor thread-safe
---
 .../java/org/apache/cxf/interceptor/StaxInInterceptor.java    | 11 +++++++----
 .../java/org/apache/cxf/interceptor/StaxOutInterceptor.java   | 11 +++++++----
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git 
a/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java 
b/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java
index d40daa30483..371552bb855 100644
--- a/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java
+++ b/core/src/main/java/org/apache/cxf/interceptor/StaxInInterceptor.java
@@ -24,9 +24,9 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.lang.reflect.InvocationTargetException;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Logger;
 
 import javax.xml.stream.XMLInputFactory;
@@ -52,7 +52,7 @@ public class StaxInInterceptor extends 
AbstractPhaseInterceptor<Message> {
 
     private static final Logger LOG = 
LogUtils.getL7dLogger(StaxInInterceptor.class);
 
-    private static Map<Object, XMLInputFactory> factories = new HashMap<>();
+    private static final Map<Object, XMLInputFactory> FACTORIES = new 
ConcurrentHashMap<>();
 
     public StaxInInterceptor() {
         super(Phase.POST_STREAM);
@@ -164,7 +164,7 @@ public class StaxInInterceptor extends 
AbstractPhaseInterceptor<Message> {
         if (o instanceof XMLInputFactory) {
             return (XMLInputFactory)o;
         } else if (o != null) {
-            XMLInputFactory xif = factories.get(o);
+            XMLInputFactory xif = FACTORIES.get(o);
             if (xif == null) {
                 Class<?> cls;
                 if (o instanceof Class) {
@@ -183,7 +183,10 @@ public class StaxInInterceptor extends 
AbstractPhaseInterceptor<Message> {
 
                 try {
                     xif = 
(XMLInputFactory)(cls.getDeclaredConstructor().newInstance());
-                    factories.put(o, xif);
+                    XMLInputFactory existing = FACTORIES.putIfAbsent(o, xif);
+                    if (existing != null) {
+                        xif = existing;
+                    }
                 } catch (InstantiationException | IllegalAccessException | 
IllegalArgumentException
                          | InvocationTargetException | NoSuchMethodException | 
SecurityException e) {
                     throw new Fault(e);
diff --git 
a/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java 
b/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java
index 08b6ca67a6f..574a6c26eec 100644
--- a/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java
+++ b/core/src/main/java/org/apache/cxf/interceptor/StaxOutInterceptor.java
@@ -23,9 +23,9 @@ import java.io.OutputStream;
 import java.io.Writer;
 import java.lang.reflect.InvocationTargetException;
 import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.ResourceBundle;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
@@ -54,7 +54,7 @@ public class StaxOutInterceptor extends 
AbstractPhaseInterceptor<Message> {
         = new StaxOutEndingInterceptor(OUTPUT_STREAM_HOLDER, WRITER_HOLDER);
 
     private static final ResourceBundle BUNDLE = 
BundleUtils.getBundle(StaxOutInterceptor.class);
-    private static Map<Object, XMLOutputFactory> factories = new HashMap<>();
+    private static final Map<Object, XMLOutputFactory> FACTORIES = new 
ConcurrentHashMap<>();
 
 
     public StaxOutInterceptor() {
@@ -167,7 +167,7 @@ public class StaxOutInterceptor extends 
AbstractPhaseInterceptor<Message> {
             m.put(FORCE_START_DOCUMENT, Boolean.TRUE);
             return (XMLOutputFactory)o;
         } else if (o != null) {
-            XMLOutputFactory xif = factories.get(o);
+            XMLOutputFactory xif = FACTORIES.get(o);
             if (xif == null) {
                 Class<?> cls;
                 if (o instanceof Class) {
@@ -185,7 +185,10 @@ public class StaxOutInterceptor extends 
AbstractPhaseInterceptor<Message> {
 
                 try {
                     xif = 
(XMLOutputFactory)(cls.getDeclaredConstructor().newInstance());
-                    factories.put(o, xif);
+                    XMLOutputFactory existing = FACTORIES.putIfAbsent(o, xif);
+                    if (existing != null) {
+                        xif = existing;
+                    }
                 } catch (InstantiationException | IllegalAccessException | 
IllegalArgumentException
                          | InvocationTargetException | NoSuchMethodException | 
SecurityException e) {
                     throw new Fault(e);

Reply via email to