camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: [CAMEL-9227] No type converter available to convert from Bean to List
Date Sat, 17 Oct 2015 06:50:06 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.16.x aa7371e69 -> eb9a9e332


[CAMEL-9227] No type converter available to convert from Bean to List


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8496831e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8496831e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8496831e

Branch: refs/heads/camel-2.16.x
Commit: 8496831ee8b6118da192c24b17f011ea035b2198
Parents: aa7371e
Author: Thomas Diesler <thomas.diesler@jboss.com>
Authored: Fri Oct 16 12:02:24 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Oct 17 08:51:43 2015 +0200

----------------------------------------------------------------------
 .../converter/dozer/DozerTypeConverter.java     | 61 ++++++-------
 .../converter/dozer/DozerBeanMappingTest.java   | 94 ++++++++++++++++++++
 .../camel/converter/dozer/model/CustomerA.java  | 66 ++++++++++++++
 .../camel/converter/dozer/model/CustomerB.java  | 84 +++++++++++++++++
 .../resources/bean-to-bean-dozer-mappings.xml   | 17 ++++
 .../resources/bean-to-map-dozer-mappings.xml    | 17 ++++
 6 files changed, 307 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8496831e/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverter.java
b/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverter.java
index e5114e4..0505a64 100644
--- a/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverter.java
+++ b/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverter.java
@@ -40,7 +40,7 @@ import org.slf4j.LoggerFactory;
 public class DozerTypeConverter extends TypeConverterSupport {
 
     private static final Logger LOG = LoggerFactory.getLogger(DozerTypeConverter.class);
-    
+
     private final DozerBeanMapper mapper;
 
     public DozerTypeConverter(DozerBeanMapper mapper) {
@@ -53,42 +53,39 @@ public class DozerTypeConverter extends TypeConverterSupport {
 
     @Override
     public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws
TypeConversionException {
-        // find the map id, so we can provide that when trying to map from source to destination
-        String mapId = null;
-        if (value != null) {
-            Class<?> sourceType = value.getClass();
-            Class<?> destType = type;
-            ClassMappingMetadata metadata = mapper.getMappingMetadata().getClassMapping(sourceType,
destType);
-            if (metadata != null) {
-                mapId = metadata.getMapId();
+
+        CamelContext context = exchange != null ? exchange.getContext() : null;
+        ClassLoader appcl = context != null ? context.getApplicationContextClassLoader()
: null;
+
+        T result;
+
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+        try {
+            if (appcl != null && appcl != tccl) {
+                LOG.debug("Switching TCCL to: {}", appcl);
+                Thread.currentThread().setContextClassLoader(appcl);
             }
-        }
 
-        // if the exchange is null, we have no chance to ensure that the TCCL is the one
from the CamelContext
-        if (exchange == null) {
-            return mapper.map(value, type, mapId);
-        }
-        
-        T answer = null;
+            // find the map id, so we can provide that when trying to map from source to
destination
+            String mapId = null;
+            if (value != null) {
+                Class<?> sourceType = value.getClass();
+                Class<?> destType = type;
+                ClassMappingMetadata metadata = mapper.getMappingMetadata().getClassMapping(sourceType,
destType);
+                if (metadata != null) {
+                    mapId = metadata.getMapId();
+                }
+            }
 
-        ClassLoader prev = Thread.currentThread().getContextClassLoader();
-        ClassLoader contextCl = exchange.getContext().getApplicationContextClassLoader();
-        if (contextCl != null) {
-            // otherwise, we ensure that the TCCL is the correct one
-            LOG.debug("Switching TCCL to: {}.", contextCl);
-            try {
-                Thread.currentThread().setContextClassLoader(contextCl);
-                answer = mapper.map(value, type, mapId);
-            } finally {
-                LOG.debug("Restored TCCL to: {}.", prev);
-                Thread.currentThread().setContextClassLoader(prev);
+            result = mapper.map(value, type, mapId);
+
+        } finally {
+            if (appcl != null && appcl != tccl) {
+                Thread.currentThread().setContextClassLoader(tccl);
+                LOG.debug("Restored TCCL to: {}", tccl);
             }
-        } else {
-            // just try with the current TCCL as-is
-            answer = mapper.map(value, type, mapId);
         }
 
-        return answer;
+        return result;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/8496831e/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/DozerBeanMappingTest.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/DozerBeanMappingTest.java
b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/DozerBeanMappingTest.java
new file mode 100644
index 0000000..50ff0f3
--- /dev/null
+++ b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/DozerBeanMappingTest.java
@@ -0,0 +1,94 @@
+/*
+ * #%L
+ * Wildfly Camel :: Testsuite
+ * %%
+ * Copyright (C) 2013 - 2014 RedHat
+ * %%
+ * Licensed 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.
+ * #L%
+ */
+
+package org.apache.camel.converter.dozer;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.converter.dozer.model.Customer;
+import org.apache.camel.converter.dozer.model.CustomerA;
+import org.apache.camel.converter.dozer.model.CustomerB;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DozerBeanMappingTest {
+
+    @Test
+    public void testMarshalViaDozer() throws Exception {
+
+        CamelContext camelctx = new DefaultCamelContext();
+        camelctx.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").convertBodyTo(HashMap.class);
+            }
+        });
+
+        DozerBeanMapperConfiguration mconfig = new DozerBeanMapperConfiguration();
+        mconfig.setMappingFiles(Arrays.asList(new String[] { "bean-to-map-dozer-mappings.xml"
}));
+        new DozerTypeConverterLoader(camelctx, mconfig);
+
+        camelctx.start();
+        try {
+            ProducerTemplate producer = camelctx.createProducerTemplate();
+            Map<?, ?> result = producer.requestBody("direct:start", new Customer("John",
"Doe", null), Map.class);
+            Assert.assertEquals("John", result.get("firstName"));
+            Assert.assertEquals("Doe", result.get("lastName"));
+        } finally {
+            camelctx.stop();
+        }
+    }
+
+    @Test
+    public void testBeanMapping() throws Exception {
+
+        CamelContext camelctx = new DefaultCamelContext();
+        camelctx.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").convertBodyTo(CustomerB.class);
+            }
+        });
+
+        DozerBeanMapperConfiguration mconfig = new DozerBeanMapperConfiguration();
+        mconfig.setMappingFiles(Arrays.asList(new String[] { "bean-to-bean-dozer-mappings.xml"
}));
+        new DozerTypeConverterLoader(camelctx, mconfig);
+
+        CustomerA customerA = new CustomerA("Peter", "Post", "SomeStreet", "12345");
+
+        camelctx.start();
+        try {
+            ProducerTemplate producer = camelctx.createProducerTemplate();
+            CustomerB result = producer.requestBody("direct:start", customerA, CustomerB.class);
+            Assert.assertEquals(customerA.getFirstName(), result.getFirstName());
+            Assert.assertEquals(customerA.getLastName(), result.getLastName());
+            Assert.assertEquals(customerA.getStreet(), result.getAddress().getStreet());
+            Assert.assertEquals(customerA.getZip(), result.getAddress().getZip());
+        } finally {
+            camelctx.stop();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8496831e/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerA.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerA.java
b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerA.java
new file mode 100644
index 0000000..22b9227
--- /dev/null
+++ b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerA.java
@@ -0,0 +1,66 @@
+/*
+ * #%L
+ * Wildfly Camel :: Testsuite
+ * %%
+ * Copyright (C) 2013 - 2014 RedHat
+ * %%
+ * Licensed 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.
+ * #L%
+ */
+
+package org.apache.camel.converter.dozer.model;
+
+
+public class CustomerA {
+
+    private String firstName;
+    private String lastName;
+    private String street;
+    private String zip;
+
+    public CustomerA() {
+    }
+
+    public CustomerA(String firstName, String lastName, String street, String zip) {
+        this.firstName = firstName;
+        this.lastName = lastName;
+        this.street = street;
+        this.zip = zip;
+    }
+
+    public String getFirstName() {
+        return firstName;
+    }
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+    public String getLastName() {
+        return lastName;
+    }
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+    public String getStreet() {
+        return street;
+    }
+    public void setStreet(String street) {
+        this.street = street;
+    }
+    public String getZip() {
+        return zip;
+    }
+    public void setZip(String zip) {
+        this.zip = zip;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8496831e/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerB.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerB.java
b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerB.java
new file mode 100644
index 0000000..c7496aa
--- /dev/null
+++ b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/model/CustomerB.java
@@ -0,0 +1,84 @@
+/*
+ * #%L
+ * Wildfly Camel :: Testsuite
+ * %%
+ * Copyright (C) 2013 - 2014 RedHat
+ * %%
+ * Licensed 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.
+ * #L%
+ */
+
+package org.apache.camel.converter.dozer.model;
+
+
+public class CustomerB {
+
+    private String firstName;
+    private String lastName;
+    private Address address;
+
+    public CustomerB() {
+    }
+
+    public CustomerB(String firstName, String lastName, Address address) {
+        this.firstName = firstName;
+        this.lastName = lastName;
+        this.address = address;
+    }
+
+    public String getFirstName() {
+        return firstName;
+    }
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+    public String getLastName() {
+        return lastName;
+    }
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+    public Address getAddress() {
+        return address;
+    }
+    public void setAddress(Address address) {
+        this.address = address;
+    }
+
+    public static class Address {
+
+        private String street;
+        private String zip;
+
+        public Address() {
+        }
+
+        public Address(String street, String zip) {
+            this.street = street;
+            this.zip = zip;
+        }
+
+        public String getStreet() {
+            return street;
+        }
+        public void setStreet(String street) {
+            this.street = street;
+        }
+        public String getZip() {
+            return zip;
+        }
+        public void setZip(String zip) {
+            this.zip = zip;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8496831e/components/camel-dozer/src/test/resources/bean-to-bean-dozer-mappings.xml
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/test/resources/bean-to-bean-dozer-mappings.xml b/components/camel-dozer/src/test/resources/bean-to-bean-dozer-mappings.xml
new file mode 100644
index 0000000..92cdf53
--- /dev/null
+++ b/components/camel-dozer/src/test/resources/bean-to-bean-dozer-mappings.xml
@@ -0,0 +1,17 @@
+<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

+    xsi:schemaLocation="http://dozer.sourceforge.net  http://dozer.sourceforge.net/schema/beanmapping.xsd">
+    
+    <mapping>
+        <class-a>org.apache.camel.converter.dozer.model.CustomerA</class-a>
+        <class-b>org.apache.camel.converter.dozer.model.CustomerB</class-b>
+        <field>
+            <a>street</a>
+            <b>address.street</b>
+        </field>
+        <field>
+            <a>zip</a>
+            <b>address.zip</b>
+        </field>
+    </mapping>
+    
+</mappings>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/8496831e/components/camel-dozer/src/test/resources/bean-to-map-dozer-mappings.xml
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/test/resources/bean-to-map-dozer-mappings.xml b/components/camel-dozer/src/test/resources/bean-to-map-dozer-mappings.xml
new file mode 100644
index 0000000..bee5cb9
--- /dev/null
+++ b/components/camel-dozer/src/test/resources/bean-to-map-dozer-mappings.xml
@@ -0,0 +1,17 @@
+<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

+    xsi:schemaLocation="http://dozer.sourceforge.net  http://dozer.sourceforge.net/schema/beanmapping.xsd">
+    
+    <mapping>
+        <class-a>org.apache.camel.converter.dozer.model.Customer</class-a>
+        <class-b>java.util.HashMap</class-b>
+        <field>
+            <a>firstName</a>
+            <b key="firstName">this</b>
+        </field>
+        <field>
+            <a>lastName</a>
+            <b key="lastName">this</b>
+        </field>
+    </mapping>
+    
+</mappings>
\ No newline at end of file


Mime
View raw message