From e8a00ed989f17ce235a201fadba37f00d2ded7c3 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Tue, 4 Aug 2015 00:41:10 +0200 Subject: [PATCH] CLOUDSTACK-8656: try-with-resource in vmsd reader moved closeable util function up the hierarchy --- .../db/src/com/cloud/utils/db/DbUtil.java | 14 ++------ .../com/cloud/utils/AutoCloseableUtil.java | 36 +++++++++++++++++++ .../vmware/mo/SnapshotDescriptor.java | 17 +++------ 3 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 utils/src/com/cloud/utils/AutoCloseableUtil.java diff --git a/framework/db/src/com/cloud/utils/db/DbUtil.java b/framework/db/src/com/cloud/utils/db/DbUtil.java index d8689050c63..2b52188faf2 100644 --- a/framework/db/src/com/cloud/utils/db/DbUtil.java +++ b/framework/db/src/com/cloud/utils/db/DbUtil.java @@ -43,6 +43,8 @@ import javax.persistence.Transient; import org.apache.log4j.Logger; +import static com.cloud.utils.AutoCloseableUtil.closeAutoCloseable; + public class DbUtil { protected final static Logger s_logger = Logger.getLogger(DbUtil.class); @@ -284,16 +286,4 @@ public class DbUtil { closeAutoCloseable(connection, "exception while close connection."); } - public static void closeAutoCloseable(AutoCloseable ac, String message) { - try { - - if (ac != null) { - ac.close(); - } - - } catch (Exception e) { - s_logger.warn("[ignored] " + message, e); - } - } - } diff --git a/utils/src/com/cloud/utils/AutoCloseableUtil.java b/utils/src/com/cloud/utils/AutoCloseableUtil.java new file mode 100644 index 00000000000..f93265b5d64 --- /dev/null +++ b/utils/src/com/cloud/utils/AutoCloseableUtil.java @@ -0,0 +1,36 @@ +// 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 com.cloud.utils; + +import org.apache.log4j.Logger; + +public class AutoCloseableUtil { + private final static Logger s_logger = Logger.getLogger(AutoCloseableUtil.class); + + public static void closeAutoCloseable(AutoCloseable ac, String message) { + try { + + if (ac != null) { + ac.close(); + } + + } catch (Exception e) { + s_logger.warn("[ignored] " + message, e); + } + } + +} diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/SnapshotDescriptor.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/SnapshotDescriptor.java index 0499be9363d..82a225e2638 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/SnapshotDescriptor.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/SnapshotDescriptor.java @@ -31,7 +31,7 @@ import org.apache.log4j.Logger; public class SnapshotDescriptor { private static final Logger s_logger = Logger.getLogger(SnapshotDescriptor.class); - private Properties _properties = new Properties(); + private final Properties _properties = new Properties(); public SnapshotDescriptor() { } @@ -90,11 +90,9 @@ public class SnapshotDescriptor { } public byte[] getVmsdContent() { - BufferedWriter out = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try { - out = new BufferedWriter(new OutputStreamWriter(bos, "UTF-8")); + try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(bos, "UTF-8"));) { out.write(".encoding = \"UTF-8\""); out.newLine(); @@ -165,13 +163,6 @@ public class SnapshotDescriptor { } catch (IOException e) { assert (false); s_logger.error("Unexpected exception ", e); - } finally { - if (out != null) { - try { - out.close(); - } catch (IOException e) { - } - } } return bos.toByteArray(); @@ -288,8 +279,8 @@ public class SnapshotDescriptor { } public static class DiskInfo { - private String _diskFileName; - private String _deviceName; + private final String _diskFileName; + private final String _deviceName; public DiskInfo(String diskFileName, String deviceName) { _diskFileName = diskFileName;