@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { if (value instanceof String) { try { return Integer.parseInt((String) value); } catch (NumberFormatException e) { throw cannotConvert(loc, propName, value, "integer"); } } else if (value instanceof Integer) { return value; } else { throw cannotConvert(loc, propName, value, "integer"); } }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { if ("off".equals(value)) { return Integer.MAX_VALUE; } else if (value instanceof String) { try { return Integer.parseInt((String) value); } catch (NumberFormatException e) { throw cannotConvert(loc, propName, value, "integer"); } } else if (value instanceof Integer) { Integer i = (Integer) value; if (i > 0) { return i; } } throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Must be an " + toString()); }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { if (value instanceof String) { try { return Double.parseDouble((String) value); } catch (NumberFormatException e) { throw cannotConvert(loc, propName, value, "number"); } } else if (value instanceof Double) { return value; } else { throw cannotConvert(loc, propName, value, "number"); } }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { if (value instanceof List) { List<?> l = (List<?>) value; boolean allStrings = true; for (Object o : l) { if (!(o instanceof String)) { allStrings = false; } } if (allStrings) { return value; } } else if (value instanceof String) { // also allow comma separated strings in a string return Arrays.asList(((String) value).split(",\\s*")); } throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Must be a " + toString()); }
/** * Loads, parses, binds, and validates a configuration object. * * @param provider the provider to to use for reading configuration files * @param path the path of the configuration file * @return a validated configuration object * @throws IOException if there is an error reading the file * @throws ConfigurationException if there is an error parsing or validating the file */ public T build(ConfigurationSourceProvider provider, String path) throws IOException, ConfigurationException { try (InputStream input = provider.open(checkNotNull(path))) { final JsonNode node = mapper.readTree(hoconFactory.createParser(input)); return build(node, path); } catch (ConfigException e) { ConfigurationParsingException.Builder builder = ConfigurationParsingException .builder("Malformed HOCON") .setCause(e) .setDetail(e.getMessage()); ConfigOrigin origin = e.origin(); if (origin != null) { builder.setLocation(origin.lineNumber(), 0); } throw builder.build(path); } }
/** * Sets the String value for current configuration's variable to config. * * @param key * The name of settings section. * @param subKey * The name of needed variable. * @param value * The value to set. */ private static void setStringToConfig(String key, String subKey, String newValue, boolean hideValue) { String fullPath = MAINKEY + "." + key + "." + subKey; if (getString(key, subKey).equals(newValue)) { String messageToLog = hideValue ? String.format("Setting secret configuration value. Key: %s.%s", key, subKey) : String.format("Configuration value unchanged. Key: %s.%s Value: %s", key, subKey, newValue); logger.info(messageToLog); return; } String processedNewValue = ConfigUtil.quoteString(newValue); // Lets make new Config object with just a single variable. // Also lets preserve the comments from the original Config. ConfigOrigin or = conf.getValue(fullPath).origin(); StringBuilder toParse = new StringBuilder(); for (String comment : or.comments()) { toParse.append("#").append(comment).append("\n"); } toParse.append(fullPath).append("=").append(processedNewValue); Config newLittleConfig = ConfigFactory.parseString(toParse.toString()); // Now we have our little Config with the single variable and old comments. // Let's merge it with the old Config. conf = newLittleConfig.withFallback(conf); if (!hideValue) { logger.info(String.format("Configuration update in RAM. Key: %s.%s Value: %s", key, subKey, newValue)); } needsSave = true; }
@Override public Object checkValue(String propName, String value, ConfigOrigin loc) { if (!choices.contains(value)) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Valid values are: " + pp(choices)); } return value; }
@Override public Object checkValue(String propName, String value, ConfigOrigin loc) { try { URI u = new URI(value); return value; } catch (Exception e) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'"); } }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { if (value instanceof String) { return Boolean.valueOf((String) value); } else if (value instanceof Boolean) { return value; } else { throw cannotConvert(loc, propName, value, "boolean"); } }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { Integer ivalue = (Integer) super.checkValue(propName, value, loc); if (ivalue <= 0) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Must be a " + toString()); } return ivalue; }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { Integer ivalue = (Integer) super.checkValue(propName, value, loc); if (ivalue < 0) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Must be a " + toString()); } return ivalue; }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { Double dvalue = (Double) super.checkValue(propName, value, loc); if (dvalue < 0) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Must be a " + toString()); } return dvalue; }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { Double dvalue = (Double) super.checkValue(propName, value, loc); if (dvalue < l || dvalue > h) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Must be a " + toString()); } return dvalue; }
@Override public Object checkValue(String propName, String value, ConfigOrigin loc) { try { WallTime.timeToSeconds(value); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid time value '" + value + "' for property '" + propName + "'. Mist be a " + toString()); } return value; }
@Override public Object checkValue(String propName, String value, ConfigOrigin loc) { String[] els = value.split(",\\s*"); if (els.length == 2) { try { Integer.parseInt(els[0]); Integer.parseInt(els[1]); return value; } catch (NumberFormatException e) { } } throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. Must be a " + toString()); }
@Override public Object checkValue(String propName, String value, ConfigOrigin loc) { File f = new File(value); if (!f.exists()) { throw new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tInvalid value '" + value + "' for property '" + propName + "'. File does not exist."); } return value; }
@Override public JsonLocation getTokenLocation() { ConfigValue current = currentConfig(); if (current == null) { return JsonLocation.NA; } ConfigOrigin nodeOrigin = current.origin(); return new JsonLocation(current, -1, nodeOrigin.lineNumber(), -1); }
@Override public ConfigOrigin origin() { return c.origin(); }
@Override public ConfigOrigin origin() { return config.origin(); }
public ValueLocationPair(Object value, ConfigOrigin loc) { this.value = value; this.loc = loc; }
public static String location(ConfigOrigin loc) { return loc.filename() + ":" + loc.lineNumber(); }
@SuppressWarnings("unchecked") public Object check(String propName, Object value, ConfigOrigin loc) { return checkValue(propName, (T) value, loc); }
protected RuntimeException cannotConvert(ConfigOrigin loc, String propName, Object value, String toWhat) { return new IllegalArgumentException(SwiftConfig.location(loc) + ":\n\tCannot convert value '" + value + "' for property '" + propName + "' to " + toWhat); }
@Override public Object checkValue(String propName, String value, ConfigOrigin loc) { // all values accepted return value; }
@Override public Object checkValue(String propName, Object value, ConfigOrigin loc) { return value; }
public SwiftConfigSchema() { validNames = new HashSet<String>(); schema = ConfigFactory.parseResources("swift.conf.schema"); schema = schema.resolve(); if (schema.isEmpty()) { throw new RuntimeException("Could not find swift.conf.schema"); } info = new ConfigTree<Info>(); for (Map.Entry<String, ConfigValue> e : schema.entrySet()) { String k = e.getKey(); String nk = k.replace("\"*\"", "*"); String type = null; Object defaultValue = null; String doc = null; ConfigOrigin loc = null; if (k.endsWith(".\"_type\"")) { type = schema.getString(k); nk = nk.substring(0, nk.lastIndexOf('.')); loc = e.getValue().origin(); } else if (k.indexOf(".\"_") == -1){ type = schema.getString(k); loc = e.getValue().origin(); } else if (k.endsWith(".\"_default\"")){ defaultValue = e.getValue().unwrapped(); nk = nk.substring(0, nk.lastIndexOf('.')); } else if (k.endsWith(".\"_doc\"")){ doc = stripDoc((String) e.getValue().unwrapped()); nk = nk.substring(0, nk.lastIndexOf('.')); } else if (k.indexOf(".\"_") != -1) { continue; } Info i = info.get(nk); if (i == null) { i = new Info(); info.put(nk, i); setValid(nk); } if (type != null) { if (type.startsWith("?")) { i.optional = true; type = type.substring(1); } i.type = getTypeInstance(type, e.getValue()); i.typeSpec = type; } if (defaultValue != null) { i.value = defaultValue; } if (doc != null) { i.doc = doc; } if (loc != null) { i.loc = loc; } } }
private String loc(ConfigOrigin o) { return o.filename() + ":" + o.lineNumber(); }
public SwiftConfigException(ConfigOrigin loc, String message) { super((loc.filename() == null ? loc.description() : loc.filename() + ":" + loc.lineNumber()) + " " + message); }
protected static String location(Config config, String key) { ConfigOrigin origin = config.getValue(key).origin(); return origin.description() + ":" + origin.lineNumber(); }
public static JsonLocation fromConfigValue(ConfigValue configValue) { ConfigOrigin configOrigin = configValue.origin(); return new JsonLocation(configValue, -1, configOrigin.lineNumber(), -1); }
public HoconJsonLocation(final ConfigOrigin origin) { super(origin.description(), -1L, origin.lineNumber(), -1); this.origin = origin; }
@Override public ConfigOrigin withComments(List<String> comments) { return origin.withComments(comments); }
@Override public ConfigOrigin withLineNumber(int i) { return origin.withLineNumber(i); }
public abstract Object checkValue(String propName, T value, ConfigOrigin loc);