package org.carlspring.strongbox.resource;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import java.io.Closeable;
import java.io.IOException;

import org.slf4j.Logger;

/**
 * A utility class for safely closing resources and logging eventual errors.
 * The purpose of this class is to avoid boiler-plate code.
 *
 * @author mtodorov
 */
public class ResourceCloser
{

    public static void close(Closeable resource, Logger logger)
    {
        if (resource != null)
        {
            try
            {
                resource.close();
            }
            catch (IOException e)
            {
                logger.error(e.getMessage(), e);
            }
        }
    }

    public static void close(AutoCloseable resource, Logger logger)
    {
        if (resource != null)
        {
            try
            {
                resource.close();
            }
            catch (Exception e)
            {
                logger.error(e.getMessage(), e);
            }
        }
    }

    public static void close(Context resource, Logger logger)
    {
        if (resource != null)
        {
            try
            {
                resource.close();
            }
            catch (Exception e)
            {
                logger.error(e.getMessage(), e);
            }
        }
    }

    public static void close(NamingEnumeration resource, Logger logger)
    {
        if (resource != null)
        {
            try
            {
                resource.close();
            }
            catch (Exception e)
            {
                logger.error(e.getMessage(), e);
            }
        }
    }

}

@@ -613,32 +613,90 @@
 )%0A            %7B%0A
+                if (logger != null)%0A                %7B%0A    
                 
@@ -720,32 +720,50 @@
 tMessage(), e);%0A
+                %7D%0A
             %7D%0A  
@@ -1007,32 +1007,90 @@
 )%0A            %7B%0A
+                if (logger != null)%0A                %7B%0A    
                 
@@ -1114,32 +1114,50 @@
 tMessage(), e);%0A
+                %7D%0A
             %7D%0A  
@@ -1395,32 +1395,90 @@
 )%0A            %7B%0A
+                if (logger != null)%0A                %7B%0A    
                 
@@ -1502,32 +1502,50 @@
 tMessage(), e);%0A
+                %7D%0A
             %7D%0A  
@@ -1793,32 +1793,90 @@
 )%0A            %7B%0A
+                if (logger != null)%0A                %7B%0A    
                 
@@ -1900,32 +1900,50 @@
 tMessage(), e);%0A
+                %7D%0A
             %7D%0A  

Make it possible to pass in null as the logger, if you don't want to login exceptions when closing a resource. Not really advisable, but useful when testing and should only really be used for that.