@Override public Object extractValue(Config config, String path) { return config.getAnyRef(path); } },
@Test public void simpleTest() { Config config = ConfigFactory.parseResources("ethereumj.conf"); System.out.println(config.root().render(ConfigRenderOptions.defaults().setComments(false))); for (Map.Entry<String, ConfigValue> entry : config.entrySet()) { // System.out.println("Name: " + entry.getKey()); // System.out.println(entry); } System.out.println("peer.listen.port: " + config.getInt("peer.listen.port")); System.out.println("peer.discovery.ip.list: " + config.getAnyRefList("peer.discovery.ip.list")); System.out.println("peer.discovery.ip.list: " + config.getAnyRefList("peer.active")); List<? extends ConfigObject> list = config.getObjectList("peer.active"); for (ConfigObject configObject : list) { if (configObject.get("url") != null) { System.out.println("URL: " + configObject.get("url")); } if (configObject.get("ip") != null) { System.out.println("IP: " + configObject); } } System.out.println("blocks.loader = " + config.hasPath("blocks.loader")); System.out.println("blocks.loader = " + config.getAnyRef("blocks.loader")); }
@Override public Object getAnyRef(String path) { return c.getAnyRef(path); }
@Override public Object resolve(final Object context, final String name) { if (context instanceof Config) { Config config = (Config) context; if (config.hasPath(name)) { return config.getAnyRef(name); } } return UNRESOLVED; }
@SuppressWarnings("unchecked") public <T> T get(final String name) { requireNonNull(name, "Option's name is required."); if (options.hasPath(name)) { return (T) options.getAnyRef(name); } return null; }
public <T> T getProperty(String propName, T defaultValue) { if (!config.hasPath(propName)) return defaultValue; String string = config.getString(propName); if (string.trim().isEmpty()) return defaultValue; return (T) config.getAnyRef(propName); }
@SuppressWarnings("rawtypes") private void list(final Config conf, final String name, final Throwing.Consumer<Object> callback) { if (conf.hasPath(name)) { Object value = conf.getAnyRef(name); List values = value instanceof List ? (List) value : ImmutableList.of(value); for (Object it : values) { callback.accept(it); } } }
private List<String> strList(final String p) { Object list = mail.getAnyRef(p); if (list instanceof String) { return ImmutableList.of(list.toString()); } return mail.getStringList(p); }
private static Predicate<String> predicate(final Config fileset, final String... extension) { String path = "assets" + extension[0]; Set<String> extensions = new HashSet<>(Arrays.asList(extension)); if (fileset.hasPath(path)) { extensions.addAll(strlist(fileset.getAnyRef(path))); } return file -> { for (String ext : extensions) { if (file.endsWith(ext)) { return true; } } return false; }; }
@SuppressWarnings("unchecked") private static Iterable<Command> commands(final Config config) { Object value = config.getAnyRef("flyway.run"); List<String> commands = new ArrayList<>(); if (value instanceof List) { commands.addAll((List<? extends String>) value); } else { Stream.of(value.toString().split(",")) .map(String::trim) .forEach(commands::add); } return commands.stream() .map(command -> Command.valueOf(command.toLowerCase())) .collect(Collectors.toList()); } }
/** * Scan the packages defined by <code>hibernate.packagesToScan</code> property or the application * package (that's the package where you application was defined) and discover persistent classes * (annotated with Entity). * * @return This module. */ public Hbm scan() { sources.add((m, conf) -> Stream.of(conf.getAnyRef("hibernate.packagesToScan")) .flatMap(it -> { if (it instanceof List) { return ((List) it).stream(); } return Stream.of(it); }) .forEach(it -> m.addPackage(it.toString())) ); return this; }
@SuppressWarnings("unchecked") @Override public void configure(final Env env, final Config conf, final Binder binder) { Config $memcached = conf.getConfig("memcached") .withFallback(conf.getConfig("memcached")); ConnectionFactoryBuilder builder = newConnectionFactoryBuilder($memcached); if (configurer != null) { configurer.accept(builder, conf); } List<String> servers = new ArrayList<>(); Object $servers = conf.getAnyRef("memcached.server"); if ($servers instanceof List) { servers.addAll((Collection<? extends String>) $servers); } else { servers.add($servers.toString()); } MemcachedClientProvider provider = new MemcachedClientProvider( builder, AddrUtil.getAddresses(servers), $memcached.getDuration("shutdownTimeout", TimeUnit.MILLISECONDS)); env.onStop(provider::destroy); binder .bind(MemcachedClient.class) .toProvider(provider) .asEagerSingleton(); }
private void tryOption(final Object source, final Config config, final Method option) { Try.run(() -> { String optionName = option.getName().replace("set", ""); Object optionValue = config.getAnyRef(optionName); Class<?> optionType = Primitives.wrap(option.getParameterTypes()[0]); if (Number.class.isAssignableFrom(optionType) && optionValue instanceof String) { // either a byte or time unit try { optionValue = config.getBytes(optionName); } catch (ConfigException.BadValue ex) { optionValue = config.getDuration(optionName, TimeUnit.MILLISECONDS); } if (optionType == Integer.class) { // to int optionValue = ((Number) optionValue).intValue(); } } log.debug("{}.{}({})", source.getClass().getSimpleName(), option.getName(), optionValue); option.invoke(source, optionValue); }).unwrap(InvocationTargetException.class) .throwException(); }
/** * Creates {@link Cors} options from {@link Config}: * * <pre> * origin: "*" * credentials: true * allowedMethods: [GET, POST] * allowedHeaders: [X-Requested-With, Content-Type, Accept, Origin] * exposedHeaders: [] * </pre> * * @param config Config to use. */ @Inject public Cors(@Named("cors") final Config config) { requireNonNull(config, "Config is required."); this.enabled = config.hasPath("enabled") ? config.getBoolean("enabled") : true; withOrigin(list(config.getAnyRef("origin"))); this.credentials = config.getBoolean("credentials"); withMethods(list(config.getAnyRef("allowedMethods"))); withHeaders(list(config.getAnyRef("allowedHeaders"))); withMaxAge((int) config.getDuration("maxAge", TimeUnit.SECONDS)); withExposedHeaders(config.hasPath("exposedHeaders") ? list(config.getAnyRef("exposedHeaders")) : Collections.emptyList()); }
private Configuration lessConf(final Config conf) { Configuration configuration = new Configuration(); configuration.setCompressing(get("compressing")); SourceMapConfiguration sourceMap = configuration.getSourceMapConfiguration(); Map<String, Object> map = get("sourceMap"); if (map == null) { sourceMap.setLinkSourceMap(false); } else { sourceMap.setEncodingCharset( map.getOrDefault("encodingCharset", conf.getAnyRef("application.charset")).toString()); sourceMap.setIncludeSourcesContent((Boolean) map.getOrDefault("includeSourcesContent", true)); sourceMap.setInline((Boolean) map.getOrDefault("inline", true)); sourceMap.setRelativizePaths((Boolean) map.getOrDefault("relativizePaths", true)); sourceMap.setLinkSourceMap((Boolean) map.getOrDefault("linkSourceMap", true)); } return configuration; }
private Config dbConfig(final String key, final Config source) { Object db = source.getAnyRef(key); if (db instanceof String) { // embedded db? return Try.apply(() -> source.getConfig("databases." + db)) .map(it -> { // Rewrite embedded db it = it.getConfig("dataSource"); Config dbtree = it.withValue("url", ConfigValueFactory.fromAnyRef( it.getString("url").replace("{mem.seed}", System.currentTimeMillis() + ""))); // write embedded with current key return ConfigFactory.empty() .withValue(key, dbtree.root()) .withFallback(source); }).orElseGet(() -> { // assume it is a just the url return ConfigFactory.empty() .withValue(key + ".url", ConfigValueFactory.fromAnyRef(db.toString())) .withFallback(source); }); } else { return source; } }
return new MemorySize(size.toBytes()); } else if (parameterClass == Object.class) { return config.getAnyRef(configPropName); } else if (parameterClass == List.class || parameterClass == Collection.class) { return getListValue(beanClass, parameterType, parameterClass, config, configPath, configPropName);
.orElse($session.getBoolean("cookie.httpOnly"))); Object maxAge = $session.getAnyRef("cookie.maxAge"); if (maxAge instanceof String) { maxAge = $session.getDuration("cookie.maxAge", TimeUnit.SECONDS);
return config.getMemorySize(configPropName); } else if (parameterClass == Object.class) { return config.getAnyRef(configPropName); } else if (parameterClass == List.class) { return getListValue(beanClass, parameterType, parameterClass, config, configPropName);
return CC.cfg.getAnyRef(args).toString();