diff --git a/agent/pom.xml b/agent/pom.xml index 7b00a93963f..14133226053 100644 --- a/agent/pom.xml +++ b/agent/pom.xml @@ -36,6 +36,10 @@ cloud-utils ${project.version} + + commons-io + commons-io + commons-daemon commons-daemon diff --git a/agent/src/com/cloud/agent/AgentShell.java b/agent/src/com/cloud/agent/AgentShell.java index 70eaceb5966..900a13f4ab1 100644 --- a/agent/src/com/cloud/agent/AgentShell.java +++ b/agent/src/com/cloud/agent/AgentShell.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -36,6 +37,7 @@ import javax.naming.ConfigurationException; import org.apache.commons.daemon.Daemon; import org.apache.commons.daemon.DaemonContext; import org.apache.commons.daemon.DaemonInitException; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; @@ -165,7 +167,7 @@ public class AgentShell implements IAgentShell, Daemon { _storage.persist(name, value); } - private void loadProperties() throws ConfigurationException { + void loadProperties() throws ConfigurationException { final File file = PropertiesUtil.findConfigFile("agent.properties"); if (file == null) { throw new ConfigurationException("Unable to find agent.properties."); @@ -173,14 +175,18 @@ public class AgentShell implements IAgentShell, Daemon { s_logger.info("agent.properties found at " + file.getAbsolutePath()); + InputStream propertiesStream = null; try { - _properties.load(new FileInputStream(file)); + propertiesStream = new FileInputStream(file); + _properties.load(propertiesStream); } catch (final FileNotFoundException ex) { throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex); } catch (final IOException ex) { throw new CloudRuntimeException("IOException in reading " + file.getAbsolutePath(), ex); + } finally { + IOUtils.closeQuietly(propertiesStream); } } @@ -304,7 +310,7 @@ public class AgentShell implements IAgentShell, Daemon { // For KVM agent, do it specially here File file = new File("/etc/cloudstack/agent/log4j-cloud.xml"); - if(file == null || !file.exists()) { + if(!file.exists()) { file = PropertiesUtil.findConfigFile("log4j-cloud.xml"); } DOMConfigurator.configureAndWatch(file.getAbsolutePath()); diff --git a/agent/test/com/cloud/agent/AgentShellTest.java b/agent/test/com/cloud/agent/AgentShellTest.java index 883790f4453..e33de48c6bd 100644 --- a/agent/test/com/cloud/agent/AgentShellTest.java +++ b/agent/test/com/cloud/agent/AgentShellTest.java @@ -22,4 +22,11 @@ public class AgentShellTest { Assert.assertEquals("pod1", shell.getPod()); Assert.assertEquals("zone1", shell.getZone()); } + @Test + public void loadProperties() throws ConfigurationException { + AgentShell shell = new AgentShell(); + shell.loadProperties(); + Assert.assertNotNull(shell.getProperties()); + Assert.assertFalse(shell.getProperties().entrySet().isEmpty()); + } }