From 4964fdcbf092cf18d6ed2a91581cf358fe13d86b Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Tue, 29 Nov 2011 09:09:50 -0500 Subject: [PATCH] In the examples, also show using the config for the app itself --- .../src/main/resources/complex1.conf | 6 +++ .../src/main/resources/complex2.conf | 3 +- .../src/main/scala/ComplexApp.scala | 37 ++++++++++++++----- .../src/main/resources/application.conf | 6 +++ .../simple-app/src/main/scala/SimpleApp.scala | 12 +++++- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/examples/complex-app/src/main/resources/complex1.conf b/examples/complex-app/src/main/resources/complex1.conf index 3dada786..19fb190c 100644 --- a/examples/complex-app/src/main/resources/complex1.conf +++ b/examples/complex-app/src/main/resources/complex1.conf @@ -1,2 +1,8 @@ +# these are our own config values defined by the app +complex-app { + something="This value comes from complex-app's complex1.conf" +} + +# Here we override some values used by a library simple-lib.foo="This value comes from complex-app's complex1.conf" simple-lib.whatever = "This value comes from complex-app's complex1.conf" \ No newline at end of file diff --git a/examples/complex-app/src/main/resources/complex2.conf b/examples/complex-app/src/main/resources/complex2.conf index e7e98d77..53758b13 100644 --- a/examples/complex-app/src/main/resources/complex2.conf +++ b/examples/complex-app/src/main/resources/complex2.conf @@ -1,8 +1,9 @@ complex-app { + something="This value comes from complex-app's complex2.conf" + # here we want a simple-lib-context unique to our app # which can be custom-configured. In code, we have to # pull out this subtree and pass it to simple-lib. - simple-lib-context = { simple-lib { foo="This value comes from complex-app's complex2.conf in its custom simple-lib-context" diff --git a/examples/complex-app/src/main/scala/ComplexApp.scala b/examples/complex-app/src/main/scala/ComplexApp.scala index b3bf01e2..507da8cb 100644 --- a/examples/complex-app/src/main/scala/ComplexApp.scala +++ b/examples/complex-app/src/main/scala/ComplexApp.scala @@ -2,20 +2,44 @@ import com.typesafe.config._ import simplelib._ object ComplexApp extends App { + // This app is "complex" because we load multiple separate app // configs into a single JVM and we have a separately-configurable // context for simple lib. + // using a custom Config with the simple-lib library + // (simple-lib is a library in this same examples/ directory) + def demoConfigInSimpleLib(config: Config) { + val context = new SimpleLibContext(config) + context.printSetting("simple-lib.foo") + context.printSetting("simple-lib.hello") + context.printSetting("simple-lib.whatever") + } + + // system property overrides work, but the properties must be set + // before the config lib is used (config lib will not notice changes + // once it loads the properties) System.setProperty("simple-lib.whatever", "This value comes from a system property") + /////////// + // "config1" is just an example of using a file other than application.conf val config1 = ConfigFactory.load("complex1") + // use the config ourselves + println("config1, complex-app.something=" + config1.getString("complex-app.something")) + + // use the config for a library demoConfigInSimpleLib(config1) + ////////// + // "config2" shows how to configure a library with a custom settings subtree val config2 = ConfigFactory.load("complex2"); + // use the config ourselves + println("config2, complex-app.something=" + config2.getString("complex-app.something")) + // pull out complex-app.simple-lib-context and move it to // the toplevel, creating a new config suitable for our SimpleLibContext. // The defaultOverrides() have to be put back on top of the stack so @@ -23,7 +47,7 @@ object ComplexApp extends App { // We fall back to config2 again to be sure we get simple-lib's // reference.conf plus any other settings we've set. You could // also just fall back to ConfigFactory.referenceConfig() if - // you don't want application.conf settings outside of + // you don't want complex2.conf settings outside of // complex-app.simple-lib-context to be used. val simpleLibConfig2 = ConfigFactory.defaultOverrides() .withFallback(config2.getConfig("complex-app.simple-lib-context")) @@ -31,7 +55,9 @@ object ComplexApp extends App { demoConfigInSimpleLib(simpleLibConfig2) - // Now let's illustrate that simple-lib will get upset if we pass it + ////////// + + // Here's an illustration that simple-lib will get upset if we pass it // a bad config. In this case, we'll fail to merge the reference // config in to complex-app.simple-lib-context, so simple-lib will // point out that some settings are missing. @@ -41,11 +67,4 @@ object ComplexApp extends App { case e: ConfigException.ValidationFailed => println("when we passed a bad config to simple-lib, it said: " + e.getMessage) } - - def demoConfigInSimpleLib(config: Config) { - val context = new SimpleLibContext(config) - context.printSetting("simple-lib.foo") - context.printSetting("simple-lib.hello") - context.printSetting("simple-lib.whatever") - } } diff --git a/examples/simple-app/src/main/resources/application.conf b/examples/simple-app/src/main/resources/application.conf index 9fa122a0..c8957d5b 100644 --- a/examples/simple-app/src/main/resources/application.conf +++ b/examples/simple-app/src/main/resources/application.conf @@ -1,2 +1,8 @@ +# these are our own config values defined by the app +simple-app { + answer=42 +} + +# Here we override some values used by a library simple-lib.foo="This value comes from simple-app's application.conf" simple-lib.whatever = "This value comes from simple-app's application.conf" diff --git a/examples/simple-app/src/main/scala/SimpleApp.scala b/examples/simple-app/src/main/scala/SimpleApp.scala index 0efda2eb..c9b27a72 100644 --- a/examples/simple-app/src/main/scala/SimpleApp.scala +++ b/examples/simple-app/src/main/scala/SimpleApp.scala @@ -2,11 +2,19 @@ import com.typesafe.config._ import simplelib._ object SimpleApp extends App { - // example of how system properties override + // example of how system properties override; note this + // must be set before the config lib is used System.setProperty("simple-lib.whatever", "This value comes from a system property") + // Load our own config values from the default location, application.conf + val conf = ConfigFactory.load() + println("The answer is: " + conf.getString("simple-app.answer")) + // In this simple app, we're allowing SimpleLibContext() to - // use the default config in application.conf + // use the default config in application.conf ; this is exactly + // the same as passing in ConfigFactory.load() here, so we could + // also write "new SimpleLibContext(conf)" and it would be the same. + // (simple-lib is a library in this same examples/ directory) val context = new SimpleLibContext() context.printSetting("simple-lib.foo") context.printSetting("simple-lib.hello")