Merge "BUG-2510: Remove all registrations when a routed rpc is closed"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / ThreadLocalTransformers.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.netconf.nettyutil.handler;
9
10 import javax.xml.transform.OutputKeys;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerConfigurationException;
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.TransformerFactoryConfigurationError;
15
16 /**
17  * Utility class for cached thread-local transformers. This class exists mostly for use by handlers.
18  */
19 final class ThreadLocalTransformers {
20     private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
21
22     private static final ThreadLocal<Transformer> DEFAULT_TRANSFORMER = new ThreadLocal<Transformer>() {
23         @Override
24         protected Transformer initialValue() {
25             try {
26                 return FACTORY.newTransformer();
27             } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
28                 throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
29             }
30         };
31
32         @Override
33         public void set(final Transformer value) {
34             throw new UnsupportedOperationException();
35         };
36     };
37
38     private static final ThreadLocal<Transformer> PRETTY_TRANSFORMER = new ThreadLocal<Transformer>() {
39         @Override
40         protected Transformer initialValue() {
41             final Transformer ret;
42
43             try {
44                 ret = FACTORY.newTransformer();
45             } catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) {
46                 throw new IllegalStateException("Unexpected error while instantiating a Transformer", e);
47             }
48
49             ret.setOutputProperty(OutputKeys.INDENT, "yes");
50             ret.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
51             return ret;
52         };
53
54         @Override
55         public void set(final Transformer value) {
56             throw new UnsupportedOperationException();
57         };
58     };
59
60     private ThreadLocalTransformers() {
61         throw new UnsupportedOperationException("Utility class");
62     }
63
64     /**
65      * Get the transformer with default configuration.
66      *
67      * @return A transformer with default configuration based on the default implementation.
68      */
69     public static Transformer getDefaultTransformer() {
70         return DEFAULT_TRANSFORMER.get();
71     }
72
73     /**
74      * Get the transformer with default configuration, but with automatic indentation
75      * and the XML declaration removed.
76      *
77      * @return A transformer with human-friendly configuration.
78      */
79     public static Transformer getPrettyTransformer() {
80         return PRETTY_TRANSFORMER.get();
81     }
82 }