Repository: ambari
Updated Branches:
  refs/heads/trunk c0225d0a6 -> 7cff55c5f


AMBARI-17922. Coverity Scan Security Vulnerability - Resource Leak 
defects.(vbrodetskyi)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/7cff55c5
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/7cff55c5
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/7cff55c5

Branch: refs/heads/trunk
Commit: 7cff55c5f218988dd2067ca01445e9201d127fbf
Parents: c0225d0
Author: Vitaly Brodetskyi <[email protected]>
Authored: Thu Jul 28 17:04:24 2016 +0300
Committer: Vitaly Brodetskyi <[email protected]>
Committed: Thu Jul 28 17:04:24 2016 +0300

----------------------------------------------------------------------
 .../org/apache/ambari/logfeeder/LogFeeder.java  | 39 ++++++---
 .../apache/ambari/logfeeder/LogFeederUtil.java  | 33 +++++---
 .../ambari/logfeeder/output/OutputFile.java     | 31 ++++---
 .../org/apache/ambari/logsearch/LogSearch.java  |  2 +-
 .../ambari/logsearch/manager/AuditMgr.java      | 16 +++-
 .../apache/ambari/logsearch/util/FileUtil.java  | 17 +++-
 .../ambari/logsearch/util/PropertiesUtil.java   | 11 ++-
 .../timeline/aggregators/AggregatorUtils.java   | 17 ++--
 .../checks/DatabaseConsistencyCheckHelper.java  | 55 ++++++++++--
 .../server/view/ViewDirectoryWatcher.java       | 31 ++++---
 .../apache/ambari/server/view/ViewRegistry.java | 88 ++++++++++++--------
 11 files changed, 243 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeeder.java
----------------------------------------------------------------------
diff --git 
a/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeeder.java
 
b/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeeder.java
index 8697f54..3cf0fff 100644
--- 
a/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeeder.java
+++ 
b/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeeder.java
@@ -23,6 +23,7 @@ import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
@@ -136,16 +137,34 @@ public class LogFeeder {
   }
 
   private void loadConfigsUsingClassLoader(String configFileName) throws 
