Repository: ambari Updated Branches: refs/heads/branch-dev-patch-upgrade 7a87817b8 -> 480792817
AMBARI-19419. Code cleanup: empty blocks (Attila Doroszlai via ncole) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/749a5b2d Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/749a5b2d Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/749a5b2d Branch: refs/heads/branch-dev-patch-upgrade Commit: 749a5b2dc583a5571b28e2710f0018abe7f12383 Parents: 95e9804 Author: Nate Cole <[email protected]> Authored: Tue Dec 20 09:54:41 2016 -0500 Committer: Nate Cole <[email protected]> Committed: Tue Dec 20 09:54:41 2016 -0500 ---------------------------------------------------------------------- ambari-server/checkstyle.xml | 8 ++ .../ambari/server/agent/HeartbeatProcessor.java | 1 + .../api/services/serializers/CsvSerializer.java | 15 +--- .../server/bootstrap/BSHostStatusCollector.java | 12 +-- .../ambari/server/bootstrap/BSRunner.java | 2 +- .../gsinstaller/ClusterDefinition.java | 9 +- .../internal/AbstractProviderModule.java | 4 +- .../CachedRoleCommandOrderProvider.java | 6 +- .../server/metadata/RoleCommandOrder.java | 3 +- .../security/encryption/CredentialProvider.java | 13 +-- .../kerberos/IPAKerberosOperationHandler.java | 16 +--- .../kerberos/MITKerberosOperationHandler.java | 5 +- .../stack/QuickLinksConfigurationModule.java | 4 - .../apache/ambari/server/stack/ThemeModule.java | 4 - .../ambari/server/state/ConfigHelper.java | 2 +- .../apache/ambari/server/state/ServiceInfo.java | 28 +++--- .../server/state/cluster/ClusterImpl.java | 3 +- .../server/upgrade/SchemaUpgradeHelper.java | 24 +++--- .../apache/ambari/server/utils/Closeables.java | 72 ++++++++++++++++ .../server/view/ViewDirectoryWatcher.java | 8 +- .../apache/ambari/server/view/ViewRegistry.java | 24 ++---- .../server/view/ViewURLStreamProvider.java | 3 +- .../ambari/server/utils/CloseablesTest.java | 91 ++++++++++++++++++++ 23 files changed, 232 insertions(+), 125 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/checkstyle.xml ---------------------------------------------------------------------- diff --git a/ambari-server/checkstyle.xml b/ambari-server/checkstyle.xml index 0c66556..0742284 100644 --- a/ambari-server/checkstyle.xml +++ b/ambari-server/checkstyle.xml @@ -28,5 +28,13 @@ </module> <module name="RedundantImport"/> <module name="UnusedImports"/> + + <!-- blocks --> + <module name="AvoidNestedBlocks"> + <property name="allowInSwitchCase" value="true"/> + </module> + <module name="EmptyBlock"> + <property name="option" value="text"/> + </module> </module> </module> http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java b/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java index ed84e45..4fbc161 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/agent/HeartbeatProcessor.java @@ -371,6 +371,7 @@ public class HeartbeatProcessor extends AbstractService{ Cluster cluster = clusterFsm.getCluster(report.getClusterName()); clusterId = cluster.getClusterId(); } catch (AmbariException e) { + // null clusterId reported and handled by the listener (DistributeRepositoriesActionListener) } } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java index 92d8b69..760eecb 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/CsvSerializer.java @@ -29,6 +29,7 @@ import org.apache.ambari.server.api.services.Result; import org.apache.ambari.server.api.services.ResultStatus; import org.apache.ambari.server.api.util.TreeNode; import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.utils.Closeables; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; @@ -107,12 +108,7 @@ public class CsvSerializer implements ResultSerializer { //todo: exception handling. Create ResultStatus 500 and call serializeError throw new RuntimeException("Unable to serialize to csv: " + e, e); } finally { - if (csvPrinter != null) { - try { - csvPrinter.close(); - } catch (IOException ex) { - } - } + Closeables.closeSilently(csvPrinter); } } } @@ -132,12 +128,7 @@ public class CsvSerializer implements ResultSerializer { //todo: exception handling. Create ResultStatus 500 and call serializeError throw new RuntimeException("Unable to serialize to csv: " + e, e); } finally { - if (csvPrinter != null) { - try { - csvPrinter.close(); - } catch (IOException ex) { - } - } + Closeables.closeSilently(csvPrinter); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSHostStatusCollector.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSHostStatusCollector.java b/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSHostStatusCollector.java index 661f284..79f6511 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSHostStatusCollector.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSHostStatusCollector.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.apache.ambari.server.utils.Closeables; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -115,13 +116,8 @@ class BSHostStatusCollector { } catch (IOException e) { LOG.info("Error reading log file " + log + ". Log file may be have not created yet"); - } - finally { - try { - reader.close(); - } - catch (Exception e) { - } + } finally { + Closeables.closeSilently(reader); } status.setLog(logString); } @@ -136,7 +132,7 @@ class BSHostStatusCollector { int reason = -1; try { reason = Integer.parseInt(statusCode); - } catch (Exception e) { + } catch (Exception ignored) { } switch (reason) { http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSRunner.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSRunner.java b/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSRunner.java index 6c77dee..3040443 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSRunner.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/bootstrap/BSRunner.java @@ -159,7 +159,7 @@ class BSRunner extends Thread { try { process.exitValue(); return true; - } catch (IllegalThreadStateException e) {} + } catch (IllegalThreadStateException ignored) {} // Check if process has terminated once per second Thread.sleep(1000); } while (System.currentTimeMillis() - startTime < timeout); http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java index 6f9876a..d183396 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/gsinstaller/ClusterDefinition.java @@ -28,6 +28,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.apache.ambari.server.utils.Closeables; + /** * Defines the cluster created by gsInstaller. */ @@ -282,12 +284,7 @@ public class ClusterDefinition { String msg = "Caught exception reading " + CLUSTER_DEFINITION_FILE + "."; throw new IllegalStateException(msg, e); } finally { - if (is != null) { - try { - is.close(); - } catch (IOException ex) { - } - } + Closeables.closeSilently(is); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractProviderModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractProviderModule.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractProviderModule.java index 5d462c5..6967fee 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractProviderModule.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractProviderModule.java @@ -747,8 +747,8 @@ public abstract class AbstractProviderModule implements ProviderModule, PropertyHelper.getPropertyId("ServiceComponentInfo", "state"), jpp, gpp)); + break; } - break; case HostComponent: { // TODO as we fill out stack metric definitions, these can be phased out PropertyProvider jpp = createJMXPropertyProvider( @@ -795,8 +795,8 @@ public abstract class AbstractProviderModule implements ProviderModule, // this follows the current pattern of relying on the management controller // to instantiate this PropertyProvider providers.add(managementController.getLoggingSearchPropertyProvider()); + break; } - break; case RootServiceComponent: providers.add(new RootServiceComponentPropertyProvider()); break; http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/metadata/CachedRoleCommandOrderProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/metadata/CachedRoleCommandOrderProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/metadata/CachedRoleCommandOrderProvider.java index e2b44ca..7cf197b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/metadata/CachedRoleCommandOrderProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/metadata/CachedRoleCommandOrderProvider.java @@ -73,7 +73,7 @@ public class CachedRoleCommandOrderProvider implements RoleCommandOrderProvider if (cluster != null && cluster.getService("GLUSTERFS") != null) { hasGLUSTERFS = true; } - } catch (AmbariException e) { + } catch (AmbariException ignored) { } try { @@ -82,7 +82,7 @@ public class CachedRoleCommandOrderProvider implements RoleCommandOrderProvider cluster.getService("HDFS").getServiceComponent("JOURNALNODE") != null) { isNameNodeHAEnabled = true; } - } catch (AmbariException e) { + } catch (AmbariException ignored) { } try { @@ -91,7 +91,7 @@ public class CachedRoleCommandOrderProvider implements RoleCommandOrderProvider cluster.getService("YARN").getServiceComponent("RESOURCEMANAGER").getServiceComponentHosts().size() > 1) { isResourceManagerHAEnabled = true; } - } catch (AmbariException e) { + } catch (AmbariException ignored) { } int clusterCacheId = new HashCodeBuilder().append(cluster.getClusterId()).append(hasGLUSTERFS).append(isNameNodeHAEnabled).append http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/metadata/RoleCommandOrder.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/metadata/RoleCommandOrder.java b/ambari-server/src/main/java/org/apache/ambari/server/metadata/RoleCommandOrder.java index 58675ae..cebc1b7 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/metadata/RoleCommandOrder.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/metadata/RoleCommandOrder.java @@ -126,7 +126,8 @@ public class RoleCommandOrder { try { stack = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion()); - } catch (AmbariException e) { + } catch (AmbariException ignored) { + // initialize() will fail with NPE } Map<String,Object> userData = stack.getRoleCommandOrder().getContent(); http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialProvider.java index 7643804..0521aa1 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialProvider.java @@ -18,7 +18,6 @@ package org.apache.ambari.server.security.encryption; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; @@ -29,6 +28,7 @@ import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.security.credential.Credential; import org.apache.ambari.server.security.credential.GenericKeyCredential; +import org.apache.ambari.server.utils.Closeables; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -191,23 +191,14 @@ public class CredentialProvider { try { fo = new FileOutputStream(writeFilePath); fo.write(passwd.getBytes()); - } catch (FileNotFoundException fe) { - fe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { - if (fo != null) { - try { - fo.close(); - } catch (IOException e) { - } - } + Closeables.closeSilently(fo); } } else { LOG.error("Alias and file path are required arguments."); } - } else if (action.equalsIgnoreCase("RESET")) { - } } else { LOG.error("No arguments provided to " + "CredentialProvider"); http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/IPAKerberosOperationHandler.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/IPAKerberosOperationHandler.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/IPAKerberosOperationHandler.java index e118747..37779c6 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/IPAKerberosOperationHandler.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/IPAKerberosOperationHandler.java @@ -40,6 +40,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.ambari.server.security.credential.PrincipalKeyCredential; +import org.apache.ambari.server.utils.Closeables; import org.apache.ambari.server.utils.ShellCommandUtil; import org.apache.commons.lang.StringUtils; import org.apache.directory.server.kerberos.shared.keytab.Keytab; @@ -1093,19 +1094,8 @@ public class IPAKerberosOperationHandler extends KerberosOperationHandler { LOG.error(message, e); throw new KerberosOperationException(message, e); } finally { - if (osw != null) { - try { - osw.close(); - } catch (IOException e) { - } - } - - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - } - } + Closeables.closeSilently(osw); + Closeables.closeSilently(reader); } if (process.exitValue() != 0) { http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java index 2880f6e..309170b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java @@ -474,7 +474,10 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler { } tries++; - try { Thread.sleep(1000 * configuration.getKerberosOperationRetryTimeout()); } catch (InterruptedException e) {} + try { + Thread.sleep(1000 * configuration.getKerberosOperationRetryTimeout()); + } catch (InterruptedException ignored) { + } String message = String.format("Retrying to execute kadmin after a wait of %d seconds :\n\tCommand: %s", configuration.getKerberosOperationRetryTimeout(), http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/stack/QuickLinksConfigurationModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/QuickLinksConfigurationModule.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/QuickLinksConfigurationModule.java index 9554f0c..818026b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/stack/QuickLinksConfigurationModule.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/QuickLinksConfigurationModule.java @@ -41,10 +41,6 @@ public class QuickLinksConfigurationModule extends BaseModule<QuickLinksConfigur public static final String QUICKLINKS_CONFIGURATION_KEY = "QuickLinksConfiguration"; - static { - } - - private QuickLinksConfigurationInfo moduleInfo; private boolean valid = true; private Set<String> errors = new HashSet<String>(); http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/stack/ThemeModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/ThemeModule.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/ThemeModule.java index f07efea..932d929 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/stack/ThemeModule.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/ThemeModule.java @@ -41,10 +41,6 @@ public class ThemeModule extends BaseModule<ThemeModule, ThemeInfo> implements V public static final String THEME_KEY = "Theme"; - static { - } - - private ThemeInfo moduleInfo; private boolean valid = true; private Set<String> errors = new HashSet<String>(); http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java index 3fb036d..54752f9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java @@ -552,7 +552,7 @@ public class ConfigHelper { String stackPropertyConfigType = fileNameToConfigType(serviceProperty.getFilename()); try { result.add(actualConfigs.get(stackPropertyConfigType).getProperties().get(serviceProperty.getName())); - } catch (Exception ex) { + } catch (Exception ignored) { } } } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceInfo.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceInfo.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceInfo.java index c9d497f..3d5ef85 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceInfo.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceInfo.java @@ -921,17 +921,13 @@ public String getVersion() { public Map<String, ThemeInfo> getThemesMap() { if (themesMap == null) { - synchronized (this) { - } - if (themesMap == null) { - Map<String, ThemeInfo> tmp = new TreeMap<String, ThemeInfo>(); - if (themes != null) { - for (ThemeInfo theme : themes) { - tmp.put(theme.getFileName(), theme); - } + Map<String, ThemeInfo> tmp = new TreeMap<>(); + if (themes != null) { + for (ThemeInfo theme : themes) { + tmp.put(theme.getFileName(), theme); } - themesMap = tmp; } + themesMap = tmp; } return themesMap; } @@ -959,17 +955,13 @@ public String getVersion() { public Map<String, QuickLinksConfigurationInfo> getQuickLinksConfigurationsMap() { if (quickLinksConfigurationsMap == null) { - synchronized (this) { - } - if (quickLinksConfigurationsMap == null) { - Map<String, QuickLinksConfigurationInfo> tmp = new TreeMap<String, QuickLinksConfigurationInfo>(); - if (quickLinksConfigurations != null) { - for (QuickLinksConfigurationInfo quickLinksConfiguration : quickLinksConfigurations) { - tmp.put(quickLinksConfiguration.getFileName(), quickLinksConfiguration); - } + Map<String, QuickLinksConfigurationInfo> tmp = new TreeMap<>(); + if (quickLinksConfigurations != null) { + for (QuickLinksConfigurationInfo quickLinksConfiguration : quickLinksConfigurations) { + tmp.put(quickLinksConfiguration.getFileName(), quickLinksConfiguration); } - quickLinksConfigurationsMap = tmp; } + quickLinksConfigurationsMap = tmp; } return quickLinksConfigurationsMap; } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java index b62c834..86ba1a1 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java @@ -3196,8 +3196,7 @@ public class ClusterImpl implements Cluster { StackId stackId = getCurrentStackVersion(); StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion()); return stackInfo.getConfigPropertiesTypes(configType); - } catch (AmbariException e) { - + } catch (AmbariException ignored) { } return new HashMap<>(); } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java index 99e50fb..590a3e8 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java @@ -97,20 +97,18 @@ public class SchemaUpgradeHelper { throw new RuntimeException("Unable to read database version", e); } finally { - { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - throw new RuntimeException("Cannot close result set"); - } + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + throw new RuntimeException("Cannot close result set"); } - if (statement != null) { - try { - statement.close(); - } catch (SQLException e) { - throw new RuntimeException("Cannot close statement"); - } + } + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { + throw new RuntimeException("Cannot close statement"); } } } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/utils/Closeables.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/utils/Closeables.java b/ambari-server/src/main/java/org/apache/ambari/server/utils/Closeables.java new file mode 100644 index 0000000..e120d4c --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/utils/Closeables.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.utils; + +import java.io.Closeable; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utilities for closing @{link Closeable}s. + * + * @see com.google.common.io.Closeables + */ +public final class Closeables { + + private static final Logger LOGGER = LoggerFactory.getLogger(Closeables.class); + + /** + * Closes <code>c</code> ignoring any <code>IOException</code>s. + */ + public static void closeSilently(Closeable c) { + close(c, null); + } + + /** + * Closes <code>c</code> logging any <code>IOException</code> as warning via this class' logger. + */ + public static void closeLoggingExceptions(Closeable c) { + closeLoggingExceptions(c, LOGGER); + } + + /** + * Closes <code>c</code> logging any <code>IOException</code> via the specified <code>logger</code>. + */ + public static void closeLoggingExceptions(Closeable c, Logger logger) { + close(c, logger); + } + + private static void close(Closeable c, Logger logger) { + if (c != null) { + try { + c.close(); + } catch (IOException e) { + if (logger != null) { + logger.warn("IOException while closing Closeable", e); + } + } + } + } + + private Closeables() { + throw new UnsupportedOperationException("No instances"); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/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 2ccf757..31f53f1 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 @@ -39,6 +39,7 @@ import java.util.zip.ZipFile; import javax.annotation.Nullable; import org.apache.ambari.server.configuration.Configuration; +import org.apache.ambari.server.utils.Closeables; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -238,12 +239,7 @@ public class ViewDirectoryWatcher implements DirectoryWatcher { LOG.info("Verification failed ", e); return false; } finally { - if (zipFile != null) { - try { - zipFile.close(); - } catch (IOException e) { - } - } + Closeables.closeSilently(zipFile); } return true; } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/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 cb1489a..df4a2f2 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 @@ -19,6 +19,7 @@ package org.apache.ambari.server.view; import java.beans.IntrospectionException; +import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -92,6 +93,7 @@ import org.apache.ambari.server.security.authorization.RoleAuthorization; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.StackId; import org.apache.ambari.server.state.stack.OsFamily; +import org.apache.ambari.server.utils.Closeables; import org.apache.ambari.server.utils.VersionUtils; import org.apache.ambari.server.view.configuration.AutoInstanceConfig; import org.apache.ambari.server.view.configuration.EntityConfig; @@ -1819,19 +1821,8 @@ public class ViewRegistry { } catch (IOException e) { LOG.error("Error occurred while configuring logs for {}", viewDefinition.getName()); } finally { - if (ambariLog4jStream != null) { - try { - ambariLog4jStream.close(); - } catch (IOException e) { - } - } - - if (viewLog4jStream != null) { - try { - viewLog4jStream.close(); - } catch (IOException e) { - } - } + Closeables.closeSilently(ambariLog4jStream); + Closeables.closeSilently(viewLog4jStream); } } } @@ -2023,11 +2014,8 @@ public class ViewRegistry { classLoader = extractor.extractViewArchive(viewDefinition, archiveFile, extractedArchiveDirFile); return true; } finally { - if (classLoader != null && classLoader instanceof ViewClassLoader) { - try { - ((ViewClassLoader) classLoader).close(); - } catch (IOException e) { - } + if (classLoader instanceof Closeable) { + Closeables.closeSilently((Closeable) classLoader); } } } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java index 24d3527..2d7d8a6 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java @@ -252,7 +252,8 @@ public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamPr Integer.toString(url.getPort() == -1 ? url.getDefaultPort() : url.getPort())); - } catch (MalformedURLException ex) { + } catch (MalformedURLException ignored) { + // actual connection attempt will throw } } http://git-wip-us.apache.org/repos/asf/ambari/blob/749a5b2d/ambari-server/src/test/java/org/apache/ambari/server/utils/CloseablesTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/utils/CloseablesTest.java b/ambari-server/src/test/java/org/apache/ambari/server/utils/CloseablesTest.java new file mode 100644 index 0000000..4eda065 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/utils/CloseablesTest.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.utils; + +import static org.easymock.EasyMock.anyString; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expectLastCall; +import static org.powermock.api.easymock.PowerMock.replayAll; +import static org.powermock.api.easymock.PowerMock.verifyAll; + +import java.io.Closeable; +import java.io.IOException; + +import org.junit.Test; +import org.slf4j.Logger; + +public class CloseablesTest { + + @Test + public void silentIfSucceeds() throws Exception { + Closeable normalCloseable = createNiceMock(Closeable.class); + normalCloseable.close(); + replayAll(); + + Closeables.closeSilently(normalCloseable); + + verifyAll(); + } + + @Test + public void silentIfThrows() throws Exception { + Closeable throwingCloseable = createNiceMock(Closeable.class); + throwingCloseable.close(); + expectLastCall().andThrow(new IOException()); + replayAll(); + + Closeables.closeSilently(throwingCloseable); + + verifyAll(); + } + + @Test + public void succeedsWithoutLog() throws Exception { + Closeable normalCloseable = createNiceMock(Closeable.class); + Logger logger = createStrictMock(Logger.class); + normalCloseable.close(); + replayAll(); + + Closeables.closeLoggingExceptions(normalCloseable, logger); + + verifyAll(); + } + + @Test + public void warnWithThrownException() throws Exception { + Closeable throwingCloseable = createNiceMock(Closeable.class); + Logger logger = createNiceMock(Logger.class); + IOException e = new IOException(); + throwingCloseable.close(); + expectLastCall().andThrow(e); + logger.warn(anyString(), eq(e)); + replayAll(); + + Closeables.closeLoggingExceptions(throwingCloseable, logger); + + verifyAll(); + } + + @Test + public void ignoresNullCloseable() { + Closeables.closeSilently(null); + } + +}
