protected void putInto(Config config, String key, Object value) { String[] keys = key.split("\\."); Config subConfig = config; int last = keys.length - 1; int i = 0; try { for (; i < last; i++) { subConfig = subConfig.config(keys[i]); } subConfig.put(keys[last], value); } catch (ClassCastException e) { String k = ""; for (int j = 0; j < i; j++) { k += keys[j] + "."; } k += keys[i]; System.out.println("WARNING! cannot overwrite config value " + k + "=" + subConfig.get(keys[i]) + " with new config " + key + "=" + value); } }
public <T> T bind(Class<T> clazz) { return bind(creteInstance(clazz)); }
protected void loadDefaultDevelopmentConfiguration(Config config) { Config server = config.config("server"); server.put("debugOutput", true); Config resources = server.config("resources"); Config resourcesCache = resources.config("cache"); resourcesCache.put("enabled", false); Config renderer = server.config("renderer"); renderer.put("viewCacheTtl", 1L); renderer.put("templateCacheTtl", 1L); }
@Override public Object get(Object key) { Object value = superGet(key); if (value != null) return value; if (parent() != null && (value = root().superGet(key)) != null) { if (value instanceof Config) { return createProxyConfig(this, (String) key, this, (Config) value); } else { return value; } } put((String) key, value = createConfig(this, (String) key)); return value; }
@Test public void test_5() { String s = "a { b = 'value'; c = 'value 2' }"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(1, config.size()); Assert.assertEquals("value", ((Map) config.get("a")).get("b")); Assert.assertEquals("value 2", ((Map) config.get("a")).get("c")); }
@Test public void test_closures() { String s = "a = 1\n" + "s = \"|${{->a}}|\"\n" + "c = {a}\n" + "a=2"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(Integer.valueOf(2), config.get("a", 0)); Assert.assertEquals("|2|", config.get("s").toString()); }
@Test public void test_merge_3() { Config configA = new Config(); Config configB = new Config(); EvalTools.prepare("a = 1").get(configA); EvalTools.prepare("a { c = 2 }").get(configB); Assert.assertEquals(1, configA.size()); Assert.assertEquals(1, configA.get("a")); configA.merge(configB); Assert.assertEquals(1, configA.size()); Assert.assertEquals(1, ((Map) configA.get("a")).size()); Assert.assertEquals(2, ((Map) configA.get("a")).get("c")); }
final Object value = get(name); boolean isConfig = value instanceof Config; reflection.setBoolean(t, get(name, Boolean.FALSE)); break; case BYTE: break; case CHAR: reflection.setChar(t, get(name, (char) 0)); break; case DOUBLE: if (config.isEmpty()) { reflection.setObject(t, null); break; reflection.setObject(t, config.bind(fieldInfo.generic.clazz)); break; Object o = get(name); throw new IllegalStateException("Cannot bind '" + o + "' of class " + o.getClass() + " to " + fieldInfo.field);
protected void readProfiles(Config config) { if (config == null || config.isEmpty()) return; Config environments = (Config) config.remove("environments"); if (environments != null) { Config env = environments.config(environment.shortName); if (env != null) config.merge(env); env = environments.config(environment.name().toLowerCase()); if (env != null) config.merge(env); } Config profiles = (Config) config.remove("profiles"); if (profiles == null || profiles.isEmpty()) return; for (String profile : this.profiles) { Config subConfig = profiles.config(profile); readProfiles(subConfig); config.merge(subConfig); } if (this.config != config) this.config.merge(config); }
public Config config(String key) { return (Config) get(key); }
@Override public <T> Dependency<T> resolve(ConfigValue annotation, Class<T> clazz) { Config config = Holders.getConfig(); String[] path = annotation.value().split("\\."); for (int i = 0; i < path.length - 1; i++) { config = config.config(path[i]); } return new SingletonDependency<>((T) config.get(path[path.length - 1])); } });
@Test public void test_profiles_5() throws IOException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, InterruptedException { server.setEnvironment(Environment.TEST); server.start(); Assert.assertEquals("bar", server.getConfig().config("foo").get("v")); Assert.assertTrue(server.getConfig().get("bar") instanceof Config); Assert.assertTrue(((Config) server.getConfig().get("bar")).isEmpty()); }
@Override public Object put(String key, Object value) { return super.put(key, value); }
public void merge(Map<String, Object> scr) { merge(this, scr); }
protected void loadBasicAuthConfiguration(ServerConfiguration.BasicAuth basicAuth) { if (basicAuth == null) return; String username = basicAuth.username; String password = basicAuth.password; if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { AuthFilter auth = new BasicAuthFilter(); boolean tokenEnabled = basicAuth.token; Config tokenized = basicAuth.tokenized; if (tokenEnabled) { TokenFilter tokenFilter = new TokenFilter(auth); long ttl = basicAuth.tokenTTL; if (ttl > 0) tokenFilter.setTTL(ttl); DependencyFactory.get().register(TokenFilter.class, new SingletonDependency<>(tokenFilter)); auth = tokenFilter; for (String name : tokenized.keySet()) { urlMapping.append("/" + name + "/*", new FileTreeHandler<>(tokenized.get(name, ""), "/" + name, name) .setShowFolder(false)); } } auth.allow(username, password); filtersMapping.addBefore("/*", auth); } }
default void init() { getConfig().bind(this); }