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 2016/03/07 21:18:08 UTC
camel git commit: CAMEL-9649: Also allow this for marshal to support
ObjectFactoryinstead of POJO must be annotated.
Repository: camel
Updated Branches:
refs/heads/master 7d751ccb8 -> 87affd2b7
CAMEL-9649: Also allow this for marshal to support ObjectFactoryinstead of POJO must be annotated.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/87affd2b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/87affd2b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/87affd2b
Branch: refs/heads/master
Commit: 87affd2b720a7f4efc00c3337d36ee323c2eaf07
Parents: 7d751cc
Author: Claus Ibsen <da...@apache.org>
Authored: Mon Mar 7 21:13:41 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon Mar 7 21:14:56 2016 +0100
----------------------------------------------------------------------
.../converter/jaxb/FallbackTypeConverter.java | 43 +-----------
.../camel/converter/jaxb/JaxbDataFormat.java | 38 +++++++++--
.../apache/camel/converter/jaxb/JaxbHelper.java | 69 ++++++++++++++++++++
3 files changed, 103 insertions(+), 47 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/87affd2b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
index 960bfb6..3155f1e 100644
--- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
@@ -34,7 +34,6 @@ import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
-import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLStreamException;
@@ -165,7 +164,7 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert
}
protected <T> boolean isJaxbType(Class<T> type) {
- return hasXmlRootElement(type) || (getJaxbElementFactoryMethod(type) != null);
+ return hasXmlRootElement(type) || JaxbHelper.getJaxbElementFactoryMethod(camelContext, type) != null;
}
private <T> T castJaxbType(Object o, Class<T> type) {
@@ -262,9 +261,9 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert
}
Object toMarshall = value;
if (!hasXmlRootElement(value.getClass())) {
- Method m = getJaxbElementFactoryMethod(value.getClass());
+ Method m = JaxbHelper.getJaxbElementFactoryMethod(camelContext, value.getClass());
try {
- toMarshall = m.invoke(getObjectFactory(value.getClass()).newInstance(), value);
+ toMarshall = m.invoke(JaxbHelper.getObjectFactory(camelContext, value.getClass()).newInstance(), value);
} catch (Exception e) {
LOG.error("Unable to create JAXBElement object for type {} due to {}", value.getClass().getName(), e);
}
@@ -344,40 +343,4 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert
return !StreamCache.class.isAssignableFrom(type);
}
- private <T> Class getObjectFactory(Class<T> type) throws ClassNotFoundException {
- Class<?> c = null;
- if (type.getPackage() != null) {
- String objectFactoryClassName = type.getPackage().getName() + ".ObjectFactory";
- c = camelContext.getClassResolver().resolveClass(objectFactoryClassName);
- }
- if (c == null) {
- throw new ClassNotFoundException(String.format("ObjectFactory for type %s was not found", type.getName()));
- } else {
- return c;
- }
- }
-
- private <T> Method getJaxbElementFactoryMethod(Class<T> type) {
- Method factoryMethod = null;
- try {
- for (Method m : getObjectFactory(type).getMethods()) {
- final XmlElementDecl a = m.getAnnotation(XmlElementDecl.class);
- if (a == null) {
- continue;
- }
- final Class<?>[] parameters = m.getParameterTypes();
- if (parameters.length == 1 && parameters[0].isAssignableFrom(type)) {
- if (factoryMethod != null) {
- throw new IllegalStateException("There are several possible XML schema mappings for class " + type.getName());
- } else {
- factoryMethod = m;
- }
- }
- }
- } catch (ClassNotFoundException e) {
- LOG.debug(e.getMessage(), e);
- }
- return factoryMethod;
- }
-
}
http://git-wip-us.apache.org/repos/asf/camel/blob/87affd2b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
index 3436561..990fdf5 100644
--- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
@@ -63,7 +64,6 @@ import org.apache.camel.util.ResourceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
/**
* A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat})
* using JAXB2 to marshal to and from XML
@@ -156,13 +156,13 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
void marshal(Exchange exchange, Object graph, OutputStream stream, Marshaller marshaller)
throws XMLStreamException, JAXBException, NoTypeConversionAvailableException, IOException, InvalidPayloadException {
- Object e = graph;
+ Object element = graph;
if (partialClass != null && getPartNamespace() != null) {
- e = new JAXBElement<Object>(getPartNamespace(), partialClass, graph);
+ element = new JAXBElement<Object>(getPartNamespace(), partialClass, graph);
}
// only marshal if its possible
- if (introspector.isElement(e)) {
+ if (introspector.isElement(element)) {
if (asXmlStreamWriter(exchange)) {
XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
if (needFiltering(exchange)) {
@@ -171,11 +171,35 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo
if (xmlStreamWriterWrapper != null) {
writer = xmlStreamWriterWrapper.wrapWriter(writer);
}
- marshaller.marshal(e, writer);
+ marshaller.marshal(element, writer);
} else {
- marshaller.marshal(e, stream);
+ marshaller.marshal(element, stream);
+ }
+ return;
+ } else if (element != null) {
+ Method m = JaxbHelper.getJaxbElementFactoryMethod(camelContext, element.getClass());
+ try {
+ Object toMarshall = m.invoke(JaxbHelper.getObjectFactory(camelContext, element.getClass()).newInstance(), element);
+ if (asXmlStreamWriter(exchange)) {
+ XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream);
+ if (needFiltering(exchange)) {
+ writer = new FilteringXmlStreamWriter(writer);
+ }
+ if (xmlStreamWriterWrapper != null) {
+ writer = xmlStreamWriterWrapper.wrapWriter(writer);
+ }
+ marshaller.marshal(toMarshall, writer);
+ } else {
+ marshaller.marshal(toMarshall, stream);
+ }
+ return;
+ } catch (Exception e) {
+ LOG.debug("Unable to create JAXBElement object for type {} due to {}", element.getClass().getName(), e);
}
- } else if (!mustBeJAXBElement) {
+ }
+
+ // cannot marshal
+ if (!mustBeJAXBElement) {
// write the graph as is to the output stream
if (LOG.isDebugEnabled()) {
LOG.debug("Attempt to marshalling non JAXBElement with type {} as InputStream", ObjectHelper.classCanonicalName(graph));
http://git-wip-us.apache.org/repos/asf/camel/blob/87affd2b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java
new file mode 100644
index 0000000..9fe2ad6
--- /dev/null
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java
@@ -0,0 +1,69 @@
+/**
+ * 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.camel.converter.jaxb;
+
+import java.lang.reflect.Method;
+import javax.xml.bind.annotation.XmlElementDecl;
+
+import org.apache.camel.CamelContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class JaxbHelper {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JaxbHelper.class);
+
+ private JaxbHelper() {
+ }
+
+ public static <T> Method getJaxbElementFactoryMethod(CamelContext camelContext, Class<T> type) {
+ Method factoryMethod = null;
+ try {
+ for (Method m : getObjectFactory(camelContext, type).getMethods()) {
+ final XmlElementDecl a = m.getAnnotation(XmlElementDecl.class);
+ if (a == null) {
+ continue;
+ }
+ final Class<?>[] parameters = m.getParameterTypes();
+ if (parameters.length == 1 && parameters[0].isAssignableFrom(type)) {
+ if (factoryMethod != null) {
+ throw new IllegalStateException("There are several possible XML schema mappings for class " + type.getName());
+ } else {
+ factoryMethod = m;
+ }
+ }
+ }
+ } catch (ClassNotFoundException e) {
+ LOG.debug(e.getMessage(), e);
+ }
+ return factoryMethod;
+ }
+
+ public static <T> Class getObjectFactory(CamelContext camelContext, Class<T> type) throws ClassNotFoundException {
+ Class<?> c = null;
+ if (type.getPackage() != null) {
+ String objectFactoryClassName = type.getPackage().getName() + ".ObjectFactory";
+ c = camelContext.getClassResolver().resolveClass(objectFactoryClassName);
+ }
+ if (c == null) {
+ throw new ClassNotFoundException(String.format("ObjectFactory for type %s was not found", type.getName()));
+ } else {
+ return c;
+ }
+ }
+
+}