Skip to content

Commit

Permalink
SAK-40891 Use a static DocumentBuilderFactory for StorageUtils (sakai…
Browse files Browse the repository at this point in the history
…project#6224)

To mitigate threads blocking on filesystem access; see JIRA issue for more detailed explanation.
  • Loading branch information
smarquard authored and ern committed Nov 6, 2018
1 parent c391d1c commit 6daf965
Showing 1 changed file with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
@Slf4j
public class StorageUtils {
private static SAXParserFactory parserFactory;
private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

/**
* Create a new DOM Document.
Expand All @@ -67,7 +68,7 @@ public static Document createDocument()
{
try
{
DocumentBuilder builder = getDocumentBuilder();
DocumentBuilder builder = dbFactory.newDocumentBuilder();
Document doc = builder.newDocument();

return doc;
Expand All @@ -93,7 +94,7 @@ public static Document readDocument(String name)
InputStream fis = null;
try
{
DocumentBuilder docBuilder = getDocumentBuilder();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
fis = new FileInputStream(name);
doc = docBuilder.parse(fis);
}
Expand All @@ -115,7 +116,7 @@ public static Document readDocument(String name)
// OK, that didn't work - the document is probably ISO-8859-1
try
{
DocumentBuilder docBuilder = getDocumentBuilder();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
InputStreamReader in = new InputStreamReader(new FileInputStream(name), "ISO-8859-1");
InputSource inputSource = new InputSource(in);
doc = docBuilder.parse(inputSource);
Expand All @@ -130,7 +131,7 @@ public static Document readDocument(String name)
// try forcing UTF-8
try
{
DocumentBuilder docBuilder = getDocumentBuilder();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
InputStreamReader in = new InputStreamReader(new FileInputStream(name), "UTF-8");
InputSource inputSource = new InputSource(in);
doc = docBuilder.parse(inputSource);
Expand All @@ -155,7 +156,7 @@ public static Document readDocumentFromString(String in)
{
try
{
DocumentBuilder docBuilder = getDocumentBuilder();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(in));
Document doc = docBuilder.parse(inputSource);
return doc;
Expand Down Expand Up @@ -225,12 +226,10 @@ public static void writeDocument(Document doc, String fileName)
try
{
out = new FileOutputStream(fileName);
// get an instance of the DOMImplementation registry
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();

// get an instance of the DOMImplementation registry
DocumentBuilder builder = dbFactory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();

DOMImplementationLS feature = (DOMImplementationLS) impl.getFeature("LS","3.0");
LSSerializer serializer = feature.createLSSerializer();
LSOutput output = feature.createLSOutput();
Expand Down Expand Up @@ -270,9 +269,7 @@ public static String writeDocumentToString(Document doc)

StringWriter sw = new StringWriter();

DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DocumentBuilder builder = dbFactory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();


Expand Down Expand Up @@ -319,16 +316,4 @@ public static String escapeSql(String value) {
return "";
}
}


/**
* @return a DocumentBuilder object for XML parsing.
*/
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

return dbf.newDocumentBuilder();
}

}

0 comments on commit 6daf965

Please sign in to comment.