Esempio n. 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;
        }
      }
    }
  }
Esempio n. 2
0
  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;
    }
  }
Esempio n. 3
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);
  }
Esempio n. 4
0
  /**
   * 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;
  }
Esempio n. 5
0
  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);
    }
  }
Esempio n. 6
0
  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;
  }
Esempio n. 7
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;
    }
  }
Esempio n. 8
0
 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;
 }
Esempio n. 9
0
 public void setCfg(String key, Object val) {
   properties.setProperty(key, "" + val);
 }
Esempio n. 10
0
  public boolean hasCfg(String key) {
    String s = properties.getProperty(key);

    return s != null && s.trim().length() > 0;
  }
Esempio n. 11
0
  public String stringCfg(String key, String defval) {
    String s = properties.getProperty(key);

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