The NetconfMessage.toString() method is synchronized on the
TRANSFORMER.
This creates bottleneck if method is processing a lot of messages.
Used XmlUtil.toString() instead of Transformer to fix the issue.
JIRA: NETCONF-1227
Change-Id: I26ec52bc53787801d2a01db4a8096a855a30718a
Signed-off-by: Matej Sramcik <matej.sramcik@pantheon.tech>
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNull;
-import java.io.StringWriter;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.api.DocumentedException;
import org.opendaylight.netconf.api.NamespaceURN;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.api.DocumentedException;
import org.opendaylight.netconf.api.NamespaceURN;
* NetconfMessage represents a wrapper around {@link Document}.
*/
public class NetconfMessage {
* NetconfMessage represents a wrapper around {@link Document}.
*/
public class NetconfMessage {
- private static final Transformer TRANSFORMER;
-
- static {
- final Transformer t;
- try {
- t = XmlUtil.newIndentingTransformer();
- } catch (TransformerConfigurationException e) {
- throw new ExceptionInInitializerError(e);
- }
- t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-
- TRANSFORMER = t;
- }
-
private final @NonNull Document document;
public NetconfMessage(final Document document) {
private final @NonNull Document document;
public NetconfMessage(final Document document) {
@Override
public final String toString() {
@Override
public final String toString() {
- final var result = new StreamResult(new StringWriter());
- final var source = new DOMSource(document.getDocumentElement());
-
- try {
- // Slight critical section is a tradeoff. This should be reasonably fast.
- synchronized (TRANSFORMER) {
- TRANSFORMER.transform(source, result);
- }
- } catch (TransformerException e) {
- throw new IllegalStateException("Failed to encode document", e);
- }
-
- return result.getWriter().toString();
+ return XmlUtil.toString(document);