From: <do...@hy...> - 2007-06-21 02:11:44
|
Author: dougm Date: 2007-06-20 19:11:41 -0700 (Wed, 20 Jun 2007) New Revision: 4854 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=4854 Modified: trunk/src/org/hyperic/hq/agent/AgentConfig.java Log: fold some duplication, may add another agent properties override file Modified: trunk/src/org/hyperic/hq/agent/AgentConfig.java =================================================================== --- trunk/src/org/hyperic/hq/agent/AgentConfig.java 2007-06-21 01:56:59 UTC (rev 4853) +++ trunk/src/org/hyperic/hq/agent/AgentConfig.java 2007-06-21 02:11:41 UTC (rev 4854) @@ -119,53 +119,61 @@ } } - public static AgentConfig newInstance(String propsFile) - throws IOException, AgentConfigException - { + private static boolean loadProps(Properties props, File file) { FileInputStream fin = null; - Properties useProps; + if (!file.exists()) { + return false; + } + if (!file.canRead()) { + return false; + } + try { - fin = new FileInputStream(propsFile); - useProps = new Properties(); - useProps.putAll(AgentConfig.getDefaultProperties()); - useProps.load(fin); + fin = new FileInputStream(file); + props.load(fin); + return true; + } catch (IOException e) { + return false; } finally { - try {if(fin != null) fin.close(); } catch(Exception exc){} + if (fin != null) { + try { fin.close(); } catch (IOException e) {} + } } + } - fin = null; - try { - File userFile; + public static AgentConfig newInstance(String propsFile) + throws IOException, AgentConfigException + { + Properties useProps = new Properties(); + File file = new File(propsFile); - userFile = new File(new File(System.getProperty("user.home"), - ".hq"), DEFAULT_PROPFILE); - if (!userFile.exists()) { - // Backwards compat - userFile = new File(new File(System.getProperty("user.home"), - ".cam"), DEFAULT_PROPFILE); - } + useProps.putAll(AgentConfig.getDefaultProperties()); - fin = new FileInputStream(userFile); - useProps.load(fin); - } catch(Exception exc){ - // Ignore failures here -- it's ok - } finally { - try {if(fin != null) fin.close(); } catch(Exception exc){} + if (!loadProps(useProps, file)) { + throw new AgentConfigException("Failed to load: " + propsFile); } - - // Check if the deployer has generated setup properties - File deployerProps = new File("deployer.properties"); - if (deployerProps.exists()) { - try { - fin = null; - fin = new FileInputStream(deployerProps); - useProps.load(fin); - } finally { - try {if(fin != null) fin.close(); } catch(Exception exc){} + + final String home = System.getProperty("user.home"); + String[] userFiles = { + //XXX Backwards compat, remove for 3.1 or 4.0? + home + File.separator + ".cam" + File.separator + DEFAULT_PROPFILE, + //User overrides + home + File.separator + ".hq" + File.separator + DEFAULT_PROPFILE, + //XXX considering yet another for windows where only cygwin can 'mkdir .hq'? + //"../" + File.separator + "hq-" + DEFAULT_PROPFILE, + //Check if the deployer has generated setup properties + "deployer.properties" + }; + + for (int i=0; i<userFiles.length; i++) { + file = new File(userFiles[i]); + if (loadProps(useProps, file)) { + //XXX logging not configured yet + //System.out.println("Loaded properties from: " + file); } } - + PropertyUtil.expandVariables(useProps); // More backwards compatibility. Strip all net.covalent prefixes |