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:15:27 UTC
svn commit: r741848 -
/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
Author: davsclaus
Date: Sat Feb 7 07:15:26 2009
New Revision: 741848
URL: http://svn.apache.org/viewvc?rev=741848&view=rev
Log:
CAMEL-1321: Avoiding creating new JAXB instances for the same classes. To avoid running out of memory.
Modified:
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
Modified: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java?rev=741848&r1=741847&r2=741848&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java (original)
+++ camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConverter.java Sat Feb 7 07:15:26 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);
-// }
}