Advertisement
Guest User

Untitled

a guest
May 11th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.26 KB | None | 0 0
  1. /**
  2.  * Main entry point of Config2 library
  3.  */
  4. object Config {
  5.   /**
  6.    * Define location to read configuration files from. <p>
  7.    * Supported protocols are classpath:, file: and no protocol (which mean file:)
  8.    */
  9.   def from(url: String): ConfigBuilder = ConfigImpl.builder(url)
  10. }
  11.  
  12. trait ConfigBuilder {
  13.   /**
  14.    * Override previous location(s) with this one. Loaded properties will also be merged, taking this location with higher precedence.
  15.    */
  16.   def overrideWith(url: String): ConfigBuilder
  17.   /**
  18.    * Override previous location(s) with this set of properties. <p>
  19.    * Typical values here are System.getProperties(), System.env() or Properties obtained from other sources  
  20.    */
  21.   def overrideWith(props: java.util.Map[_, _]): ConfigBuilder
  22.   /**
  23.    * Override previous location(s) with this command-line arguments. <p>
  24.    * Key names are prefixed with dashes, keys and values are separated by space.
  25.    * I.e. "-app.port 8888" will set "app.port" property to "8888"
  26.    */
  27.   def overrideWith(cmdlineArgs: Array[String]): ConfigBuilder
  28.  
  29.   /**
  30.    * Enable environment support. <p>
  31.    * Environments are used to override configuration with environment specific values.
  32.    * Default implementation looks for files in $(url)/$(envName)/ subfolder, where
  33.    * envName is hostname or can be specified via config.environment system property.
  34.    */
  35.   def env(resolver: EnvironmentResolver = EnvironmentResolver()): ConfigBuilder
  36.  
  37.   /**
  38.    * Declare configuration parameter. <p>
  39.    * Used to validate collected configuration and print app usage information.
  40.    */
  41.   def params[Type](name: String, description: String)(implicit mf: Manifest[Type]): ConfigBuilder
  42.  
  43.   /**
  44.    * Configure output and verbosity of Config2 logging. <p>
  45.    * By default, information on all loaded files is printed to stdout. Verbose mode also prints information
  46.    * on tried, but not loaded files. <p>
  47.    * Generally this method cannot use Slf4j because Config2 can be used to configure logging itself!
  48.    */
  49.   def log(logger: String => Unit = println, verbose: Boolean = false): ConfigBuilder
  50.  
  51.   /**
  52.    * Create instance of Config
  53.    */
  54.   def build(): Config
  55. }
  56.  
  57. /**
  58.  * Configured Config (pun intended) instance. <p>
  59.  * Can be used to load Properties and files, using paths and settings provided by ConfigBuilder
  60.  */
  61. trait Config {
  62.   /**
  63.    * Read properties from standard Java Properties file. Multiple paths are tried as configured before.
  64.    * Resulting Properties will be merged from all available sources
  65.    * @file relative file name
  66.    */
  67.   def readProperties(file: String): ConfigPropeties
  68.  
  69.   /**
  70.    * Read text file with specified encoding. Merge is not supported, so file with most precedence wins.
  71.    * @file relative file name
  72.    */
  73.   def readText(file: String, encoding: Charset = StandardCharsets.UTF_8): String
  74.  
  75.   /**
  76.    * Read text file with specified encoding. Merge is not supported, so file with most precedence wins.
  77.    * Caller are responsible for closing the stream.
  78.    * @file relative file name
  79.    */
  80.   def readFile(file: String): InputStream
  81.  
  82.   /**
  83.    * Set of parameters declared in ConfigBuilder. Useful to log loaded parameters
  84.    */
  85.   val knownParams: Set[String]
  86.  
  87.   /**
  88.    * Print usage information for declared parameters
  89.    */
  90.   def printUsage(w: PrintStream  = System.out)
  91. }
  92.  
  93. /**
  94.  * Useful wrapper around java.util.Properties
  95.  */
  96. trait ConfigPropeties {
  97.   /**
  98.    * Set of parameters declared in ConfigBuilder. Useful to log loaded parameters
  99.    */
  100.   val knownParams: Set[String]
  101.   /**
  102.    * Scala Map representation of Properties
  103.    */
  104.   val map: Map[String, String]
  105.  
  106.   /**
  107.    * Copy of contents of this class. Modifications of resulting object will not be reflected in this class!
  108.    */
  109.   def props(): Properties
  110.  
  111.   /**
  112.    * Typed getter for configuration values.
  113.    * Supported types are String, Int, Long, Float, Double
  114.    * This method throws runtime exceptions if value cannot be cast to required type
  115.    */
  116.   def get[Type](key: String)(implicit mf: Manifest[Type]): Option[Type]
  117.  
  118.   /**
  119.    * Validate this Properties against declared parameters.
  120.    * This method returns list of validation error messages.
  121.    */
  122.   def validate(): List[String]
  123. }
  124.  
  125. object ConfigPropeties {
  126.   implicit def toProperties(confProps: ConfigPropeties): Properties = confProps.props()
  127. }
  128.  
  129. /**
  130.  * Produce locations to look for environment-specific configuration files.
  131.  * Returned list of urls should point to files in increasing precedence.
  132.  * Generally this list starts with "url + file" entry.
  133.  */
  134. trait EnvironmentResolver {
  135.   def resolve(url: String, file: String): List[String]
  136. }
  137.  
  138. /**
  139.  * Useful EnvironmentResolver-s
  140.  */
  141. object EnvironmentResolver {
  142.   def apply(): EnvironmentResolver = {
  143.     val name = System.getProperty("config.environment") match {
  144.       case env if env != null && !env.trim().isEmpty() => env
  145.       case _ => InetAddress.getLocalHost().getHostName()
  146.     }
  147.    
  148.     apply(name)
  149.   }
  150.  
  151.   def apply(name: String): EnvironmentResolver = {
  152.     new EnvironmentResolverImpl(name)
  153.   }
  154.  
  155.   def apply(cmdlineArgs: Array[String]): EnvironmentResolver = {
  156.     cmdlineArgs.toList.dropWhile(_ != "-env") match {
  157.       case "-env" :: env :: _ => apply(env)
  158.       case _ => apply()
  159.     }
  160.   }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement