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) { if (cleanScript == null) {
throw new RuntimeException("Unable to clean the database because I can't find " + file); throw new RuntimeException("Unable to clean the database because I can't find " + file);
} }
Connection conn = TransactionLegacy.getStandaloneConnection(); Connection conn = TransactionLegacy.getStandaloneConnection();
ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError); ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError);
FileReader reader; try(FileReader reader = new FileReader(cleanScript);)
try { {
reader = new FileReader(cleanScript); runner.runScript(reader);
conn.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new RuntimeException("Unable to read " + file, 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); 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) { } catch (SQLException e) {
throw new RuntimeException("Unable to close DB connection", e); throw new RuntimeException("Unable to close DB connection", e);
} }
@ -62,24 +52,15 @@ public class DbTestUtils {
if (cleanScript == null) { if (cleanScript == null) {
throw new RuntimeException("Unable to clean the database because I can't find " + file); throw new RuntimeException("Unable to clean the database because I can't find " + file);
} }
Connection conn = TransactionLegacy.getStandaloneUsageConnection(); Connection conn = TransactionLegacy.getStandaloneUsageConnection();
ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError); ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError);
FileReader reader; try(FileReader reader = new FileReader(cleanScript);) {
try {
reader = new FileReader(cleanScript);
} catch (FileNotFoundException e) {
throw new RuntimeException("Unable to read " + file, e);
}
try {
runner.runScript(reader); runner.runScript(reader);
} catch (IOException e){ } catch (IOException e){
throw new RuntimeException("Unable to read " + file, e); throw new RuntimeException("executeUsageScript:Exception:"+e.getMessage(),e);
}catch (SQLException e){ }catch (SQLException e){
throw new RuntimeException("Unable to execute " + file, e); throw new RuntimeException("executeUsageScript:Exception:"+e.getMessage(),e);
} }
try { try {
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -199,11 +199,14 @@ public class NexentaNmsClient {
if (!isSuccess(status)) { if (!isSuccess(status)) {
throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + status); throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + status);
} }
BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); try(BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));) {
String tmp; String tmp;
while ((tmp = buffer.readLine()) != null) { while ((tmp = buffer.readLine()) != null) {
sb.append(tmp); sb.append(tmp);
} }
}catch (IOException ex) {
throw new CloudRuntimeException(ex.getMessage());
}
} catch (ClientProtocolException ex) { } catch (ClientProtocolException ex) {
throw new CloudRuntimeException(ex.getMessage()); throw new CloudRuntimeException(ex.getMessage());
} catch (IOException ex) { } catch (IOException ex) {

View File

@ -1669,13 +1669,14 @@ public class SolidFireUtil {
throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + response.getStatusLine().getStatusCode()); throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + response.getStatusLine().getStatusCode());
} }
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); try(BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));) {
String strOutput; String strOutput;
while ((strOutput = br.readLine()) != null) { while ((strOutput = br.readLine()) != null) {
sb.append(strOutput); sb.append(strOutput);
} }
}catch (IOException ex) {
throw new CloudRuntimeException(ex.getMessage());
}
} catch (UnsupportedEncodingException ex) { } catch (UnsupportedEncodingException ex) {
throw new CloudRuntimeException(ex.getMessage()); throw new CloudRuntimeException(ex.getMessage());
} catch (ClientProtocolException ex) { } catch (ClientProtocolException ex) {

View File

@ -105,8 +105,12 @@ public class RegionsApiUtil {
xstream.aliasField("networkdomain", RegionAccount.class, "networkDomain"); xstream.aliasField("networkdomain", RegionAccount.class, "networkDomain");
xstream.aliasField("id", RegionUser.class, "uuid"); xstream.aliasField("id", RegionUser.class, "uuid");
xstream.aliasField("accountId", RegionUser.class, "accountUuid"); xstream.aliasField("accountId", RegionUser.class, "accountUuid");
ObjectInputStream in = xstream.createObjectInputStream(is); try(ObjectInputStream in = xstream.createObjectInputStream(is);) {
return (RegionAccount) in.readObject(); return (RegionAccount) in.readObject();
}catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else { } else {
return null; return null;
} }
@ -143,8 +147,12 @@ public class RegionsApiUtil {
xstream.aliasField("id", RegionDomain.class, "uuid"); xstream.aliasField("id", RegionDomain.class, "uuid");
xstream.aliasField("parentdomainid", RegionDomain.class, "parentUuid"); xstream.aliasField("parentdomainid", RegionDomain.class, "parentUuid");
xstream.aliasField("networkdomain", DomainVO.class, "networkDomain"); xstream.aliasField("networkdomain", DomainVO.class, "networkDomain");
ObjectInputStream in = xstream.createObjectInputStream(is); try(ObjectInputStream in = xstream.createObjectInputStream(is);) {
return (RegionDomain) in.readObject(); return (RegionDomain) in.readObject();
}catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else { } else {
return null; return null;
} }

View File

@ -692,8 +692,11 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S
if (!destFile.createNewFile()) { if (!destFile.createNewFile()) {
s_logger.warn("Reusing existing file " + destFile.getPath()); s_logger.warn("Reusing existing file " + destFile.getPath());
} }
FileOutputStream outputStream = new FileOutputStream(destFile); try(FileOutputStream outputStream = new FileOutputStream(destFile);) {
entity.writeTo(outputStream); entity.writeTo(outputStream);
}catch (IOException e) {
s_logger.debug("downloadFromUrlToNfs:Exception:"+e.getMessage(),e);
}
return new File(destFile.getAbsolutePath()); return new File(destFile.getAbsolutePath());
} catch (IOException e) { } catch (IOException e) {
s_logger.debug("Faild to get url:" + url + ", due to " + e.toString()); 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()); s_logger.info("agent.properties found at " + file.getAbsolutePath());
try { try(FileInputStream fs = new FileInputStream(file);) {
properties.load(new FileInputStream(file)); properties.load(fs);
} catch (final FileNotFoundException ex) { } catch (final FileNotFoundException ex) {
throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex); throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex);
} catch (final IOException ex) { } catch (final IOException ex) {

View File

@ -94,11 +94,9 @@ public class NfsSecondaryStorageResourceTest extends TestCase {
if (file == null) { if (file == null) {
throw new ConfigurationException("Unable to find agent.properties."); throw new ConfigurationException("Unable to find agent.properties.");
} }
s_logger.info("agent.properties found at " + file.getAbsolutePath()); s_logger.info("agent.properties found at " + file.getAbsolutePath());
try(FileInputStream fs = new FileInputStream(file);) {
try { properties.load(fs);
properties.load(new FileInputStream(file));
} catch (final FileNotFoundException ex) { } catch (final FileNotFoundException ex) {
throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex); throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex);
} catch (final IOException ex) { } catch (final IOException ex) {