Skip to content

Commit

Permalink
NIFI-5308: Avoid holding Template DOM Nodes in heap. This closes apac…
Browse files Browse the repository at this point in the history
  • Loading branch information
markap14 authored and mcgilman committed Jun 13, 2018
1 parent da99f87 commit 275b8cb
Showing 1 changed file with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.OutputStream;
import java.util.Map;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -82,12 +81,6 @@ public class StandardFlowSerializer implements FlowSerializer<Document> {

private final StringEncryptor encryptor;

// Cache of template to DOM Node for that template. This is done because when we serialize templates, we have to first
// take the template DTO, then serialize that into a byte[], then parse that byte[] as XML DOM objects. Then we can use that
// XML DOM Object in the serialized flow. This is expensive, so we cache these XML DOM objects here. We use a WeakHashMap
// because we don't get notified when the template has been removed from the system.
private static final Map<Template, Node> templateNodes = new WeakHashMap<>();

public StandardFlowSerializer(final StringEncryptor encryptor) {
this.encryptor = encryptor;
}
Expand Down Expand Up @@ -625,25 +618,18 @@ private static void addTextElement(final Element element, final String name, fin
element.appendChild(toAdd);
}


public static synchronized void addTemplate(final Element element, final Template template) {
public static void addTemplate(final Element element, final Template template) {
try {
Node templateNode = templateNodes.get(template);
if (templateNode == null) {
final byte[] serialized = TemplateSerializer.serialize(template.getDetails());

final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
final Document document;
try (final InputStream in = new ByteArrayInputStream(serialized)) {
document = docBuilder.parse(in);
}

templateNode = document.getDocumentElement();
templateNodes.put(template, templateNode);
final byte[] serialized = TemplateSerializer.serialize(template.getDetails());

final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
final Document document;
try (final InputStream in = new ByteArrayInputStream(serialized)) {
document = docBuilder.parse(in);
}

templateNode = element.getOwnerDocument().importNode(templateNode, true);
final Node templateNode = element.getOwnerDocument().importNode(document.getDocumentElement(), true);
element.appendChild(templateNode);
} catch (final Exception e) {
throw new FlowSerializationException(e);
Expand Down

0 comments on commit 275b8cb

Please sign in to comment.