From bd190192b92a24de048b8a03edbe9d26a3fcef66 Mon Sep 17 00:00:00 2001 From: Santhosh Edukulla Date: Mon, 13 Oct 2014 17:49:07 +0530 Subject: [PATCH] Fixed Resource Leaks Signed-off-by: Santhosh Edukulla (cherry picked from commit 350ac4c4b0e71156ed27307b2d625f46453500a2) Conflicts: engine/schema/src/com/cloud/upgrade/dao/Upgrade441to450.java --- .../test/com/cloud/utils/db/DbTestUtils.java | 39 +++++-------------- .../datastore/util/NexentaNmsClient.java | 11 ++++-- .../storage/datastore/util/SolidFireUtil.java | 13 ++++--- .../cloudstack/region/RegionsApiUtil.java | 16 ++++++-- .../resource/NfsSecondaryStorageResource.java | 7 +++- .../LocalNfsSecondaryStorageResourceTest.java | 4 +- .../NfsSecondaryStorageResourceTest.java | 6 +-- 7 files changed, 45 insertions(+), 51 deletions(-) diff --git a/framework/db/test/com/cloud/utils/db/DbTestUtils.java b/framework/db/test/com/cloud/utils/db/DbTestUtils.java index 038809fe0e7..4d0ecc664db 100644 --- a/framework/db/test/com/cloud/utils/db/DbTestUtils.java +++ b/framework/db/test/com/cloud/utils/db/DbTestUtils.java @@ -32,26 +32,16 @@ public class DbTestUtils { if (cleanScript == null) { throw new RuntimeException("Unable to clean the database because I can't find " + file); } - Connection conn = TransactionLegacy.getStandaloneConnection(); - ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError); - FileReader reader; - try { - reader = new FileReader(cleanScript); + try(FileReader reader = new FileReader(cleanScript);) + { + runner.runScript(reader); + conn.close(); } catch (FileNotFoundException e) { throw new RuntimeException("Unable to read " + file, e); - } - try { - runner.runScript(reader); - } catch (IOException e) { + }catch (IOException e) { throw new RuntimeException("Unable to read " + file, e); - } catch (SQLException e) { - throw new RuntimeException("Unable to execute " + file, e); - } - - try { - conn.close(); } catch (SQLException e) { throw new RuntimeException("Unable to close DB connection", e); } @@ -62,24 +52,15 @@ public class DbTestUtils { if (cleanScript == null) { throw new RuntimeException("Unable to clean the database because I can't find " + file); } - Connection conn = TransactionLegacy.getStandaloneUsageConnection(); - ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError); - FileReader reader; - try { - reader = new FileReader(cleanScript); - } catch (FileNotFoundException e) { - throw new RuntimeException("Unable to read " + file, e); - } - try { + try(FileReader reader = new FileReader(cleanScript);) { runner.runScript(reader); - } catch (IOException e) { - throw new RuntimeException("Unable to read " + file, e); - } catch (SQLException e) { - throw new RuntimeException("Unable to execute " + file, e); + } catch (IOException e){ + throw new RuntimeException("executeUsageScript:Exception:"+e.getMessage(),e); + }catch (SQLException e){ + throw new RuntimeException("executeUsageScript:Exception:"+e.getMessage(),e); } - try { conn.close(); } catch (SQLException e) { diff --git a/plugins/storage/volume/nexenta/src/org/apache/cloudstack/storage/datastore/util/NexentaNmsClient.java b/plugins/storage/volume/nexenta/src/org/apache/cloudstack/storage/datastore/util/NexentaNmsClient.java index c0cd51da136..e1a59f78fac 100644 --- a/plugins/storage/volume/nexenta/src/org/apache/cloudstack/storage/datastore/util/NexentaNmsClient.java +++ b/plugins/storage/volume/nexenta/src/org/apache/cloudstack/storage/datastore/util/NexentaNmsClient.java @@ -199,10 +199,13 @@ public class NexentaNmsClient { if (!isSuccess(status)) { throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + status); } - BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - String tmp; - while ((tmp = buffer.readLine()) != null) { - sb.append(tmp); + try(BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));) { + String tmp; + while ((tmp = buffer.readLine()) != null) { + sb.append(tmp); + } + }catch (IOException ex) { + throw new CloudRuntimeException(ex.getMessage()); } } catch (ClientProtocolException ex) { throw new CloudRuntimeException(ex.getMessage()); diff --git a/plugins/storage/volume/solidfire/src/org/apache/cloudstack/storage/datastore/util/SolidFireUtil.java b/plugins/storage/volume/solidfire/src/org/apache/cloudstack/storage/datastore/util/SolidFireUtil.java index 97f5b653363..59885063fe4 100644 --- a/plugins/storage/volume/solidfire/src/org/apache/cloudstack/storage/datastore/util/SolidFireUtil.java +++ b/plugins/storage/volume/solidfire/src/org/apache/cloudstack/storage/datastore/util/SolidFireUtil.java @@ -1669,12 +1669,13 @@ public class SolidFireUtil { throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + response.getStatusLine().getStatusCode()); } - BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - - String strOutput; - - while ((strOutput = br.readLine()) != null) { - sb.append(strOutput); + try(BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));) { + String strOutput; + while ((strOutput = br.readLine()) != null) { + sb.append(strOutput); + } + }catch (IOException ex) { + throw new CloudRuntimeException(ex.getMessage()); } } catch (UnsupportedEncodingException ex) { throw new CloudRuntimeException(ex.getMessage()); diff --git a/server/src/org/apache/cloudstack/region/RegionsApiUtil.java b/server/src/org/apache/cloudstack/region/RegionsApiUtil.java index 679f6d88a75..7894e3797da 100644 --- a/server/src/org/apache/cloudstack/region/RegionsApiUtil.java +++ b/server/src/org/apache/cloudstack/region/RegionsApiUtil.java @@ -105,8 +105,12 @@ public class RegionsApiUtil { xstream.aliasField("networkdomain", RegionAccount.class, "networkDomain"); xstream.aliasField("id", RegionUser.class, "uuid"); xstream.aliasField("accountId", RegionUser.class, "accountUuid"); - ObjectInputStream in = xstream.createObjectInputStream(is); - return (RegionAccount)in.readObject(); + try(ObjectInputStream in = xstream.createObjectInputStream(is);) { + return (RegionAccount) in.readObject(); + }catch (IOException e) { + s_logger.error(e.getMessage()); + return null; + } } else { return null; } @@ -143,8 +147,12 @@ public class RegionsApiUtil { xstream.aliasField("id", RegionDomain.class, "uuid"); xstream.aliasField("parentdomainid", RegionDomain.class, "parentUuid"); xstream.aliasField("networkdomain", DomainVO.class, "networkDomain"); - ObjectInputStream in = xstream.createObjectInputStream(is); - return (RegionDomain)in.readObject(); + try(ObjectInputStream in = xstream.createObjectInputStream(is);) { + return (RegionDomain) in.readObject(); + }catch (IOException e) { + s_logger.error(e.getMessage()); + return null; + } } else { return null; } diff --git a/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java b/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java index c0b4da07938..230ef84db37 100755 --- a/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java +++ b/services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java @@ -692,8 +692,11 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S if (!destFile.createNewFile()) { s_logger.warn("Reusing existing file " + destFile.getPath()); } - FileOutputStream outputStream = new FileOutputStream(destFile); - entity.writeTo(outputStream); + try(FileOutputStream outputStream = new FileOutputStream(destFile);) { + entity.writeTo(outputStream); + }catch (IOException e) { + s_logger.debug("downloadFromUrlToNfs:Exception:"+e.getMessage(),e); + } return new File(destFile.getAbsolutePath()); } catch (IOException e) { s_logger.debug("Faild to get url:" + url + ", due to " + e.toString()); diff --git a/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/LocalNfsSecondaryStorageResourceTest.java b/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/LocalNfsSecondaryStorageResourceTest.java index e0fcbae51dd..52bde6a11da 100644 --- a/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/LocalNfsSecondaryStorageResourceTest.java +++ b/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/LocalNfsSecondaryStorageResourceTest.java @@ -131,8 +131,8 @@ public class LocalNfsSecondaryStorageResourceTest extends TestCase { s_logger.info("agent.properties found at " + file.getAbsolutePath()); - try { - properties.load(new FileInputStream(file)); + try(FileInputStream fs = new FileInputStream(file);) { + properties.load(fs); } catch (final FileNotFoundException ex) { throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex); } catch (final IOException ex) { diff --git a/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResourceTest.java b/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResourceTest.java index e0ae4c5f365..13ddb3531a8 100644 --- a/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResourceTest.java +++ b/services/secondary-storage/server/test/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResourceTest.java @@ -94,11 +94,9 @@ public class NfsSecondaryStorageResourceTest extends TestCase { if (file == null) { throw new ConfigurationException("Unable to find agent.properties."); } - s_logger.info("agent.properties found at " + file.getAbsolutePath()); - - try { - properties.load(new FileInputStream(file)); + try(FileInputStream fs = new FileInputStream(file);) { + properties.load(fs); } catch (final FileNotFoundException ex) { throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex); } catch (final IOException ex) {