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 2015/03/19 07:29:45 UTC
[2/4] camel git commit: CAMEL-8511: Add encoding option to properties
component to allow reading the properties in a different charset such as
utf-8 instead of latin1 which is the default.
CAMEL-8511: Add encoding option to properties component to allow reading the properties in a different charset such as utf-8 instead of latin1 which is the default.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/024c6adb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/024c6adb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/024c6adb
Branch: refs/heads/camel-2.15.x
Commit: 024c6adb5ccc176837edfa3defb44f4e1e214d12
Parents: f6240e6
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Mar 19 07:28:46 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Mar 19 07:31:11 2015 +0100
----------------------------------------------------------------------
.../properties/DefaultPropertiesResolver.java | 29 ++++++++--
.../properties/PropertiesComponent.java | 17 +++++-
.../PropertiesComponentEncodingTest.java | 59 ++++++++++++++++++++
.../PropertiesComponentRestartTest.java | 4 +-
.../component/properties/myutf8.properties | 19 +++++++
.../xml/AbstractCamelContextFactoryBean.java | 1 +
.../xml/CamelPropertyPlaceholderDefinition.java | 11 ++++
7 files changed, 133 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/024c6adb/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
index dd730d4..e1b46c9 100644
--- a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
+++ b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
@@ -16,10 +16,13 @@
*/
package org.apache.camel.component.properties;
+import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.util.Map;
import java.util.Properties;
@@ -38,6 +41,12 @@ import org.apache.camel.util.ObjectHelper;
*/
public class DefaultPropertiesResolver implements PropertiesResolver {
+ private final PropertiesComponent propertiesComponent;
+
+ public DefaultPropertiesResolver(PropertiesComponent propertiesComponent) {
+ this.propertiesComponent = propertiesComponent;
+ }
+
public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, String... uri) throws Exception {
Properties answer = new Properties();
@@ -69,15 +78,21 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
}
InputStream is = null;
+ Reader reader = null;
try {
is = new FileInputStream(path);
- answer.load(is);
+ if (propertiesComponent.getEncoding() != null) {
+ reader = new BufferedReader(new InputStreamReader(is, propertiesComponent.getEncoding()));
+ answer.load(reader);
+ } else {
+ answer.load(is);
+ }
} catch (FileNotFoundException e) {
if (!ignoreMissingLocation) {
throw e;
}
} finally {
- IOHelper.close(is);
+ IOHelper.close(reader, is);
}
return answer;
@@ -91,15 +106,21 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
}
InputStream is = context.getClassResolver().loadResourceAsStream(path);
+ Reader reader = null;
if (is == null) {
if (!ignoreMissingLocation) {
throw new FileNotFoundException("Properties file " + path + " not found in classpath");
}
} else {
try {
- answer.load(is);
+ if (propertiesComponent.getEncoding() != null) {
+ reader = new BufferedReader(new InputStreamReader(is, propertiesComponent.getEncoding()));
+ answer.load(reader);
+ } else {
+ answer.load(is);
+ }
} finally {
- IOHelper.close(is);
+ IOHelper.close(reader, is);
}
}
return answer;
http://git-wip-us.apache.org/repos/asf/camel/blob/024c6adb/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index 3b53d0a..e9de896 100644
--- a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -72,10 +72,11 @@ public class PropertiesComponent extends DefaultComponent {
private static final Logger LOG = LoggerFactory.getLogger(PropertiesComponent.class);
private final Map<CacheKey, Properties> cacheMap = new LRUSoftCache<CacheKey, Properties>(1000);
private final Map<String, PropertiesFunction> functions = new HashMap<String, PropertiesFunction>();
- private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver();
+ private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver(this);
private PropertiesParser propertiesParser = new DefaultPropertiesParser(this);
private String[] locations;
private boolean ignoreMissingLocation;
+ private String encoding;
private boolean cache = true;
private String propertyPrefix;
private String propertyPrefixResolved;
@@ -209,6 +210,20 @@ public class PropertiesComponent extends DefaultComponent {
setLocations(location.split(","));
}
+ public String getEncoding() {
+ return encoding;
+ }
+
+ /**
+ * Encoding to use when loading properties file from the file system or classpath.
+ * <p/>
+ * If no encoding has been set, then the properties files is loaded using ISO-8859-1 encoding (latin-1)
+ * as documented by {@link java.util.Properties#load(java.io.InputStream)}
+ */
+ public void setEncoding(String encoding) {
+ this.encoding = encoding;
+ }
+
public PropertiesResolver getPropertiesResolver() {
return propertiesResolver;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/024c6adb/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEncodingTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEncodingTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEncodingTest.java
new file mode 100644
index 0000000..9b8f8c0
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEncodingTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.component.properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version
+ */
+public class PropertiesComponentEncodingTest extends ContextTestSupport {
+
+ public void testPropertiesComponent() throws Exception {
+ final String title = "Hello Thai Elephant \u0E08";
+
+ getMockEndpoint("mock:result").expectedBodiesReceived(title);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .transform().constant("{{elephant}}")
+ .to("mock:result");
+ }
+ };
+ }
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ PropertiesComponent prop = new PropertiesComponent("classpath:org/apache/camel/component/properties/myutf8.properties");
+ prop.setEncoding("UTF-8");
+ context.addComponent("properties", prop);
+ return context;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/024c6adb/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java
index 02e4c78..6d7c919 100644
--- a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java
@@ -51,11 +51,11 @@ public class PropertiesComponentRestartTest extends ContextTestSupport {
@Override
protected CamelContext createCamelContext() throws Exception {
- PropertiesComponent pc = new PropertiesComponent("classpath:org/apache/camel/component/properties/myproperties.properties");
+ final PropertiesComponent pc = new PropertiesComponent("classpath:org/apache/camel/component/properties/myproperties.properties");
pc.setPropertiesResolver(new PropertiesResolver() {
public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, String... uri) throws Exception {
resolvedCount++;
- return new DefaultPropertiesResolver().resolveProperties(context, ignoreMissingLocation, uri);
+ return new DefaultPropertiesResolver(pc).resolveProperties(context, ignoreMissingLocation, uri);
}
});
http://git-wip-us.apache.org/repos/asf/camel/blob/024c6adb/camel-core/src/test/resources/org/apache/camel/component/properties/myutf8.properties
----------------------------------------------------------------------
diff --git a/camel-core/src/test/resources/org/apache/camel/component/properties/myutf8.properties b/camel-core/src/test/resources/org/apache/camel/component/properties/myutf8.properties
new file mode 100644
index 0000000..66bbe8a
--- /dev/null
+++ b/camel-core/src/test/resources/org/apache/camel/component/properties/myutf8.properties
@@ -0,0 +1,19 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+## there is an UTF-8 character as \u0E08
+elephant=Hello Thai Elephant จ
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/024c6adb/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
index 9d807cc..3ea87a0 100644
--- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
@@ -508,6 +508,7 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex
PropertiesComponent pc = new PropertiesComponent();
pc.setLocation(def.getLocation());
+ pc.setEncoding(def.getEncoding());
if (def.isCache() != null) {
pc.setCache(def.isCache());
http://git-wip-us.apache.org/repos/asf/camel/blob/024c6adb/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
index cc61c2f..b07fdc0 100644
--- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
@@ -38,6 +38,9 @@ public class CamelPropertyPlaceholderDefinition extends IdentifiedType {
private String location;
@XmlAttribute
+ private String encoding;
+
+ @XmlAttribute
private Boolean cache;
@XmlAttribute
@@ -75,6 +78,14 @@ public class CamelPropertyPlaceholderDefinition extends IdentifiedType {
this.location = location;
}
+ public String getEncoding() {
+ return encoding;
+ }
+
+ public void setEncoding(String encoding) {
+ this.encoding = encoding;
+ }
+
public Boolean isCache() {
return cache;
}