Fixing coverity issues related to resource leak on FileInputStream being created anonymously

Signed-off-by: Daan Hoogland <daan@onecht.net>
This commit is contained in:
wilderrodrigues 2013-11-18 15:35:34 +01:00 committed by Daan Hoogland
parent 2774b62d64
commit d19688103b
5 changed files with 104 additions and 83 deletions

View File

@ -58,6 +58,7 @@ import org.apache.axis2.databinding.ADBBean;
import org.apache.axis2.databinding.ADBException; import org.apache.axis2.databinding.ADBException;
import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLSerializer; import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLSerializer;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport; import org.springframework.web.context.support.SpringBeanAutowiringSupport;
@ -103,7 +104,6 @@ import com.amazon.ec2.RunInstancesResponse;
import com.amazon.ec2.StartInstancesResponse; import com.amazon.ec2.StartInstancesResponse;
import com.amazon.ec2.StopInstancesResponse; import com.amazon.ec2.StopInstancesResponse;
import com.amazon.ec2.TerminateInstancesResponse; import com.amazon.ec2.TerminateInstancesResponse;
import com.cloud.bridge.model.UserCredentialsVO; import com.cloud.bridge.model.UserCredentialsVO;
import com.cloud.bridge.persist.dao.CloudStackUserDaoImpl; import com.cloud.bridge.persist.dao.CloudStackUserDaoImpl;
import com.cloud.bridge.persist.dao.OfferingDaoImpl; import com.cloud.bridge.persist.dao.OfferingDaoImpl;
@ -203,12 +203,16 @@ public class EC2RestServlet extends HttpServlet {
if (null != propertiesFile) { if (null != propertiesFile) {
logger.info("Use EC2 properties file: " + propertiesFile.getAbsolutePath()); logger.info("Use EC2 properties file: " + propertiesFile.getAbsolutePath());
EC2Prop = new Properties(); EC2Prop = new Properties();
FileInputStream ec2PropFile = null;
try { try {
EC2Prop.load(new FileInputStream(propertiesFile)); ec2PropFile = new FileInputStream(propertiesFile);
EC2Prop.load(ec2PropFile);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e); logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e);
} catch (IOException e) { } catch (IOException e) {
logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e); logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(ec2PropFile);
} }
String keystore = EC2Prop.getProperty("keystore"); String keystore = EC2Prop.getProperty("keystore");
keystorePassword = EC2Prop.getProperty("keystorePass"); keystorePassword = EC2Prop.getProperty("keystorePass");

View File

@ -34,15 +34,14 @@ import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.axis2.AxisFault; import org.apache.axis2.AxisFault;
import org.apache.cloudstack.managed.context.ManagedContextTimerTask;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator; import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.amazon.ec2.AmazonEC2SkeletonInterface; import com.amazon.ec2.AmazonEC2SkeletonInterface;
import com.amazon.s3.AmazonS3SkeletonInterface; import com.amazon.s3.AmazonS3SkeletonInterface;
import org.apache.cloudstack.managed.context.ManagedContextTimerTask;
import com.cloud.bridge.model.MHostVO; import com.cloud.bridge.model.MHostVO;
import com.cloud.bridge.model.SHost; import com.cloud.bridge.model.SHost;
import com.cloud.bridge.model.SHostVO; import com.cloud.bridge.model.SHostVO;
@ -111,8 +110,8 @@ public class ServiceProvider extends ManagerBase {
instance = this; instance = this;
} }
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
initialize(); initialize();
return true; return true;
} }
@ -268,12 +267,16 @@ public class ServiceProvider extends ManagerBase {
File propertiesFile = ConfigurationHelper.findConfigurationFile("cloud-bridge.properties"); File propertiesFile = ConfigurationHelper.findConfigurationFile("cloud-bridge.properties");
properties = new Properties(); properties = new Properties();
if (propertiesFile != null) { if (propertiesFile != null) {
FileInputStream startProps = null;
try { try {
properties.load(new FileInputStream(propertiesFile)); startProps = new FileInputStream(propertiesFile);
properties.load(startProps);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e); logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e);
} catch (IOException e) { } catch (IOException e) {
logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e); logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(startProps);
} }
logger.info("Use startup properties file: " + propertiesFile.getAbsolutePath()); logger.info("Use startup properties file: " + propertiesFile.getAbsolutePath());

View File

