Repository: camel Updated Branches: refs/heads/master 21c366b8d -> 5b8d8e8cb
CAMEL-7932 Added setter, getter and test for initial properties in PropertiesComponent Removed unnecessary null check Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5b8d8e8c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5b8d8e8c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5b8d8e8c Branch: refs/heads/master Commit: 5b8d8e8cbdbe4498ea3c81ea2804bfbc5fce6af0 Parents: 21c366b Author: Jyrki Ruuskanen <yur...@kotikone.fi> Authored: Sun Oct 5 19:38:43 2014 +0300 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Mon Oct 20 15:37:14 2014 +0800 ---------------------------------------------------------------------- .../properties/PropertiesComponent.java | 34 +++++++-- ...ropertiesComponentInitialPropertiesTest.java | 72 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5b8d8e8c/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 24b2eba..31162fc 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 @@ -82,6 +82,7 @@ public class PropertiesComponent extends DefaultComponent { private boolean fallbackToUnaugmentedProperty = true; private String prefixToken = DEFAULT_PREFIX_TOKEN; private String suffixToken = DEFAULT_SUFFIX_TOKEN; + private Properties initialProperties; private Properties overrideProperties; public PropertiesComponent() { @@ -120,7 +121,14 @@ public class PropertiesComponent extends DefaultComponent { } public String parseUri(String uri, String... paths) throws Exception { - Properties prop = null; + Properties prop = new Properties(); + + // use initial properties + if (null != initialProperties) { + prop.putAll(initialProperties); + } + + // use locations if (paths != null) { // location may contain JVM system property or OS environment variables // so we need to parse those @@ -128,17 +136,18 @@ public class PropertiesComponent extends DefaultComponent { // check cache first CacheKey key = new CacheKey(locations); - prop = cache ? cacheMap.get(key) : null; - if (prop == null) { - prop = propertiesResolver.resolveProperties(getCamelContext(), ignoreMissingLocation, locations); + Properties locationsProp = cache ? cacheMap.get(key) : null; + if (locationsProp == null) { + locationsProp = propertiesResolver.resolveProperties(getCamelContext(), ignoreMissingLocation, locations); if (cache) { - cacheMap.put(key, prop); + cacheMap.put(key, locationsProp); } } + prop.putAll(locationsProp); } // use override properties - if (prop != null && overrideProperties != null) { + if (overrideProperties != null) { // make a copy to avoid affecting the original properties Properties override = new Properties(); override.putAll(prop); @@ -272,6 +281,19 @@ public class PropertiesComponent extends DefaultComponent { } } + public Properties getInitialProperties() { + return initialProperties; + } + + /** + * Sets initial properties which will be used before any locations are resolved. + * + * @param initialProperties properties that are added first + */ + public void setInitialProperties(Properties initialProperties) { + this.initialProperties = initialProperties; + } + public Properties getOverrideProperties() { return overrideProperties; } http://git-wip-us.apache.org/repos/asf/camel/blob/5b8d8e8c/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInitialPropertiesTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInitialPropertiesTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInitialPropertiesTest.java new file mode 100644 index 0000000..7bd4e1e --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInitialPropertiesTest.java @@ -0,0 +1,72 @@ +/** + * 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 java.util.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version + */ +public class PropertiesComponentInitialPropertiesTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + public void testPropertiesComponentEndpoint() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("{{first}}") + .to("mock:{{second}}") + .to("{{cool.end}}"); + } + }); + context.start(); + + getMockEndpoint("mock:first").expectedMessageCount(1); + getMockEndpoint("mock:second").expectedMessageCount(1); + getMockEndpoint("mock:result").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + + PropertiesComponent pc = new PropertiesComponent(); + pc.setLocation("classpath:org/apache/camel/component/properties/myproperties.properties"); + context.addComponent("properties", pc); + + Properties initial = new Properties(); + initial.put("first", "mock:first"); + initial.put("second", "second"); + pc.setInitialProperties(initial); + + return context; + } + +} \ No newline at end of file