[java-opensaml] 02/03: IDP-2156: Wire in support for feeding properties from the IdP ...

Brent Putman putmanb at georgetown.edu
Fri Aug 25 03:15:32 UTC 2023


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch main
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=20cd4b9024b2d4031e365f7755ff9df69edb042a

commit 20cd4b9024b2d4031e365f7755ff9df69edb042a
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Wed Aug 23 23:21:06 2023 -0400

    IDP-2156: Wire in support for feeding properties from the IdP ...
    
    Wire in support for feeding properties from the IdP into OpenSAML's
    configuration
---
 .../opensaml/core/config/ConfigurationService.java | 71 +++++++++++++++++++++-
 .../SpringConfigurationPropertiesSource.java       | 51 ++++++++++++++++
 .../spring/config/SpringPropertiesAdapter.java     | 59 ++++++++++++++++++
 .../org/opensaml/spring/config/package-info.java   | 21 +++++++
 4 files changed, 199 insertions(+), 3 deletions(-)

diff --git a/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java b/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java
index 6f5be68c6..21777ae6d 100644
--- a/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java
+++ b/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java
@@ -71,6 +71,9 @@ public class ConfigurationService {
     /** The configuration instance to use. */
     @Nullable private static Configuration configuration;
     
+    /** A default configuration properties source to use. */
+    @Nullable private static ConfigurationPropertiesSource defaultConfigurationPropertiesSource;
+    
     /** Constructor. */
     protected ConfigurationService() { }
     
@@ -144,9 +147,25 @@ public class ConfigurationService {
      * service itself.
      * 
      * <p>
-     * The properties set is obtained from the first registered instance of 
+     * The properties set is obtained from the first configured instance of 
      * {@link ConfigurationPropertiesSource} which returns a non-null properties set.
-     * The implementations of properties sources to use are obtained via the Java Services API.
+     * </p>
+     * 
+     * <p>
+     * The first properties source to evaluate is the instance configured by
+     * {@link #setDefaultConfigurationPropertiesSource(ConfigurationPropertiesSource)}.
+     * </p>
+     * 
+     * <p>
+     * If that is null or produces a null properties set, and there are no properties sources 
+     * configured via the Java Services API, then a default implementation which exposes the
+     * standard Java system properties from {@link System#getProperties()} set is used.
+     * </p>
+     * 
+     * <p>
+     * If properties sources are configured via the Java Services API, then those are 
+     * evaluated in order, and the first non-null properties set returned is used.
+     * If no configured sources return a properties set, then null is returned.
      * </p>
      * 
      * <p>
@@ -157,7 +176,21 @@ public class ConfigurationService {
      * @return the set of configuration meta-properties
      */
     @Nullable public static ConfigurationProperties getConfigurationProperties() {
-        LOG.trace("Resolving configuration properties source");
+        final ConfigurationPropertiesSource defaultSource = defaultConfigurationPropertiesSource;
+        if (defaultSource != null) {
+            final ConfigurationProperties props = defaultSource.getProperties();
+            if (props != null) {
+                LOG.trace("Resolved configuration properties from configured default properties source: {}",
+                        defaultSource.getClass().getName());
+                return props;
+            } else {
+                LOG.trace("A default properties source was configured, but produced a null properties set");
+            }
+        } else {
+            LOG.trace("No default configuration properties source was configured");
+        }
+
+        LOG.trace("Attempting to resolve configuration properties source candidates via Java Services API");
         final Iterator<ConfigurationPropertiesSource> iter = configPropertiesLoader.iterator();
         
         if (!iter.hasNext()) {
@@ -179,6 +212,38 @@ public class ConfigurationService {
         return null;
     }
     
+    /**
+     * Get the default {@link ConfigurationPropertiesSource} instance to use.
+     * 
+     * <p>
+     * The configuration properties source to use is normally resolved via the Java Services API,
+     * or is defaulted if none are configured. However, this method is provided to allow the default
+     * properties source instance to be supplied externally, perhaps using an application-specific
+     * means such as Spring dependency injection.
+     * </p>
+     * 
+     * @return the default ConfigurationPropertiesSource, possibly null
+     */
+    @Nullable public static ConfigurationPropertiesSource getDefaultConfigurationPropertiesSource() {
+        return defaultConfigurationPropertiesSource;
+    }
+    
+    /**
+     * Set the default {@link ConfigurationPropertiesSource} instance to use.
+     * 
+     * <p>
+     * The configuration properties source to use is normally resolved via the Java Services API,
+     * or is defaulted if none are configured. However, this method is provided to allow the default
+     * properties source instance to be supplied externally, perhaps using an application-specific
+     * means such as Spring dependency injection.
+     * </p>
+     * 
+     * @param source the default ConfigurationPropertiesSource instance to use
+     */
+    public static void setDefaultConfigurationPropertiesSource(@Nullable final ConfigurationPropertiesSource source) {
+        defaultConfigurationPropertiesSource = source;
+    }
+    
     /**
      * Set the {@link Configuration} instance to use.
      * 
diff --git a/opensaml-spring/src/main/java/org/opensaml/spring/config/SpringConfigurationPropertiesSource.java b/opensaml-spring/src/main/java/org/opensaml/spring/config/SpringConfigurationPropertiesSource.java
new file mode 100644
index 000000000..fc210af37
--- /dev/null
+++ b/opensaml-spring/src/main/java/org/opensaml/spring/config/SpringConfigurationPropertiesSource.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed 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.opensaml.spring.config;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.core.config.ConfigurationProperties;
+import org.opensaml.core.config.ConfigurationPropertiesSource;
+import org.springframework.core.env.PropertyResolver;
+
+import net.shibboleth.shared.logic.Constraint;
+
+/**
+ * An implementation of {@link ConfigurationPropertiesSource} that delegates to a supplied
+ * Spring {@link PropertyResolver}.
+ */
+public class SpringConfigurationPropertiesSource implements ConfigurationPropertiesSource {
+    
+    /** The supplied property resolver. */
+    @Nonnull private final PropertyResolver propertyResolver;
+
+    /**
+     * Constructor.
+     *
+     * @param resolver the Spring property resolver
+     */
+    public SpringConfigurationPropertiesSource(@Nonnull final PropertyResolver resolver) {
+        propertyResolver = Constraint.isNotNull(resolver, "PropertyResolver was null");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    @Nullable
+    public ConfigurationProperties getProperties() {
+        return new SpringPropertiesAdapter(propertyResolver);
+    }
+
+}
diff --git a/opensaml-spring/src/main/java/org/opensaml/spring/config/SpringPropertiesAdapter.java b/opensaml-spring/src/main/java/org/opensaml/spring/config/SpringPropertiesAdapter.java
new file mode 100644
index 000000000..6a8c4aeff
--- /dev/null
+++ b/opensaml-spring/src/main/java/org/opensaml/spring/config/SpringPropertiesAdapter.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed 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.opensaml.spring.config;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.core.config.ConfigurationProperties;
+import org.springframework.core.env.PropertyResolver;
+
+import net.shibboleth.shared.logic.Constraint;
+
+/**
+ * An implementation of {@link ConfigurationProperties} which wraps an instance of Spring {@link PropertyResolver}.
+ */
+public class SpringPropertiesAdapter implements ConfigurationProperties {
+    
+    /** The wrapped properties instance. */
+    @Nonnull private PropertyResolver propertyResolver;
+
+    /**
+     * Constructor.
+     *
+     * @param wrappedResolver the wrapped properties resolver instance
+     */
+    public SpringPropertiesAdapter(@Nonnull final PropertyResolver wrappedResolver) {
+        propertyResolver = Constraint.isNotNull(wrappedResolver, "Wrapped PropertyResolver was null");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    @Nullable
+    public String getProperty(@Nonnull final String key) {
+        Constraint.isNotNull(key, "Key was null");
+        return propertyResolver.getProperty(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    @Nonnull
+    public String getProperty(@Nonnull final String key, @Nonnull final String defaultValue) {
+        Constraint.isNotNull(key, "Key was null");
+        Constraint.isNotNull(defaultValue, "Default value was null");
+        return propertyResolver.getProperty(key, defaultValue);
+    }
+
+}
diff --git a/opensaml-spring/src/main/java/org/opensaml/spring/config/package-info.java b/opensaml-spring/src/main/java/org/opensaml/spring/config/package-info.java
new file mode 100644
index 000000000..8ec4ae61f
--- /dev/null
+++ b/opensaml-spring/src/main/java/org/opensaml/spring/config/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed 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.
+ */
+
+/**
+ * Beans for configuration classes.
+ */
+ at NonnullElements
+package org.opensaml.spring.config;
+
+import net.shibboleth.shared.annotation.constraint.NonnullElements;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list