This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch properties in repository https://gitbox.apache.org/repos/asf/camel.git
commit 713750699a1c4ecd259385ea02ee808d83e8a906 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jul 3 14:01:47 2019 +0200 CAMEL-13708: Properties component should have API to make it easier to lookup a property on-demand or from all pre-loaded properties. --- .../properties/DefaultPropertiesLookup.java | 20 +++++++-- .../component/properties/PropertiesComponent.java | 51 ++++------------------ .../component/properties/RefPropertiesSource.java | 31 ++++++++++--- .../apache/camel/processor/LogPropertiesTest.java | 45 ------------------- 4 files changed, 51 insertions(+), 96 deletions(-) diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java index 217cd83..bb66c1b 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.properties; +import java.util.Iterator; +import java.util.List; import java.util.Properties; /** @@ -23,14 +25,24 @@ import java.util.Properties; */ public class DefaultPropertiesLookup implements PropertiesLookup { - private final Properties properties; + private final Properties loadedProperties; + private final List<PropertiesSource> sources; - public DefaultPropertiesLookup(Properties properties) { - this.properties = properties; + public DefaultPropertiesLookup(Properties loadedProperties, List<PropertiesSource> sources) { + this.loadedProperties = loadedProperties; + this.sources = sources; } @Override public String lookup(String name) { - return properties.getProperty(name); + String answer = loadedProperties.getProperty(name); + if (answer == null && sources != null) { + // try till first found source + Iterator<PropertiesSource> it = sources.iterator(); + while (answer == null && it.hasNext()) { + answer = it.next().getProperty(name); + } + } + return answer; } } diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java index 299c1b7..5f446ae 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java @@ -39,11 +39,13 @@ import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.DefaultComponent; +import org.apache.camel.support.EndpointHelper; import org.apache.camel.support.OrderedComparator; import org.apache.camel.support.service.ServiceHelper; import org.apache.camel.util.FilePathResolver; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.OrderedProperties; +import org.apache.camel.util.URISupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -152,7 +154,9 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { String endpointUri = parseUri(remaining); - log.debug("Endpoint uri parsed as: {}", endpointUri); + if (LOG.isDebugEnabled()) { + log.debug("Endpoint uri parsed as: {}", URISupport.sanitizeUri(endpointUri)); + } Endpoint delegate = getCamelContext().getEndpoint(endpointUri); PropertiesEndpoint answer = new PropertiesEndpoint(uri, delegate, this); @@ -167,10 +171,10 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. if (cachedLoadedProperties == null) { cachedLoadedProperties = doLoadProperties(); } - return parseUri(uri, cachedLoadedProperties); + return parseUri(uri, new DefaultPropertiesLookup(cachedLoadedProperties, sources)); } else { Properties prop = doLoadProperties(); - return parseUri(uri, prop); + return parseUri(uri, new DefaultPropertiesLookup(prop, sources)); } } @@ -224,7 +228,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. return prop; } - protected String parseUri(String uri, Properties prop) { + protected String parseUri(String uri, PropertiesLookup properties) { // enclose tokens if missing if (!uri.contains(prefixToken) && !uri.startsWith(prefixToken)) { uri = prefixToken + uri; @@ -233,8 +237,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. uri = uri + suffixToken; } - log.trace("Parsing uri {} with properties: {}", uri, prop); - PropertiesLookup properties = new DefaultPropertiesLookup(prop); + log.trace("Parsing uri {}", uri); return propertiesParser.parseUri(uri, properties, prefixToken, suffixToken, defaultFallbackEnabled); } @@ -627,40 +630,4 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. return answer; } - /** - * Key used in the locations cache - */ - private static final class CacheKey implements Serializable { - private static final long serialVersionUID = 1L; - private final List<PropertiesLocation> locations; - - private CacheKey(List<PropertiesLocation> locations) { - this.locations = new ArrayList<>(locations); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - CacheKey that = (CacheKey) o; - - return locations.equals(that.locations); - } - - @Override - public int hashCode() { - return locations.hashCode(); - } - - @Override - public String toString() { - return "LocationKey[" + locations.toString() + "]"; - } - } - } diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java index 1c9511a..946ab45 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java @@ -23,10 +23,16 @@ import java.util.Properties; import org.apache.camel.RuntimeCamelException; import org.apache.camel.util.OrderedProperties; -public class RefPropertiesSource extends LocationPropertiesSourceSupport { +public class RefPropertiesSource implements LocationPropertiesSource { + + private final PropertiesComponent propertiesComponent; + private final PropertiesLocation location; + private final boolean ignoreMissingLocation; public RefPropertiesSource(PropertiesComponent propertiesComponent, PropertiesLocation location, boolean ignoreMissingLocation) { - super(propertiesComponent, location, ignoreMissingLocation); + this.propertiesComponent = propertiesComponent; + this.location = location; + this.ignoreMissingLocation = ignoreMissingLocation; } @Override @@ -34,7 +40,23 @@ public class RefPropertiesSource extends LocationPropertiesSourceSupport { return "RefPropertiesSource[" + getLocation().getPath() + "]"; } - protected Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, boolean ignoreMissingLocation, PropertiesLocation location) { + @Override + public PropertiesLocation getLocation() { + return location; + } + + @Override + public String getProperty(String name) { + // this will lookup the property on-demand + Properties properties = lookupPropertiesInRegistry(propertiesComponent, ignoreMissingLocation, location); + if (properties != null) { + return properties.getProperty(name); + } else { + return null; + } + } + + protected Properties lookupPropertiesInRegistry(PropertiesComponent propertiesComponent, boolean ignoreMissingLocation, PropertiesLocation location) { String path = location.getPath(); Properties answer; try { @@ -48,7 +70,6 @@ public class RefPropertiesSource extends LocationPropertiesSourceSupport { if (answer == null && (!ignoreMissingLocation && !location.isOptional())) { throw RuntimeCamelException.wrapRuntimeCamelException(new FileNotFoundException("Properties " + path + " not found in registry")); } - return answer != null ? answer : new OrderedProperties(); + return answer; } - } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/LogPropertiesTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/LogPropertiesTest.java deleted file mode 100644 index 4b655fd..0000000 --- a/core/camel-core/src/test/java/org/apache/camel/processor/LogPropertiesTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.processor; - -import org.apache.camel.ContextTestSupport; -import org.apache.camel.builder.RouteBuilder; -import org.junit.Test; - -public class LogPropertiesTest extends ContextTestSupport { - - @Test - public void testLogProperties() throws Exception { - getMockEndpoint("mock:result").expectedMessageCount(1); - - 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") - .to("properties:dude?locations=org/apache/camel/processor/foo.properties") - .to("mock:result"); - } - }; - } -}