Skip to content

Commit d32d6a2

Browse files
committed
Merge pull request apache#649 from
* pr/649: CLOUDSTACK-8656: checkstyle no longer used import removed CLOUDSTACK-8656: messages on SQL exception in DbUtils! CLOUDSTACK-8656: replace empty catch block on close by try-with-resource CLOUDSTACK-8656: 30x legacy upgrade code exception messages CLOUDSTACK-8656: removed redundant implements CLOUDSTACK-8656: silent close failure of clustering socket log as info CLOUDSTACK-8656: try with resource te eliminate empty catch clauses CLOUDSTACK-8656: log messages on exception in legacy sql upgrade code CLOUDSTACK-8656: removed unused input stream there was code to close a stream that was never created CLOUDSTACK-8656: info on error closing peering channels CLOUDSTACK-8656: messages on errors closing streams for local templates CLOUDSTACK-8656: handle template properties loading Signed-off-by: Daan Hoogland <daan@onecht.net>
2 parents 118e954 + 75093dc commit d32d6a2

21 files changed

Lines changed: 290 additions & 744 deletions

File tree

core/src/com/cloud/storage/template/LocalTemplateDownloader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,31 @@ public long download(boolean resume, DownloadCompleteCallback callback) {
123123
try {
124124
fic.close();
125125
} catch (IOException e) {
126+
s_logger.info("[ignore] error while closing file input channel.");
126127
}
127128
}
128129

129130
if (foc != null) {
130131
try {
131132
foc.close();
132133
} catch (IOException e) {
134+
s_logger.info("[ignore] error while closing file output channel.");
133135
}
134136
}
135137

136138
if (fis != null) {
137139
try {
138140
fis.close();
139141
} catch (IOException e) {
142+
s_logger.info("[ignore] error while closing file input stream.");
140143
}
141144
}
142145

143146
if (fos != null) {
144147
try {
145148
fos.close();
146149
} catch (IOException e) {
150+
s_logger.info("[ignore] error while closing file output stream.");
147151
}
148152
}
149153

core/src/com/cloud/storage/template/TemplateLocation.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,10 @@ public boolean purge() {
9494
}
9595

