You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2018/05/22 10:51:15 UTC

svn commit: r1832016 - /santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java

Author: coheigea
Date: Tue May 22 10:51:15 2018
New Revision: 1832016

URL: http://svn.apache.org/viewvc?rev=1832016&view=rev
Log:
Re-applying ThreadLocal patch for SANTUARIO-394

Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java?rev=1832016&r1=1832015&r2=1832016&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java Tue May 22 10:51:15 2018
@@ -55,6 +55,15 @@ public final class XMLUtils {
     private static boolean ignoreLineBreaks =
         AccessController.doPrivileged(
             (PrivilegedAction<Boolean>) () -> Boolean.getBoolean("org.apache.xml.security.ignoreLineBreaks"));
+    
+    @SuppressWarnings("unchecked")
+    private static final ThreadLocal<DocumentBuilder> tl[][] = new ThreadLocal[2][2];
+    static {
+        tl[0][0] = new MyThreadLocal(false, false);
+        tl[0][1] = new MyThreadLocal(false, true);
+        tl[1][0] = new MyThreadLocal(true, false);
+        tl[1][1] = new MyThreadLocal(true, true);
+    }
 
     private static volatile String dsPrefix = "ds";
     private static volatile String ds11Prefix = "dsig11";
@@ -1052,18 +1061,11 @@ public final class XMLUtils {
     public static DocumentBuilder createDocumentBuilder(
         boolean validating, boolean disAllowDocTypeDeclarations
     ) throws ParserConfigurationException {
-        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
-        dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
-        if (disAllowDocTypeDeclarations) {
-            dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
-        }
-        dfactory.setValidating(validating);        
-        dfactory.setNamespaceAware(true);
-        
-        return dfactory.newDocumentBuilder();
+        DocumentBuilder documentBuilder = tl[validating ? 1 : 0][disAllowDocTypeDeclarations ? 1 : 0].get();
+        documentBuilder.reset();
+        return documentBuilder;
     }
 
-
     /**
      * Return this document builder to be reused
      * @param db DocumentBuilder returned from any of {@link #createDocumentBuilder} methods.
@@ -1118,5 +1120,32 @@ public final class XMLUtils {
 
         return resizedBytes;
     }
+    
+    private static final class MyThreadLocal extends ThreadLocal<DocumentBuilder> {
+        private final boolean validating;
+        private final boolean disAllowDocTypeDeclarations;
+
+        public MyThreadLocal(boolean validating, boolean disAllowDocTypeDeclarations) {
+            this.validating = validating;
+            this.disAllowDocTypeDeclarations = disAllowDocTypeDeclarations;
+        }
+
+        @Override
+        protected DocumentBuilder initialValue() {
+            try {
+                DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
+                dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
+                if (disAllowDocTypeDeclarations) {
+                    dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+                }
+                dfactory.setValidating(validating);        
+                dfactory.setNamespaceAware(true);
+                
+                return dfactory.newDocumentBuilder();
+            } catch (ParserConfigurationException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
 
 }