CLOUDSTACK-8656: try-with-resource in vmsd reader

moved closeable util function up the hierarchy
This commit is contained in:
Daan Hoogland 2015-08-04 00:41:10 +02:00 committed by Daan Hoogland
parent afcdbc42ab
commit e8a00ed989
3 changed files with 42 additions and 25 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;