9696
public boolean load() throws IOException {
97-
FileInputStream strm = null;
98-
try {
99-
strm = new FileInputStream(_file);
97+
try (FileInputStream strm = new FileInputStream(_file);) {
10098
_props.load(strm);
101-
} finally {
102-
if (strm != null) {
103-
try {
104-
strm.close();
105-
} catch (IOException e) {
106-
}
107-
}
99+
} catch (IOException e) {
100+
s_logger.warn("Unable to load the template properties", e);
108101
}
109102

110103
for (ImageFormat format : ImageFormat.values()) {
@@ -142,20 +135,11 @@ public boolean save() {
142135
_props.setProperty(info.format.getFileExtension() + ".size", Long.toString(info.size));
143136
_props.setProperty(info.format.getFileExtension() + ".virtualsize", Long.toString(info.virtualSize));
144137
}
145-
FileOutputStream strm = null;
146-
try {
147-
strm = new FileOutputStream(_file);
138+
try (FileOutputStream strm = new FileOutputStream(_file);) {
148139
_props.store(strm, "");
149140
} catch (IOException e) {
150141
s_logger.warn("Unable to save the template properties ", e);
151142
return false;
152-
} finally {
153-
if (strm != null) {
154-
try {
155-
strm.close();
156-
} catch (IOException e) {
157-
}
158-
}
159143
}
160144
return true;
161145
}

engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ public boolean stop() {
585585
s_logger.info("Closing: " + ch.toString());
586586
ch.close();
587587
} catch (IOException e) {
588+
s_logger.info("[ignored] error on closing channel: " +ch.toString(), e);
588589
}
589590
}
590591
}

engine/schema/src/com/cloud/certificate/dao/CertificateDaoImpl.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
// under the License.
1717
package com.cloud.certificate.dao;
1818

19-
import java.io.BufferedInputStream;
20-
import java.io.IOException;
21-
2219
import javax.ejb.Local;
2320

2421
import org.apache.log4j.Logger;
@@ -41,7 +38,6 @@ public CertificateDaoImpl() {
4138

4239
@Override
4340
public Long persistCustomCertToDb(String certStr, CertificateVO cert, Long managementServerId) {
44-
BufferedInputStream f = null;
4541
try {
4642
cert.setCertificate(certStr);
4743
cert.setUpdated("Y");
@@ -50,12 +46,6 @@ public Long persistCustomCertToDb(String certStr, CertificateVO cert, Long manag
5046
} catch (Exception e) {
5147
s_logger.warn("Unable to read the certificate: " + e);
5248
return new Long(0);
53-
} finally {
54-
if (f != null)
55-
try {
56-
f.close();
57-
} catch (IOException ignored) {
58-
}
5949
}
6050
}
6151
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.upgrade.dao;
18+
19+
import org.apache.log4j.Logger;
20+
21+
public abstract class LegacyDbUpgrade implements DbUpgrade{
22+
23+
final static Logger s_logger = Logger.getLogger(LegacyDbUpgrade.class);
24+
25+
public LegacyDbUpgrade() {
26+
super();
27+
}
28+
29+
/**
30+
* @param closable
31+
*/
32+
protected void closeAutoCloseable(AutoCloseable closable) {
33+
if (closable != null) {
34+
try {
35+
closable.close();
36+
} catch (Exception e) {
37+
s_logger.info("[ignored]",e);
38+
}
39+
}
40+
}
41+
42+
}

engine/schema/src/com/cloud/upgrade/dao/Upgrade2214to30.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ private void encryptConfigValues(Connection conn) {
411411
pstmt.close();
412412
}
413413
} catch (SQLException e) {
414+
s_logger.info("[ignored]",e);
414415
}
415416
}
416417
s_logger.debug("Done encrypting Config values");
@@ -861,6 +862,7 @@ protected void updateReduntantRouters(Connection conn) {
861862
pstmt.executeUpdate();
862863
pstmt.close();
863864
} catch (SQLException e) {
865+
s_logger.info("[ignored]",e);
864866
}
865867
TransactionLegacy.closePstmts(pstmt2Close);
866868
}
@@ -1000,6 +1002,7 @@ protected void switchAccountSpecificNetworksToIsolated(Connection conn) {
10001002
pstmt.executeUpdate();
10011003
pstmt.close();
10021004
} catch (SQLException e) {
1005+
s_logger.info("[ignored]",e);
10031006
}
10041007
TransactionLegacy.closePstmts(pstmt2Close);
10051008
}
@@ -1053,6 +1056,7 @@ private void migrateUserConcentratedPlannerChoice(Connection conn) {
10531056
pstmt.close();
10541057
}
10551058
} catch (SQLException e) {
1059+
s_logger.info("[ignored]",e);
10561060
}
10571061
}
10581062
}
@@ -1151,11 +1155,10 @@ protected String fixNetworksWithExternalDevices(Connection conn) {
11511155
} catch (SQLException e) {
11521156
throw new CloudRuntimeException("Unable to switch networks to the new network offering", e);
11531157
} finally {
1154-
try {
1155-
pstmt = conn.prepareStatement("DROP TABLE `cloud`.`network_offerings2`");
1156-
pstmt.executeUpdate();
1157-
pstmt.close();
1158+
try (PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE `cloud`.`network_offerings2`");){
1159+
dropStatement.executeUpdate();
11581160
} catch (SQLException e) {
1161+
s_logger.info("[ignored]",e);
11591162
}
11601163
TransactionLegacy.closePstmts(pstmt2Close);
11611164
}

engine/schema/src/com/cloud/upgrade/dao/Upgrade222to224.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,21 @@ private void updateClusterIdInOpHostCapacity(Connection conn) {
207207
try {
208208
pstmtUpdate.close();
209209
} catch (SQLException e) {
210+
s_logger.info("[ignored]",e);
210211
}
211212
}
212213
if (rs != null) {
213214
try {
214215
rs.close();
215216
} catch (SQLException e) {
217+
s_logger.info("[ignored]",e);
216218
}
217219
}
218220
if (pstmt != null) {
219221
try {
220222
pstmt.close();
221223
} catch (SQLException e) {
224+
s_logger.info("[ignored]",e);
222225
}
223226
}
224227

@@ -406,18 +409,21 @@ private void updateTotalCPUInOpHostCapacity(Connection conn) {
406409
try {
407410
pstmtUpdate.close();
408411
} catch (SQLException e) {
412+
s_logger.info("[ignored]",e);
409413
}
410414
}
411415
if (rs != null) {
412416
try {
413417
rs.close();
414418
} catch (SQLException e) {
419+
s_logger.info("[ignored]",e);
415420
}
416421
}
417422
if (pstmt != null) {
418423
try {
419424
pstmt.close();
420425
} catch (SQLException e) {
426+
s_logger.info("[ignored]",e);
421427
}
422428
}
423429

engine/schema/src/com/cloud/upgrade/dao/Upgrade229to2210.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,41 +68,29 @@ public File[] getCleanupScripts() {
6868
}
6969

7070
private void updateSnapshots(Connection conn) {
71-
PreparedStatement pstmt = null;
72-
ResultSet rs = null;
7371
long currentSnapshotId = 0;
74-
try {
75-
pstmt =
76-
conn.prepareStatement("select id, prev_snap_id from snapshots where sechost_id is NULL and prev_snap_id is not NULL and status=\"BackedUp\" and removed is NULL order by id");
77-
rs = pstmt.executeQuery();
72+
try (
73+
PreparedStatement pstmt = conn.prepareStatement("select id, prev_snap_id from snapshots where sechost_id is NULL and prev_snap_id is not NULL and status=\"BackedUp\" and removed is NULL order by id");
74+
ResultSet rs = pstmt.executeQuery();
75+
PreparedStatement pstmt2 = conn.prepareStatement("select sechost_id from snapshots where id=? and sechost_id is not NULL");
76+
PreparedStatement updateSnapshotStatement = conn.prepareStatement("update snapshots set sechost_id=? where id=?");
77+
){
7878
while (rs.next()) {
7979
long id = rs.getLong(1);
8080
long preSnapId = rs.getLong(2);
8181
currentSnapshotId = id;
82-
pstmt = conn.prepareStatement("select sechost_id from snapshots where id=? and sechost_id is not NULL");
83-
pstmt.setLong(1, preSnapId);
84-
ResultSet sechost = pstmt.executeQuery();
85-
if (sechost.next()) {
86-
long secHostId = sechost.getLong(1);
87-
pstmt = conn.prepareStatement("update snapshots set sechost_id=? where id=?");
88-
pstmt.setLong(1, secHostId);
89-
pstmt.setLong(2, id);
90-
pstmt.executeUpdate();
82+
pstmt2.setLong(1, preSnapId);
83+
try (ResultSet sechost = pstmt2.executeQuery();) {
84+
if (sechost.next()) {
85+
long secHostId = sechost.getLong(1);
86+
updateSnapshotStatement.setLong(1, secHostId);
87+
updateSnapshotStatement.setLong(2, id);
88+
updateSnapshotStatement.executeUpdate();
89+
}
9190
}
9291
}
9392
} catch (SQLException e) {
9493
throw new CloudRuntimeException("Unable to update snapshots id=" + currentSnapshotId, e);
95-
} finally {
96-
try {
97-
if (rs != null) {
98-
rs.close();
99-
}
100-
101-
if (pstmt != null) {
102-
pstmt.close();
103-
}
104-
} catch (SQLException e) {
105-
}
10694
}
10795
}
10896

@@ -192,6 +180,7 @@ private void updateFirewallRules(Connection conn) {
192180
pstmt.close();
193181
}
194182
} catch (SQLException e) {
183+
s_logger.info("[ignored]",e);
195184
}
196185
}
197186
}

0 commit comments

Comments
 (0)