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 639b2c064510dfebdbdb0b698a13d6a8810ee5a6 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jul 2 14:56:35 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. --- ...AugmentedPropertyNameAwarePropertiesParser.java | 4 +-- .../properties/DefaultPropertiesLookup.java | 36 ++++++++++++++++++++++ .../properties/DefaultPropertiesParser.java | 12 ++++---- .../component/properties/PropertiesComponent.java | 6 ++-- .../component/properties/PropertiesLookup.java | 32 +++++++++++++++++++ .../component/properties/PropertiesParser.java | 8 ++--- 6 files changed, 82 insertions(+), 16 deletions(-) diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java index 4789c71..adc88803 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java @@ -16,8 +16,6 @@ */ package org.apache.camel.component.properties; -import java.util.Properties; - /** * Interface for property parses that can attempt parsing property names using a fixed property name prefix and suffix. */ @@ -49,6 +47,6 @@ public interface AugmentedPropertyNameAwarePropertiesParser extends PropertiesPa * @throws IllegalArgumentException if uri syntax is not valid or a property * is not found */ - String parseUri(String text, Properties properties, String prefixToken, String suffixToken, + String parseUri(String text, PropertiesLookup properties, String prefixToken, String suffixToken, String propertyPrefix, String propertySuffix, boolean fallbackToUnaugmentedProperty, boolean defaultFallbackEnabled) throws IllegalArgumentException; } 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 new file mode 100644 index 0000000..217cd83 --- /dev/null +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java @@ -0,0 +1,36 @@ +/* + * 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; + +/** + * Default {@link PropertiesLookup} which lookup properties from a {@link java.util.Properties} with all existing properties. + */ +public class DefaultPropertiesLookup implements PropertiesLookup { + + private final Properties properties; + + public DefaultPropertiesLookup(Properties properties) { + this.properties = properties; + } + + @Override + public String lookup(String name) { + return properties.getProperty(name); + } +} diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java index f035912..9c01340 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java @@ -53,19 +53,19 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper } @Override - public String parseUri(String text, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException { + public String parseUri(String text, PropertiesLookup properties, String prefixToken, String suffixToken) throws IllegalArgumentException { return parseUri(text, properties, prefixToken, suffixToken, null, null, false, false); } @Override - public String parseUri(String text, Properties properties, + public String parseUri(String text, PropertiesLookup properties, String prefixToken, String suffixToken, String propertyPrefix, String propertySuffix, boolean fallbackToUnaugmentedProperty, boolean defaultFallbackEnabled) throws IllegalArgumentException { ParsingContext context = new ParsingContext(properties, prefixToken, suffixToken, propertyPrefix, propertySuffix, fallbackToUnaugmentedProperty, defaultFallbackEnabled); return context.parse(text); } - public String parseProperty(String key, String value, Properties properties) { + public String parseProperty(String key, String value, PropertiesLookup properties) { return value; } @@ -73,7 +73,7 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper * This inner class helps replacing properties. */ private final class ParsingContext { - private final Properties properties; + private final PropertiesLookup properties; private final String prefixToken; private final String suffixToken; private final String propertyPrefix; @@ -81,7 +81,7 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper private final boolean fallbackToUnaugmentedProperty; private final boolean defaultFallbackEnabled; - ParsingContext(Properties properties, String prefixToken, String suffixToken, String propertyPrefix, String propertySuffix, + ParsingContext(PropertiesLookup properties, String prefixToken, String suffixToken, String propertyPrefix, String propertySuffix, boolean fallbackToUnaugmentedProperty, boolean defaultFallbackEnabled) { this.properties = properties; this.prefixToken = prefixToken; @@ -327,7 +327,7 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper } if (value == null && properties != null) { - value = properties.getProperty(key); + value = properties.lookup(key); if (value != null) { log.debug("Found property: {} with value: {} to be used.", key, value); } 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 0697fcc..28fdad0 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 @@ -272,11 +272,13 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. } log.trace("Parsing uri {} with properties: {}", uri, prop); + + PropertiesLookup properties = new DefaultPropertiesLookup(prop); if (propertiesParser instanceof AugmentedPropertyNameAwarePropertiesParser) { return ((AugmentedPropertyNameAwarePropertiesParser) propertiesParser).parseUri( uri, - prop, + properties, prefixToken, suffixToken, propertyPrefixResolved, @@ -284,7 +286,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. fallbackToUnaugmentedProperty, defaultFallbackEnabled); } else { - return propertiesParser.parseUri(uri, prop, prefixToken, suffixToken); + return propertiesParser.parseUri(uri, properties, prefixToken, suffixToken); } } diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesLookup.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesLookup.java new file mode 100644 index 0000000..19411c7 --- /dev/null +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesLookup.java @@ -0,0 +1,32 @@ +/* + * 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; + +/** + * Used by {@link PropertiesParser} to lookup properties by their name + */ +public interface PropertiesLookup { + + /** + * Lookup the property with the given name + * + * @param name property name + * @return the property value, or <tt>null</tt> if the properties does not exist. + */ + String lookup(String name); + +} diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesParser.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesParser.java index cbc6602..992cb43 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesParser.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesParser.java @@ -16,8 +16,6 @@ */ package org.apache.camel.component.properties; -import java.util.Properties; - /** * A parser to parse properties for a given input */ @@ -33,10 +31,10 @@ public interface PropertiesParser { * @return the parsed text with replaced placeholders * @throws IllegalArgumentException if uri syntax is not valid or a property is not found */ - String parseUri(String text, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException; + String parseUri(String text, PropertiesLookup properties, String prefixToken, String suffixToken) throws IllegalArgumentException; /** - * While parsing the uri using {@link #parseUri(String, java.util.Properties, String, String) parseUri} each + * While parsing the uri using {@link #parseUri(String, PropertiesLookup, String, String) parseUri} each * parsed property found invokes this callback. * <p/> * This strategy method allows you to hook into the parsing and do custom lookup and return the actual value to use. @@ -46,5 +44,5 @@ public interface PropertiesParser { * @param properties the properties resolved which values should be looked up * @return the value to use */ - String parseProperty(String key, String value, Properties properties); + String parseProperty(String key, String value, PropertiesLookup properties); }