Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9964010883 for ; Thu, 19 Mar 2015 06:28:54 +0000 (UTC) Received: (qmail 82047 invoked by uid 500); 19 Mar 2015 06:28:48 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 81991 invoked by uid 500); 19 Mar 2015 06:28:48 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 81982 invoked by uid 99); 19 Mar 2015 06:28:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Mar 2015 06:28:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 33735E18C9; Thu, 19 Mar 2015 06:28:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Thu, 19 Mar 2015 06:28:48 -0000 Message-Id: <0d6c912a23cc4596ae5e0c8949079c15@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] 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. Repository: camel Updated Branches: refs/heads/camel-2.14.x 2dcc05dfe -> 6aadc0d6e 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/fa3b83c2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fa3b83c2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fa3b83c2 Branch: refs/heads/camel-2.14.x Commit: fa3b83c2b0c2d1636924920508a96e6942597070 Parents: 2dcc05d Author: Claus Ibsen Authored: Thu Mar 19 07:28:46 2015 +0100 Committer: Claus Ibsen Committed: Thu Mar 19 07:30:33 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/fa3b83c2/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/fa3b83c2/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 520c4d0..3fa97be 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 cacheMap = new LRUSoftCache(1000); private final Map functions = new HashMap(); - 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. + *

+ * 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/fa3b83c2/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/fa3b83c2/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/fa3b83c2/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/fa3b83c2/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 32b84a8..7e4644b 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 @@ -502,6 +502,7 @@ public abstract class AbstractCamelContextFactoryBean