From 7546cc291716e48b9c79c26bbf5b98d0797a2bc2 Mon Sep 17 00:00:00 2001 From: Vitaly Brodetskyi Date: Thu, 28 Jul 2016 17:06:28 +0300 Subject: AMBARI-17922. Coverity Scan Security Vulnerability - Resource Leak defects.(vbrodetskyi) --- .../org/apache/ambari/logfeeder/LogFeeder.java | 39 +++++++--- .../org/apache/ambari/logfeeder/LogFeederUtil.java | 33 +++++--- .../apache/ambari/logfeeder/output/OutputFile.java | 31 +++++--- .../org/apache/ambari/logsearch/LogSearch.java | 2 +- .../apache/ambari/logsearch/manager/AuditMgr.java | 16 +++- .../org/apache/ambari/logsearch/util/FileUtil.java | 17 ++++- .../ambari/logsearch/util/PropertiesUtil.java | 11 ++- .../timeline/aggregators/AggregatorUtils.java | 17 +++-- .../checks/DatabaseConsistencyCheckHelper.java | 55 ++++++++++++-- .../ambari/server/view/ViewDirectoryWatcher.java | 31 +++++--- .../apache/ambari/server/view/ViewRegistry.java | 88 +++++++++++++--------- 11 files changed, 243 insertions(+), 97 deletions(-) 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 8697f54f7b..3cf0fffa85 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) { + } + } } } 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 9881b55782..a86d9896ae 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) { + } + } } } 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 b4d2bbb8a5..aef8dc5ca6 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 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) { + } + } } } 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 735a83ac14..819d3b910c 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); 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 3dd814696d..d4f2986fa0 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) { + } + } } } 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 ab52b06556..658635cba4 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) { + } + } } } 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 f32152d066..16ebae210c 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); 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 9e41c87e5c..55b1c1bbf3 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) { } } } 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 3035de9875..36a2d998b4 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 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 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 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> clusterServiceVersionMap = new HashMap<>(); Map> 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); + } + } } } 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 c3d443a574..48cb85308c 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; } 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 83a37616a7..f49f604f6e 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) { + } + } + } } } } -- cgit v1.2.3