Fixed Resource Leaks

Signed-off-by: Santhosh Edukulla <santhosh.edukulla@gmail.com>
(cherry picked from commit 350ac4c4b0e71156ed27307b2d625f46453500a2)

Conflicts:
	engine/schema/src/com/cloud/upgrade/dao/Upgrade441to450.java
This commit is contained in:
Santhosh Edukulla 2014-10-13 17:49:07 +05:30 committed by Rohit Yadav
parent 98b416b7f0
commit bd190192b9
7 changed files with 45 additions and 51 deletions

View File

@ -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) {

View File

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

View File

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

View File

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

View File

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

View File

@ -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) {

View File

@ -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) {