Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Main entry point of Config2 library
- */
- object Config {
- /**
- * Define location to read configuration files from. <p>
- * Supported protocols are classpath:, file: and no protocol (which mean file:)
- */
- def from(url: String): ConfigBuilder = ConfigImpl.builder(url)
- }
- trait ConfigBuilder {
- /**
- * Override previous location(s) with this one. Loaded properties will also be merged, taking this location with higher precedence.
- */
- def overrideWith(url: String): ConfigBuilder
- /**
- * Override previous location(s) with this set of properties. <p>
- * Typical values here are System.getProperties(), System.env() or Properties obtained from other sources
- */
- def overrideWith(props: java.util.Map[_, _]): ConfigBuilder
- /**
- * Override previous location(s) with this command-line arguments. <p>
- * Key names are prefixed with dashes, keys and values are separated by space.
- * I.e. "-app.port 8888" will set "app.port" property to "8888"
- */
- def overrideWith(cmdlineArgs: Array[String]): ConfigBuilder
- /**
- * Enable environment support. <p>
- * Environments are used to override configuration with environment specific values.
- * Default implementation looks for files in $(url)/$(envName)/ subfolder, where
- * envName is hostname or can be specified via config.environment system property.
- */
- def env(resolver: EnvironmentResolver = EnvironmentResolver()): ConfigBuilder
- /**
- * Declare configuration parameter. <p>
- * Used to validate collected configuration and print app usage information.
- */
- def params[Type](name: String, description: String)(implicit mf: Manifest[Type]): ConfigBuilder
- /**
- * Configure output and verbosity of Config2 logging. <p>
- * By default, information on all loaded files is printed to stdout. Verbose mode also prints information
- * on tried, but not loaded files. <p>
- * Generally this method cannot use Slf4j because Config2 can be used to configure logging itself!
- */
- def log(logger: String => Unit = println, verbose: Boolean = false): ConfigBuilder
- /**
- * Create instance of Config
- */
- def build(): Config
- }
- /**
- * Configured Config (pun intended) instance. <p>
- * Can be used to load Properties and files, using paths and settings provided by ConfigBuilder
- */
- trait Config {
- /**
- * Read properties from standard Java Properties file. Multiple paths are tried as configured before.
- * Resulting Properties will be merged from all available sources
- * @file relative file name
- */
- def readProperties(file: String): ConfigPropeties
- /**
- * Read text file with specified encoding. Merge is not supported, so file with most precedence wins.
- * @file relative file name
- */
- def readText(file: String, encoding: Charset = StandardCharsets.UTF_8): String
- /**
- * Read text file with specified encoding. Merge is not supported, so file with most precedence wins.
- * Caller are responsible for closing the stream.
- * @file relative file name
- */
- def readFile(file: String): InputStream
- /**
- * Set of parameters declared in ConfigBuilder. Useful to log loaded parameters
- */
- val knownParams: Set[String]
- /**
- * Print usage information for declared parameters
- */
- def printUsage(w: PrintStream = System.out)
- }
- /**
- * Useful wrapper around java.util.Properties
- */
- trait ConfigPropeties {
- /**
- * Set of parameters declared in ConfigBuilder. Useful to log loaded parameters
- */
- val knownParams: Set[String]
- /**
- * Scala Map representation of Properties
- */
- val map: Map[String, String]
- /**
- * Copy of contents of this class. Modifications of resulting object will not be reflected in this class!
- */
- def props(): Properties
- /**
- * Typed getter for configuration values.
- * Supported types are String, Int, Long, Float, Double
- * This method throws runtime exceptions if value cannot be cast to required type
- */
- def get[Type](key: String)(implicit mf: Manifest[Type]): Option[Type]
- /**
- * Validate this Properties against declared parameters.
- * This method returns list of validation error messages.
- */
- def validate(): List[String]
- }
- object ConfigPropeties {
- implicit def toProperties(confProps: ConfigPropeties): Properties = confProps.props()
- }
- /**
- * Produce locations to look for environment-specific configuration files.
- * Returned list of urls should point to files in increasing precedence.
- * Generally this list starts with "url + file" entry.
- */
- trait EnvironmentResolver {
- def resolve(url: String, file: String): List[String]
- }
- /**
- * Useful EnvironmentResolver-s
- */
- object EnvironmentResolver {
- def apply(): EnvironmentResolver = {
- val name = System.getProperty("config.environment") match {
- case env if env != null && !env.trim().isEmpty() => env
- case _ => InetAddress.getLocalHost().getHostName()
- }
- apply(name)
- }
- def apply(name: String): EnvironmentResolver = {
- new EnvironmentResolverImpl(name)
- }
- def apply(cmdlineArgs: Array[String]): EnvironmentResolver = {
- cmdlineArgs.toList.dropWhile(_ != "-env") match {
- case "-env" :: env :: _ => apply(env)
- case _ => apply()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement