You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2012/07/31 23:45:27 UTC

svn commit: r1367792 - in /webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src: main/java/org/apache/axiom/om/ds/jaxb/ test/java/org/apache/axiom/om/ds/jaxb/

Author: veithen
Date: Tue Jul 31 21:45:27 2012
New Revision: 1367792

URL: http://svn.apache.org/viewvc?rev=1367792&view=rev
Log:
Improved error handling in JAXBOMDataSource to avoid meaningless exceptions such as the one shown in AXIS2-4721 (which is almost certainly caused by an I/O error).

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/ExceptionXMLStreamWriterWrapper.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSource.java
    webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSource.java?rev=1367792&r1=1367791&r2=1367792&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSource.java Tue Jul 31 21:45:27 2012
@@ -101,7 +101,13 @@ public class JAXBOMDataSource extends Ab
             }
             marshaller.marshal(object, writer);
         } catch (JAXBException ex) {
-            throw new XMLStreamException("Error marshalling JAXB object", ex);
+            // Try to propagate the original exception if possible (to avoid unreadable stacktraces)
+            Throwable cause = ex.getCause();
+            if (cause instanceof XMLStreamException) {
+                throw (XMLStreamException)cause;
+            } else {
+                throw new XMLStreamException("Error marshalling JAXB object", ex);
+            }
         }
     }
 

Added: webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/ExceptionXMLStreamWriterWrapper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/ExceptionXMLStreamWriterWrapper.java?rev=1367792&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/ExceptionXMLStreamWriterWrapper.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/ExceptionXMLStreamWriterWrapper.java Tue Jul 31 21:45:27 2012
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.om.ds.jaxb;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.util.stax.wrapper.XMLStreamWriterWrapper;
+
+public class ExceptionXMLStreamWriterWrapper extends XMLStreamWriterWrapper {
+    private final XMLStreamException exception;
+    
+    public ExceptionXMLStreamWriterWrapper(XMLStreamWriter parent, XMLStreamException exception) {
+        super(parent);
+        this.exception = exception;
+    }
+
+    @Override
+    public void writeCharacters(String text) throws XMLStreamException {
+        exception.fillInStackTrace();
+        throw exception;
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/ExceptionXMLStreamWriterWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java?rev=1367792&r1=1367791&r2=1367792&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java Tue Jul 31 21:45:27 2012
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -29,7 +30,9 @@ import java.io.ByteArrayOutputStream;
 import javax.activation.DataHandler;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
 import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.axiom.attachments.Attachments;
@@ -197,4 +200,25 @@ public class JAXBOMDataSourceTest {
         // provided by JAXBOMDataSource with the actual name of the element
         element.getFirstOMChild();
     }
+    
+    /**
+     * Tests that an {@link XMLStreamException} thrown by the {@link XMLStreamWriter} during
+     * serialization is propagated without being wrapped. Note that this implies that the data must
+     * unwrap {@link JAXBException} to extract the cause.
+     */
+    @Test
+    public void testExceptionDuringSerialization() throws Exception {
+        OMFactory omFactory = OMAbstractFactory.getOMFactory();
+        JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
+        DocumentBean object = new DocumentBean();
+        object.setId("test");
+        OMSourcedElement element = omFactory.createOMElement(new JAXBOMDataSource(context, object));
+        XMLStreamException exception = new XMLStreamException("TEST");
+        try {
+            element.serialize(new ExceptionXMLStreamWriterWrapper(StAXUtils.createXMLStreamWriter(new ByteArrayOutputStream()), exception));
+            fail("Expected XMLStreamException");
+        } catch (XMLStreamException ex) {
+            assertSame(exception, ex);
+        }
+    }
 }