CAMEL-8547 Usage of camel-xmlbeans depends on TCCL
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0d7cfb62 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0d7cfb62 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0d7cfb62 Branch: refs/heads/master Commit: 0d7cfb6212c184c52987b18c51b97d7afd58f02e Parents: 07587b9 Author: Willem Jiang <[email protected]> Authored: Thu Mar 26 16:23:51 2015 +0800 Committer: Willem Jiang <[email protected]> Committed: Thu Mar 26 16:41:44 2015 +0800 ---------------------------------------------------------------------- .../org/apache/camel/util/ObjectHelper.java | 38 ++++++++++ .../converter/xmlbeans/XmlBeansConverter.java | 73 ++++++++++++++------ .../converter/xmlbeans/XmlBeansDataFormat.java | 24 +++++-- .../xmlbeans/XmlBeansConverterTest.java | 42 +++++------ 4 files changed, 128 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java index 4c1d22b..3aba156 100644 --- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -43,6 +43,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Properties; import java.util.Scanner; +import java.util.concurrent.Callable; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -1815,4 +1816,41 @@ public final class ObjectHelper { // value must be a number return value.equals(Float.NaN) || value.equals(Double.NaN); } + + /** + * Calling the Callable with the setting of TCCL with the camel context application classloader; + * + * @param call the Callable instance + * @param exchange the exchange + * @return the result of Callable return + */ + public static Object callWithTCCL(Callable<?> call, Exchange exchange) throws Exception { + ClassLoader apcl = null; + if (exchange != null && exchange.getContext() != null) { + apcl = exchange.getContext().getApplicationContextClassLoader(); + } + return callWithTCCL(call, apcl); + } + + /** + * Calling the Callable with the setting of TCCL with a given classloader; + * + * @param call the Callable instance + * @param the exchange + * @return the result of Callable return + */ + public static Object callWithTCCL(Callable<?> call, ClassLoader classloader) throws Exception { + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + try { + if (classloader != null) { + Thread.currentThread().setContextClassLoader(classloader); + } + return call.call(); + } finally { + if (tccl != null) { + Thread.currentThread().setContextClassLoader(tccl); + } + } + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java ---------------------------------------------------------------------- diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java index 8068e86..3b034b8 100644 --- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java +++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java @@ -17,23 +17,25 @@ package org.apache.camel.converter.xmlbeans; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.nio.ByteBuffer; +import java.util.concurrent.Callable; + import javax.xml.transform.Source; import org.w3c.dom.Node; import org.apache.camel.Converter; import org.apache.camel.Exchange; -import org.apache.camel.NoTypeConversionAvailableException; import org.apache.camel.converter.IOConverter; import org.apache.camel.converter.NIOConverter; -import org.apache.xmlbeans.XmlException; +import org.apache.camel.util.ObjectHelper; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.impl.piccolo.xml.XMLStreamReader; + + /** * A <a href="http://camel.apache.org/type-coverter.html">Type Converter</a> * of XMLBeans objects @@ -43,50 +45,75 @@ public final class XmlBeansConverter { private XmlBeansConverter() { } - + @Converter - public static XmlObject toXmlObject(File value) throws IOException, XmlException { - return XmlObject.Factory.parse(value); + public static XmlObject toXmlObject(final File value, Exchange exchange) throws Exception { + return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() { + public XmlObject call() throws Exception { + return XmlObject.Factory.parse(value); + } + }, exchange); + } @Converter - public static XmlObject toXmlObject(Reader value) throws IOException, XmlException { - return XmlObject.Factory.parse(value); + public static XmlObject toXmlObject(final Reader value, Exchange exchange) throws Exception { + return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() { + public XmlObject call() throws Exception { + return XmlObject.Factory.parse(value); + } + }, exchange); } @Converter - public static XmlObject toXmlObject(Node value) throws IOException, XmlException { - return XmlObject.Factory.parse(value); + public static XmlObject toXmlObject(final Node value, Exchange exchange) throws Exception { + return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() { + public XmlObject call() throws Exception { + return XmlObject.Factory.parse(value); + } + }, exchange); } @Converter - public static XmlObject toXmlObject(InputStream value) throws IOException, XmlException { - return XmlObject.Factory.parse(value); + public static XmlObject toXmlObject(final InputStream value, Exchange exchange) throws Exception { + return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() { + public XmlObject call() throws Exception { + return XmlObject.Factory.parse(value); + } + }, exchange); } @Converter - public static XmlObject toXmlObject(String value, Exchange exchange) throws IOException, XmlException { - return toXmlObject(IOConverter.toInputStream(value, exchange)); + public static XmlObject toXmlObject(String value, Exchange exchange) throws Exception { + return toXmlObject(IOConverter.toInputStream(value, exchange), exchange); } @Converter - public static XmlObject toXmlObject(byte[] value) throws IOException, XmlException { - return toXmlObject(IOConverter.toInputStream(value)); + public static XmlObject toXmlObject(byte[] value, Exchange exchange) throws Exception { + return toXmlObject(IOConverter.toInputStream(value), exchange); } @Converter - public static XmlObject toXmlObject(ByteBuffer value) throws IOException, XmlException { - return toXmlObject(NIOConverter.toInputStream(value)); + public static XmlObject toXmlObject(ByteBuffer value, Exchange exchange) throws Exception { + return toXmlObject(NIOConverter.toInputStream(value), exchange); } @Converter - public static XmlObject toXmlObject(XMLStreamReader value) throws IOException, XmlException { - return XmlObject.Factory.parse(value); + public static XmlObject toXmlObject(final XMLStreamReader value, Exchange exchange) throws Exception { + return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() { + public XmlObject call() throws Exception { + return XmlObject.Factory.parse(value); + } + }, exchange); } @Converter - public static XmlObject toXmlObject(Source value, Exchange exchange) throws IOException, XmlException, NoTypeConversionAvailableException { - Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value); - return XmlObject.Factory.parse(reader); + public static XmlObject toXmlObject(Source value, Exchange exchange) throws Exception { + final Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value); + return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() { + public XmlObject call() throws Exception { + return XmlObject.Factory.parse(reader); + } + }, exchange); } } http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java index 6559813..41dd0ff 100644 --- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java +++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java @@ -18,10 +18,12 @@ package org.apache.camel.converter.xmlbeans; import java.io.InputStream; import java.io.OutputStream; +import java.util.concurrent.Callable; import org.apache.camel.Exchange; import org.apache.camel.spi.DataFormat; import org.apache.camel.util.ExchangeHelper; +import org.apache.camel.util.ObjectHelper; import org.apache.xmlbeans.XmlObject; /** @@ -30,12 +32,24 @@ import org.apache.xmlbeans.XmlObject; */ public class XmlBeansDataFormat implements DataFormat { - public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception { - XmlObject object = ExchangeHelper.convertToMandatoryType(exchange, XmlObject.class, body); - object.save(stream); + public void marshal(final Exchange exchange, final Object body, final OutputStream stream) throws Exception { + ObjectHelper.callWithTCCL(new Callable<Void>() { + @Override + public Void call() throws Exception { + XmlObject object = ExchangeHelper.convertToMandatoryType(exchange, XmlObject.class, body); + object.save(stream); + return null; + } + }, exchange); + } - public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { - return XmlObject.Factory.parse(stream); + public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { + return ObjectHelper.callWithTCCL(new Callable<Object>() { + @Override + public Object call() throws Exception { + return XmlObject.Factory.parse(stream); + } + }, exchange); } } http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java index 86aac74..3638268 100644 --- a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java +++ b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java @@ -20,26 +20,21 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; -import java.io.IOException; import java.io.StringReader; import java.nio.ByteBuffer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.InputSource; -import org.xml.sax.SAXException; import org.apache.camel.BytesSource; import org.apache.camel.Exchange; import org.apache.camel.Message; -import org.apache.camel.NoTypeConversionAvailableException; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultExchange; import org.apache.camel.test.junit4.CamelTestSupport; -import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.impl.piccolo.xml.XMLStreamReader; import org.junit.Test; @@ -64,19 +59,21 @@ public class XmlBeansConverterTest extends CamelTestSupport { } @Test - public void toXmlObjectFromFile() throws IOException, XmlException { - XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml")); + public void toXmlObjectFromFile() throws Exception { + XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml"), + new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromReader() throws IOException, XmlException { - XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml")); + public void toXmlObjectFromReader() throws Exception { + XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml"), + new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromNode() throws IOException, XmlException, ParserConfigurationException, SAXException { + public void toXmlObjectFromNode() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); @@ -84,42 +81,45 @@ public class XmlBeansConverterTest extends CamelTestSupport { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(PAYLOAD))); - XmlObject result = XmlBeansConverter.toXmlObject(document); + XmlObject result = XmlBeansConverter.toXmlObject(document, + new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromInputStream() throws IOException, XmlException { - XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml")); + public void toXmlObjectFromInputStream() throws Exception { + XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml"), + new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromString() throws IOException, XmlException { + public void toXmlObjectFromString() throws Exception { XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD, new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromByteArray() throws IOException, XmlException { - XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes()); + public void toXmlObjectFromByteArray() throws Exception { + XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes(), new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromByteBuffer() throws IOException, XmlException { - XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes())); + public void toXmlObjectFromByteBuffer() throws Exception { + XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes()), new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromXMLStreamReader() throws IOException, XmlException { - XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false)); + public void toXmlObjectFromXMLStreamReader() throws Exception { + XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false), + new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); } @Test - public void toXmlObjectFromSource() throws IOException, XmlException, NoTypeConversionAvailableException { + public void toXmlObjectFromSource() throws Exception { XmlObject result = XmlBeansConverter.toXmlObject(new BytesSource(PAYLOAD.getBytes()), new DefaultExchange(new DefaultCamelContext())); assertBuyStocks(result); }
