Search in sources :

Example 1 with ConfigSyntax

use of com.typesafe.config.ConfigSyntax in project config by typesafehub.

the class SimpleIncluder method fromBasename.

// this function is a little tricky because there are three places we're
// trying to use it; for 'include "basename"' in a .conf file, for
// loading app.{conf,json,properties} from classpath, and for
// loading app.{conf,json,properties} from the filesystem.
static ConfigObject fromBasename(NameSource source, String name, ConfigParseOptions options) {
    ConfigObject obj;
    if (name.endsWith(".conf") || name.endsWith(".json") || name.endsWith(".properties")) {
        ConfigParseable p = source.nameToParseable(name, options);
        obj = p.parse(p.options().setAllowMissing(options.getAllowMissing()));
    } else {
        ConfigParseable confHandle = source.nameToParseable(name + ".conf", options);
        ConfigParseable jsonHandle = source.nameToParseable(name + ".json", options);
        ConfigParseable propsHandle = source.nameToParseable(name + ".properties", options);
        boolean gotSomething = false;
        List<ConfigException.IO> fails = new ArrayList<ConfigException.IO>();
        ConfigSyntax syntax = options.getSyntax();
        obj = SimpleConfigObject.empty(SimpleConfigOrigin.newSimple(name));
        if (syntax == null || syntax == ConfigSyntax.CONF) {
            try {
                obj = confHandle.parse(confHandle.options().setAllowMissing(false).setSyntax(ConfigSyntax.CONF));
                gotSomething = true;
            } catch (ConfigException.IO e) {
                fails.add(e);
            }
        }
        if (syntax == null || syntax == ConfigSyntax.JSON) {
            try {
                ConfigObject parsed = jsonHandle.parse(jsonHandle.options().setAllowMissing(false).setSyntax(ConfigSyntax.JSON));
                obj = obj.withFallback(parsed);
                gotSomething = true;
            } catch (ConfigException.IO e) {
                fails.add(e);
            }
        }
        if (syntax == null || syntax == ConfigSyntax.PROPERTIES) {
            try {
                ConfigObject parsed = propsHandle.parse(propsHandle.options().setAllowMissing(false).setSyntax(ConfigSyntax.PROPERTIES));
                obj = obj.withFallback(parsed);
                gotSomething = true;
            } catch (ConfigException.IO e) {
                fails.add(e);
            }
        }
        if (!options.getAllowMissing() && !gotSomething) {
            if (ConfigImpl.traceLoadsEnabled()) {
                // the individual exceptions should have been logged already
                // with tracing enabled
                ConfigImpl.trace("Did not find '" + name + "' with any extension (.conf, .json, .properties); " + "exceptions should have been logged above.");
            }
            if (fails.isEmpty()) {
                // this should not happen
                throw new ConfigException.BugOrBroken("should not be reached: nothing found but no exceptions thrown");
            } else {
                StringBuilder sb = new StringBuilder();
                for (Throwable t : fails) {
                    sb.append(t.getMessage());
                    sb.append(", ");
                }
                sb.setLength(sb.length() - 2);
                throw new ConfigException.IO(SimpleConfigOrigin.newSimple(name), sb.toString(), fails.get(0));
            }
        } else if (!gotSomething) {
            if (ConfigImpl.traceLoadsEnabled()) {
                ConfigImpl.trace("Did not find '" + name + "' with any extension (.conf, .json, .properties); but '" + name + "' is allowed to be missing. Exceptions from load attempts should have been logged above.");
            }
        }
    }
    return obj;
}
Also used : ConfigParseable(com.typesafe.config.ConfigParseable) ConfigSyntax(com.typesafe.config.ConfigSyntax) ArrayList(java.util.ArrayList) ConfigException(com.typesafe.config.ConfigException) ConfigObject(com.typesafe.config.ConfigObject)

Aggregations

ConfigException (com.typesafe.config.ConfigException)1 ConfigObject (com.typesafe.config.ConfigObject)1 ConfigParseable (com.typesafe.config.ConfigParseable)1 ConfigSyntax (com.typesafe.config.ConfigSyntax)1 ArrayList (java.util.ArrayList)1