camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [09/14] camel git commit: CAMEL-11756: camel-spring-boot2 - Work in progress
Date Sat, 09 Sep 2017 14:19:02 GMT
CAMEL-11756: camel-spring-boot2 - Work in progress


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/48506574
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/48506574
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/48506574

Branch: refs/heads/boot2
Commit: 48506574ec80039eef02980ef939e87f827a9b44
Parents: c5990d2
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Sep 9 13:59:08 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Sep 9 13:59:08 2017 +0200

----------------------------------------------------------------------
 .../security/CamelSSLAutoConfiguration.java     |   9 +-
 .../util/HierarchicalPropertiesEvaluator.java   |   6 +-
 .../spring/boot/util/PropertySourceUtils.java   |  91 +++++++
 .../camel/spring/boot/util/RelaxedNames.java    | 258 +++++++++++++++++++
 .../boot/util/RelaxedPropertyResolver.java      | 155 +++++++++++
 5 files changed, 510 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/48506574/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/security/CamelSSLAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/security/CamelSSLAutoConfiguration.java
b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/security/CamelSSLAutoConfiguration.java
index dd1a42d..d8d54ad 100644
--- a/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/security/CamelSSLAutoConfiguration.java
+++ b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/security/CamelSSLAutoConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.camel.spring.boot.security;
 import java.util.Map;
 
 import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.spring.boot.util.RelaxedPropertyResolver;
 import org.apache.camel.util.jsse.GlobalSSLContextParametersSupplier;
 import org.apache.camel.util.jsse.SSLContextParameters;
 import org.springframework.boot.autoconfigure.AutoConfigureBefore;
@@ -47,16 +48,14 @@ public class CamelSSLAutoConfiguration {
     public static class Condition extends SpringBootCondition {
         @Override
         public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata
annotatedTypeMetadata) {
-            // TODO: need to find a solution for this, we can maybe copy the code from 1.x
-            /*RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(),
"camel.ssl.config");
+            RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(),
"camel.ssl.config");
             Map<String, Object> sslProperties = resolver.getSubProperties(".");
             ConditionMessage.Builder message = ConditionMessage.forCondition("camel.ssl.config");
             if (sslProperties.size() > 0) {
                 return ConditionOutcome.match(message.because("enabled"));
-            }*/
+            }
 
-            //return ConditionOutcome.noMatch(message.because("not enabled"));
-            return ConditionOutcome.noMatch("not enabled");
+            return ConditionOutcome.noMatch(message.because("not enabled"));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/48506574/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/HierarchicalPropertiesEvaluator.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/HierarchicalPropertiesEvaluator.java
b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/HierarchicalPropertiesEvaluator.java
index 37f2778..a898d55 100644
--- a/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/HierarchicalPropertiesEvaluator.java
+++ b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/HierarchicalPropertiesEvaluator.java
@@ -46,13 +46,11 @@ public final class HierarchicalPropertiesEvaluator {
     }
 
     private static boolean isEnabled(Environment environment, String prefix, boolean defaultValue)
{
-        // TODO: need to find a solution for this, we can maybe copy the code from 1.x
-        /*RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
+        RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
             environment,
             prefix.endsWith(".") ? prefix : prefix + "."
         );
 
-        return resolver.getProperty("enabled", Boolean.class, defaultValue);*/
-        return defaultValue;
+        return resolver.getProperty("enabled", Boolean.class, defaultValue);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/48506574/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/PropertySourceUtils.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/PropertySourceUtils.java
b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/PropertySourceUtils.java
new file mode 100644
index 0000000..2b0e53c
--- /dev/null
+++ b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/PropertySourceUtils.java
@@ -0,0 +1,91 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.spring.boot.util;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.springframework.core.env.EnumerablePropertySource;
+import org.springframework.core.env.PropertySource;
+import org.springframework.core.env.PropertySources;
+
+/**
+ * Convenience class for manipulating PropertySources.
+ *
+ * @author Dave Syer
+ * @see PropertySource
+ * @see PropertySources
+ */
+public abstract class PropertySourceUtils {
+
+    /**
+     * Return a Map of all values from the specified {@link PropertySources} that start
+     * with a particular key.
+     * @param propertySources the property sources to scan
+     * @param keyPrefix the key prefixes to test
+     * @return a map of all sub properties starting with the specified key prefixes.
+     * @see PropertySourceUtils#getSubProperties(PropertySources, String, String)
+     */
+    public static Map<String, Object> getSubProperties(PropertySources propertySources,
+                                                       String keyPrefix) {
+        return PropertySourceUtils.getSubProperties(propertySources, null, keyPrefix);
+    }
+
+    /**
+     * Return a Map of all values from the specified {@link PropertySources} that start
+     * with a particular key.
+     * @param propertySources the property sources to scan
+     * @param rootPrefix a root prefix to be prepended to the keyPrefix (can be
+     * {@code null})
+     * @param keyPrefix the key prefixes to test
+     * @return a map of all sub properties starting with the specified key prefixes.
+     * @see #getSubProperties(PropertySources, String, String)
+     */
+    public static Map<String, Object> getSubProperties(PropertySources propertySources,
+                                                       String rootPrefix, String keyPrefix)
{
+        RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix);
+        Map<String, Object> subProperties = new LinkedHashMap<String, Object>();
+        for (PropertySource<?> source : propertySources) {
+            if (source instanceof EnumerablePropertySource) {
+                for (String name : ((EnumerablePropertySource<?>) source)
+                    .getPropertyNames()) {
+                    String key = PropertySourceUtils.getSubKey(name, rootPrefix,
+                        keyPrefixes);
+                    if (key != null && !subProperties.containsKey(key)) {
+                        subProperties.put(key, source.getProperty(name));
+                    }
+                }
+            }
+        }
+        return Collections.unmodifiableMap(subProperties);
+    }
+
+    private static String getSubKey(String name, String rootPrefixes,
+                                    RelaxedNames keyPrefix) {
+        rootPrefixes = (rootPrefixes == null ? "" : rootPrefixes);
+        for (String rootPrefix : new RelaxedNames(rootPrefixes)) {
+            for (String candidateKeyPrefix : keyPrefix) {
+                if (name.startsWith(rootPrefix + candidateKeyPrefix)) {
+                    return name.substring((rootPrefix + candidateKeyPrefix).length());
+                }
+            }
+        }
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/48506574/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedNames.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedNames.java
b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedNames.java
new file mode 100644
index 0000000..680a168
--- /dev/null
+++ b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedNames.java
@@ -0,0 +1,258 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.spring.boot.util;
+
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.springframework.util.StringUtils;
+
+/**
+ * Generates relaxed name variations from a given source.
+ *
+ * @author Phillip Webb
+ * @author Dave Syer
+ * @see RelaxedPropertyResolver
+ */
+public final class RelaxedNames implements Iterable<String> {
+
+    private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([^A-Z-])([A-Z])");
+
+    private static final Pattern SEPARATED_TO_CAMEL_CASE_PATTERN = Pattern
+        .compile("[_\\-.]");
+
+    private final String name;
+
+    private final Set<String> values = new LinkedHashSet<String>();
+
+    /**
+     * Create a new {@link RelaxedNames} instance.
+     * @param name the source name. For the maximum number of variations specify the name
+     * using dashed notation (e.g. {@literal my-property-name}
+     */
+    public RelaxedNames(String name) {
+        this.name = (name == null ? "" : name);
+        initialize(RelaxedNames.this.name, this.values);
+    }
+
+    @Override
+    public Iterator<String> iterator() {
+        return this.values.iterator();
+    }
+
+    private void initialize(String name, Set<String> values) {
+        if (values.contains(name)) {
+            return;
+        }
+        for (Variation variation : Variation.values()) {
+            for (Manipulation manipulation : Manipulation.values()) {
+                String result = name;
+                result = manipulation.apply(result);
+                result = variation.apply(result);
+                values.add(result);
+                initialize(result, values);
+            }
+        }
+    }
+
+    /**
+     * Name variations.
+     */
+    enum Variation {
+
+        NONE {
+
+            @Override
+            public String apply(String value) {
+                return value;
+            }
+
+        },
+
+        LOWERCASE {
+
+            @Override
+            public String apply(String value) {
+                return value.isEmpty() ? value : value.toLowerCase();
+            }
+
+        },
+
+        UPPERCASE {
+
+            @Override
+            public String apply(String value) {
+                return value.isEmpty() ? value : value.toUpperCase();
+            }
+
+        };
+
+        public abstract String apply(String value);
+
+    }
+
+    /**
+     * Name manipulations.
+     */
+    enum Manipulation {
+
+        NONE {
+
+            @Override
+            public String apply(String value) {
+                return value;
+            }
+
+        },
+
+        HYPHEN_TO_UNDERSCORE {
+
+            @Override
+            public String apply(String value) {
+                return value.indexOf('-') != -1 ? value.replace('-', '_') : value;
+            }
+
+        },
+
+        UNDERSCORE_TO_PERIOD {
+
+            @Override
+            public String apply(String value) {
+                return value.indexOf('_') != -1 ? value.replace('_', '.') : value;
+            }
+
+        },
+
+        PERIOD_TO_UNDERSCORE {
+
+            @Override
+            public String apply(String value) {
+                return value.indexOf('.') != -1 ? value.replace('.', '_') : value;
+            }
+
+        },
+
+        CAMELCASE_TO_UNDERSCORE {
+
+            @Override
+            public String apply(String value) {
+                if (value.isEmpty()) {
+                    return value;
+                }
+                Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
+                if (!matcher.find()) {
+                    return value;
+                }
+                matcher = matcher.reset();
+                StringBuffer result = new StringBuffer();
+                while (matcher.find()) {
+                    matcher.appendReplacement(result, matcher.group(1) + '_'
+                        + StringUtils.uncapitalize(matcher.group(2)));
+                }
+                matcher.appendTail(result);
+                return result.toString();
+            }
+
+        },
+
+        CAMELCASE_TO_HYPHEN {
+
+            @Override
+            public String apply(String value) {
+                if (value.isEmpty()) {
+                    return value;
+                }
+                Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
+                if (!matcher.find()) {
+                    return value;
+                }
+                matcher = matcher.reset();
+                StringBuffer result = new StringBuffer();
+                while (matcher.find()) {
+                    matcher.appendReplacement(result, matcher.group(1) + '-'
+                        + StringUtils.uncapitalize(matcher.group(2)));
+                }
+                matcher.appendTail(result);
+                return result.toString();
+            }
+
+        },
+
+        SEPARATED_TO_CAMELCASE {
+
+            @Override
+            public String apply(String value) {
+                return separatedToCamelCase(value, false);
+            }
+
+        },
+
+        CASE_INSENSITIVE_SEPARATED_TO_CAMELCASE {
+
+            @Override
+            public String apply(String value) {
+                return separatedToCamelCase(value, true);
+            }
+
+        };
+
+        private static final char[] SUFFIXES = new char[] { '_', '-', '.' };
+
+        public abstract String apply(String value);
+
+        private static String separatedToCamelCase(String value,
+                                                   boolean caseInsensitive) {
+            if (value.isEmpty()) {
+                return value;
+            }
+            StringBuilder builder = new StringBuilder();
+            for (String field : SEPARATED_TO_CAMEL_CASE_PATTERN.split(value)) {
+                field = (caseInsensitive ? field.toLowerCase() : field);
+                builder.append(
+                    builder.length() == 0 ? field : StringUtils.capitalize(field));
+            }
+            char lastChar = value.charAt(value.length() - 1);
+            for (char suffix : SUFFIXES) {
+                if (lastChar == suffix) {
+                    builder.append(suffix);
+                    break;
+                }
+            }
+            return builder.toString();
+        }
+
+    }
+
+    /**
+     * Return a {@link RelaxedNames} for the given source camelCase source name.
+     * @param name the source name in camelCase
+     * @return the relaxed names
+     */
+    public static RelaxedNames forCamelCase(String name) {
+        StringBuilder result = new StringBuilder();
+        for (char c : name.toCharArray()) {
+            result.append(Character.isUpperCase(c) && result.length() > 0
+                && result.charAt(result.length() - 1) != '-'
+                ? "-" + Character.toLowerCase(c) : c);
+        }
+        return new RelaxedNames(result.toString());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/48506574/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedPropertyResolver.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedPropertyResolver.java
b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedPropertyResolver.java
new file mode 100644
index 0000000..d169966
--- /dev/null
+++ b/components/camel-spring-boot2/src/main/java/org/apache/camel/spring/boot/util/RelaxedPropertyResolver.java
@@ -0,0 +1,155 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.spring.boot.util;
+
+import java.util.Map;
+
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.PropertyResolver;
+import org.springframework.core.env.PropertySourcesPropertyResolver;
+import org.springframework.util.Assert;
+
+/**
+ * {@link PropertyResolver} that attempts to resolve values using {@link RelaxedNames}.
+ *
+ * @author Phillip Webb
+ * @see RelaxedNames
+ */
+public class RelaxedPropertyResolver implements PropertyResolver {
+
+    private final PropertyResolver resolver;
+
+    private final String prefix;
+
+    public RelaxedPropertyResolver(PropertyResolver resolver) {
+        this(resolver, null);
+    }
+
+    public RelaxedPropertyResolver(PropertyResolver resolver, String prefix) {
+        Assert.notNull(resolver, "PropertyResolver must not be null");
+        this.resolver = resolver;
+        this.prefix = (prefix == null ? "" : prefix);
+    }
+
+    @Override
+    public String getRequiredProperty(String key) throws IllegalStateException {
+        return getRequiredProperty(key, String.class);
+    }
+
+    @Override
+    public <T> T getRequiredProperty(String key, Class<T> targetType)
+        throws IllegalStateException {
+        T value = getProperty(key, targetType);
+        Assert.state(value != null, String.format("required key [%s] not found", key));
+        return value;
+    }
+
+    @Override
+    public String getProperty(String key) {
+        return getProperty(key, String.class, null);
+    }
+
+    @Override
+    public String getProperty(String key, String defaultValue) {
+        return getProperty(key, String.class, defaultValue);
+    }
+
+    @Override
+    public <T> T getProperty(String key, Class<T> targetType) {
+        return getProperty(key, targetType, null);
+    }
+
+    @Override
+    public <T> T getProperty(String key, Class<T> targetType, T defaultValue)
{
+        RelaxedNames prefixes = new RelaxedNames(this.prefix);
+        RelaxedNames keys = new RelaxedNames(key);
+        for (String prefix : prefixes) {
+            for (String relaxedKey : keys) {
+                if (this.resolver.containsProperty(prefix + relaxedKey)) {
+                    return this.resolver.getProperty(prefix + relaxedKey, targetType);
+                }
+            }
+        }
+        return defaultValue;
+    }
+
+    @Override
+    public boolean containsProperty(String key) {
+        RelaxedNames prefixes = new RelaxedNames(this.prefix);
+        RelaxedNames keys = new RelaxedNames(key);
+        for (String prefix : prefixes) {
+            for (String relaxedKey : keys) {
+                if (this.resolver.containsProperty(prefix + relaxedKey)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public String resolvePlaceholders(String text) {
+        throw new UnsupportedOperationException(
+            "Unable to resolve placeholders with relaxed properties");
+    }
+
+    @Override
+    public String resolveRequiredPlaceholders(String text)
+        throws IllegalArgumentException {
+        throw new UnsupportedOperationException(
+            "Unable to resolve placeholders with relaxed properties");
+    }
+
+    /**
+     * Return a Map of all values from all underlying properties that start with the
+     * specified key. NOTE: this method can only be used if the underlying resolver is a
+     * {@link ConfigurableEnvironment}.
+     * @param keyPrefix the key prefix used to filter results
+     * @return a map of all sub properties starting with the specified key prefix.
+     * @see PropertySourceUtils#getSubProperties
+     */
+    public Map<String, Object> getSubProperties(String keyPrefix) {
+        Assert.isInstanceOf(ConfigurableEnvironment.class, this.resolver,
+            "SubProperties not available.");
+        ConfigurableEnvironment env = (ConfigurableEnvironment) this.resolver;
+        return PropertySourceUtils.getSubProperties(env.getPropertySources(), this.prefix,
+            keyPrefix);
+    }
+
+    /**
+     * Return a property resolver for the environment, preferring one that ignores
+     * unresolvable nested placeholders.
+     * @param environment the source environment
+     * @param prefix the prefix
+     * @return a property resolver for the environment
+     * @since 1.4.3
+     */
+    public static RelaxedPropertyResolver ignoringUnresolvableNestedPlaceholders(
+        Environment environment, String prefix) {
+        Assert.notNull(environment, "Environment must not be null");
+        PropertyResolver resolver = environment;
+        if (environment instanceof ConfigurableEnvironment) {
+            resolver = new PropertySourcesPropertyResolver(
+                ((ConfigurableEnvironment) environment).getPropertySources());
+            ((PropertySourcesPropertyResolver) resolver)
+                .setIgnoreUnresolvableNestedPlaceholders(true);
+        }
+        return new RelaxedPropertyResolver(resolver, prefix);
+    }
+
+}


Mime
View raw message