[spring-extensions] branch master updated: JSE-38 - Provide a command line wrapper for Spring contexts

Scott Cantor cantor.2 at osu.edu
Wed Jul 1 17:23:54 UTC 2020


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

scantor pushed a commit to branch master
in repository spring-extensions.

View the commit online:
http://git.shibboleth.net/view/?p=spring-extensions.git;a=commit;h=6adcd52b3265ca095745ecfe79ae9fe4a60fb20b

The following commit(s) were added to refs/heads/master by this push:
       new  6adcd52   JSE-38 - Provide a command line wrapper for Spring contexts
6adcd52 is described below

commit 6adcd52b3265ca095745ecfe79ae9fe4a60fb20b
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Jul 1 13:23:50 2020 -0400

    JSE-38 - Provide a command line wrapper for Spring contexts
    
    https://issues.shibboleth.net/jira/browse/JSE-38
    
    Add property source support.
---
 .../ext/spring/cli/AbstractCommandLine.java        | 57 +++++++++++++++++++++-
 .../spring/cli/AbstractCommandLineArguments.java   | 19 +++++---
 .../ext/spring/cli/CommandLineArguments.java       |  7 +++
 3 files changed, 73 insertions(+), 10 deletions(-)

diff --git a/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLine.java b/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLine.java
index 664a936..164944e 100644
--- a/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLine.java
+++ b/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLine.java
@@ -17,20 +17,30 @@
 
 package net.shibboleth.ext.spring.cli;
 
+import java.io.IOException;
 import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import org.slf4j.Logger;
 import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.env.PropertySource;
 import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.core.io.support.PropertiesLoaderUtils;
+import org.springframework.core.io.support.ResourcePropertySource;
 
 import com.beust.jcommander.JCommander;
 
 import net.shibboleth.ext.spring.resource.PreferFileSystemResourceLoader;
 import net.shibboleth.ext.spring.util.ApplicationContextBuilder;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.logic.Constraint;
 
 /**
  * A simple driver for a Spring-based CLI.
@@ -134,6 +144,30 @@ public abstract class AbstractCommandLine<T extends CommandLineArguments> {
         }
     }
 
+    /**
+     * Merge in properties from the resource.
+     * 
+     * @param sink if non-null use this instance as the target
+     * @param resource the resource
+     * @return properties loaded from the resource or {@code  null} if loading failed
+     */
+    @Nullable public Properties loadProperties(@Nullable final Properties sink, @Nonnull final Resource resource) {
+        Constraint.isNotNull(resource, "Resource cannot be null");
+        try {
+            final Properties properties;
+            if (sink != null) {
+                properties = sink;
+            } else {
+                properties = new Properties();
+            }
+            PropertiesLoaderUtils.fillProperties(properties, resource);
+            return properties;
+        } catch (final IOException e) {
+            getLogger().warn("Unable to load properties from resource '{}'", resource, e);
+            return null;
+        }
+    }
+
     /**
      * The execution method to override.
      * 
@@ -145,11 +179,30 @@ public abstract class AbstractCommandLine<T extends CommandLineArguments> {
      */
     protected int doRun(@Nonnull final T args) {
         try {
-            final Resource config = new PreferFileSystemResourceLoader().getResource(args.getOtherArgs().get(0));
+            final ResourceLoader loader = new PreferFileSystemResourceLoader();
+            final Resource config = loader.getResource(args.getOtherArgs().get(0));
             
             getLogger().debug("Initializing Spring context with configuration file {}", config.getURI());
+
+            final List<Resource> resources =
+                    args.getPropertyFiles().stream().map(loader::getResource).collect(Collectors.toUnmodifiableList());
+            final List<PropertySource<?>> propertySources = new ArrayList<>(resources.size());
+            resources.forEach(r -> {
+                try {
+                    propertySources.add(new ResourcePropertySource(r));
+                } catch (final IOException e) {
+                    if (args.isVerboseOutput()) {
+                        getLogger().error("Unable to load properties from {}", r, e);
+                    } else {
+                        getLogger().error("Unable to load properties from {}", r, e.getMessage());
+                    }
+                }
+            });
             
-            applicationContext = new ApplicationContextBuilder().setServiceConfiguration(config).build();
+            applicationContext = new ApplicationContextBuilder()
+                    .setServiceConfiguration(config)
+                    .setPropertySources(propertySources)
+                    .build();
             
             // Register a shutdown hook for the context, so that beans will be
             // correctly destroyed before the CLI exits.
diff --git a/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLineArguments.java b/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLineArguments.java
index a3d04d5..c1d7fff 100644
--- a/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLineArguments.java
+++ b/src/main/java/net/shibboleth/ext/spring/cli/AbstractCommandLineArguments.java
@@ -40,8 +40,6 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
     @Parameter
     @Nonnull private List<String> otherArgs = new ArrayList<>();
     
-    // Logging
-    
     /**
      * Verbose logging has been requested.
      */
@@ -59,8 +57,6 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
      */
     @Parameter(names = "--logConfig")
     @Nullable private String logConfig;
-
-    // Help
     
     /**
      * Help has been requested.
@@ -68,14 +64,16 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
     @Parameter(names = "--help", help=true)
     private boolean help;
 
-    // Version
-
     /**
      * Version has been requested.
      */
     @Parameter(names = "--version")
     private boolean version;
 
+    /** Spring property sources. */
+    @Parameter(names = "--propertyFiles")
+    @Nonnull private List<String> propertySources = new ArrayList<>();
+    
     /** {@inheritDoc} */
     public boolean isVerboseOutput() {
         return verbose;
@@ -101,6 +99,11 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
         return version;
     }
     
+    /** {@inheritDoc} */
+    @Nonnull @Unmodifiable @NotLive public List<String> getPropertyFiles() {
+        return propertySources;
+    }
+    
     /** {@inheritDoc} */
     @Nonnull @Unmodifiable @NotLive public List<String> getOtherArgs() {
         return otherArgs;
@@ -120,12 +123,12 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
         out.println();
         out.println(String.format("  --%-20s %s", "help", "Prints this help information"));
         out.println(String.format("  --%-20s %s", "version", "Prints version"));
+        out.println(String.format("  --%-20s %s", "propertyFiles", "Comma-separated list of Spring property files"));
         out.println();
 
         out.println("Logging Options - these options are mutually exclusive");
         out.println(String.format("  --%-20s %s", "verbose", "Turn on verbose messages."));
-        out.println(String.format("  --%-20s %s", "quiet",
-                "Restrict output messages to errors and warnings."));
+        out.println(String.format("  --%-20s %s", "quiet", "Restrict output messages to errors and warnings."));
         out.println();
         out.println(String.format("  --%-20s %s", "logConfig",
                 "Specifies a logback configuration file to use to configure logging."));
diff --git a/src/main/java/net/shibboleth/ext/spring/cli/CommandLineArguments.java b/src/main/java/net/shibboleth/ext/spring/cli/CommandLineArguments.java
index e859c3c..a2bf7a0 100644
--- a/src/main/java/net/shibboleth/ext/spring/cli/CommandLineArguments.java
+++ b/src/main/java/net/shibboleth/ext/spring/cli/CommandLineArguments.java
@@ -65,6 +65,13 @@ public interface CommandLineArguments {
      */
     boolean isVersion();
 
+    /**
+     * Get list of property filenames to load.
+     * 
+     * @return property filenames
+     */
+    @Nonnull @Unmodifiable @NotLive public List<String> getPropertyFiles();
+    
     /**
      * Get unparsed arguments.
      * 

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


More information about the commits mailing list