Last active
March 2, 2017 15:13
-
-
Save dwalend/5a193daa24af8dbfbdc5 to your computer and use it in GitHub Desktop.
Revisions
-
dwalend revised this gist
Jun 22, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ * * @author dwalend */ object ExampleConfigSource { //load from application.conf and the usual TypeSafe Config chain val atomicConfig = new AtomicConfigSource(ConfigFactory.load()) -
dwalend revised this gist
Jun 16, 2015 . 2 changed files with 9 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,11 +38,13 @@ class AtomicConfigSource(baseConfig:Config) { tryT match { case Success(t) => { if(ok) t else throw new IllegalStateException( s"Expected config from ${blockConfig.origin()} to be from ${atomicConfigRef.get().origin()} instead.") } case Failure(x) => { if(ok) throw x else throw new IllegalStateException( s"Throwable in block and expected config from ${blockConfig.origin()} to be from ${atomicConfigRef.get().origin()} instead.",x) } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,10 +3,12 @@ * * @author dwalend */ object ExampeConfigSource { //load from application.conf and the usual TypeSafe Config chain val atomicConfig = new AtomicConfigSource(ConfigFactory.load()) def config:Config = atomicConfig.config def configForBlock[T](key:String,value:AnyRef,origin:String)(block: => T):T = atomicConfig.configForBlock(key,value,origin)(block) } -
dwalend renamed this gist
Jun 15, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dwalend created this gist
Jun 15, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ /** * A little object to let you reach your config from anywhere. * * @author dwalend */ object AtomicConfigSourceExample { val atomicConfig = new AtomicConfigSource(ConfigFactory.load()) //load from application.conf and the usual TypeSafe Config chain def config:Config = atomicConfig.config def configForBlock[T](key:String,value:AnyRef,origin:String)(block: => T):T = atomicConfig.configForBlock(key,value,origin)(block) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ import java.util.concurrent.atomic.AtomicReference import scala.util.{Failure, Success, Try} import com.typesafe.config.{Config, ConfigFactory} /** Use to tweak a Config without clearing and reloading a new config (for testing). @author dwalend */ class AtomicConfigSource(baseConfig:Config) { val atomicConfigRef = new AtomicReference[Config](ConfigFactory.empty()) /** * Get the Atomic Config. Be sure to use defs for all config values that might be changed. */ def config:Config = atomicConfigRef.get().withFallback(baseConfig) /** * Use the config in a block of code. */ def configForBlock[T](key:String,value:AnyRef,origin:String)(block: => T):T = { val configPairs = Map(key -> value) configForBlock(configPairs,origin)(block) } /** * Use the config in a block of code. */ def configForBlock[T](configPairs:Map[String, _ <: AnyRef],origin:String)(block: => T):T = { import scala.collection.JavaConverters.mapAsJavaMapConverter val configPairsJava:java.util.Map[String, _ <: AnyRef] = configPairs.asJava val blockConfig:Config = ConfigFactory.parseMap(configPairsJava,origin) val originalConfig:Config = atomicConfigRef.getAndSet(blockConfig) val tryT:Try[T] = Try(block) val ok = atomicConfigRef.compareAndSet(blockConfig,originalConfig) tryT match { case Success(t) => { if(ok) t else throw new IllegalStateException(s"Expected config from ${blockConfig.origin()} to be from ${atomicConfigRef.get().origin()} instead.") } case Failure(x) => { if(ok) throw x else throw new IllegalStateException(s"Throwable in block and expected config from ${blockConfig.origin()} to be from ${atomicConfigRef.get().origin()} instead.",x) } } } }