Repository: camel Updated Branches: refs/heads/camel-2.13.x e3e5c4b3c -> aca020e61 refs/heads/master 43c1a5135 -> baeddbc1b
CAMEL-7312: Added type converter for file/is -> properties Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/baeddbc1 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/baeddbc1 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/baeddbc1 Branch: refs/heads/master Commit: baeddbc1bbfad4545d1f0672ed5acf28e890e6ac Parents: 43c1a51 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Mar 20 09:45:05 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Mar 20 09:45:14 2014 +0100 ---------------------------------------------------------------------- .../org/apache/camel/converter/IOConverter.java | 30 +++++++++++++++++++- .../apache/camel/converter/IOConverterTest.java | 19 +++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/baeddbc1/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java index 02995fa..312d432 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java +++ b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java @@ -40,6 +40,7 @@ import java.io.UnsupportedEncodingException; import java.io.Writer; import java.net.URL; import java.nio.charset.UnsupportedCharsetException; +import java.util.Properties; import org.apache.camel.Converter; import org.apache.camel.Exchange; @@ -427,6 +428,33 @@ public final class IOConverter { return new ByteArrayInputStream(os.toByteArray()); } + @Converter + public static Properties toProperties(File file) throws IOException { + return toProperties(new FileInputStream(file)); + } + + @Converter + public static Properties toProperties(InputStream is) throws IOException { + Properties prop = new Properties(); + try { + prop.load(is); + } finally { + IOHelper.close(is); + } + return prop; + } + + @Converter + public static Properties toProperties(Reader reader) throws IOException { + Properties prop = new Properties(); + try { + prop.load(reader); + } finally { + IOHelper.close(reader); + } + return prop; + } + /** * Gets the charset name if set as header or property {@link Exchange#CHARSET_NAME}. * @@ -510,5 +538,5 @@ public final class IOConverter { public static void validateCharset(String charset) throws UnsupportedCharsetException { IOHelper.validateCharset(charset); } - + } http://git-wip-us.apache.org/repos/asf/camel/blob/baeddbc1/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java b/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java index af701e1..383a0f4 100644 --- a/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java +++ b/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java @@ -30,6 +30,7 @@ import java.io.Reader; import java.io.StringReader; import java.io.Writer; import java.net.URL; +import java.util.Properties; import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; @@ -222,4 +223,22 @@ public class IOConverterTest extends ContextTestSupport { assertEquals("Get a wrong result", data, result); } + public void testToPropertiesFromReader() throws Exception { + Reader br = IOHelper.buffered(new StringReader("foo=123\nbar=456")); + Properties p = IOConverter.toProperties(br); + assertNotNull(p); + assertEquals(2, p.size()); + assertEquals("123", p.get("foo")); + assertEquals("456", p.get("bar")); + } + + public void testToPropertiesFromFile() throws Exception { + Properties p = IOConverter.toProperties(new File("src/test/resources/log4j.properties")); + assertNotNull(p); + assertTrue("Should be 8 or more properties, was " + p.size(), p.size() >= 8); + String root = (String) p.get("log4j.rootLogger"); + assertNotNull(root); + assertTrue(root.contains("INFO")); + } + }