You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2018/11/02 23:59:38 UTC

[jspwiki] 09/17: use try with resources

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit f36ad8f85985686bde64c2c5c850a52fb2dbf0b3
Author: juanpablo <ju...@apache.org>
AuthorDate: Fri Nov 2 22:04:07 2018 +0100

    use try with resources
---
 .../java/org/apache/wiki/util/PropertyReader.java  | 58 +++++++++++-----------
 1 file changed, 28 insertions(+), 30 deletions(-)

diff --git a/jspwiki-main/src/main/java/org/apache/wiki/util/PropertyReader.java b/jspwiki-main/src/main/java/org/apache/wiki/util/PropertyReader.java
index 5b5ed05..81cdfb5 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/util/PropertyReader.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/util/PropertyReader.java
@@ -20,6 +20,7 @@ package org.apache.wiki.util;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Enumeration;
@@ -34,7 +35,6 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.Validate;
 import org.apache.log4j.Logger;
-import org.apache.wiki.Release;
 
 
 /**
@@ -114,22 +114,8 @@ public final class PropertyReader {
      */
     public static Properties loadWebAppProps( ServletContext context ) {
         String propertyFile = getInitParameter( context, PARAM_CUSTOMCONFIG );
-        InputStream propertyStream = null;
-
-        try {
-            //
-            //  Figure out where our properties lie.
-            //
-            if( propertyFile == null ) {
-                LOG.info( "No " + PARAM_CUSTOMCONFIG + " defined for this context, " +
-                             "looking for custom properties file with default name of: " + CUSTOM_JSPWIKI_CONFIG );
-                //  Use the custom property file at the default location
-                propertyStream =  locateClassPathResource(context, CUSTOM_JSPWIKI_CONFIG);
-            } else {
-                LOG.info(PARAM_CUSTOMCONFIG + " defined, using " + propertyFile + " as the custom properties file.");
-                propertyStream = new FileInputStream( new File(propertyFile) );
-            }
 
+        try( InputStream propertyStream = loadCustomPropertiesFile(context, propertyFile) ) {
             Properties props = getDefaultProperties();
             if( propertyStream == null ) {
                 LOG.info("No custom property file found, relying on JSPWiki defaults.");
@@ -148,15 +134,33 @@ public final class PropertyReader {
 
             return props;
         } catch( Exception e ) {
-            LOG.error( Release.APPNAME + ": Unable to load and setup properties from jspwiki.properties. " +
-                         e.getMessage() );
-        } finally {
-        	IOUtils.closeQuietly( propertyStream );
+            LOG.error( "JSPWiki: Unable to load and setup properties from jspwiki.properties. " + e.getMessage() );
         }
 
         return null;
     }
 
+    /**
+     * Figure out where our properties lie.
+     * 
+     * @param context
+     * @param propertyFile
+     * @return
+     * @throws FileNotFoundException
+     */
+	static InputStream loadCustomPropertiesFile(ServletContext context, String propertyFile) throws FileNotFoundException {
+		InputStream propertyStream;
+		if( propertyFile == null ) {
+		    LOG.info( "No " + PARAM_CUSTOMCONFIG + " defined for this context, looking for custom properties file with default name of: " + CUSTOM_JSPWIKI_CONFIG );
+		    //  Use the custom property file at the default location
+		    propertyStream =  locateClassPathResource(context, CUSTOM_JSPWIKI_CONFIG);
+		} else {
+		    LOG.info( PARAM_CUSTOMCONFIG + " defined, using " + propertyFile + " as the custom properties file." );
+		    propertyStream = new FileInputStream( new File(propertyFile) );
+		}
+		return propertyStream;
+	}
+
 
     /**
      *  Returns the property set as a Properties object.
@@ -236,7 +240,6 @@ public final class PropertyReader {
         // get into cascade...
         int depth = 0;
         boolean more = true;
-        InputStream propertyStream = null;
         while( more ) {
             depth++;
             String propertyFile = getInitParameter( context, PARAM_CUSTOMCONFIG_CASCADEPREFIX + depth );
@@ -246,18 +249,13 @@ public final class PropertyReader {
                 break;
             }
 
-            try {
+            try( InputStream propertyStream = new FileInputStream( new File( propertyFile ) ) ) {
                 LOG.info( " Reading additional properties from " + propertyFile + " and merge to cascade." );
                 Properties additionalProps = new Properties();
-                propertyStream = new FileInputStream( new File( propertyFile ) );
-                additionalProps.load(propertyStream);
-                defaultProperties.putAll(additionalProps);
+                additionalProps.load( propertyStream );
+                defaultProperties.putAll( additionalProps );
             } catch( Exception e ) {
-                LOG.error( " " + Release.APPNAME +
-                             ": Unable to load and setup properties from " + propertyFile + "." +
-                             e.getMessage() );
-            } finally {
-            	IOUtils.closeQuietly( propertyStream );
+                LOG.error( "JSPWiki: Unable to load and setup properties from " + propertyFile + "." + e.getMessage() );
             }
         }