Skip to content

Commit e8a00ed

Browse files
committed
CLOUDSTACK-8656: try-with-resource in vmsd reader
moved closeable util function up the hierarchy
1 parent afcdbc4 commit e8a00ed

3 files changed

Lines changed: 42 additions & 25 deletions

File tree

framework/db/src/com/cloud/utils/db/DbUtil.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
import org.apache.log4j.Logger;
4545

46+
import static com.cloud.utils.AutoCloseableUtil.closeAutoCloseable;
47+
4648
public class DbUtil {
4749
protected final static Logger s_logger = Logger.getLogger(DbUtil.class);
4850

@@ -284,16 +286,4 @@ public static void closeConnection(final Connection connection) {
284286
closeAutoCloseable(connection, "exception while close connection.");
285287
}
286288

287-
public static void closeAutoCloseable(AutoCloseable ac, String message) {
288-
try {
289-
290-
if (ac != null) {
291-
ac.close();
292-
}
293-
294-
} catch (Exception e) {
295-
s_logger.warn("[ignored] " + message, e);
296-
}
297-
}
298-
299289
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.utils;
18+
19+
import org.apache.log4j.Logger;
20+
21+
public class AutoCloseableUtil {
22+
private final static Logger s_logger = Logger.getLogger(AutoCloseableUtil.class);
23+
24+
public static void closeAutoCloseable(AutoCloseable ac, String message) {
25+
try {
26+
27+
if (ac != null) {
28+
ac.close();
29+
}
30+
31+
} catch (Exception e) {
32+
s_logger.warn("[ignored] " + message, e);
33+
}
34+
}
35+
36+
}

vmware-base/src/com/cloud/hypervisor/vmware/mo/SnapshotDescriptor.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public class SnapshotDescriptor {
3232
private static final Logger s_logger = Logger.getLogger(SnapshotDescriptor.class);
3333

34-
private Properties _properties = new Properties();
34+
private final Properties _properties = new Properties();
3535

3636
public SnapshotDescriptor() {
3737
}
@@ -90,11 +90,9 @@ public void removeDiskReferenceFromSnapshot(String diskFileName) {
9090
}
9191

9292
public byte[] getVmsdContent() {
93-
BufferedWriter out = null;
9493
ByteArrayOutputStream bos = new ByteArrayOutputStream();
9594

96-
try {
97-
out = new BufferedWriter(new OutputStreamWriter(bos, "UTF-8"));
95+
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(bos, "UTF-8"));) {
9896

9997
out.write(".encoding = \"UTF-8\"");
10098
out.newLine();
@@ -165,13 +163,6 @@ public byte[] getVmsdContent() {
165163
} catch (IOException e) {
166164
assert (false);
167165
s_logger.error("Unexpected exception ", e);
168-
} finally {
169-
if (out != null) {
170-
try {
171-
out.close();
172-
} catch (IOException e) {
173-
}
174-
}
175166
}
176167

177168
return bos.toByteArray();
@@ -288,8 +279,8 @@ public String toString() {
288279
}
289280

290281
public static class DiskInfo {
291-
private String _diskFileName;
292-
private String _deviceName;
282+
private final String _diskFileName;
283+
private final String _deviceName;
293284

294285
public DiskInfo(String diskFileName, String deviceName) {
295286
_diskFileName = diskFileName;

0 commit comments

Comments
 (0)