You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tv...@apache.org on 2018/08/16 10:37:08 UTC

svn commit: r1838186 - in /db/torque/torque4/trunk: torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ torque-generator/src/main/java/org/apache/torque/generator/configuration/option/ torque-generator/src/main/java/org...

Author: tv
Date: Thu Aug 16 10:37:08 2018
New Revision: 1838186

URL: http://svn.apache.org/viewvc?rev=1838186&view=rev
Log:
Use try-with-resources where applicable

Modified:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationXmlParser.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/PropertiesOptionConfiguration.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/XmlOptionConfiguration.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationXmlParser.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/jdbc/JdbcMetadataSource.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java
    db/torque/torque4/trunk/torque-maven-plugin/src/main/java/org/apache/torque/generator/maven/TorqueGeneratorMojo.java

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationXmlParser.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationXmlParser.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationXmlParser.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationXmlParser.java Thu Aug 16 10:37:08 2018
@@ -64,15 +64,7 @@ public class ControlConfigurationXmlPars
             saxFactory.setFeature(
                     "http://apache.org/xml/features/validation/schema", true);
         }
-        catch (SAXNotSupportedException e)
-        {
-            throw new RuntimeException(e);
-        }
-        catch (SAXNotRecognizedException e)
-        {
-            throw new RuntimeException(e);
-        }
-        catch (ParserConfigurationException e)
+        catch (SAXNotSupportedException | SAXNotRecognizedException | ParserConfigurationException e)
         {
             throw new RuntimeException(e);
         }

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/PropertiesOptionConfiguration.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/PropertiesOptionConfiguration.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/PropertiesOptionConfiguration.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/PropertiesOptionConfiguration.java Thu Aug 16 10:37:08 2018
@@ -24,8 +24,6 @@ import java.io.InputStream;
 import java.util.Collection;
 import java.util.Properties;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.torque.generator.configuration.ConfigurationException;
 import org.apache.torque.generator.configuration.ConfigurationProvider;
 import org.apache.torque.generator.option.Option;
