import io.netty.handler.codec.MessageToByteEncoder;
import java.io.IOException;
import java.io.OutputStream;
-import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.sax.SAXTransformerFactory;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.openexi.proc.common.EXIOptionsException;
import org.openexi.sax.Transmogrifier;
import org.slf4j.LoggerFactory;
public final class NetconfMessageToEXIEncoder extends MessageToByteEncoder<NetconfMessage> {
-
private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToEXIEncoder.class);
-
- private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
private final NetconfEXICodec codec;
public NetconfMessageToEXIEncoder(final NetconfEXICodec codec) {
final Transmogrifier transmogrifier = codec.getTransmogrifier();
transmogrifier.setOutputStream(os);
- final Transformer transformer = saxTransformerFactory.newTransformer();
- transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(transmogrifier.getSAXTransmogrifier()));
+ ThreadLocalTransformers.getDefaultTransformer().transform(new DOMSource(msg.getDocument()), new SAXResult(transmogrifier.getSAXTransmogrifier()));
}
}
}
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.opendaylight.controller.netconf.api.NetconfMessage;
public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMessage> {
private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class);
- private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
private final Optional<String> clientId;
this(Optional.<String>absent());
}
- public NetconfMessageToXMLEncoder(Optional<String> clientId) {
+ public NetconfMessageToXMLEncoder(final Optional<String> clientId) {
this.clientId = clientId;
}
@Override
@VisibleForTesting
- public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
+ public void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws IOException, TransformerException {
LOG.trace("Sent to encode : {}", msg);
if (clientId.isPresent()) {
}
try (OutputStream os = new ByteBufOutputStream(out)) {
- Transformer transformer = FACTORY.newTransformer();
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-
// Wrap OutputStreamWriter with BufferedWriter as suggested in javadoc for OutputStreamWriter
StreamResult result = new StreamResult(new BufferedWriter(new OutputStreamWriter(os)));
DOMSource source = new DOMSource(msg.getDocument());
- transformer.transform(source, result);
+ ThreadLocalTransformers.getPrettyTransformer().transform(source, result);
}
}
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.netconf.nettyutil.handler;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+
+/**
+ * Utility class for cached thread-local transformers. This class exists mostly for use by handlers.
+ */
+final class ThreadLocalTransformers {
+ private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
+
+ private static final ThreadLocal<Transformer> DEFAULT_TRANSFORMER = new ThreadLocal<Transformer>() {
+ @Override
+ protected Transformer initialValue() {
+ try {
+ return FACTORY.newTransformer();
+ } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
+ throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
+ }
+ };
+
+ @Override
+ public void set(final Transformer value) {
+ throw new UnsupportedOperationException();
+ };
+ };
+
+ private static final ThreadLocal<Transformer> PRETTY_TRANSFORMER = new ThreadLocal<Transformer>() {
+ @Override
+ protected Transformer initialValue() {
+ final Transformer ret;
+
+ try {
+ ret = FACTORY.newTransformer();
+ } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
+ throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
+ }
+
+ ret.setOutputProperty(OutputKeys.INDENT, "yes");
+ ret.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ return ret;
+ };
+
+ @Override
+ public void set(final Transformer value) {
+ throw new UnsupportedOperationException();
+ };
+ };
+
+ private ThreadLocalTransformers() {
+ throw new UnsupportedOperationException("Utility class");
+ }
+
+ /**
+ * Get the transformer with default configuration.
+ *
+ * @return A transformer with default configuration based on the default implementation.
+ */
+ public static Transformer getDefaultTransformer() {
+ return DEFAULT_TRANSFORMER.get();
+ }
+
+ /**
+ * Get the transformer with default configuration, but with automatic indentation
+ * and the XML declaration removed.
+ *
+ * @return A transformer with human-friendly configuration.
+ */
+ public static Transformer getPrettyTransformer() {
+ return PRETTY_TRANSFORMER.get();
+ }
+}