示例#1
0
  private void setBaseProps() {
    if (!properties.containsKey("slac.log.dir")) {
      properties.setProperty(
          "slac.log.dir", SlacUtil.ensureDir(SlacUtil.path(homeDir, "log")).getPath());
    }

    if (!properties.containsKey("slac.data.dir")) {
      properties.setProperty(
          "slac.data.dir", SlacUtil.ensureDir(SlacUtil.path(homeDir, "data")).getPath());
    }

    if (!properties.containsKey("slac.conf.dir")) {
      properties.setProperty(
          "slac.conf.dir", SlacUtil.ensureDir(SlacUtil.path(homeDir, "conf")).getPath());
    }

    if (!properties.containsKey("slac.backup.dir")) {
      properties.setProperty("slac.backup.dir", SlacUtil.path(homeDir, "backup"));
    }

    if (!properties.containsKey("phantom.binary")) {
      for (String path :
          new String[] {
            SlacUtil.path(homeDir, "phantomjs"), "/usr/local/bin/phantomjs", "/usr/bin/phantomjs"
          }) {
        if (new File(path).canExecute()) {
          properties.setProperty("phantom.binary", path);
          break;
        }
      }
    }
  }
示例#2
0
  protected void loadProperties(String home, String fname, String defPath) {
    homeDir = home;
    properties = defaultProperties(defPath);
    String propPath = SlacUtil.path(homeDir, fname);
    loadCfg(properties, propPath, true);

    properties.put(PROP_HOME_DIR, homeDir);
  }
示例#3
0
@Singleton
public class SlacConfig {

  private static final Logger log = LoggerFactory.getLogger(SlacConfig.class);

  /** Configuration properties */
  protected Properties properties;

  /** Home directory */
  protected String homeDir;

  public static final String PROP_HOME_DIR = "slac.home.dir";
  public static final String DEFAULT_CONF_PATH = "/com/jitlogic/slac/slac.properties";

  @Inject
  public SlacConfig(@Named("slac.home.dir") String home) {
    loadProperties(home, "slac.properties", DEFAULT_CONF_PATH);
    setBaseProps();
  }

  private void setBaseProps() {
    if (!properties.containsKey("slac.log.dir")) {
      properties.setProperty(
          "slac.log.dir", SlacUtil.ensureDir(SlacUtil.path(homeDir, "log")).getPath());
    }

    if (!properties.containsKey("slac.data.dir")) {
      properties.setProperty(
          "slac.data.dir", SlacUtil.ensureDir(SlacUtil.path(homeDir, "data")).getPath());
    }

    if (!properties.containsKey("slac.conf.dir")) {
      properties.setProperty(
          "slac.conf.dir", SlacUtil.ensureDir(SlacUtil.path(homeDir, "conf")).getPath());
    }

    if (!properties.containsKey("slac.backup.dir")) {
      properties.setProperty("slac.backup.dir", SlacUtil.path(homeDir, "backup"));
    }

    if (!properties.containsKey("phantom.binary")) {
      for (String path :
          new String[] {
            SlacUtil.path(homeDir, "phantomjs"), "/usr/local/bin/phantomjs", "/usr/bin/phantomjs"
          }) {
        if (new File(path).canExecute()) {
          properties.setProperty("phantom.binary", path);
          break;
        }
      }
    }
  }

  /**
   * Returns configuration properties.
   *
   * @return configuration properties
   */
  public Properties getProperties() {
    return properties;
  }

  public String getHomeDir() {
    return homeDir;
  }

  /**
   * Returns path to log directory.
   *
   * @return directory where agent will write its logs.
   */
  public String getLogDir() {
    return SlacUtil.path(homeDir, "log");
  }

