Skip to content
Permalink
Browse files

Refactoring to have the Container Context as root object registered b…

…y the Context Activator
  • Loading branch information
atooni committed Feb 14, 2020
1 parent c79856c commit 751970e7c93c10529f826533614a5542c015d801
Showing with 344 additions and 185 deletions.
  1. +2 −2 blended.akka/src/main/scala/blended/akka/OSGIActorConfig.scala
  2. +0 −32 blended.container.context.api/src/main/scala/blended/container/context/api/ConfigLocator.scala
  3. +65 −28 blended.container.context.api/src/main/scala/blended/container/context/api/ContainerContext.scala
  4. +0 −12 ...ntainer.context.api/src/main/scala/blended/container/context/api/ContainerIdentifierService.scala
  5. +0 −1 blended.container.context.impl/null/pollspec/test.xml
  6. +20 −0 ...xt.impl/src/main/scala/blended/container/context/impl/internal/AbstractContainerContextImpl.scala
  7. +73 −0 ...container.context.impl/src/main/scala/blended/container/context/impl/internal/ConfigLocator.scala
  8. +7 −8 ...ntext.impl/src/main/scala/blended/container/context/impl/internal/ContainerContextActivator.scala
  9. +39 −15 ...er.context.impl/src/main/scala/blended/container/context/impl/internal/ContainerContextImpl.scala
  10. +10 −18 ....impl/src/main/scala/blended/container/context/impl/internal/ContainerIdentifierServiceImpl.scala
  11. +20 −19 ...ntext.impl/src/main/scala/blended/container/context/impl/internal/ContainerPropertyResolver.scala
  12. +3 −0 blended.container.context.impl/src/test/resources/plain.conf
  13. +6 −0 blended.container.context.impl/src/test/resources/replace.conf
  14. +33 −0 ...ainer.context.impl/src/test/scala/blended/container/context/impl/internal/ConfigLocatorSpec.scala
  15. +8 −8 ...er.context.impl/src/test/scala/blended/container/context/impl/internal/PropertyResolverSpec.scala
  16. +2 −2 blended.domino/src/main/scala/blended/domino/TypesafeConfigWatching.scala
  17. +5 −5 blended.domino/src/main/scala/blended/domino/internal/TypesafeConfigCapsule.scala
  18. +2 −2 blended.mgmt.base/src/main/scala/blended/mgmt/base/internal/FrameworkService.scala
  19. +8 −8 blended.mgmt.mock/src/main/scala/blended/mgmt/mock/clients/MgmtMockClients.scala
  20. +11 −11 ...ity.scep.standalone/src/main/scala/blended/security/scep/standalone/ScepAppContainerContext.scala
  21. +14 −12 blended.testsupport.pojosr/src/main/scala/blended/testsupport/pojosr/MockContainerContext.scala
  22. +15 −2 project/BlendedContainerContextApi.scala
  23. +1 −0 project/ProjectSettings.scala
@@ -1,14 +1,14 @@
package blended.akka

import akka.actor.ActorSystem
import blended.container.context.api.ContainerIdentifierService
import blended.container.context.api.ContainerContext
import com.typesafe.config.Config
import org.osgi.framework.BundleContext

case class OSGIActorConfig(
bundleContext : BundleContext,
system : ActorSystem,
config : Config,
idSvc : ContainerIdentifierService
ctContext : ContainerContext
)

This file was deleted.

@@ -1,11 +1,15 @@
package blended.container.context.api

import java.io.File
import java.util.concurrent.atomic.AtomicLong

import blended.security.crypto.ContainerCryptoSupport
import blended.util.logging.Logger
import com.typesafe.config.{Config, ConfigFactory}

import scala.util.{Failure, Success, Try}

class PropertyResolverException(msg : String) extends Exception(msg)

