From 7a6751aa770eaf8065864f497bff401012f553ae Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Thu, 14 Nov 2013 08:37:02 +0100 Subject: [PATCH] Make sure that if the file does not exist an Exception is thrown and that once it exists it is also closed after the properties are loaded. Signed-off-by: Hugo Trippaers --- .../management/ManagementNetworkGuru.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/plugins/network-elements/juniper-contrail/src/org/apache/cloudstack/network/contrail/management/ManagementNetworkGuru.java b/plugins/network-elements/juniper-contrail/src/org/apache/cloudstack/network/contrail/management/ManagementNetworkGuru.java index e86e98a215b..d3d9366ce4b 100644 --- a/plugins/network-elements/juniper-contrail/src/org/apache/cloudstack/network/contrail/management/ManagementNetworkGuru.java +++ b/plugins/network-elements/juniper-contrail/src/org/apache/cloudstack/network/contrail/management/ManagementNetworkGuru.java @@ -19,6 +19,8 @@ package org.apache.cloudstack.network.contrail.management; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Map; import java.util.Properties; @@ -60,12 +62,29 @@ public class ManagementNetworkGuru extends ContrailGuru { @Override public boolean configure(String name, Map params) throws ConfigurationException { File configFile = PropertiesUtil.findConfigFile(configuration); + FileInputStream inputFile = null; + + try { + if (null == configFile) { + throw new FileNotFoundException("Configuration file was not found!"); + } + inputFile = new FileInputStream(configFile); + } catch (FileNotFoundException e) { + s_logger.error(e.getMessage()); + throw new ConfigurationException(e.getMessage()); + } + final Properties configProps = new Properties(); try { - configProps.load(new FileInputStream(configFile)); - } catch (Exception ex) { - ex.printStackTrace(); - throw new ConfigurationException(ex.getMessage()); + configProps.load(inputFile); + } catch (IOException e) { + s_logger.error(e.getMessage()); + throw new ConfigurationException(e.getMessage()); + } finally { + try { + inputFile.close(); + } catch (IOException e) { + } } _mgmt_cidr = configProps.getProperty("management.cidr"); _mgmt_gateway = configProps.getProperty("management.gateway"); @@ -100,8 +119,8 @@ public class ManagementNetworkGuru extends ContrailGuru { return null; } NetworkVO network = - new NetworkVO(offering.getTrafficType(), Mode.Dhcp, BroadcastDomainType.Lswitch, offering.getId(), Network.State.Allocated, plan.getDataCenterId(), - plan.getPhysicalNetworkId()); + new NetworkVO(offering.getTrafficType(), Mode.Dhcp, BroadcastDomainType.Lswitch, offering.getId(), Network.State.Allocated, plan.getDataCenterId(), + plan.getPhysicalNetworkId()); if (_mgmt_cidr != null) { network.setCidr(_mgmt_cidr); network.setGateway(_mgmt_gateway);