public String getString(String key, String defaultValue){ Assert.notBlank(key, "Key is null or empty!"); String value = hashtable.get(key); return StringUtils.isBlank(value) ? defaultValue : value; }
public String getString(String key){ Assert.notBlank(key, "Key is null or empty!"); if (!hashtable.containsKey(key)) { thrownew ConfigurationKeyNotFoundException("Configuration key '" + key + "' not found!"); } return hashtable.get(key); }
publicintgetInt(String key, int defaultValue){ if (!hashtable.containsKey(key)) { return defaultValue; } String value = hashtable.get(key); try { return Integer.parseInt(value); } catch (NumberFormatException e) { if (defaultWhenParseFailed) { return defaultValue; } thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to int"); } }
publicintgetInt(String key){ if (!hashtable.containsKey(key)) { thrownew ConfigurationKeyNotFoundException("Configuration key '" + key + "' not found!"); } String value = hashtable.get(key); try { return Integer.parseInt(value); } catch (NumberFormatException e) { thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to int"); } }
publiclonggetLong(String key, long defaultValue){ if (!hashtable.containsKey(key)) { return defaultValue; } String value = hashtable.get(key); try { return Long.parseLong(value); } catch (NumberFormatException e) { if (defaultWhenParseFailed) { return defaultValue; } thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to long"); } }
publiclonggetLong(String key){ if (!hashtable.containsKey(key)) { thrownew ConfigurationKeyNotFoundException("Configuration key '" + key + "' not found!"); } String value = hashtable.get(key); try { return Long.parseLong(value); } catch (NumberFormatException e) { thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to long"); } }
publicdoublegetDouble(String key, double defaultValue){ if (!hashtable.containsKey(key)) { return defaultValue; } String value = hashtable.get(key); try { return Double.parseDouble(value); } catch (NumberFormatException e) { if (defaultWhenParseFailed) { return defaultValue; } thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to double"); } }
publicdoublegetDouble(String key){ if (!hashtable.containsKey(key)) { thrownew ConfigurationKeyNotFoundException("Configuration key '" + key + "' not found!"); } String value = hashtable.get(key); try { return Double.parseDouble(value); } catch (NumberFormatException e) { thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to double"); } }
publicbooleangetBoolean(String key, boolean defaultValue){ if (!hashtable.containsKey(key)) { return defaultValue; } String value = hashtable.get(key); if ("true".equalsIgnoreCase(value)) { returntrue; } if ("false".equalsIgnoreCase(value)) { returnfalse; } if (defaultWhenParseFailed) { return Boolean.parseBoolean(value); } thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to boolean"); }
publicbooleangetBoolean(String key){ if (!hashtable.containsKey(key)) { thrownew ConfigurationKeyNotFoundException("Configuration key '" + key + "' not found!"); } String value = hashtable.get(key); if ("true".equalsIgnoreCase(value)) { returntrue; } if ("false".equalsIgnoreCase(value)) { returnfalse; } thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to boolean"); }
public Date getDate(String key, Date defaultValue){ if (!hashtable.containsKey(key)) { return defaultValue; } String value = hashtable.get(key); try { returnnew SimpleDateFormat(dateFormat).parse(value); } catch (ParseException e) { if (defaultWhenParseFailed) { return defaultValue; } thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to date"); } }
public Date getDate(String key){ if (!hashtable.containsKey(key)) { thrownew ConfigurationKeyNotFoundException("Configuration key '" + key + "' not found!"); } String value = hashtable.get(key); try { returnnew SimpleDateFormat(dateFormat).parse(value); } catch (ParseException e) { thrownew ConfigurationValueParseException("'" + value + "' cannot be parsed to date"); } }
public Builder fromFile(String confFile){ return fromFile(new File(confFile)); }
public Builder fromFile(File confFile){ if (!confFile.exists()) { thrownew ConfigurationFileNotFoundException(); } if (!confFile.canRead()) { thrownew ConfigurationFileReadException("Read configuration file is not permitted!"); } InputStream in = null; try { in = new FileInputStream(confFile); Properties props = new Properties(); props.load(in); hashtable = pfu.rectifyProperties(props); LOGGER.debug("Load configuration from {} at {}", confFile.getAbsolutePath(), new Date()); } catch (IOException e) { thrownew ConfigurationFileReadException("Cannot load config file: " + confFile, e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { thrownew ConfigurationException("Cannot close input stream.", e); } } } returnthis; }
public Builder dateFormat(String dateFormat){ this.dateFormat = dateFormat; returnthis; }
public Builder defaultWhenParseFailed(boolean defaultWhenParseFailed){ this.defaultWhenParseFailed = defaultWhenParseFailed; returnthis; }
public Configuration build(){ if (hashtable.isEmpty()) { thrownew ConfigurationException("Configuration source not specified!"); } Configuration result = new Configuration(hashtable); result.setDateFormat(dateFormat); result.setDefaultWhenParseFailed(defaultWhenParseFailed); return result; } } }