Skip to content

Instantly share code, notes, and snippets.

@ralph-tice
Created April 17, 2013 15:05
Show Gist options
  • Save ralph-tice/5405052 to your computer and use it in GitHub Desktop.
Save ralph-tice/5405052 to your computer and use it in GitHub Desktop.
2.9 vs 2.10.1
/**
* allow for hierarchical properties
* {{{
* if we have prefix = "edda.collection",
* propName = "enabled",
* nameContext = "test.us-east-1.aws.addresses"
* then it will look for (in this order):
* edda.collection.test.us-east-1.aws.addresses.enabled
* edda.collection.test.us-east-1.aws.enabled
* edda.collection.us-east-1.aws.addresses.enabled
* edda.collection.test.us-east-1.enabled
* edda.collection.us-east-1.aws.enabled
* edda.collection.aws.addresses.enabled
* edda.collection.test.enabled
* edda.collection.us-east-1.enabled
* edda.collection.aws.enabled
* edda.collection.addresses.enabled
* edda.collection.enabled
* else return default
* }}}
* @param props group of available properties
* @param prefix root prefix, generally "edda.something"
* @param propName property name (ie "enabled")
* @param nameContext set property names to search though
* @param default the default value to return if no matching properties are found
* @return the best matching property value
*/
def getProperty(prefix: String, propName: String, nameContext: String, default: String): DynamicStringProperty = {
val parts = nameContext.split('.')
Range(1, parts.size + 1).reverse.map(
ix => parts.sliding(ix).map(prefix + "." + _.mkString(".") + "." + propName)).flatten collectFirst {
case prop: String if Option(DynamicProperty.getInstance(prop).getString()).isDefined => DynamicPropertyFactory.getInstance().getStringProperty(prop, default)
} match {
case Some(v) => v
case None => DynamicPropertyFactory.getInstance().getStringProperty(prefix + "." + propName, default)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment