Author: juanpablo
Date: Thu Dec 26 18:30:56 2013
New Revision: 1553545

URL: http://svn.apache.org/r1553545
Log:
sonar: Method may fail to clean up stream or resource on checked exception  

Modified:
    
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/AuthenticationManager.java
    
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/authorize/JDBCGroupDatabase.java
    
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java

Modified: 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/AuthenticationManager.java
URL: 
http://svn.apache.org/viewvc/jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/AuthenticationManager.java?rev=1553545&r1=1553544&r2=1553545&view=diff
==============================================================================
--- 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/AuthenticationManager.java
 (original)
+++ 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/AuthenticationManager.java
 Thu Dec 26 18:30:56 2013
@@ -40,6 +40,7 @@ import javax.security.auth.spi.LoginModu
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.log4j.Logger;
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiSession;
@@ -631,6 +632,7 @@ public class AuthenticationManager {
         
         if( engine.getServletContext() != null )
         {
+               OutputStream os = null;
             try
             {
                 //  create a tmp file of the policy loaded as an InputStream 
and return the URL to it
@@ -639,7 +641,7 @@ public class AuthenticationManager {
                 File tmpFile = File.createTempFile( "temp." + name, "" );
                 tmpFile.deleteOnExit();
 
-                OutputStream os = new FileOutputStream(tmpFile);
+                os = new FileOutputStream(tmpFile);
 
                 byte[] buff = new byte[1024];
 
@@ -648,10 +650,7 @@ public class AuthenticationManager {
                     os.write(buff);
                 }
 
-                os.close();
-
                 path = tmpFile.toURI().toURL();
-
             }
             catch( MalformedURLException e )
             {
@@ -662,6 +661,10 @@ public class AuthenticationManager {
             {
                log.error("failed to load security policy from file " + name + 
",stacktrace follows", e);
             }
+            finally 
+            {
+               IOUtils.closeQuietly( os );
+            }
         }
         return path;
     }

Modified: 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/authorize/JDBCGroupDatabase.java
URL: 
http://svn.apache.org/viewvc/jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/authorize/JDBCGroupDatabase.java?rev=1553545&r1=1553544&r2=1553545&view=diff
==============================================================================
--- 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/authorize/JDBCGroupDatabase.java
 (original)
+++ 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/auth/authorize/JDBCGroupDatabase.java
 Thu Dec 26 18:30:56 2013
@@ -132,8 +132,8 @@ import org.apache.wiki.auth.WikiSecurity
  * 
  * @since 2.3
  */
-public class JDBCGroupDatabase implements GroupDatabase
-{
+public class JDBCGroupDatabase implements GroupDatabase {
+       
     /** Default column name that stores the JNDI name of the DataSource. */
     public static final String DEFAULT_GROUPDB_DATASOURCE = 
"jdbc/GroupDatabase";
 
@@ -234,7 +234,7 @@ public class JDBCGroupDatabase implement
      * @deprecated there is no need to call this method because the save and
      *             delete methods contain their own commit logic
      */
-    @SuppressWarnings("deprecation")
+    @Deprecated
     public void commit() throws WikiSecurityException
     {
     }
@@ -259,6 +259,7 @@ public class JDBCGroupDatabase implement
 
         String groupName = group.getName();
         Connection conn = null;
+        PreparedStatement ps = null;
         try
         {
             // Open the database connection
@@ -268,7 +269,7 @@ public class JDBCGroupDatabase implement
                 conn.setAutoCommit( false );
             }
 
-            PreparedStatement ps = conn.prepareStatement( m_deleteGroup );
+            ps = conn.prepareStatement( m_deleteGroup );
             ps.setString( 1, groupName );
             ps.execute();
             ps.close();
@@ -276,7 +277,6 @@ public class JDBCGroupDatabase implement
             ps = conn.prepareStatement( m_deleteGroupMembers );
             ps.setString( 1, groupName );
             ps.execute();
-            ps.close();
 
             // Commit and close connection
             if( m_supportsCommits )
@@ -286,17 +286,12 @@ public class JDBCGroupDatabase implement
         }
         catch( SQLException e )
         {
+               closeQuietly( conn, ps, null );
             throw new WikiSecurityException( "Could not delete group " + 
groupName + ": " + e.getMessage(), e );
         }
         finally
         {
-            try
-            {
-                if( conn != null ) conn.close();
-            }
-            catch( Exception e )
-            {
-            }
+            closeQuietly( conn, ps, null );
         }
     }
 
@@ -315,13 +310,15 @@ public class JDBCGroupDatabase implement
     {
         Set<Group> groups = new HashSet<Group>();
         Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
         try
         {
             // Open the database connection
             conn = m_ds.getConnection();
 
-            PreparedStatement ps = conn.prepareStatement( m_findAll );
-            ResultSet rs = ps.executeQuery();
+            ps = conn.prepareStatement( m_findAll );
+            rs = ps.executeQuery();
             while ( rs.next() )
             {
                 String groupName = rs.getString( m_name );
@@ -340,21 +337,15 @@ public class JDBCGroupDatabase implement
                     groups.add( group );
                 }
             }
-            ps.close();
         }
         catch( SQLException e )
         {
+               closeQuietly( conn, ps, rs );
             throw new WikiSecurityException( e.getMessage(), e );
         }
         finally
         {
-            try
-            {
-                if( conn != null ) conn.close();
-            }
-            catch( Exception e )
-            {
-            }
+            closeQuietly( conn, ps, rs );
         }
 
         return groups.toArray( new Group[groups.size()] );
@@ -382,7 +373,7 @@ public class JDBCGroupDatabase implement
 
         boolean exists = exists( group );
         Connection conn = null;
-
+        PreparedStatement ps = null;
         try
         {
             // Open the database connection
@@ -391,8 +382,7 @@ public class JDBCGroupDatabase implement
             {
                 conn.setAutoCommit( false );
             }
-
-            PreparedStatement ps;
+            
             Timestamp ts = new Timestamp( System.currentTimeMillis() );
             Date modDate = new Date( ts.getTime() );
             if( !exists )
@@ -443,7 +433,6 @@ public class JDBCGroupDatabase implement
                 ps.setString( 2, member.getName() );
                 ps.execute();
             }
-            ps.close();
 
             // Commit and close connection
             if( m_supportsCommits )
@@ -453,17 +442,12 @@ public class JDBCGroupDatabase implement
         }
         catch( SQLException e )
         {
+               closeQuietly(conn, ps, null );
             throw new WikiSecurityException( e.getMessage(), e );
         }
         finally
         {
-            try
-            {
-                if( conn != null ) conn.close();
-            }
-            catch( Exception e )
-            {
-            }
+            closeQuietly(conn, ps, null );
         }
     }
 
@@ -524,27 +508,23 @@ public class JDBCGroupDatabase implement
 
         // Test connection by doing a quickie select
         Connection conn = null;
+        PreparedStatement ps = null;
         try
         {
             conn = m_ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement( m_findAll );
+            ps = conn.prepareStatement( m_findAll );
             ps.executeQuery();
             ps.close();
         }
         catch( SQLException e )
         {
+               closeQuietly( conn, ps, null );
             log.error( "DB connectivity error: " + e.getMessage() );
             throw new WikiSecurityException("DB connectivity error: " + 
e.getMessage(), e );
         }
         finally
         {
-            try
-            {
-                if( conn != null ) conn.close();
-            }
-            catch( Exception e )
-            {
-            }
+            closeQuietly( conn, ps, null );
         }
         log.info( "JDBCGroupDatabase initialized from JNDI DataSource: " + 
jndiName );
 
@@ -562,17 +542,12 @@ public class JDBCGroupDatabase implement
         }
         catch( SQLException e )
         {
+               closeQuietly( conn, null, null );
             log.warn( "JDBCGroupDatabase warning: user database doesn't seem 
to support transactions. Reason: " + e);
         }
         finally
         {
-            try
-            {
-                if( conn != null ) conn.close();
-            }
-            catch( Exception e )
-            {
-            }
+            closeQuietly( conn, null, null );
         }
     }
 
@@ -610,15 +585,17 @@ public class JDBCGroupDatabase implement
         Group group = null;
         boolean found = false;
         boolean unique = true;
+        ResultSet rs = null;
+       PreparedStatement ps = null;
         Connection conn = null;
         try
         {
             // Open the database connection
             conn = m_ds.getConnection();
 
-            PreparedStatement ps = conn.prepareStatement( m_findGroup );
+            ps = conn.prepareStatement( m_findGroup );
             ps.setString( 1, index );
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
             while ( rs.next() )
             {
                 if( group != null )
@@ -634,21 +611,15 @@ public class JDBCGroupDatabase implement
                 populateGroup( group );
                 found = true;
             }
-            ps.close();
         }
         catch( SQLException e )
         {
+               closeQuietly( conn, ps, rs );
             throw new NoSuchPrincipalException( e.getMessage() );
         }
         finally
         {
-            try
-            {
-                if( conn != null ) conn.close();
-            }
-            catch( Exception e )
-            {
-            }
+            closeQuietly( conn, ps, rs );
         }
 
         if( !found )
@@ -670,15 +641,17 @@ public class JDBCGroupDatabase implement
      */
     private Group populateGroup( Group group )
     {
+       ResultSet rs = null;
+       PreparedStatement ps = null;
         Connection conn = null;
         try
         {
             // Open the database connection
             conn = m_ds.getConnection();
 
-            PreparedStatement ps = conn.prepareStatement( m_findMembers );
+            ps = conn.prepareStatement( m_findMembers );
             ps.setString( 1, group.getName() );
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
             while ( rs.next() )
             {
                 String memberName = rs.getString( m_member );
@@ -688,7 +661,6 @@ public class JDBCGroupDatabase implement
                     group.add( principal );
                 }
             }
-            ps.close();
         }
         catch( SQLException e )
         {
@@ -696,15 +668,30 @@ public class JDBCGroupDatabase implement
         }
         finally
         {
-            try
-            {
-                if( conn != null ) conn.close();
-            }
-            catch( Exception e )
-            {
-            }
+            closeQuietly( conn, ps, rs );
         }
         return group;
     }
