KEYCLOAK-4148 Instantiate XML DocumentBuilder in singleton-like manner

This commit is contained in:
Hynek Mlnarik 2017-01-05 16:07:50 +01:00
parent ad9210a7a7
commit 2035398ef4

View file

@ -97,10 +97,9 @@ public class DocumentUtil {
* @throws ParserConfigurationException * @throws ParserConfigurationException
*/ */
public static Document createDocument() throws ConfigurationException { public static Document createDocument() throws ConfigurationException {
DocumentBuilderFactory factory = getDocumentBuilderFactory();
DocumentBuilder builder; DocumentBuilder builder;
try { try {
builder = factory.newDocumentBuilder(); builder = getDocumentBuilder();
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
throw new ConfigurationException(e); throw new ConfigurationException(e);
} }
@ -118,8 +117,7 @@ public class DocumentUtil {
*/ */
public static Document createDocumentWithBaseNamespace(String baseNamespace, String localPart) throws ProcessingException { public static Document createDocumentWithBaseNamespace(String baseNamespace, String localPart) throws ProcessingException {
try { try {
DocumentBuilderFactory factory = getDocumentBuilderFactory(); DocumentBuilder builder = getDocumentBuilder();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.getDOMImplementation().createDocument(baseNamespace, localPart, null); return builder.getDOMImplementation().createDocument(baseNamespace, localPart, null);
} catch (DOMException e) { } catch (DOMException e) {
throw logger.processingError(e); throw logger.processingError(e);
@ -157,8 +155,7 @@ public class DocumentUtil {
*/ */
public static Document getDocument(Reader reader) throws ConfigurationException, ProcessingException, ParsingException { public static Document getDocument(Reader reader) throws ConfigurationException, ProcessingException, ParsingException {
try { try {
DocumentBuilderFactory factory = getDocumentBuilderFactory(); DocumentBuilder builder = getDocumentBuilder();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(reader)); return builder.parse(new InputSource(reader));
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
throw logger.configurationError(e); throw logger.configurationError(e);
@ -181,9 +178,8 @@ public class DocumentUtil {
* @throws SAXException * @throws SAXException
*/ */
public static Document getDocument(File file) throws ConfigurationException, ProcessingException, ParsingException { public static Document getDocument(File file) throws ConfigurationException, ProcessingException, ParsingException {
DocumentBuilderFactory factory = getDocumentBuilderFactory();
try { try {
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = getDocumentBuilder();
return builder.parse(file); return builder.parse(file);
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
throw logger.configurationError(e); throw logger.configurationError(e);
@ -206,9 +202,8 @@ public class DocumentUtil {
* @throws SAXException * @throws SAXException
*/ */
public static Document getDocument(InputStream is) throws ConfigurationException, ProcessingException, ParsingException { public static Document getDocument(InputStream is) throws ConfigurationException, ProcessingException, ParsingException {
DocumentBuilderFactory factory = getDocumentBuilderFactory();
try { try {
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = getDocumentBuilder();
return builder.parse(is); return builder.parse(is);
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
throw logger.configurationError(e); throw logger.configurationError(e);
@ -502,6 +497,25 @@ public class DocumentUtil {
} }
} }
private static final ThreadLocal<DocumentBuilder> XML_DOCUMENT_BUILDER = new ThreadLocal<DocumentBuilder>() {
@Override
protected DocumentBuilder initialValue() {
DocumentBuilderFactory factory = getDocumentBuilderFactory();
try {
return factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new RuntimeException(ex);
}
}
};
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
DocumentBuilder res = XML_DOCUMENT_BUILDER.get();
res.reset();
return res;
}
/** /**
* <p> Creates a namespace aware {@link DocumentBuilderFactory}. The returned instance is cached and shared between * <p> Creates a namespace aware {@link DocumentBuilderFactory}. The returned instance is cached and shared between
* different threads. </p> * different threads. </p>