@ -35,6 +35,7 @@ import javax.inject.Inject;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -122,12 +123,18 @@ public class EC2Engine extends ManagerBase {
if (null != propertiesFile) { if (null != propertiesFile) {
logger.info("Use EC2 properties file: " + propertiesFile.getAbsolutePath()); logger.info("Use EC2 properties file: " + propertiesFile.getAbsolutePath());
Properties EC2Prop = new Properties(); Properties EC2Prop = new Properties();
FileInputStream ec2PropFile = null;
try { try {
EC2Prop.load(new FileInputStream(propertiesFile)); EC2Prop.load(new FileInputStream(propertiesFile));
ec2PropFile = new FileInputStream( propertiesFile );
EC2Prop.load(ec2PropFile);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e); logger.warn("Unable to open properties file: " + propertiesFile.getAbsolutePath(), e);
} catch (IOException e) { } catch (IOException e) {
logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e); logger.warn("Unable to read properties file: " + propertiesFile.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(ec2PropFile);
} }
managementServer = EC2Prop.getProperty("managementServer"); managementServer = EC2Prop.getProperty("managementServer");
cloudAPIPort = EC2Prop.getProperty("cloudAPIPort", null); cloudAPIPort = EC2Prop.getProperty("cloudAPIPort", null);
@ -1217,7 +1224,7 @@ public class EC2Engine extends ManagerBase {
throw new EC2ServiceException(ServerError.InternalError, "No Customize Disk Offering Found"); throw new EC2ServiceException(ServerError.InternalError, "No Customize Disk Offering Found");
} }
// // -> no volume name is given in the Amazon request but is required in the cloud API // -> no volume name is given in the Amazon request but is required in the cloud API
CloudStackVolume vol = CloudStackVolume vol =
getApi().createVolume(UUID.randomUUID().toString(), null, diskOfferingId, null, size, snapshotId, toZoneId(request.getZoneName(), null)); getApi().createVolume(UUID.randomUUID().toString(), null, diskOfferingId, null, size, snapshotId, toZoneId(request.getZoneName(), null));
if (vol != null) { if (vol != null) {

View File

@ -40,13 +40,12 @@ import javax.ejb.Local;
import javax.inject.Inject; import javax.inject.Inject;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import org.apache.cloudstack.framework.config.ConfigDepot; import org.apache.cloudstack.framework.config.ConfigDepot;
import org.apache.cloudstack.framework.config.ConfigKey; import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.Configurable; import org.apache.cloudstack.framework.config.Configurable;
import org.apache.cloudstack.managed.context.ManagedContextRunnable; import org.apache.cloudstack.managed.context.ManagedContextRunnable;
import org.apache.cloudstack.utils.identity.ManagementServerNode; import org.apache.cloudstack.utils.identity.ManagementServerNode;
import org.apache.log4j.Logger;
import com.cloud.cluster.dao.ManagementServerHostDao; import com.cloud.cluster.dao.ManagementServerHostDao;
import com.cloud.cluster.dao.ManagementServerHostPeerDao; import com.cloud.cluster.dao.ManagementServerHostPeerDao;
@ -625,8 +624,10 @@ public class ClusterManagerImpl extends ManagerBase implements ClusterManager, C
if (_heartbeatConnection != null) { if (_heartbeatConnection != null) {
Connection conn = TransactionLegacy.getStandaloneConnection(); Connection conn = TransactionLegacy.getStandaloneConnection();
if (conn != null) { if (conn != null) {
_heartbeatConnection.reset(TransactionLegacy.getStandaloneConnection()); _heartbeatConnection.reset(conn);
} }
// The stand-alone connection does not have to be closed here because there will be another reference to it.
// As a matter of fact, it will be assigned to the connection instance variable in the ConnectionConcierge class.
} }
} }
@ -734,7 +735,6 @@ public class ClusterManagerImpl extends ManagerBase implements ClusterManager, C
default: default:
break; break;
} }
} }

View File

@ -19,6 +19,7 @@ package com.cloud.consoleproxy;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -35,11 +36,10 @@ import java.util.concurrent.Executor;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.xml.DOMConfigurator; import org.apache.log4j.xml.DOMConfigurator;
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpServer;
import com.cloud.consoleproxy.util.Logger; import com.cloud.consoleproxy.util.Logger;
import com.cloud.utils.PropertiesUtil; import com.cloud.utils.PropertiesUtil;
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpServer;
/** /**
* *
@ -387,6 +387,12 @@ public class ConsoleProxy {
conf.load(confs); conf.load(confs);
} catch (Exception e) { } catch (Exception e) {
s_logger.error(e.toString(), e); s_logger.error(e.toString(), e);
} finally {
try {
confs.close();
} catch (IOException ioex) {
s_logger.error(ioex.toString(), ioex);
}
} }
} }
start(conf); start(conf);
@ -454,6 +460,7 @@ public class ConsoleProxy {
throw new AuthenticationException("Cannot use the existing viewer " + viewer + ": bad sid"); throw new AuthenticationException("Cannot use the existing viewer " + viewer + ": bad sid");
if (!viewer.isFrontEndAlive()) { if (!viewer.isFrontEndAlive()) {
authenticationExternally(param); authenticationExternally(param);
viewer.initClient(param); viewer.initClient(param);
reportLoadChange = true; reportLoadChange = true;