1 17 20 package org.apache.forrest.forrestbot.webapp; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 import java.util.Properties ; 26 27 import org.apache.log4j.Logger; 28 import org.apache.log4j.PropertyConfigurator; 29 30 33 public class Config { 34 private static Logger log = Logger.getLogger(Config.class); 35 private static Properties p = new Properties (); 36 37 private static final class SingletonHolder { 39 static final Config a = new Config(); 40 } 41 public static Config getInstance() { 43 return SingletonHolder.a; 44 } 45 46 private Config() { 48 configureLog4j(); 49 configureProperties(); 50 validateProperties(); 51 debugProperties(); 52 } 53 54 private static void configureLog4j() { 55 Properties log4j = new Properties (); 56 try { 57 log4j.load(Config.class.getClassLoader().getResourceAsStream("log4j.properties")); 58 } catch (IOException e1) { 59 log.warn("can't load log4j.properties", e1); 60 } 61 PropertyConfigurator.configure(log4j); 62 } 63 64 private static void configureProperties() { 65 log.info("loading settings.properties"); 66 try { 67 p.load( 68 Config.class.getClassLoader().getResourceAsStream("settings.properties")); 69 } catch (IOException e) { 70 log.error("can't load settings.properties", e); 71 } 72 } 73 74 public static void validateProperties() { 75 String [] requiredProperties = { "forrest-exec", "config-dir", "build-dir", "logs-dir", "build-url", "refreshrate", "debug-exec", "targets.build", "targets.deploy" }; 76 String [] filesToCheck = { "forrest-exec" }; 77 String [] directoriesToCheck = { "config-dir", "build-dir", "logs-dir" }; 78 79 for (int i = 0; i < requiredProperties.length; i++) { 80 if (getProperty(requiredProperties[i]) == null) { 81 log.error("Property " + requiredProperties[i] + " is required."); 82 } 83 } 84 for (int i = 0; i < filesToCheck.length; i++) { 85 File f = new File (getProperty(filesToCheck[i])); 86 if (!f.isFile()) { 87 log.error("Property " + filesToCheck[i] + " must reference a file. Current value: " + f.toString()); 88 } 89 } 90 for (int i = 0; i < directoriesToCheck.length; i++) { 91 File f = new File (getProperty(directoriesToCheck[i])); 92 if (!f.isDirectory()) { 93 log.error("Property " + directoriesToCheck[i] + " must reference a directory. Current value: " + f.toString()); 94 } 95 } 96 } 97 98 protected static void debugProperties() { 99 log.debug("properties loaded from settings.properties:"); 100 for (Iterator i = p.keySet().iterator(); i.hasNext();) { 101 String key = (String )i.next(); 102 log.debug(key + "=" + p.getProperty(key)); 103 } 104 } 105 106 public static String getProperty(String arg0, String arg1) { 107 return p.getProperty(arg0, arg1); 108 } 109 110 public static String getProperty(String arg0) { 111 return p.getProperty(arg0); 112 } 113 114 } 115 | Popular Tags |