+    
+    void closeQuietly( Connection conn, PreparedStatement ps, ResultSet rs ) {
+       if( conn != null ) {
+               try {
+                   conn.close();
+               } catch( Exception e ) {
+               }
+       }
+               if( ps != null )  {
+                       try {
+                           ps.close();
+                       } catch( Exception e ) {
+                       }
+               }
+               if( rs != null )  {
+                       try {
+                           rs.close();
+                       } catch( Exception e ) {
+                       }
+               }
+       }
 
 }

Modified: 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java
URL: 
http://svn.apache.org/viewvc/jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java?rev=1553545&r1=1553544&r2=1553545&view=diff
==============================================================================
--- 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java
 (original)
+++ 
jspwiki/trunk/jspwiki-war/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java
 Thu Dec 26 18:30:56 2013
@@ -36,6 +36,7 @@ import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.log4j.Logger;
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiPage;
@@ -286,41 +287,42 @@ public class BasicAttachmentProvider
      *  Writes the page properties back to the file system.
      *  Note that it WILL overwrite any previous properties.
      */
-    private void putPageProperties( Attachment att, Properties properties )
-        throws IOException,
-               ProviderException
-    {
+    private void putPageProperties( Attachment att, Properties properties ) 
throws IOException, ProviderException {
         File attDir = findAttachmentDir( att );
         File propertyFile = new File( attDir, PROPERTY_FILE );
 
-        OutputStream out = new FileOutputStream( propertyFile );
-
-        properties.store( out, 
-                          " JSPWiki page properties for "+
-                          att.getName()+
-                          ". DO NOT MODIFY!" );
-
-        out.close();
+        OutputStream out = null;
+        
+        try {
+               out = new FileOutputStream( propertyFile );
+            properties.store( out, " JSPWiki page properties for " + 
att.getName() + ". DO NOT MODIFY!" );
+        } catch ( IOException ioe ) {
+               IOUtils.closeQuietly( out );
+               throw ioe;
+        } finally {
+               IOUtils.closeQuietly( out );
+        }
     }
 
     /**
      *  Reads page properties from the file system.
      */
-    private Properties getPageProperties( Attachment att )
-        throws IOException,
-               ProviderException
-    {
+    private Properties getPageProperties( Attachment att ) throws IOException, 
ProviderException {
         Properties props = new Properties();
 
         File propertyFile = new File( findAttachmentDir(att), PROPERTY_FILE );
 
-        if( propertyFile.exists() )
-        {
-            InputStream in = new FileInputStream( propertyFile );
-
-            props.load(in);
-
-            in.close();
+        if( propertyFile.exists() ) {
+            InputStream in = null;
+            try {
+               in = new FileInputStream( propertyFile );
+                props.load( in );
+            } catch ( IOException ioe ) {
+               IOUtils.closeQuietly( in );
+               throw ioe;
+            } finally {
+               IOUtils.closeQuietly( in );
+            }
         }
         
         return props;
@@ -329,10 +331,7 @@ public class BasicAttachmentProvider
     /**
      *  {@inheritDoc}
      */
-    public void putAttachmentData( Attachment att, InputStream data )
-        throws ProviderException,
-               IOException
-    {
+    public void putAttachmentData( Attachment att, InputStream data ) throws 
ProviderException, IOException {
         OutputStream out = null;
         File attDir = findAttachmentDir( att );
 
@@ -358,8 +357,6 @@ public class BasicAttachmentProvider
 
             FileUtil.copyContents( data, out );
 
-            out.close();
-
             Properties props = getPageProperties( att );
 
             String author = att.getAuthor();
@@ -382,11 +379,12 @@ public class BasicAttachmentProvider
         catch( IOException e )
         {
             log.error( "Could not save attachment data: ", e );
+            IOUtils.closeQuietly( out );
             throw (IOException) e.fillInStackTrace();
         }
         finally
         {
-            if( out != null ) out.close();
+            IOUtils.closeQuietly( out );
         }
     }
 


Reply via email to