You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/02/07 08:20:15 UTC

svn commit: r741849 - in /camel/branches/camel-1.x: ./ components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java

Author: davsclaus
Date: Sat Feb  7 07:20:15 2009
New Revision: 741849

URL: http://svn.apache.org/viewvc?rev=741849&view=rev
Log:
Merged revisions 741848 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r741848 | davsclaus | 2009-02-07 08:15:26 +0100 (Sat, 07 Feb 2009) | 1 line
  
  CAMEL-1321: Avoiding creating new JAXB instances for the same classes. To avoid running out of memory.
........

Modified:
    camel/branches/camel-1.x/   (props changed)
    camel/branches/camel-1.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sat Feb  7 07:20:15 2009
@@ -1 +1 @@
-/camel/trunk:739733,739904,740251,740295,740306,740596,740663
+/camel/trunk:739733,739904,740251,740295,740306,740596,740663,741848

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-1.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java?rev=741849&r1=741848&r2=741849&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java (original)
+++ camel/branches/camel-1.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java Sat Feb  7 07:20:15 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.converter.jaxb;
 
+import java.util.HashMap;
+import java.util.Map;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
@@ -31,13 +33,12 @@
 import org.apache.camel.converter.HasAnnotation;
 import org.apache.camel.converter.jaxp.XmlConverter;
 
-
-
 /**
  * @version $Revision$
  */
-public class JaxbConverter {
+public final class JaxbConverter {
     private XmlConverter jaxbConverter;
+    private Map<Class, JAXBContext> contexts = new HashMap<Class, JAXBContext>();
 
     public XmlConverter getJaxbConverter() {
         if (jaxbConverter == null) {
@@ -51,15 +52,14 @@
     }
 
     @Converter
-    public static JAXBSource toSource(@HasAnnotation(XmlRootElement.class)Object value) throws JAXBException {
-        JAXBContext context = createJaxbContext(value);
+    public JAXBSource toSource(@HasAnnotation(XmlRootElement.class)Object value) throws JAXBException {
+        JAXBContext context = getJaxbContext(value);
         return new JAXBSource(context, value);
     }
 
     @Converter
-    public Document toDocument(
-            @HasAnnotation(XmlRootElement.class)Object value) throws JAXBException, ParserConfigurationException {
-        JAXBContext context = createJaxbContext(value);
+    public Document toDocument(@HasAnnotation(XmlRootElement.class)Object value) throws JAXBException, ParserConfigurationException {
+        JAXBContext context = getJaxbContext(value);
         Marshaller marshaller = context.createMarshaller();
 
         Document doc = getJaxbConverter().createDocument();
@@ -79,18 +79,20 @@
         return answer;
     }
 
-    protected static JAXBContext createJaxbContext(Object value) throws JAXBException {
+    private synchronized JAXBContext getJaxbContext(Object value) throws JAXBException {
+        JAXBContext context = contexts.get(value.getClass());
+        if (context == null) {
+            context = createJaxbContext(value);
+            contexts.put(value.getClass(), context);
+        }
+        return context;
+    }
+
+    private JAXBContext createJaxbContext(Object value) throws JAXBException {
         if (value == null) {
             throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
         }
-        JAXBContext context = JAXBContext.newInstance(value.getClass());
-        return context;
+        return JAXBContext.newInstance(value.getClass());
     }
 
-//    public void write(OutputStream out, Object value) throws JAXBException {
-//        JAXBContext context = JAXBContext.newInstance(value.getClass());
-//        Marshaller marshaller = context.createMarshaller();
-//        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
-//        marshaller.marshal(value, out);
-//    }
 }