[java-shib-shared] branch main updated: Fix null and annotation bugs.

Scott Cantor cantor.2 at osu.edu
Mon Nov 7 21:13:55 UTC 2022


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

scantor pushed a commit to branch main
in repository java-shib-shared.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=d414afa41b148a7115542afd43f0ce054e18543d

The following commit(s) were added to refs/heads/main by this push:
     new d414afa4 Fix null and annotation bugs.
d414afa4 is described below

commit d414afa41b148a7115542afd43f0ce054e18543d
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon Nov 7 16:13:52 2022 -0500

    Fix null and annotation bugs.
---
 .../net/shibboleth/shared/cli/AbstractCommandLine.java  | 12 +++++++-----
 .../shared/spring/velocity/SpringResourceLoader.java    | 17 ++++++++++++-----
 .../shared/spring/velocity/VelocityConfigurer.java      | 14 +++++---------
 .../shared/spring/velocity/VelocityEngineFactory.java   | 13 ++++++++-----
 .../shibboleth/shared/spring/velocity/VelocityView.java | 16 +++++++++++++---
 5 files changed, 45 insertions(+), 27 deletions(-)

diff --git a/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java b/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java
index c729196e..c851f6b9 100644
--- a/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java
+++ b/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java
@@ -151,10 +151,11 @@ public abstract class AbstractCommandLine<T extends CommandLineArguments> {
      * @return Spring context
      */
     @Nonnull protected GenericApplicationContext getApplicationContext() {
-        if (applicationContext == null) {
-            throw new IllegalStateException("No application context installed");
+        if (applicationContext != null) {
+            return applicationContext;
         }
-        return applicationContext;
+        
+        throw new IllegalStateException("No application context installed");
     }
 
     /**
@@ -231,8 +232,9 @@ public abstract class AbstractCommandLine<T extends CommandLineArguments> {
      * @param args command line arguments
      */
     protected void initLogging(@Nonnull final T args) {
-        if (args.getLoggingConfiguration() != null) {
-            setLoggingProperty(args.getLoggingConfiguration());
+        final String lc = args.getLoggingConfiguration();
+        if (lc != null) {
+            setLoggingProperty(lc);
         } else if (args.isVerboseOutput()) {
             setLoggingToLocalResource("logger-verbose.xml");
         } else if (args.isQuietOutput()) {
diff --git a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/SpringResourceLoader.java b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/SpringResourceLoader.java
index f2c5d7e3..6b75aa5b 100644
--- a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/SpringResourceLoader.java
+++ b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/SpringResourceLoader.java
@@ -79,8 +79,13 @@ public class SpringResourceLoader extends ResourceLoader {
     @Nullable private org.springframework.core.io.ResourceLoader resourceLoader;
 
     /** Resource loader paths. */
-    @Nullable private String[] resourceLoaderPaths;
+    @Nonnull private String[] resourceLoaderPaths;
 
+    /** Constructor. */
+    public SpringResourceLoader() {
+        resourceLoaderPaths = new String[0];
+    }
+    
     /** {@inheritDoc} */
     @Override
     public void init(final ExtProperties configuration) {
@@ -110,14 +115,16 @@ public class SpringResourceLoader extends ResourceLoader {
 
     /** {@inheritDoc} */
     @Override
-    public Reader getResourceReader(final String source, final String encoding)
-            throws ResourceNotFoundException {
+    public Reader getResourceReader(final String source, final String encoding) throws ResourceNotFoundException {
+        
         log.debug("Looking for Velocity resource with name '{}'", source);
         for (final String resourceLoaderPath : resourceLoaderPaths) {
             final org.springframework.core.io.Resource resource =
-                    resourceLoader.getResource(resourceLoaderPath + source);
+                    resourceLoader != null ? resourceLoader.getResource(resourceLoaderPath + source) : null;
             try {
-                return new InputStreamReader(resource.getInputStream(), encoding);
+                if (resource != null) {
+                    return new InputStreamReader(resource.getInputStream(), encoding);
+                }
             } catch (final IOException ex) {
                 log.debug("Could not find Velocity resource: {}", resource);
             }
diff --git a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityConfigurer.java b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityConfigurer.java
index 9d3a15b6..03145abb 100644
--- a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityConfigurer.java
+++ b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityConfigurer.java
@@ -89,7 +89,7 @@ public class VelocityConfigurer extends VelocityEngineFactory
     @Nullable private VelocityEngine velocityEngine;
     
     /** {@inheritDoc} */
-    public void setServletContext(final ServletContext context) {
+    public void setServletContext(@Nonnull final ServletContext context) {
         servletContext = context;
     }
     
@@ -113,15 +113,11 @@ public class VelocityConfigurer extends VelocityEngineFactory
      */
     @Override
     protected void postProcessVelocityEngine(@Nonnull final VelocityEngine engine) {
+        engine.setApplicationAttribute(ServletContext.class.getName(), servletContext);
+        engine.setProperty(SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
+        engine.addProperty(VelocityEngine.RESOURCE_LOADERS, SPRING_MACRO_RESOURCE_LOADER_NAME);
+        engine.addProperty(VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);
         velocityEngine = engine;
-        
-        velocityEngine.setApplicationAttribute(ServletContext.class.getName(), servletContext);
-        velocityEngine.setProperty(
-                SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
-        velocityEngine.addProperty(
-                VelocityEngine.RESOURCE_LOADERS, SPRING_MACRO_RESOURCE_LOADER_NAME);
-        velocityEngine.addProperty(
-                VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);
     }
 
     /** {@inheritDoc} */
diff --git a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityEngineFactory.java b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityEngineFactory.java
index 5870580f..fe8fe48e 100644
--- a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityEngineFactory.java
+++ b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityEngineFactory.java
@@ -237,9 +237,10 @@ public class VelocityEngineFactory {
         final Map<String,Object> props = new HashMap<>();
 
         // Load config file if set.
-        if (configLocation != null) {
-            log.info("Loading Velocity config from '{}'", configLocation);
-            CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(configLocation), props);
+        final Resource configLocation2 = configLocation;
+        if (configLocation2 != null) {
+            log.info("Loading Velocity config from '{}'", configLocation2);
+            CollectionUtils.mergePropertiesIntoMap(PropertiesLoaderUtils.loadProperties(configLocation2), props);
         }
 
         // Merge local properties if set.
@@ -300,7 +301,9 @@ public class VelocityEngineFactory {
      */
     protected void initVelocityResourceLoader(@Nonnull final VelocityEngine velocityEngine,
             @Nullable final String loaderPath) {
-        if (preferFileSystemAccess && resourceLoader != null) {
+        
+        final ResourceLoader loader = resourceLoader;
+        if (preferFileSystemAccess && loader != null) {
             // Try to load via the file system, fall back to SpringResourceLoader
             // (for hot detection of template changes, if possible).
             
@@ -321,7 +324,7 @@ public class VelocityEngineFactory {
                 }
                 
                 try {
-                    final Resource resource = resourceLoader.getResource(path);
+                    final Resource resource = loader.getResource(path);
                     
                     // Will fail if not resolvable in the file system.
                     final File file = resource.getFile();  
diff --git a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityView.java b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityView.java
index 711ad707..83e753de 100644
--- a/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityView.java
+++ b/shib-velocity-spring/src/main/java/net/shibboleth/shared/spring/velocity/VelocityView.java
@@ -33,6 +33,7 @@ import org.apache.velocity.exception.ResourceNotFoundException;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.BeanFactoryUtils;
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextException;
 import org.springframework.web.servlet.view.AbstractTemplateView;
 import org.springframework.web.util.NestedServletException;
@@ -174,8 +175,12 @@ public class VelocityView extends AbstractTemplateView {
      */
     @Nonnull protected VelocityEngine autodetectVelocityEngine() throws BeansException {
         try {
+            final ApplicationContext context = getApplicationContext();
+            if (context == null) {
+                throw new ApplicationContextException("ApplicationContext not set");
+            }
             final VelocityConfig velocityConfig = BeanFactoryUtils.beanOfTypeIncludingAncestors(
-                    getApplicationContext(), VelocityConfig.class, true, false);
+                    context, VelocityConfig.class, true, false);
             return velocityConfig.getVelocityEngine();
         } catch (final NoSuchBeanDefinitionException ex) {
             throw new ApplicationContextException(
@@ -337,9 +342,14 @@ public class VelocityView extends AbstractTemplateView {
      * @throws Exception if thrown by Velocity
      */
     protected Template getTemplate(final String name) throws Exception {
+        final VelocityEngine engine = getVelocityEngine();
+        if (engine == null) {
+            throw new ResourceNotFoundException("VelocityEngine not set");
+        }
+        
         return getEncoding() != null ?
-                getVelocityEngine().getTemplate(name, getEncoding()) :
-                getVelocityEngine().getTemplate(name);
+                engine.getTemplate(name, getEncoding()) :
+                engine.getTemplate(name);
     }
 
     /**

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


More information about the commits mailing list