Repository: camel Updated Branches: refs/heads/master a1e02eb19 -> e6eca45cb
CAMEL-7430 Support property setting in BeanIODataFormat Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e6eca45c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e6eca45c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e6eca45c Branch: refs/heads/master Commit: e6eca45cb707c0c46bcff988e00e7af9de0ae296 Parents: a1e02eb Author: Willem Jiang <[email protected]> Authored: Mon May 12 11:55:37 2014 +0800 Committer: Willem Jiang <[email protected]> Committed: Mon May 12 12:07:29 2014 +0800 ---------------------------------------------------------------------- .../dataformat/beanio/BeanIODataFormat.java | 16 +++++- .../beanio/csv/CsvTestWithProperties.java | 60 ++++++++++++++++++++ .../beanio/csv/mappingsWithProperties.xml | 14 +++++ 3 files changed, 89 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/e6eca45c/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java index 313d2cb..4b244ed 100644 --- a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java +++ b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java @@ -26,6 +26,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Properties; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; @@ -62,6 +63,7 @@ public class BeanIODataFormat extends ServiceSupport implements DataFormat, Came private boolean ignoreUnexpectedRecords; private boolean ignoreInvalidRecords; private Charset encoding = Charset.defaultCharset(); + private Properties properties; public BeanIODataFormat() { } @@ -81,7 +83,11 @@ public class BeanIODataFormat extends ServiceSupport implements DataFormat, Came // Load the mapping file using the resource helper to ensure it can be loaded in OSGi and other environments InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), mapping); try { - factory.load(is); + if (properties != null) { + factory.load(is, properties); + } else { + factory.load(is); + } } finally { IOHelper.close(is); } @@ -247,4 +253,12 @@ public class BeanIODataFormat extends ServiceSupport implements DataFormat, Came public void setStreamName(String streamName) { this.streamName = streamName; } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/e6eca45c/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/csv/CsvTestWithProperties.java ---------------------------------------------------------------------- diff --git a/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/csv/CsvTestWithProperties.java b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/csv/CsvTestWithProperties.java new file mode 100644 index 0000000..3963995 --- /dev/null +++ b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/csv/CsvTestWithProperties.java @@ -0,0 +1,60 @@ +/** + * 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.dataformat.beanio.csv; + +import java.util.Properties; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.dataformat.beanio.BeanIODataFormat; +import org.apache.camel.spi.DataFormat; + +public class CsvTestWithProperties extends CsvTest { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // START SNIPPET: e1 + // setup beanio data format using the mapping file, loaded from the classpath + BeanIODataFormat format = new BeanIODataFormat( + "org/apache/camel/dataformat/beanio/csv/mappingsWithProperties.xml", + "stream1"); + Properties properties = new Properties(); + properties.setProperty("field1", "firstName"); + properties.setProperty("field2", "lastName"); + + format.setProperties(properties); + + // a route which uses the bean io data format to format a CSV data + // to java objects + from("direct:unmarshal") + .unmarshal(format) + // and then split the message body so we get a message for each row + .split(body()) + .to("mock:beanio-unmarshal"); + + // convert list of java objects back to flat format + from("direct:marshal") + .marshal(format) + .to("mock:beanio-marshal"); + // END SNIPPET: e1 + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/e6eca45c/components/camel-beanio/src/test/resources/org/apache/camel/dataformat/beanio/csv/mappingsWithProperties.xml ---------------------------------------------------------------------- diff --git a/components/camel-beanio/src/test/resources/org/apache/camel/dataformat/beanio/csv/mappingsWithProperties.xml b/components/camel-beanio/src/test/resources/org/apache/camel/dataformat/beanio/csv/mappingsWithProperties.xml new file mode 100644 index 0000000..8615a6e --- /dev/null +++ b/components/camel-beanio/src/test/resources/org/apache/camel/dataformat/beanio/csv/mappingsWithProperties.xml @@ -0,0 +1,14 @@ +<?xml version='1.0' encoding='UTF-8' ?> +<beanio xmlns="http://www.beanio.org/2012/03" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.beanio.org/2011/01 http://www.beanio.org/2011/01/mapping.xsd"> + + <stream name="stream1" format="csv"> + <!-- TODO warning - if class is ommitted then this test case just does nothing; no messages consumed and no warnings! :) --> + <record name="record1" class="map"> + <field name="${field1}" /> + <field name="${field2}" /> + <field name="age" type="int"/> + </record> + </stream> +</beanio> \ No newline at end of file
