Author: rfscholte
Date: Thu Aug 21 20:26:28 2014
New Revision: 1619564

URL: http://svn.apache.org/r1619564
Log:
Ensure that Writer is always closed

Modified:
    
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/rss/VelocityTemplate.java

Modified: 
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/rss/VelocityTemplate.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/rss/VelocityTemplate.java?rev=1619564&r1=1619563&r2=1619564&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/rss/VelocityTemplate.java
 (original)
+++ 
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/rss/VelocityTemplate.java
 Thu Aug 21 20:26:28 2014
@@ -89,27 +89,21 @@ public class VelocityTemplate
     public void generate( String outputFilename, String template, Context 
context )
         throws VelocityException, MojoExecutionException, IOException
     {
-        File f;
+        Writer writer = null;
 
         try
         {
-            f = new File( outputFilename );
+            File f = new File( outputFilename );
 
             if ( !f.getParentFile().exists() )
             {
                 f.getParentFile().mkdirs();
             }
 
-            Writer writer = new FileWriter( f );
+            writer = new FileWriter( f );
 
             getVelocity().getEngine().mergeTemplate( templateDirectory + "/" + 
template, context, writer );
-
-            writer.flush();
-            writer.close();
-
-            getLog().debug( "File " + outputFilename + " created..." );
         }
-
         catch ( ResourceNotFoundException e )
         {
             throw new ResourceNotFoundException( "Template not found: " + 
templateDirectory + "/" + template, e );
@@ -126,6 +120,16 @@ public class VelocityTemplate
         {
             throw new MojoExecutionException( e.getMessage(), e );
         }
+        finally
+        {
+            if ( writer != null )
+            {
+                writer.flush();
+                writer.close();
+
+                getLog().debug( "File " + outputFilename + " created..." );
+            }
+        }
     }
 
     public void setTemplateDirectory( String templateDirectory )


Reply via email to