@Override public Config configVal(final String path) throws InvalidValuePathException, InvalidValueTypeException { return (Config) val(path); }
@SuppressWarnings("unchecked") private void putBranch( final String key, final Map<String, Object> schemaBranch, final Object val ) throws InvalidValuePathException, InvalidValueTypeException { final Config branch; if(val instanceof Config) { branch = new BasicConfig((Config) val); } else if(val instanceof Map) { branch = new BasicConfig(pathSep, schemaBranch, (Map<String, Object>) val); } else { throw new InvalidValuePathException(key); } node.put(key, branch); }
@Override public void val(final String path, final Object val) throws InvalidValuePathException, InvalidValueTypeException { final int sepPos = path.indexOf(pathSep); if(sepPos == 0 || sepPos == path.length() - 1) { throw new InvalidValuePathException(path); } if(sepPos > 0) { final String key = path.substring(0, sepPos); final String childPath = path.substring(sepPos + 1); val(key, childPath, val); } else { leafVal(path, val); } }
if(schemaVal instanceof Map) { try { putBranch(key, (Map<String, Object>) schemaVal, val); } catch(final InvalidValuePathException e) { throw new InvalidValuePathException(key + pathSep + e.path()); putLeaf(key, (Class) schemaVal, val); } catch(final InvalidValuePathException e) { throw new InvalidValuePathException(key + pathSep + e.path());
protected void leafVal(final String key, final Object val) throws InvalidValuePathException, InvalidValueTypeException { final Object schemaVal = schema.get(key); if(schemaVal instanceof Class) { putLeaf(key, (Class) schemaVal, val); } else { throw new InvalidValuePathException(key); } }
static Config initSlice(final Config config) { final Config configSlice = new BasicConfig(config); // disable the distributed mode on the slave nodes configSlice.val("load-step-node-addrs", Collections.EMPTY_LIST); return configSlice; }
@Override public String stringVal(final String path) throws InvalidValuePathException, NoSuchElementException { return (String) val(path); }
protected LoadStepBase( final Config config, final List<Extension> extensions, final List<Config> ctxConfigs, final MetricsManager metricsMgr) { this.config = new BasicConfig(config); this.extensions = extensions; this.ctxConfigs = ctxConfigs; this.metricsMgr = metricsMgr; Loggers.CONFIG.info(ConfigUtil.toString(config)); }
@Override @SuppressWarnings("unchecked") public <E> List<E> listVal(final String path) throws InvalidValuePathException, NoSuchElementException { final Object v = val(path); try { return (List<E>) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, List.class, v.getClass()); } }
static Config merge(final String pathSep, final List<Config> configs) { final Map<String, Object> schema = configs.stream() .map(Config::schema) .reduce(TreeUtil::addBranches) .orElseGet(Collections::emptyMap); final Map<String, Object> configTree = configs.stream() .map(Config::deepToMap) .reduce(TreeUtil::addBranches) .orElseGet(Collections::emptyMap); return new BasicConfig(pathSep, schema, configTree); }
@Override public char charVal(final String path) throws InvalidValueTypeException, NoSuchElementException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Character.TYPE, null); } try { return (char) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Character.TYPE, v.getClass()); } }
static Config resolveAndReduce( final String id, final ClassLoader clsLoader, final String pathSep, final Map<String, Object> schema ) throws Exception { final List<Config> resolvedConfigs = resolve(id, clsLoader, pathSep, schema); if(resolvedConfigs == null || resolvedConfigs.size() == 0) { return null; } final List<Map<String, Object>> configForest = resolvedConfigs .stream() .map(c -> c.mapVal(Config.ROOT_PATH)) .collect(Collectors.toList()); final Map<String, Object> configTree = reduceForest(configForest); return new BasicConfig(pathSep, schema, configTree); } }
@Override public float floatVal(final String path) throws InvalidValueTypeException, NoSuchElementException, NumberFormatException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Float.TYPE, null); } else if(v instanceof String) { return Float.parseFloat((String) v); } else { try { return (float) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Float.TYPE, v.getClass()); } } }
@Override public boolean boolVal(final String path) throws InvalidValuePathException, InvalidValueTypeException, NoSuchElementException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Boolean.TYPE, null); } else if(v instanceof String) { return Boolean.parseBoolean((String) v); } else { try { return (boolean) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Boolean.TYPE, v.getClass()); } } }
@Override public int intVal(final String path) throws InvalidValuePathException, NoSuchElementException, NumberFormatException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Integer.TYPE, null); } else if(v instanceof String) { return Integer.parseInt((String) v); } else if(v instanceof Short) { return (short) v; } else { try { return (int) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Integer.TYPE, v.getClass()); } } }
@Override public byte byteVal(final String path) throws InvalidValueTypeException, NoSuchElementException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Byte.TYPE, null); } else if(v instanceof String) { return Byte.parseByte((String) v); } else { try { return (byte) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Byte.TYPE, v.getClass()); } } }
@Override public short shortVal(final String path) throws InvalidValueTypeException, NoSuchElementException, NumberFormatException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Short.TYPE, null); } else if(v instanceof String) { return Short.parseShort((String) v); } else { try { return (short) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Short.TYPE, v.getClass()); } } }
@Override @SuppressWarnings("unchecked") public <V> Map<String, V> mapVal(final String path) throws InvalidValuePathException, NoSuchElementException { final Object v = val(path); if(v instanceof Config) { return ((Config) v).mapVal(ROOT_PATH); } else { try { return (Map<String, V>) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Map.class, v.getClass()); } } }
@Override public double doubleVal(final String path) throws InvalidValuePathException, NoSuchElementException, NumberFormatException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Double.TYPE, null); } else if(v instanceof String) { return Double.parseDouble((String) v); } else if(v instanceof Float) { return (float) v; } else { try { return (double) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Double.TYPE, v.getClass()); } } }
@Override public long longVal(final String path) throws InvalidValuePathException, NoSuchElementException, NumberFormatException { final Object v = val(path); if(v == null) { throw new InvalidValueTypeException(path, Long.TYPE, null); } else if(v instanceof String) { return Long.parseLong((String) v); } else if(v instanceof Short) { return (short) v; } else if(v instanceof Integer) { return (int) v; } else { try { return (long) v; } catch(final ClassCastException e) { throw new InvalidValueTypeException(path, Long.TYPE, v.getClass()); } } }