You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2015/03/11 02:45:10 UTC
camel git commit: [CAMEL-8468] Usage of camel-xstream depends on TCCL
Repository: camel
Updated Branches:
refs/heads/master 08e4143c7 -> 88f7a86b7
[CAMEL-8468] Usage of camel-xstream 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/88f7a86b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/88f7a86b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/88f7a86b
Branch: refs/heads/master
Commit: 88f7a86b70f25b2899d2b42a4d07d04892a67fdc
Parents: 08e4143
Author: Thomas Diesler <th...@jboss.com>
Authored: Tue Mar 10 09:40:44 2015 +0100
Committer: Willem Jiang <wi...@gmail.com>
Committed: Wed Mar 11 09:42:19 2015 +0800
----------------------------------------------------------------------
.../xstream/AbstractXStreamWrapper.java | 45 ++++++++++++++++----
.../dataformat/xstream/JsonDataFormat.java | 12 +++---
2 files changed, 42 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/88f7a86b/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java
----------------------------------------------------------------------
diff --git a/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java b/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java
index a3f5fab..b4e6423 100644
--- a/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java
+++ b/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/AbstractXStreamWrapper.java
@@ -31,6 +31,8 @@ import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+
+import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.converter.jaxp.StaxConverter;
import org.apache.camel.spi.ClassResolver;
@@ -42,7 +44,7 @@ import org.apache.camel.util.ObjectHelper;
* ({@link DataFormat}) interface which leverage the XStream library for XML or JSON's marshaling and unmarshaling
*/
public abstract class AbstractXStreamWrapper implements DataFormat {
-
+
private XStream xstream;
private HierarchicalStreamDriver xstreamDriver;
private StaxConverter staxConverter;
@@ -54,7 +56,7 @@ public abstract class AbstractXStreamWrapper implements DataFormat {
public AbstractXStreamWrapper() {
}
-
+
public AbstractXStreamWrapper(XStream xstream) {
this.xstream = xstream;
}
@@ -73,11 +75,32 @@ public abstract class AbstractXStreamWrapper implements DataFormat {
return xstream;
}
+ /**
+ * Resolves the XStream instance to be used by this data format. If XStream is not explicitly set, new instance will
+ * be created and cached.
+ *
+ * @param context to be used during a configuration of the XStream instance
+ * @return XStream instance used by this data format.
+ */
+ public XStream getXStream(CamelContext context) {
+ if (xstream == null) {
+ xstream = createXStream(context.getClassResolver(), context.getApplicationContextClassLoader());
+ }
+ return xstream;
+ }
+
public void setXStream(XStream xstream) {
this.xstream = xstream;
}
+ /**
+ * @deprecated Use {@link #createXStream(ClassResolver, ClassLoader)}
+ */
protected XStream createXStream(ClassResolver resolver) {
+ return createXStream(resolver, null);
+ }
+
+ protected XStream createXStream(ClassResolver resolver, ClassLoader classLoader) {
if (xstreamDriver != null) {
xstream = new XStream(xstreamDriver);
} else {
@@ -88,6 +111,10 @@ public abstract class AbstractXStreamWrapper implements DataFormat {
xstream.setMode(getModeFromString(mode));
}
+ if (classLoader != null) {
+ xstream.setClassLoader(classLoader);
+ }
+
try {
if (this.implicitCollections != null) {
for (Entry<String, String[]> entry : this.implicitCollections.entrySet()) {
@@ -141,14 +168,14 @@ public abstract class AbstractXStreamWrapper implements DataFormat {
xstream.registerConverter(converter);
}
}
-
+
} catch (Exception e) {
throw new RuntimeException("Unable to build XStream instance", e);
}
return xstream;
- }
-
+ }
+
protected int getModeFromString(String modeString) {
int result;
if ("NO_REFERENCES".equalsIgnoreCase(modeString)) {
@@ -219,11 +246,11 @@ public abstract class AbstractXStreamWrapper implements DataFormat {
public void setXstreamDriver(HierarchicalStreamDriver xstreamDriver) {
this.xstreamDriver = xstreamDriver;
}
-
+
public String getMode() {
return mode;
}
-
+
public void setMode(String mode) {
this.mode = mode;
}
@@ -239,7 +266,7 @@ public abstract class AbstractXStreamWrapper implements DataFormat {
public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
HierarchicalStreamWriter writer = createHierarchicalStreamWriter(exchange, body, stream);
try {
- getXStream(exchange.getContext().getClassResolver()).marshal(body, writer);
+ getXStream(exchange.getContext()).marshal(body, writer);
} finally {
writer.close();
}
@@ -248,7 +275,7 @@ public abstract class AbstractXStreamWrapper implements DataFormat {
public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
HierarchicalStreamReader reader = createHierarchicalStreamReader(exchange, stream);
try {
- return getXStream(exchange.getContext().getClassResolver()).unmarshal(reader);
+ return getXStream(exchange.getContext()).unmarshal(reader);
} finally {
reader.close();
}
http://git-wip-us.apache.org/repos/asf/camel/blob/88f7a86b/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/JsonDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/JsonDataFormat.java b/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/JsonDataFormat.java
index 238bef7..7f6d714 100644
--- a/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/JsonDataFormat.java
+++ b/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/JsonDataFormat.java
@@ -39,13 +39,13 @@ import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
* A <a href="http://camel.apache.org/data-format.html">data format</a>
* ({@link DataFormat}) using XStream and Jettison to marshal to and from JSON
*
- * @version
+ * @version
*/
public class JsonDataFormat extends AbstractXStreamWrapper {
private final MappedXMLOutputFactory mof;
private final MappedXMLInputFactory mif;
-
+
public JsonDataFormat() {
final Map<?, ?> nstjsons = new HashMap<Object, Object>();
mof = new MappedXMLOutputFactory(nstjsons);
@@ -53,8 +53,8 @@ public class JsonDataFormat extends AbstractXStreamWrapper {
}
@Override
- protected XStream createXStream(ClassResolver resolver) {
- XStream xs = super.createXStream(resolver);
+ protected XStream createXStream(ClassResolver resolver, ClassLoader classLoader) {
+ XStream xs = super.createXStream(resolver, classLoader);
if (getMode() != null) {
xs.setMode(getModeFromString(getMode()));
} else {
@@ -63,11 +63,11 @@ public class JsonDataFormat extends AbstractXStreamWrapper {
return xs;
}
- protected HierarchicalStreamWriter createHierarchicalStreamWriter(Exchange exchange, Object body, OutputStream stream) throws XMLStreamException {
+ protected HierarchicalStreamWriter createHierarchicalStreamWriter(Exchange exchange, Object body, OutputStream stream) throws XMLStreamException {
return new StaxWriter(new QNameMap(), mof.createXMLStreamWriter(stream));
}
- protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
+ protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
return new StaxReader(new QNameMap(), mif.createXMLStreamReader(stream));
}
}