Exception {
-    BufferedInputStream fileInputStream = (BufferedInputStream) this
-      .getClass().getClassLoader()
-      .getResourceAsStream(configFileName);
-    if (fileInputStream != null) {
-      BufferedReader br = new BufferedReader(new InputStreamReader(
-        fileInputStream));
-      String configData = readFile(br);
-      loadConfigs(configData);
-    } else {
-      throw new Exception("Can't find configFile=" + configFileName);
+    BufferedInputStream fileInputStream = null;
+    BufferedReader br = null;
+    try {
+      fileInputStream = (BufferedInputStream) this
+        .getClass().getClassLoader()
+        .getResourceAsStream(configFileName);
+      if (fileInputStream != null) {
+        br = new BufferedReader(new InputStreamReader(
+          fileInputStream));
+        String configData = readFile(br);
+        loadConfigs(configData);
+      } else {
+        throw new Exception("Can't find configFile=" + configFileName);
+      }
+    } finally {
+      if (br != null) {
+        try {
+          br.close();
+        } catch (IOException e) {
+        }
+      }
+
+      if (fileInputStream != null) {
+        try {
+          fileInputStream.close();
+        } catch (IOException e) {
+        }
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeederUtil.java
----------------------------------------------------------------------
diff --git 
a/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeederUtil.java
 
b/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeederUtil.java
index 9881b55..a86d989 100644
--- 
a/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeederUtil.java
+++ 
b/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/LogFeederUtil.java
@@ -27,7 +27,6 @@ import java.lang.reflect.Type;
 import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
-import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
@@ -137,17 +136,27 @@ public class LogFeederUtil {
     }
 
     if (!propLoaded) {
-      // Properties not yet loaded, let's try from class loader
-      BufferedInputStream fileInputStream = (BufferedInputStream) 
LogFeeder.class
-        .getClassLoader().getResourceAsStream(propFile);
-      if (fileInputStream != null) {
-        logger.info("Loading properties file " + propFile
-          + " from classpath");
-        props.load(fileInputStream);
-        propLoaded = true;
-      } else {
-        logger.fatal("Properties file not found in classpath. properties file 
name= "
-          + propFile);
+      BufferedInputStream fileInputStream = null;
+      try {
+        // Properties not yet loaded, let's try from class loader
+        fileInputStream = (BufferedInputStream) LogFeeder.class
+          .getClassLoader().getResourceAsStream(propFile);
+        if (fileInputStream != null) {
+          logger.info("Loading properties file " + propFile
+            + " from classpath");
+          props.load(fileInputStream);
+          propLoaded = true;
+        } else {
+          logger.fatal("Properties file not found in classpath. properties 
file name= "
+            + propFile);
+        }
+      } finally {
+        if (fileInputStream != null) {
+          try {
+            fileInputStream.close();
+          } catch (IOException e) {
+          }
+        }
       }
     }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/output/OutputFile.java
----------------------------------------------------------------------
diff --git 
a/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/output/OutputFile.java
 
b/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/output/OutputFile.java
index b4d2bbb..aef8dc5 100644
--- 
a/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/output/OutputFile.java
+++ 
b/ambari-logsearch/ambari-logsearch-logfeeder/src/main/java/org/apache/ambari/logfeeder/output/OutputFile.java
@@ -22,6 +22,7 @@ package org.apache.ambari.logfeeder.output;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Map;
 
@@ -94,17 +95,27 @@ public class OutputFile extends Output {
   public void write(Map<String, Object> jsonObj, InputMarker inputMarker)
     throws Exception {
     String outStr = null;
-    if (codec.equals("csv")) {
-      CSVPrinter csvPrinter = new CSVPrinter(outWriter, CSVFormat.RFC4180);
-      //TODO:
-    } else {
-      outStr = LogFeederUtil.getGson().toJson(jsonObj);
-    }
-    if (outWriter != null && outStr != null) {
-      statMetric.count++;
+    CSVPrinter csvPrinter = null;
+    try {
+      if (codec.equals("csv")) {
+        csvPrinter = new CSVPrinter(outWriter, CSVFormat.RFC4180);
+        //TODO:
+      } else {
+        outStr = LogFeederUtil.getGson().toJson(jsonObj);
+      }
+      if (outWriter != null && outStr != null) {
+        statMetric.count++;
 
-      outWriter.println(outStr);
-      outWriter.flush();
+        outWriter.println(outStr);
+        outWriter.flush();
+      }
+    } finally {
+      if (csvPrinter != null) {
+        try {
+          csvPrinter.close();
+        } catch (IOException e) {
+        }
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
----------------------------------------------------------------------
diff --git 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
index 735a83a..819d3b9 100644
--- 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
+++ 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
@@ -166,7 +166,7 @@ public class LogSearch {
     ServerSocket serverSocket = null;
     boolean portBusy = false;
     try {
-      new ServerSocket(port);
+      serverSocket = new ServerSocket(port);
     } catch (IOException ex) {
       portBusy = true;
       logger.error(ex.getLocalizedMessage() + " PORT :" + port);

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/AuditMgr.java
----------------------------------------------------------------------
diff --git 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/AuditMgr.java
 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/AuditMgr.java
index 3dd8146..d4f2986 100644
--- 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/AuditMgr.java
+++ 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/manager/AuditMgr.java
@@ -19,6 +19,9 @@
 
 package org.apache.ambari.logsearch.manager;
 
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -29,9 +32,6 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
 import org.apache.ambari.logsearch.common.LogSearchConstants;
 import org.apache.ambari.logsearch.common.ManageStartEndTime;
 import org.apache.ambari.logsearch.common.MessageEnums;
@@ -556,6 +556,7 @@ public class AuditMgr extends MgrBase {
     queryGenerator.setRowCount(solrQuery, 0);
 
     String dataFormat = (String) searchCriteria.getParamValue("format");
+    FileOutputStream fis = null;
     try {
       QueryResponse queryResponse = auditSolrDao.process(solrQuery);
       if(queryResponse == null){
@@ -667,7 +668,7 @@ public class AuditMgr extends MgrBase {
         + ".";
       File file = File.createTempFile(fileName, dataFormat);
 
-      FileOutputStream fis = new FileOutputStream(file);
+      fis = new FileOutputStream(file);
       fis.write(data.getBytes());
       return Response
         .ok(file, MediaType.APPLICATION_OCTET_STREAM)
@@ -679,6 +680,13 @@ public class AuditMgr extends MgrBase {
       logger.error("Error during solrQuery=" + e);
       throw restErrorUtil.createRESTException(MessageEnums.SOLR_ERROR
           .getMessage().getMessage(), MessageEnums.ERROR_SYSTEM);
+    } finally {
+      if (fis != null) {
+        try {
+          fis.close();
+        } catch (IOException e) {
+        }
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java
----------------------------------------------------------------------
diff --git 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java
 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java
index ab52b06..658635c 100644
--- 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java
+++ 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/FileUtil.java
@@ -19,15 +19,16 @@
 
 package org.apache.ambari.logsearch.util;
 
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.net.URL;
 import java.util.List;
 import java.util.Set;
 
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
 import org.apache.ambari.logsearch.common.MessageEnums;
 import org.apache.ambari.logsearch.view.VHost;
 import org.apache.ambari.logsearch.view.VSummary;
@@ -46,6 +47,7 @@ public class FileUtil {
   @SuppressWarnings("resource")
   public Response saveToFile(String text, String fileName, VSummary vsummary) {
     String mainExportedFile = "";
+    FileOutputStream fis = null;
     try {
       mainExportedFile = mainExportedFile
         + "**********************Summary**********************\n";
@@ -112,7 +114,7 @@ public class FileUtil {
         + "\n";
       mainExportedFile = mainExportedFile + text + "\n";
       File file = File.createTempFile(fileName, vsummary.getFormat());
-      FileOutputStream fis = new FileOutputStream(file);
+      fis = new FileOutputStream(file);
       fis.write(mainExportedFile.getBytes());
       return Response
         .ok(file, MediaType.APPLICATION_OCTET_STREAM)
@@ -123,6 +125,13 @@ public class FileUtil {
       logger.error(e.getMessage());
       throw restErrorUtil.createRESTException(e.getMessage(),
         MessageEnums.ERROR_SYSTEM);
+    } finally {
+      if (fis != null) {
+        try {
+          fis.close();
+        } catch (IOException e) {
+        }
+      }
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/PropertiesUtil.java
----------------------------------------------------------------------
diff --git 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/PropertiesUtil.java
 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/PropertiesUtil.java
index f32152d..16ebae2 100644
--- 
a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/PropertiesUtil.java
+++ 
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/PropertiesUtil.java
@@ -47,11 +47,20 @@ public class PropertiesUtil extends 
PropertyPlaceholderConfigurer {
     Properties properties = new Properties();
     URL fileCompleteUrl = Thread.currentThread()
         .getContextClassLoader().getResource(LOGSEARCH_PROP_FILE);
+    FileInputStream fileInputStream = null;
     try {
       File file = new File(fileCompleteUrl.toURI());
-      properties.load(new FileInputStream(file.getAbsoluteFile()));
+      fileInputStream = new FileInputStream(file.getAbsoluteFile());
+      properties.load(fileInputStream);
     } catch (IOException | URISyntaxException e) {
       logger.error("error loading prop for protocol config",e);
+    } finally {
+      if (fileInputStream != null) {
+        try {
+          fileInputStream.close();
+        } catch (IOException e) {
+        }
+      }
     }
     for (String key : properties.stringPropertyNames()) {
       String value = properties.getProperty(key);

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/aggregators/AggregatorUtils.java
----------------------------------------------------------------------
diff --git 
a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/aggregators/AggregatorUtils.java
 
b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/aggregators/AggregatorUtils.java
index 9e41c87..55b1c1b 100644
--- 
a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/aggregators/AggregatorUtils.java
+++ 
b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/aggregators/AggregatorUtils.java
@@ -18,9 +18,6 @@
 package 
org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.aggregators;
 
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
 import java.io.BufferedReader;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -30,6 +27,9 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  *
  */
@@ -84,12 +84,19 @@ public class AggregatorUtils {
         whitelistedMetrics.add(strLine);
       }
     } catch (IOException ioEx) {
-      LOG.error("Unable to parse metric whitelist file");
+      LOG.error("Unable to parse metric whitelist file", ioEx);
+    } finally {
       if (br != null) {
         try {
           br.close();
         } catch (IOException e) {
-          LOG.error("Unable to close whitelist file reader");
+        }
+      }
+
+      if (fstream != null) {
+        try {
+          fstream.close();
+        } catch (IOException e) {
         }
       }
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
index 3035de9..36a2d99 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java
@@ -150,6 +150,7 @@ public class DatabaseConsistencyCheckHelper {
     String GET_NOT_MAPPED_CONFIGS_QUERY = "select type_name from clusterconfig 
where type_name not in (select type_name from clusterconfigmapping)";
     Set<String> nonSelectedConfigs = new HashSet<>();
     ResultSet rs = null;
+    Statement statement = null;
 
     if (connection == null) {
       if (dbAccessor == null) {
@@ -159,7 +160,7 @@ public class DatabaseConsistencyCheckHelper {
     }
 
     try {
-      Statement statement = 
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
+      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
       rs = statement.executeQuery(GET_NOT_MAPPED_CONFIGS_QUERY);
       if (rs != null) {
         while (rs.next()) {
@@ -180,6 +181,14 @@ public class DatabaseConsistencyCheckHelper {
           LOG.error("Exception occurred during result set closing procedure: 
", e);
         }
       }
+
+      if (statement != null) {
+        try {
+          statement.close();
+        } catch (SQLException e) {
+          LOG.error("Exception occurred during statement closing procedure: ", 
e);
+        }
+      }
     }
   }
 
@@ -198,6 +207,7 @@ public class DatabaseConsistencyCheckHelper {
             "having sum(selected) > 1";
     Multimap<String, String> clusterConfigTypeMap = HashMultimap.create();
     ResultSet rs = null;
+    Statement statement = null;
 
     if (connection == null) {
       if (dbAccessor == null) {
@@ -207,7 +217,7 @@ public class DatabaseConsistencyCheckHelper {
     }
 
     try {
-      Statement statement = 
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
+      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
       rs = statement.executeQuery(GET_CONFIGS_SELECTED_MORE_THAN_ONCE_QUERY);
       if (rs != null) {
         while (rs.next()) {
@@ -231,6 +241,14 @@ public class DatabaseConsistencyCheckHelper {
           LOG.error("Exception occurred during result set closing procedure: 
", e);
         }
       }
+
+      if (statement != null) {
+        try {
+          statement.close();
+        } catch (SQLException e) {
+          LOG.error("Exception occurred during statement closing procedure: ", 
e);
+        }
+      }
     }
   }
 
@@ -245,6 +263,7 @@ public class DatabaseConsistencyCheckHelper {
     String GET_HOSTS_WITHOUT_STATUS_QUERY = "select host_name from hosts where 
host_id not in (select host_id from hoststate)";
     Set<String> hostsWithoutStatus = new HashSet<>();
     ResultSet rs = null;
+    Statement statement = null;
 
     if (connection == null) {
       if (dbAccessor == null) {
@@ -254,7 +273,7 @@ public class DatabaseConsistencyCheckHelper {
     }
 
     try {
-      Statement statement = 
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
+      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
       rs = statement.executeQuery(GET_HOSTS_WITHOUT_STATUS_QUERY);
       if (rs != null) {
         while (rs.next()) {
@@ -277,6 +296,14 @@ public class DatabaseConsistencyCheckHelper {
           LOG.error("Exception occurred during result set closing procedure: 
", e);
         }
       }
+
+      if (statement != null) {
+        try {
+          statement.close();
+        } catch (SQLException e) {
+          LOG.error("Exception occurred during statement closing procedure: ", 
e);
+        }
+      }
     }
   }
 
@@ -297,6 +324,7 @@ public class DatabaseConsistencyCheckHelper {
     int hostComponentDesiredStateCount = 0;
     int mergedCount = 0;
     ResultSet rs = null;
+    Statement statement = null;
 
     if (connection == null) {
       if (dbAccessor == null) {
@@ -306,7 +334,7 @@ public class DatabaseConsistencyCheckHelper {
     }
 
     try {
-      Statement statement = 
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
+      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
 
       rs = statement.executeQuery(GET_HOST_COMPONENT_STATE_COUNT_QUERY);
       if (rs != null) {
@@ -344,6 +372,14 @@ public class DatabaseConsistencyCheckHelper {
           LOG.error("Exception occurred during result set closing procedure: 
", e);
         }
       }
+
+      if (statement != null) {
+        try {
+          statement.close();
+        } catch (SQLException e) {
+          LOG.error("Exception occurred during statement closing procedure: ", 
e);
+        }
+      }
     }
 
   }
@@ -389,6 +425,7 @@ public class DatabaseConsistencyCheckHelper {
     Map<String, Multimap<String, String>> clusterServiceVersionMap = new 
HashMap<>();
     Map<String, Multimap<String, String>> clusterServiceConfigType = new 
HashMap<>();
     ResultSet rs = null;
+    Statement statement = null;
 
     if (connection == null) {
       if (dbAccessor == null) {
@@ -402,7 +439,7 @@ public class DatabaseConsistencyCheckHelper {
     }
 
     try {
-      Statement statement = 
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
+      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
 
       rs = statement.executeQuery(GET_SERVICES_WITHOUT_CONFIGS_QUERY);
       if (rs != null) {
@@ -575,6 +612,14 @@ public class DatabaseConsistencyCheckHelper {
           LOG.error("Exception occurred during result set closing procedure: 
", e);
         }
       }
+
+      if (statement != null) {
+        try {
+          statement.close();
+        } catch (SQLException e) {
+          LOG.error("Exception occurred during statement closing procedure: ", 
e);
+        }
+      }
     }
 
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDirectoryWatcher.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDirectoryWatcher.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDirectoryWatcher.java
index c3d443a..48cb853 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDirectoryWatcher.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDirectoryWatcher.java
@@ -18,15 +18,10 @@
 
 package org.apache.ambari.server.view;
 
-import com.google.common.base.Function;
-import com.google.common.collect.Lists;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.lang.Thread.sleep;
 import javax.annotation.Nullable;
+
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Path;
@@ -41,8 +36,14 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.zip.ZipFile;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.Thread.sleep;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
 
 @Singleton
 public class ViewDirectoryWatcher implements DirectoryWatcher {
@@ -225,14 +226,22 @@ public class ViewDirectoryWatcher implements 
DirectoryWatcher {
    * @return
    */
   private boolean verify(Path resolvedPath) {
+    ZipFile zipFile = null;
     try {
       File file = resolvedPath.toAbsolutePath().toFile();
       checkArgument(!file.isDirectory());
       checkArgument(file.length() > 0);
-      new ZipFile(file);
+      zipFile = new ZipFile(file);
     } catch (Exception e) {
       LOG.info("Verification failed ", e);
       return false;
+    } finally {
+      if (zipFile != null) {
+        try {
+          zipFile.close();
+        } catch (IOException e) {
+        }
+      }
     }
     return true;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7cff55c5/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java 
b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
index 83a3761..f49f604 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
@@ -18,14 +18,31 @@
 
 package org.apache.ambari.server.view;
 
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.Sets;
-import com.google.common.eventbus.AllowConcurrentEvents;
-import com.google.common.eventbus.Subscribe;
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.persist.Transactional;
+import javax.inject.Inject;
+import javax.inject.Provider;
+import javax.inject.Singleton;
+import javax.xml.bind.JAXBException;
+
+import java.beans.IntrospectionException;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.ClusterNotFoundException;
 import org.apache.ambari.server.api.resources.ResourceInstanceFactoryImpl;
@@ -105,29 +122,14 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.xml.sax.SAXException;
 
-import javax.inject.Inject;
-import javax.inject.Provider;
-import javax.inject.Singleton;
-import javax.xml.bind.JAXBException;
-import java.beans.IntrospectionException;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.nio.file.Path;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.Sets;
+import com.google.common.eventbus.AllowConcurrentEvents;
+import com.google.common.eventbus.Subscribe;
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.Transactional;
 
 /**
  * Registry for view and view instance definitions.
@@ -1737,13 +1739,14 @@ public class ViewRegistry {
    */
   private void configureViewLogging(ViewEntity viewDefinition, ClassLoader cl) 
{
     InputStream viewLog4jStream = cl.getResourceAsStream(VIEW_LOG_FILE);
+    InputStream ambariLog4jStream = null;
     if (null != viewLog4jStream) {
       try {
         Properties viewLog4jConfig = new Properties();
         viewLog4jConfig.load(viewLog4jStream);
         LOG.info("setting up logging for view {} as per property file {}", 
viewDefinition.getName(), VIEW_LOG_FILE);
 
-        InputStream ambariLog4jStream = 
cl.getResourceAsStream(AMBARI_LOG_FILE);
+        ambariLog4jStream = cl.getResourceAsStream(AMBARI_LOG_FILE);
         if (null != ambariLog4jStream) {
           Properties ambariLog4jConfig = new Properties();
           ambariLog4jConfig.load(ambariLog4jStream);
@@ -1763,6 +1766,13 @@ public class ViewRegistry {
         PropertyConfigurator.configure(viewLog4jConfig);
       } catch (IOException e) {
         LOG.error("Error occurred while configuring logs for {}", 
viewDefinition.getName());
+      } finally {
+        if (ambariLog4jStream != null) {
+          try {
+            ambariLog4jStream.close();
+          } catch (IOException e) {
+          }
+        }
       }
     }
   }
@@ -1939,8 +1949,18 @@ public class ViewRegistry {
         ViewEntity viewDefinition = new ViewEntity(viewConfig, configuration, 
extractedArchiveDirPath);
 
         if (!systemOnly || viewDefinition.isSystem()) {
-          extractor.extractViewArchive(viewDefinition, archiveFile, 
extractedArchiveDirFile);
-          return true;
+          ClassLoader classLoader = null;
+          try {
+            classLoader = extractor.extractViewArchive(viewDefinition, 
archiveFile, extractedArchiveDirFile);
+            return true;
+          } finally {
+            if (classLoader != null && classLoader instanceof ViewClassLoader) 
{
+              try {
+                ((ViewClassLoader)classLoader).close();
+              } catch (IOException e) {
+              }
+            }
+          }
         }
       }
     }

Reply via email to