  /**
   * Returns default configuration properties. This is read from property file embedded in agent jar
   * file.
   *
   * @return default configuration properties.
   */
  public static Properties defaultProperties(String path) {
    Properties props = new Properties();

    InputStream is = null;

    try {
      is = SlacConfig.class.getResourceAsStream(path);
      props.load(is);
      is.close();
    } catch (IOException e) {
      log.error("Error loading property file", e);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          log.error("Error closing property file", e);
        }
      }
    }
    return props;
  }

  public Properties loadCfg(Properties properties, String propPath, boolean verbose) {
    InputStream is = null;
    try {
      is = new FileInputStream(propPath);
      properties.load(is);
      return properties;
    } catch (IOException e) {
      if (verbose) {
        log.error("Error loading property file", e);
      }
    } finally {
      if (is != null)
        try {
          is.close();
        } catch (IOException e) {
          if (verbose) {
            log.error("Error closing property file", e);
          }
        }
    }
    return null;
  }

  public List<String> listCfg(String key, String... defVals) {
    return listCfg(properties, key, defVals);
  }

  protected List<String> listCfg(Properties properties, String key, String... defVals) {
    String s = properties.getProperty(key);

    if (s != null) {
      String[] ss = s.split(",");
      List<String> lst = new ArrayList<String>(ss.length);
      for (String str : ss) {
        str = str.trim();
        if (str.length() > 0) {
          lst.add(str);
        }
      }
      return lst;
    } else {
      return Arrays.asList(defVals);
    }
  }

  private static Map<String, Long> kilos =
      SlacUtil.constMap(
          "k",
          1024L,
          "K",
          1024L,
          "m",
          1024 * 1024L,
          "M",
          1024 * 1024L,
          "g",
          1024 * 1024 * 1024L,
          "G",
          1024 * 1024 * 1024L,
          "t",
          1024 * 1024 * 1024 * 1024L,
          "T",
          1024 * 1024 * 1024 * 1024L);

  private static Pattern kiloRe = Pattern.compile("^([0-9]+)([kKmMgGtT])$");

  public Long kiloCfg(String key, Long defval) {
    String s = properties.getProperty(key);

    long multi = 1L;

    if (s != null) {
      Matcher matcher = kiloRe.matcher(s);

      if (matcher.matches()) {
        s = matcher.group(1);
        multi = kilos.get(matcher.group(2));
      }
    }

    try {
      if (s != null) {
        return Long.parseLong(s.trim()) * multi;
      } else {
        return defval;
      }
    } catch (NumberFormatException e) {
      log.error(
          "Cannot parse key '"
              + key
              + "' -> '"
              + s
              + "'. Returning default value of "
              + defval
              + ".",
          e);
      return defval;
    }
  }

  public String stringCfg(String key) {
    return stringCfg(key, null);
  }

  public String stringCfg(String key, String defval) {
    String s = properties.getProperty(key);

    return s != null ? s.trim() : defval;
  }

  public Long longCfg(String key, Long defval) {
    String s = properties.getProperty(key);

    try {
      if (s != null) {
        return Long.parseLong(s.trim());
      } else {
        return defval;
      }
    } catch (NumberFormatException e) {
      log.error(
          "Cannot parse key '"
              + key
              + "' -> '"
              + s
              + "'. Returning default value of "
              + defval
              + ".",
          e);
      return defval;
    }
  }

  public Integer intCfg(String key) {
    return intCfg(key, 0);
  }

  public Integer intCfg(String key, Integer defval) {
    String s = properties.getProperty(key);

    try {
      if (s != null) {
        return Integer.parseInt(s.trim());
      } else {
        return defval;
      }
    } catch (NumberFormatException e) {
      log.error(
          "Cannot parse key '"
              + key
              + "' -> '"
              + s
              + "'. Returning default value of "
              + defval
              + ".",
          e);
      return defval;
    }
  }

  public Boolean boolCfg(String key, Boolean defval) {
    String s = properties.getProperty(key);

    if (s != null) {
      s = s.trim();

      if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase(("yes"))) {
        return true;
      } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("no")) {
        return false;
      } else {
        log.error(
            "Invalid value for '" + key + "' -> '" + s + "'. Setting default value of '" + defval);
      }
    }

    return defval;
  }

  public boolean hasCfg(String key) {
    String s = properties.getProperty(key);

    return s != null && s.trim().length() > 0;
  }

  public void setCfg(String key, Object val) {
    properties.setProperty(key, "" + val);
  }

  protected void loadProperties(String home, String fname, String defPath) {
    homeDir = home;
    properties = defaultProperties(defPath);
    String propPath = SlacUtil.path(homeDir, fname);
    loadCfg(properties, propPath, true);

    properties.put(PROP_HOME_DIR, homeDir);
  }
}
示例#4
0
 /**
  * Returns path to log directory.
  *
  * @return directory where agent will write its logs.
  */
 public String getLogDir() {
   return SlacUtil.path(homeDir, "log");
 }