NIFI-5308: Avoid holding Template DOM Nodes in heap. This closes #2790

This commit is contained in:
Mark Payne 2018-06-13 15:37:02 -04:00 committed by Matt Gilman
parent da99f873a7
commit 275b8cbf23
No known key found for this signature in database
GPG Key ID: DF61EC19432AEE37

View File

@ -22,7 +22,6 @@ import java.io.InputStream;
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;
@ -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;
}
@ -625,25 +618,18 @@ public class StandardFlowSerializer implements FlowSerializer<Document> {
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 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 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);