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 2014/03/20 09:43:08 UTC
[2/2] git commit: CAMEL-7312: Added type converter for file/is ->
properties
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/aca020e6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/aca020e6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/aca020e6
Branch: refs/heads/camel-2.13.x
Commit: aca020e611e3294685410119708d638c808b1f11
Parents: e3e5c4b
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Mar 20 09:45:05 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Mar 20 09:45:53 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/aca020e6/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/aca020e6/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"));
+ }
+
}