Repository: camel Updated Branches: refs/heads/camel-2.15.x 7624bc4cb -> aca7225f8 refs/heads/camel-2.16.x 7e8171e0a -> 94ddb6078 refs/heads/master c2f902451 -> 1596d267e
CAMEL-9178: camel-dozer type converter with map-id in mapping files should be supported. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1596d267 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1596d267 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1596d267 Branch: refs/heads/master Commit: 1596d267ec00ca3e6ea7b92801d3ad5ea1efccb8 Parents: c2f9024 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Oct 13 08:03:03 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Oct 13 08:03:03 2015 +0200 ---------------------------------------------------------------------- .../converter/dozer/DozerTypeConverter.java | 18 ++++++-- .../SpringDozerTypeConverterWithMapIdTest.java | 43 ++++++++++++++++++++ .../resources/application-context-map-id.xml | 41 +++++++++++++++++++ .../src/test/resources/mapping-map-id.xml | 32 +++++++++++++++ 4 files changed, 131 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1596d267/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 28a35e5..e5114e4 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 @@ -23,6 +23,7 @@ import org.apache.camel.TypeConverter; import org.apache.camel.support.TypeConverterSupport; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; +import org.dozer.metadata.ClassMappingMetadata; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,9 +53,20 @@ 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(); + } + } + // 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); + return mapper.map(value, type, mapId); } T answer = null; @@ -66,14 +78,14 @@ public class DozerTypeConverter extends TypeConverterSupport { LOG.debug("Switching TCCL to: {}.", contextCl); try { Thread.currentThread().setContextClassLoader(contextCl); - answer = mapper.map(value, type); + answer = mapper.map(value, type, mapId); } finally { LOG.debug("Restored TCCL to: {}.", prev); Thread.currentThread().setContextClassLoader(prev); } } else { // just try with the current TCCL as-is - answer = mapper.map(value, type); + answer = mapper.map(value, type, mapId); } return answer; http://git-wip-us.apache.org/repos/asf/camel/blob/1596d267/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/SpringDozerTypeConverterWithMapIdTest.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/SpringDozerTypeConverterWithMapIdTest.java b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/SpringDozerTypeConverterWithMapIdTest.java new file mode 100644 index 0000000..931da52 --- /dev/null +++ b/components/camel-dozer/src/test/java/org/apache/camel/converter/dozer/SpringDozerTypeConverterWithMapIdTest.java @@ -0,0 +1,43 @@ +/** + * 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.dozer; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.apache.camel.converter.dozer.DozerTestArtifactsFactory.createServiceCustomer; + +public class SpringDozerTypeConverterWithMapIdTest extends CamelSpringTestSupport { + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("application-context-map-id.xml"); + } + + @Test + public void verifyCamelConversionViaDozer() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:verify-model"); + mock.expectedMessageCount(1); + + template.sendBody("direct:service-in", createServiceCustomer()); + + assertMockEndpointsSatisfied(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1596d267/components/camel-dozer/src/test/resources/application-context-map-id.xml ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/application-context-map-id.xml b/components/camel-dozer/src/test/resources/application-context-map-id.xml new file mode 100644 index 0000000..2a2700e --- /dev/null +++ b/components/camel-dozer/src/test/resources/application-context-map-id.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:service-in"/> + <to uri="bean:customer-processor"/> + <to uri="mock:verify-model"/> + </route> + </camelContext> + + <bean id="customer-processor" class="org.apache.camel.converter.dozer.CustomerProcessor"/> + <bean id="dozerConverterLoader" class="org.apache.camel.converter.dozer.DozerTypeConverterLoader"/> + + <bean id="mapper" class="org.dozer.DozerBeanMapper"> + <property name="mappingFiles"> + <list> + <value>mapping-map-id.xml</value> + </list> + </property> + </bean> +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1596d267/components/camel-dozer/src/test/resources/mapping-map-id.xml ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/test/resources/mapping-map-id.xml b/components/camel-dozer/src/test/resources/mapping-map-id.xml new file mode 100644 index 0000000..4d86dbd --- /dev/null +++ b/components/camel-dozer/src/test/resources/mapping-map-id.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<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 map-id="foo"> + <class-a>org.apache.camel.converter.dozer.service.Customer</class-a> + <class-b>org.apache.camel.converter.dozer.model.Customer</class-b> + <field> + <a>street</a> + <b>address.streetName</b> + </field> + <field> + <a>zip</a> + <b>address.zipCode</b> + </field> + </mapping> +</mappings> \ No newline at end of file