@@ -37,10 +35,6 @@ import org.apache.torque.generator.optio
  */
 public class PropertiesOptionConfiguration extends FileOptionsConfiguration
 {
-    /** The class log. */
-    private static Log log
-            = LogFactory.getLog(PropertiesOptionConfiguration.class);
-
     /**
      * Reads the options from the property file given in the path and
      * returns them.
@@ -57,41 +51,16 @@ public class PropertiesOptionConfigurati
             throws ConfigurationException
     {
         Properties properties = new Properties();
-        InputStream optionsInputStream = null;
-        try
+        try (InputStream optionsInputStream = configurationProvider.getOptionsInputStream(getPath()))
         {
-            optionsInputStream
-                    = configurationProvider.getOptionsInputStream(getPath());
             properties.load(optionsInputStream);
         }
-        catch (IOException e)
-        {
-            throw new ConfigurationException("Error reading options file "
-                        + getPath(),
-                    e);
-        }
-        catch (RuntimeException e)
+        catch (IOException | RuntimeException e)
         {
             throw new ConfigurationException("Error reading options file "
                         + getPath(),
                     e);
         }
-        finally
-        {
-            if (optionsInputStream != null)
-            {
-                try
-                {
-                    optionsInputStream.close();
-                }
-                catch (IOException e)
-                {
-                    log.warn("Could not close OptionsInputStream "
-                                + optionsInputStream,
-                            e);
-                }
-            }
-        }
         return toOptions(properties);
     }
 }

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/XmlOptionConfiguration.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/XmlOptionConfiguration.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/XmlOptionConfiguration.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/option/XmlOptionConfiguration.java Thu Aug 16 10:37:08 2018
@@ -24,8 +24,6 @@ import java.io.InputStream;
 import java.util.Collection;
 import java.util.Properties;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.torque.generator.configuration.ConfigurationException;
 import org.apache.torque.generator.configuration.ConfigurationProvider;
 import org.apache.torque.generator.option.Option;
@@ -38,9 +36,6 @@ import org.apache.torque.generator.optio
  */
 public class XmlOptionConfiguration extends FileOptionsConfiguration
 {
-    /** The class log. */
-    private static Log log = LogFactory.getLog(XmlOptionConfiguration.class);
-
     /**
      * Reads the options from the XML file given in the path and
      * returns them.
@@ -57,43 +52,17 @@ public class XmlOptionConfiguration exte
             throws ConfigurationException
     {
         Properties properties = new Properties();
-        InputStream optionsInputStream = null;
-        try
+        try (InputStream optionsInputStream = configurationProvider.getOptionsInputStream(getPath()))
         {
-            optionsInputStream
-                    = configurationProvider.getOptionsInputStream(getPath());
             properties.loadFromXML(optionsInputStream);
         }
-        catch (IOException e)
+        catch (IOException | RuntimeException e)
         {
             throw new ConfigurationException("Error reading options file "
                         + getPath(),
                     e);
 
         }
-        catch (RuntimeException e)
-        {
-            throw new ConfigurationException("Error reading options file "
-                        + getPath(),
-                    e);
-
-        }
-        finally
-        {
-            if (optionsInputStream != null)
-            {
-                try
-                {
-                    optionsInputStream.close();
-                }
-                catch (IOException e)
-                {
-                    log.warn("Could not close OptionsInputStream "
-                                + optionsInputStream,
-                            e);
-                }
-            }
-        }
         return toOptions(properties);
     }
 }

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationXmlParser.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationXmlParser.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationXmlParser.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationXmlParser.java Thu Aug 16 10:37:08 2018
@@ -134,12 +134,9 @@ public class OutletConfigurationXmlParse
 
         for (String outletConfigName : outletConfigNames)
         {
-            InputStream inputStream = null;
-            try
+            try (InputStream inputStream = configurationProvider.getOutletConfigurationInputStream(
+                    outletConfigName))
             {
-                inputStream
-                        = configurationProvider.getOutletConfigurationInputStream(
-                            outletConfigName);
                 OutletConfigFileContent fileContent
                         = readOutletConfig(
                                 inputStream,
@@ -171,23 +168,6 @@ public class OutletConfigurationXmlParse
                             + outletConfigName,
                         e);
             }
-            finally
-            {
-                if (inputStream != null)
-                {
-                    try
-                    {
-                        inputStream.close();
-                    }
-                    catch (IOException e)
-                    {
-                        log.warn("Could not close "
-                                    + "outletConfigurationInputStream "
-                                    + outletConfigName,
-                                e);
-                    }
-                }
-            }
         }
         // add outlets defined implicitly by templates
         scanTemplatesForOutlets(

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/jdbc/JdbcMetadataSource.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/jdbc/JdbcMetadataSource.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/jdbc/JdbcMetadataSource.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/jdbc/JdbcMetadataSource.java Thu Aug 16 10:37:08 2018
@@ -173,11 +173,8 @@ public class JdbcMetadataSource extends
             log.debug("DB driver " + driver + " loaded");
         }
 
-        Connection con = null;
-        try
+        try (Connection con = DriverManager.getConnection(url, username, password))
         {
-
-            con = DriverManager.getConnection(url, username, password);
             log.debug("DB connection to database " + url + " established");
 
             DatabaseMetaData dbMetaData = con.getMetaData();
@@ -286,21 +283,7 @@ public class JdbcMetadataSource extends
             throw new SourceException(
                     "Could not retrieve JDBC Metadata from url " + url, e);
         }
-        finally
-        {
-            if (con != null)
-            {
-                try
-                {
-                    con.close();
-                }
-                catch (SQLException e)
-                {
-                    log.warn("Could not close database connection", e);
-                }
-                con = null;
-            }
-        }
+
         return rootElement;
     }
 
@@ -327,12 +310,10 @@ public class JdbcMetadataSource extends
     {
         log.debug("Getting table list...");
         List<String> tables = new ArrayList<String>();
-        ResultSet tableNames = null;
         // these are the entity types we want from the database
         String[] types = {"TABLE", "VIEW"};
-        try
+        try (ResultSet tableNames = dbMeta.getTables(null, dbSchema, "%", types))
         {
-            tableNames = dbMeta.getTables(null, dbSchema, "%", types);
             while (tableNames.next())
             {
                 String name = tableNames.getString(
@@ -340,13 +321,6 @@ public class JdbcMetadataSource extends
                 tables.add(name);
             }
         }
-        finally
-        {
-            if (tableNames != null)
-            {
-                tableNames.close();
-            }
-        }
         return tables;
     }
 
@@ -368,10 +342,8 @@ public class JdbcMetadataSource extends
             throws SQLException
     {
         List<ColumnMetadata> columns = new ArrayList<ColumnMetadata>();
-        ResultSet columnSet = null;
-        try
+        try (ResultSet columnSet = dbMeta.getColumns(null, dbSchema, tableName, null))
         {
-            columnSet = dbMeta.getColumns(null, dbSchema, tableName, null);
             while (columnSet.next())
             {
                 String name = columnSet.getString(
@@ -397,13 +369,7 @@ public class JdbcMetadataSource extends
                 columns.add(column);
             }
         }
-        finally
-        {
-            if (columnSet != null)
-            {
-                columnSet.close();
-            }
-        }
+
         return columns;
     }
 
@@ -423,23 +389,15 @@ public class JdbcMetadataSource extends
             throws SQLException
     {
         Set<String> pk = new HashSet<String>();
-        ResultSet parts = null;
-        try
+        try (ResultSet parts = dbMeta.getPrimaryKeys(null, schemaName, tableName))
         {
-            parts = dbMeta.getPrimaryKeys(null, schemaName, tableName);
             while (parts.next())
             {
                 pk.add(parts.getString(
                         COLUMN_NAME_POS_IN_PRIMARY_KEY_METADATA));
             }
         }
-        finally
-        {
-            if (parts != null)
-            {
-                parts.close();
-            }
-        }
+
         return pk;
     }
 
@@ -459,10 +417,8 @@ public class JdbcMetadataSource extends
     {
         Map<String, ForeignKeyMetadata> foreignKeys
                 = new HashMap<String, ForeignKeyMetadata>();
-        ResultSet resultSet = null;
-        try
+        try (ResultSet resultSet = dbMeta.getImportedKeys(null, schemaName, tableName))
         {
-            resultSet = dbMeta.getImportedKeys(null, schemaName, tableName);
             while (resultSet.next())
             {
                 String refTableName = resultSet.getString(
@@ -497,13 +453,7 @@ public class JdbcMetadataSource extends
                         + " : "
                         + e.getMessage());
         }
-        finally
-        {
-            if (resultSet != null)
-            {
-                resultSet.close();
-            }
-        }
+
         return foreignKeys.values();
     }
 
@@ -526,7 +476,7 @@ public class JdbcMetadataSource extends
     {
         // Although we could determine a checksum for the content,
         // doing so makes no sense because we cannot determine
-        // al last modified date.
+        // a last modified date.
         return null;
     }
 }

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/stream/FileSource.java Thu Aug 16 10:37:08 2018
@@ -133,10 +133,8 @@ public class FileSource extends SourceIm
             log.debug("start creating root Element");
         }
         SourceElement result;
-        InputStream inputStream = null;
-        try
+        try (InputStream inputStream = new FileInputStream(path))
         {
-            inputStream = new FileInputStream(path);
             MessageDigest messageDigest = MessageDigest.getInstance("MD5");
             DigestInputStream digestInputStream
                     = new DigestInputStream(inputStream, messageDigest);
@@ -153,28 +151,19 @@ public class FileSource extends SourceIm
                     "File not found: " + path.getAbsolutePath(),
                     e);
         }
-        catch (NoSuchAlgorithmException e)
+        catch (IOException e)
         {
             throw new SourceException(
-                    "MD5 message Digest not implemented",
+                    "Could not close: " + path.getAbsolutePath(),
                     e);
         }
-        finally
+        catch (NoSuchAlgorithmException e)
         {
-            if (inputStream != null)
-            {
-                try
-                {
-                    inputStream.close();
-                }
-                catch (IOException e)
-                {
-                    log.error("error closing input stream", e);
-                }
-            }
+            throw new SourceException(
+                    "MD5 message Digest not implemented",
+                    e);
         }
 
-
         if (log.isDebugEnabled())
         {
             log.debug("finished creating root Element, source is\n"

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java Thu Aug 16 10:37:08 2018
@@ -112,9 +112,7 @@ public class AttributeTransformer implem
     public AttributeTransformer(Reader transformDefinition)
             throws SourceTransformerException
     {
-        BufferedReader reader = new BufferedReader(transformDefinition);
-
-        try
+        try (BufferedReader reader = new BufferedReader(transformDefinition))
         {
             String line = reader.readLine();
             while (line != null)
@@ -161,20 +159,6 @@ public class AttributeTransformer implem
                     "Could not read transformDefinition",
                     e);
         }
-        finally
-        {
-            if (reader != null)
-            {
-                try
-                {
-                    reader.close();
-                }
-                catch (IOException e)
-                {
-                    log.warn("Could not close reader", e);
-                }
-            }
-        }
     }
 
 

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java Thu Aug 16 10:37:08 2018
@@ -26,8 +26,6 @@ import java.io.Reader;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.torque.generator.configuration.ConfigurationException;
 import org.apache.torque.generator.configuration.ConfigurationProvider;
 import org.apache.torque.generator.control.ControllerState;
@@ -42,9 +40,6 @@ public abstract class TemplateOutletImpl
         extends OutletImpl
         implements TemplateOutlet
 {
-    /** The log of the class. */
-    private static Log log = LogFactory.getLog(TemplateOutletImpl.class);
-
     /**
      * The content of the templates.
      * The key is the resolved template path, the value is the template content.
@@ -130,32 +125,16 @@ public abstract class TemplateOutletImpl
         String result = contentMap.get(detokenizedPath);
         if (result == null)
         {
-            InputStream templateInputStream = null;
-            try
-            {
-                templateInputStream
+            try (InputStream templateInputStream
                         = configurationProvider.getTemplateInputStream(
-                            detokenizedPath);
+                            detokenizedPath))
+            {
                 result = load(templateInputStream, encoding, templateFilter);
             }
             catch (IOException e)
             {
                 throw new ConfigurationException(e);
             }
-            finally
-            {
-                if (templateInputStream != null)
-                {
-                    try
-                    {
-                        templateInputStream.close();
-                    }
-                    catch (IOException e)
-                    {
-                        log.warn("Could not close template reader", e);
-                    }
-                }
-            }
             contentMap.put(detokenizedPath, result);
         }
 

Modified: db/torque/torque4/trunk/torque-maven-plugin/src/main/java/org/apache/torque/generator/maven/TorqueGeneratorMojo.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-maven-plugin/src/main/java/org/apache/torque/generator/maven/TorqueGeneratorMojo.java?rev=1838186&r1=1838185&r2=1838186&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-maven-plugin/src/main/java/org/apache/torque/generator/maven/TorqueGeneratorMojo.java (original)
+++ db/torque/torque4/trunk/torque-maven-plugin/src/main/java/org/apache/torque/generator/maven/TorqueGeneratorMojo.java Thu Aug 16 10:37:08 2018
@@ -21,7 +21,6 @@ package org.apache.torque.generator.mave
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -575,36 +574,15 @@ public class TorqueGeneratorMojo extends
             if (optionsFile != null)
             {
                 Properties optionProperties = new Properties();
-                FileInputStream optionsFileInputStream = null;
-                try
+                try (FileInputStream optionsFileInputStream = new FileInputStream(optionsFile))
                 {
-                    optionsFileInputStream = new FileInputStream(optionsFile);
                     optionProperties.load(optionsFileInputStream);
                 }
-                catch (FileNotFoundException e)
-                {
-                    getLog().error(e);
-                    throw new MojoExecutionException(e.getMessage());
-                }
                 catch (IOException e)
                 {
                     getLog().error(e);
                     throw new MojoExecutionException(e.getMessage());
                 }
-                finally
-                {
-                    if (optionsFileInputStream != null)
-                    {
-                        try
-                        {
-                            optionsFileInputStream.close();
-                        }
-                        catch (IOException e)
-                        {
-                            getLog().error(e);
-                        }
-                    }
-                }
                 getLog().debug("loaded options file from "
                         + optionsFile.getAbsolutePath() + ", contents: "
                         + optionProperties);



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org