object ContainerContext {

val transactionCounter = new AtomicLong(0)
@@ -21,32 +25,65 @@ object ContainerContext {

trait ContainerContext {

def getContainerDirectory() : String
def getContainerConfigDirectory() : String
def getContainerLogDirectory() : String

def getProfileDirectory() : String
def getProfileConfigDirectory() : String

def getContainerHostname() : String

def getContainerCryptoSupport() : ContainerCryptoSupport

// application.conf + application_overlay.conf
def getContainerConfig() : Config

def getConfig(id : String) : Config = {

ConfigLocator.config(
new File(getContainerConfigDirectory()), s"$id.conf", getContainerConfig()
) match {
case empty if empty.isEmpty =>
val cfg = getContainerConfig()
if (cfg.hasPath(id)) cfg.getConfig(id) else ConfigFactory.empty()

case cfg => cfg
}
}

private[this] val log : Logger = Logger[ContainerContext]
/**
* The home directory of the container, usually defined by the system property <code>blended.home</code>
*/
def containerDirectory : String


def containerConfigDirectory : String

/**
* The target directory for the container log files
*/
def containerLogDirectory : String

/**
* The base directory for the current container profile
*/
def profileDirectory() : String

/**
* The config directory for all profile specific configuration files. Usually this is
* <code>getProfileDirectory()/etc</code>. This is the main config directory.
*/
def profileConfigDirectory : String

/**
* The hostname of the current container as defined by the netowrk layer.
*/
def containerHostname : String

/**
* Provide access to encryption and decryption facilities, optionally secured with a secret file.
*/
def containerCryptoSupport : ContainerCryptoSupport

/**
* The application.conf, optionally modified with an overlay.
*/
def containerConfig : Config

/**
* Read a config with a given id from the profile config directory and apply all blended
* replacements in the result.
* @param id The id to retrieve the config for. This is usually the bundle symbolic name.
*/
def getConfig(id : String) : Config

/**
* Access to the Container Identifier Service
*/
def identifierService() : ContainerIdentifierService

/**
* Access to a blended resolver for config values
*/
def resolveString(s : String, additionalProps : Map[String, Any] = Map.empty) : Try[AnyRef]

/**
* Provide access to the next transaction number
*/
def getNextTransactionCounter() : Long = ContainerContext.nextTransactionCounter
}
@@ -3,9 +3,6 @@ package blended.container.context.api
import java.util.UUID

import scala.beans.BeanProperty
import scala.util.Try

class PropertyResolverException(msg : String) extends Exception(msg)

/**
* Each container within the infrastructure has a unique ID. Once the unique ID is assigned to
@@ -19,15 +16,6 @@ trait ContainerIdentifierService {
lazy val uuid : String = UUID.randomUUID().toString()
@BeanProperty
val properties : Map[String, String]
@BeanProperty
val containerContext : ContainerContext

/**
* Try to resolve the properties inside a given String and return a string with the replaced properties values.
*/
def resolvePropertyString(value : String) : Try[AnyRef] = resolvePropertyString(value, Map.empty)

def resolvePropertyString(value : String, additionalProps : Map[String, Any]) : Try[AnyRef]
}

object ContainerIdentifierService {

This file was deleted.

@@ -0,0 +1,20 @@
package blended.container.context.impl.internal

import blended.container.context.api.{ContainerContext, ContainerIdentifierService}

import scala.util.Try

abstract class AbstractContainerContextImpl extends ContainerContext {

private val resolver : ContainerPropertyResolver = new ContainerPropertyResolver(this)

/**
* Access to the Container Identifier Service
*/
override val identifierService: ContainerIdentifierService = new ContainerIdentifierServiceImpl(this)

/**
* Access to a blended resolver for config values
*/
override def resolveString(s: String, additionalProps: Map[String, Any]): Try[AnyRef] = resolver.resolve(s, additionalProps)
}
@@ -0,0 +1,73 @@
package blended.container.context.impl.internal

import java.io.File

import blended.container.context.api.ContainerContext
import blended.util.logging.Logger
import com.typesafe.config._

import scala.collection.JavaConverters._
import scala.util.{Failure, Success, Try}

/**
* A helper class to read HOCON config files from the file system and apply blended
* replacements in all the config values.
*/
object ConfigLocator {

private[this] val log : Logger = Logger[ConfigLocator.type]

private[this] val sysProps : Config = ConfigFactory.systemProperties()
private[this] val envProps : Config = ConfigFactory.systemEnvironment()

private[this] def readConfigFile(f : File, fallback : Config) : List[(String, ConfigValue)] = {
val cfg : Config = ConfigFactory.parseFile(f)
.withFallback(fallback)
.withFallback(sysProps)
.withFallback(envProps)
.resolve()

cfg.entrySet().asScala.map(k => (k.getKey(), k.getValue)).toList
}

private[this] def evaluatedConfig(f : File, fallback : Config, ctContext : ContainerContext) : Try[Config] = Try {

if (f.exists && f.isFile && f.canRead) {

val rawValues : List[(String, ConfigValue)]= readConfigFile(f, fallback)

val cfgObj : ConfigObject = rawValues.foldLeft(ConfigFactory.empty().root()){
case (current, (key, value)) =>
val replace : ConfigValue = value match {
case s if s.valueType() == ConfigValueType.STRING =>
val rawValue : String = s.unwrapped().toString()
ConfigValueFactory.fromAnyRef(rawValue)
case v => v
}

current.withValue(key, replace)
}

cfgObj.toConfig()
} else {
ConfigFactory.empty()
}
}

/**
* Read a configuration file from a given directory.
*/
def config(cfgDir : String, fileName : String, fallback: Config, ctContext: ContainerContext) : Try[Config] = {
val file = new File(cfgDir, fileName)
log.debug(s"Retrieving config from [${file.getAbsolutePath()}]")
evaluatedConfig(file, fallback, ctContext)
}

def safeConfig(cfgDir : String, fileName: String, fallback: Config, ctContext : ContainerContext) : Config =
config(cfgDir, fileName, fallback, ctContext) match {
case Failure(e) =>
log.warn(s"Error reading [$fileName] : [${e.getMessage()}], using empty config")
ConfigFactory.empty()
case Success(cfg) => cfg
}
}
@@ -1,6 +1,6 @@
package blended.container.context.impl.internal

import blended.container.context.api.ContainerIdentifierService
import blended.container.context.api.{ContainerContext, ContainerIdentifierService}
import blended.util.logging.Logger
import domino.DominoActivator

@@ -16,15 +16,14 @@ class ContainerContextActivator extends DominoActivator {

whenBundleActive {
try {
val containerContext = new ContainerContextImpl()
val idSvc = new ContainerIdentifierServiceImpl(containerContext)
val ctContext = new ContainerContextImpl()

log.info(s"Container identifier is [${idSvc.uuid}]")
log.info(s"Profile home directory is [${containerContext.getProfileDirectory()}]")
log.info(s"Container Context properties are : ${idSvc.properties.mkString("[", ",", "]")}")
log.info(s"Container identifier is [${ctContext.identifierService.uuid}]")
log.info(s"Profile home directory is [${ctContext.profileDirectory}]")
log.info(s"Container Context properties are : ${ctContext.identifierService.properties.mkString("[", ",", "]")}")

Logger.setProps(mdcMap(idSvc))
idSvc.providesService[ContainerIdentifierService]
Logger.setProps(mdcMap(ctContext.identifierService))
ctContext.providesService[ContainerContext]
} catch {
case NonFatal(e) =>
log.error(e.getMessage())

0 comments on commit 751970e

Please sign in to comment.
You can’t perform that action at this time.