44import java .io .Serializable ;
55import java .io .StringReader ;
66import java .io .StringWriter ;
7+ import java .util .HashMap ;
78
89import javax .xml .bind .JAXBContext ;
910import javax .xml .bind .JAXBElement ;
2324public final class XmlUtility {
2425 private static Log logger = LogFactory .getLog (XmlUtility .class );
2526 private static final String XmlHeader = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" yes\" ?>" ;
27+ private static JAXBContext request_ctx = null ;
28+ private static JAXBContext response_ctx = null ;
29+ private static HashMap <String , JAXBContext > jaxbContext = new HashMap <String , JAXBContext >();
30+
2631
2732 /**
2833 * Default C'tor, cannot be instantiated
2934 */
3035 private XmlUtility () {
3136 }
32-
37+
3338 /**
3439 * Helper method to serialize an object to XML. Requires object to be Serializable
3540 * @param entity Object to serialize
@@ -38,15 +43,24 @@ private XmlUtility() {
3843 * @throws IOException if errors during serialization
3944 * @throws JAXBException if errors during serialization
4045 */
41- public static <T extends Serializable > String getXml (T entity ) throws IOException , JAXBException
46+ public static synchronized <T extends Serializable > String getXml (T entity ) throws IOException , JAXBException
4247 {
4348 StringWriter sw = new StringWriter ();
4449
4550 if ( null != entity )
4651 {
47- JAXBContext ctx = JAXBContext .newInstance (entity .getClass ());
48-
49- Marshaller m = ctx .createMarshaller ();
52+
53+ if (!jaxbContext .containsKey (entity .getClass ().toString ()))
54+ {
55+ request_ctx = JAXBContext .newInstance (entity .getClass ());
56+ jaxbContext .put (entity .getClass ().toString (), request_ctx );
57+ }
58+ else
59+ {
60+ request_ctx = jaxbContext .get (entity .getClass ().toString ());
61+ }
62+
63+ Marshaller m = request_ctx .createMarshaller ();
5064 m .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
5165
5266 m .marshal (entity , sw );
@@ -66,14 +80,23 @@ public static <T extends Serializable> String getXml(T entity) throws IOExceptio
6680 * @throws JAXBException if errors during de-serialization
6781 */
6882 @ SuppressWarnings ("unchecked" )
69- public static <T extends Serializable > T create (String xml , Class <T > classType ) throws JAXBException
83+ public static synchronized <T extends Serializable > T create (String xml , Class <T > classType ) throws JAXBException
7084 {
7185 T entity = null ;
7286 //make sure we have not null and not-empty string to de-serialize
7387 if ( null != xml && !xml .trim ().isEmpty ())
7488 {
75- JAXBContext ctx = JAXBContext .newInstance (classType );
76- Unmarshaller um = ctx .createUnmarshaller ();
89+ if (!jaxbContext .containsKey (classType .toString ()))
90+ {
91+ response_ctx = JAXBContext .newInstance (classType );
92+ jaxbContext .put (classType .toString (), response_ctx );
93+ }
94+ else
95+ {
96+ response_ctx = jaxbContext .get (classType .toString ());
97+ }
98+
99+ Unmarshaller um = response_ctx .createUnmarshaller ();
77100 try {
78101 Object unmarshaled = um .unmarshal (new StringReader (xml ));
79102 if ( null != unmarshaled )
0 commit comments