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/21 16:35:35 UTC
svn commit: r1831976 - in /santuario/xml-security-java/trunk/src:
main/java/org/apache/xml/security/utils/XMLUtils.java
test/java/org/apache/xml/security/test/dom/utils/DocumentBuilderPoolingTest.java
Author: coheigea
Date: Mon May 21 16:35:35 2018
New Revision: 1831976
URL: http://svn.apache.org/viewvc?rev=1831976&view=rev
Log:
Switch to using ThreadLocals instead of a a Thread pool in XMLUtils
Removed:
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/utils/DocumentBuilderPoolingTest.java
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=1831976&r1=1831975&r2=1831976&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 Mon May 21 16:35:35 2018
@@ -18,9 +18,7 @@
*/
package org.apache.xml.security.utils;
-import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.AccessController;
@@ -36,23 +34,17 @@ import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.validation.Schema;
import org.apache.xml.security.c14n.CanonicalizationException;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.w3c.dom.Attr;
-import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
-import org.xml.sax.EntityResolver;
-import org.xml.sax.ErrorHandler;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
/**
* DOM and XML accessibility and comfort functions.
@@ -65,12 +57,12 @@ public final class XMLUtils {
(PrivilegedAction<Boolean>) () -> Boolean.getBoolean("org.apache.xml.security.ignoreLineBreaks"));
@SuppressWarnings("unchecked")
- private static final WeakObjectPool<DocumentBuilder, ParserConfigurationException> pools[] = new WeakObjectPool[4];
+ private static final ThreadLocal<DocumentBuilder> tl[][] = new ThreadLocal[2][2];
static {
- pools[0] = new DocumentBuilderPool(false, false);
- pools[1] = new DocumentBuilderPool(false, true);
- pools[2] = new DocumentBuilderPool(true, false);
- pools[3] = new DocumentBuilderPool(true, true);
+ 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";
@@ -1069,25 +1061,14 @@ public final class XMLUtils {
public static DocumentBuilder createDocumentBuilder(
boolean validating, boolean disAllowDocTypeDeclarations
) throws ParserConfigurationException {
- int idx = getPoolsIndex(validating, disAllowDocTypeDeclarations);
- return pools[idx].getObject();
+ 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.
- * @return whether it was successfully returned to the pool
- */
+ @Deprecated
public static boolean repoolDocumentBuilder(DocumentBuilder db) {
- if (!(db instanceof DocumentBuilderProxy)) {
- return false;
- }
- db.reset();
- boolean disAllowDocTypeDeclarations =
- ((DocumentBuilderProxy)db).disAllowDocTypeDeclarations();
- int idx = getPoolsIndex(db.isValidating(), disAllowDocTypeDeclarations);
- return pools[idx].repool(db);
+ return true;
}
/**
@@ -1135,121 +1116,30 @@ public final class XMLUtils {
return resizedBytes;
}
- /**
- * We need this proxy wrapping DocumentBuilder to record the value
- * passed to disAllowDoctypeDeclarations. It's needed to figure out
- * on which pool to return.
- */
- private static class DocumentBuilderProxy extends DocumentBuilder {
- private final DocumentBuilder delegate;
- private final boolean disAllowDocTypeDeclarations;
-
- private DocumentBuilderProxy(DocumentBuilder actual, boolean disAllowDocTypeDeclarations) {
- delegate = actual;
- this.disAllowDocTypeDeclarations = disAllowDocTypeDeclarations;
- }
-
- boolean disAllowDocTypeDeclarations() {
- return disAllowDocTypeDeclarations;
- }
-
- public void reset() {
- delegate.reset();
- }
-
- public Document parse(InputStream is) throws SAXException, IOException {
- return delegate.parse(is);
- }
-
- public Document parse(InputStream is, String systemId)
- throws SAXException, IOException {
- return delegate.parse(is, systemId);
- }
-
- public Document parse(String uri) throws SAXException, IOException {
- return delegate.parse(uri);
- }
-
- public Document parse(File f) throws SAXException, IOException {
- return delegate.parse(f);
- }
-
- public Schema getSchema() {
- return delegate.getSchema();
- }
-
- public boolean isXIncludeAware() {
- return delegate.isXIncludeAware();
- }
-
- @Override
- public Document parse(InputSource is) throws SAXException, IOException {
- return delegate.parse(is);
- }
-
- @Override
- public boolean isNamespaceAware() {
- return delegate.isNamespaceAware();
- }
-
- @Override
- public boolean isValidating() {
- return delegate.isValidating();
- }
-
- @Override
- public void setEntityResolver(EntityResolver er) {
- delegate.setEntityResolver(er);
- }
-
- @Override
- public void setErrorHandler(ErrorHandler eh) {
- delegate.setErrorHandler(eh);
- }
-
- @Override
- public Document newDocument() {
- return delegate.newDocument();
- }
-
- @Override
- public DOMImplementation getDOMImplementation() {
- return delegate.getDOMImplementation();
- }
-
- }
-
- private static final class DocumentBuilderPool
- extends WeakObjectPool<DocumentBuilder, ParserConfigurationException> {
+ private static final class MyThreadLocal extends ThreadLocal<DocumentBuilder> {
private final boolean validating, disAllowDocTypeDeclarations;
- public DocumentBuilderPool(boolean validating, boolean disAllowDocTypeDeclarations) {
+ public MyThreadLocal(boolean validating, boolean disAllowDocTypeDeclarations) {
this.validating = validating;
this.disAllowDocTypeDeclarations = disAllowDocTypeDeclarations;
}
@Override
- protected DocumentBuilder createObject() 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);
+ 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);
}
- dfactory.setValidating(validating);
- dfactory.setNamespaceAware(true);
- return new DocumentBuilderProxy(dfactory.newDocumentBuilder(), disAllowDocTypeDeclarations);
}
}
- /**
- * Maps the two boolean configuration options for the factories to the array index for the WeakObjectPool
- * @param validating
- * @param disAllowDocTypeDeclarations
- * @return the index to the {@link #pools}
- */
- private static int getPoolsIndex(boolean validating, boolean disAllowDocTypeDeclarations) {
- return (validating ? 2 : 0) + (disAllowDocTypeDeclarations ? 1 : 0);
- }
-
}