Author: davsclaus
Date: Thu Oct 16 13:07:48 2008
New Revision: 705344

URL: http://svn.apache.org/viewvc?rev=705344&view=rev
Log:
CAMEL-980: Should close stream during unmarshal in case of exception then eg a 
file stream is open and the file can then not be deleted.

Modified:
    
activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
    
activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
    
activemq/camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties

Modified: 
activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java?rev=705344&r1=705343&r2=705344&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
 (original)
+++ 
activemq/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
 Thu Oct 16 13:07:48 2008
@@ -16,10 +16,7 @@
  */
 package org.apache.camel.converter.jaxb;
 
-import java.io.InputStream;
-import java.io.Reader;
-import java.io.StringReader;
-import java.io.StringWriter;
+import java.io.*;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
@@ -34,6 +31,7 @@
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.spi.TypeConverterAware;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -93,31 +91,29 @@
         if (parentTypeConverter != null) {
             InputStream inputStream = 
parentTypeConverter.convertTo(InputStream.class, value);
             if (inputStream != null) {
-                Object unmarshalled = unmarshaller.unmarshal(inputStream);
+                Object unmarshalled = unmarshal(unmarshaller, inputStream);
                 return type.cast(unmarshalled);
             }
             Reader reader = parentTypeConverter.convertTo(Reader.class, value);
             if (reader != null) {
-                Object unmarshalled = unmarshaller.unmarshal(reader);
+                Object unmarshalled = unmarshal(unmarshaller, reader);
                 return type.cast(unmarshalled);
             }
             Source source = parentTypeConverter.convertTo(Source.class, value);
             if (source != null) {
-                Object unmarshalled = unmarshaller.unmarshal(source);
+                Object unmarshalled = unmarshal(unmarshaller, source);
                 return type.cast(unmarshalled);
             }
         }
+
         if (value instanceof String) {
             value = new StringReader((String) value);
         }
-        if (value instanceof InputStream) {
-            Object unmarshalled = unmarshaller.unmarshal((InputStream) value);
-            return type.cast(unmarshalled);
-        }
-        if (value instanceof Reader) {
-            Object unmarshalled = unmarshaller.unmarshal((Reader) value);
+        if (value instanceof InputStream || value instanceof Reader) {
+            Object unmarshalled = unmarshal(unmarshaller, value);
             return type.cast(unmarshalled);
         }
+
         return null;
     }
 
@@ -143,6 +139,31 @@
         return null;
     }
 
+    /**
+     * Unmarshals
+     *
+     * @param unmarshaller  the unmarshaller
+     * @param value  the stream to unarmashal (will close it after use, also 
if exception is thrown)
+     * @return  the value
+     * @throws JAXBException is thrown if an exception occur while 
unmarshalling
+     */
+    protected Object unmarshal(Unmarshaller unmarshaller, Object value) throws 
JAXBException {
+        try {
+            if (value instanceof InputStream) {
+                return unmarshaller.unmarshal((InputStream) value);
+            } else if (value instanceof Reader) {
+                return unmarshaller.unmarshal((Reader) value);
+            } else if (value instanceof Source) {
+                return unmarshaller.unmarshal((Source) value);
+            }
+        } finally {
+            if (value instanceof Closeable) {
+                ObjectHelper.close((Closeable) value, "Unmarshalling", LOG);
+            }
+        }
+        return null;
+    }
+
     protected <T> JAXBContext createContext(Class<T> type) throws 
JAXBException {
         JAXBContext context = JAXBContext.newInstance(type);
         return context;

Modified: 
activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java?rev=705344&r1=705343&r2=705344&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
 (original)
+++ 
activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/JAXBConvertTest.java
 Thu Oct 16 13:07:48 2008
@@ -16,9 +16,13 @@
  */
 package org.apache.camel.example;
 
-import junit.framework.TestCase;
+import javax.xml.bind.UnmarshalException;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 
+import junit.framework.TestCase;
 import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.impl.DefaultCamelContext;
 
@@ -37,4 +41,26 @@
         assertEquals("name", "foo", purchaseOrder.getName());
         assertEquals("amount", 123.45, purchaseOrder.getAmount());
     }
+
+    public void testStreamShouldBeClosed() throws Exception {
+        String data = "<purchaseOrder name='foo' amount='123.45' 
price='2.22'/>";
+        InputStream is = new ByteArrayInputStream(data.getBytes());
+
+        PurchaseOrder purchaseOrder = converter.convertTo(PurchaseOrder.class, 
is);
+        assertNotNull(purchaseOrder);
+        assertEquals(-1, is.read());
+    }
+
+    public void testStreamShouldBeClosedEvenForException() throws Exception {
+        String data = "<errorOrder name='foo' amount='123.45' price='2.22'/>";
+        InputStream is = new ByteArrayInputStream(data.getBytes());
+
+        try {
+            converter.convertTo(PurchaseOrder.class, is);
+        } catch (RuntimeCamelException e) {
+            assertTrue(e.getCause() instanceof UnmarshalException);
+        }
+        assertEquals(-1, is.read());
+    }
+
 }

Modified: 
activemq/camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties?rev=705344&r1=705343&r2=705344&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties 
(original)
+++ 
activemq/camel/trunk/components/camel-jaxb/src/test/resources/log4j.properties 
Thu Oct 16 13:07:48 2008
@@ -21,7 +21,7 @@
 log4j.rootLogger=INFO, file
 
 # uncomment this to turn on debug of camel
-#log4j.logger.org.apache.camel=DEBUG
+log4j.logger.org.apache.camel=DEBUG
 
 log4j.logger.org.apache.activemq.spring=WARN
 
@@ -31,8 +31,8 @@
 log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} 
- %m%n
 
 # File appender
-log4j.appender.out=org.apache.log4j.FileAppender
-log4j.appender.out.layout=org.apache.log4j.PatternLayout
-log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - 
%m%n
-log4j.appender.out.file=target/camel-jaxb-test.log
-log4j.appender.out.append=true
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - 
%m%n
+log4j.appender.file.file=target/camel-jaxb-test.log
+log4j.appender.file.append=true


Reply via email to