001package com.typesafe.config;
002
003import java.io.File;
004import java.net.MalformedURLException;
005import java.net.URL;
006
007/**
008 * Default config loading strategy. Able to load resource, file or URL.
009 * Behavior may be altered by defining one of VM properties
010 * {@code config.resource}, {@code config.file} or {@code config.url}
011 */
012public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy {
013    @Override
014    public Config parseApplicationConfig(ConfigParseOptions parseOptions) {
015        ClassLoader loader = parseOptions.getClassLoader();
016        if (loader == null)
017            throw new ConfigException.BugOrBroken(
018                    "ClassLoader should have been set here; bug in ConfigFactory. "
019                            + "(You can probably work around this bug by passing in a class loader or calling currentThread().setContextClassLoader() though.)");
020
021        int specified = 0;
022
023        // override application.conf with config.file, config.resource,
024        // config.url if requested.
025        String resource = System.getProperty("config.resource");
026        if (resource != null)
027            specified += 1;
028        String file = System.getProperty("config.file");
029        if (file != null)
030            specified += 1;
031        String url = System.getProperty("config.url");
032        if (url != null)
033            specified += 1;
034
035        if (specified == 0) {
036            return ConfigFactory.parseResourcesAnySyntax("application", parseOptions);
037        } else if (specified > 1) {
038            throw new ConfigException.Generic("You set more than one of config.file='" + file
039                                                      + "', config.url='" + url + "', config.resource='" + resource
040                                                      + "'; don't know which one to use!");
041        } else {
042            // the override file/url/resource MUST be present or it's an error
043            ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false);
044            if (resource != null) {
045                if (resource.startsWith("/"))
046                    resource = resource.substring(1);
047                // this deliberately does not parseResourcesAnySyntax; if
048                // people want that they can use an include statement.
049                return ConfigFactory.parseResources(loader, resource, overrideOptions);
050            } else if (file != null) {
051                return ConfigFactory.parseFile(new File(file), overrideOptions);
052            } else {
053                try {
054                    return ConfigFactory.parseURL(new URL(url), overrideOptions);
055                } catch (MalformedURLException e) {
056                    throw new ConfigException.Generic("Bad URL in config.url system property: '"
057                                                              + url + "': " + e.getMessage(), e);
058                }
059            }
060        }
061    }
062}