Skip to content
Permalink
Browse files

Refactoring bundles to new Container Context API

  • Loading branch information
atooni committed Feb 15, 2020
1 parent 1b71364 commit 6aba8b16d3ff93519e1a9530e7221b7ca87ea62b
Showing with 78 additions and 56 deletions.
  1. +1 −1 ...tivemq.brokerstarter/src/main/scala/blended/activemq/brokerstarter/internal/BrokerActivator.scala
  2. +4 −4 ....activemq.brokerstarter/src/main/scala/blended/activemq/brokerstarter/internal/BrokerConfig.scala
  3. +1 −1 ...emq.brokerstarter/src/main/scala/blended/activemq/brokerstarter/internal/BrokerControlActor.scala
  4. +3 −3 blended.akka/src/main/scala/blended/akka/internal/BlendedAkkaActivator.scala
  5. +1 −1 blended.container.context.api/src/main/scala/blended/container/context/api/ContainerContext.scala
  6. +1 −1 ...er.context.impl/src/test/scala/blended/container/context/impl/internal/PropertyResolverSpec.scala
  7. +4 −2 blended.jms.utils/src/main/scala/blended/jms/utils/BlendedJMSConnectionConfig.scala
  8. +1 −1 blended.jms.utils/src/main/scala/blended/jms/utils/ConnectionFactoryActivator.scala
  9. +3 −3 blended.mgmt.base/src/main/scala/blended/mgmt/base/internal/FrameworkService.scala
  10. +3 −3 blended.mgmt.base/src/main/scala/blended/mgmt/base/internal/MgmtBaseActivator.scala
  11. 0 blended.mgmt.base/src/test/resources/container/etc/application.conf
  12. +32 −0 blended.mgmt.base/src/test/scala/blended/mgmt/base/internal/MgmtBaseActivatorSpec.scala
  13. +9 −26 blended.testsupport.pojosr/src/main/scala/blended/testsupport/pojosr/MockContainerContext.scala
  14. +5 −9 blended.testsupport.pojosr/src/main/scala/blended/testsupport/pojosr/PojoSrTestHelper.scala
  15. +6 −0 doc/_notes/ContainerContext.adoc
  16. +4 −1 project/BlendedMgmtBase.scala
@@ -17,7 +17,7 @@ class BrokerActivator

val brokerConfigs = osgiCfg.config.getConfigMap("broker", Map.empty).map {
case (brokerName, cfg) =>
brokerName -> BrokerConfig.create(brokerName, osgiCfg.idSvc, cfg).get
brokerName -> BrokerConfig.create(brokerName, osgiCfg.ctContext, cfg).get
}

val withSsl = brokerConfigs.values.exists(_.withSsl)
@@ -1,6 +1,6 @@
package blended.activemq.brokerstarter.internal

import blended.container.context.api.ContainerIdentifierService
import blended.container.context.api.{ContainerContext, ContainerIdentifierService}
import blended.jms.utils.{BlendedJMSConnectionConfig, ConnectionConfig}
import blended.util.config.Implicits._
import com.typesafe.config.Config
@@ -65,14 +65,14 @@ object BrokerConfig {

val anonymousGroups : Config => List[String] = cfg => cfg.getStringList("anonymousGroups", List.empty)

def create(brokerName : String, idSvc : ContainerIdentifierService, cfg : Config) : Try[BrokerConfig] = Try {
def create(brokerName : String, ctCtxt : ContainerContext, cfg : Config) : Try[BrokerConfig] = Try {

val resolve : String => Try[Any] = value => idSvc.resolvePropertyString(value)
val resolve : String => Try[AnyRef] = value => ctCtxt.resolveString(value)

val v = vendor(resolve)(cfg).getOrElse("activemq")
val p = provider(resolve)(cfg).getOrElse("activemq")

val jmsConfig : BlendedJMSConnectionConfig = BlendedJMSConnectionConfig.fromConfig(resolve)(v, p, cfg)
val jmsConfig : BlendedJMSConnectionConfig = BlendedJMSConnectionConfig.fromConfig(ctCtxt)(v, p, cfg)

BrokerConfig(
vendor = jmsConfig.vendor,
@@ -94,7 +94,7 @@ class BrokerControlActor(brokerCfg : BrokerConfig, cfg : OSGIActorConfig, sslCtx

val oldLoader = Thread.currentThread().getContextClassLoader()

val cfgDir = cfg.idSvc.containerContext.getProfileConfigDirectory()
val cfgDir = cfg.ctContext.profileConfigDirectory
val uri = s"file://$cfgDir/${brokerCfg.file}"

try {
@@ -1,7 +1,7 @@
package blended.akka.internal

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

@@ -14,8 +14,8 @@ class BlendedAkkaActivator extends DominoActivator {
private[this] val log = Logger[BlendedAkkaActivator]

whenBundleActive {
whenServicePresent[ContainerIdentifierService] { svc =>
val ctConfig = svc.containerContext.getContainerConfig()
whenServicePresent[ContainerContext] { ctCtxt =>
val ctConfig = ctCtxt.containerConfig

log.trace(s"$ctConfig")

@@ -42,7 +42,7 @@ trait ContainerContext {
/**
* The base directory for the current container profile
*/
def profileDirectory() : String
def profileDirectory : String

/**
* The config directory for all profile specific configuration files. Usually this is
@@ -3,7 +3,7 @@ package blended.container.context.impl.internal
import blended.container.context.api.{ContainerContext, PropertyResolverException}
import blended.testsupport.BlendedTestSupport
import blended.util.RichTry._
import org.scalatest.{DoNotDiscover, FreeSpec, Matchers}
import org.scalatest.{FreeSpec, Matchers}

import scala.util.control.NonFatal

@@ -1,6 +1,6 @@
package blended.jms.utils

import blended.container.context.api.ContainerIdentifierService
import blended.container.context.api.{ContainerContext, ContainerIdentifierService}
import blended.jms.utils.ConnectionFactoryActivator.{CF_JNDI_NAME, DEFAULT_PWD, DEFAULT_USER, USE_JNDI}
import blended.updater.config.util.ConfigPropertyMapConverter
import blended.util.RichTry._
@@ -41,11 +41,13 @@ object BlendedJMSConnectionConfig {

// scalastyle:off method.length
def fromConfig(
stringResolver : String => Try[Any]
ctContext : ContainerContext
)(
vendor : String, provider : String, cfg : Config
) : BlendedJMSConnectionConfig = {

val stringResolver : String => Try[AnyRef] = s => ctContext.resolveString(s)

val enabled : Config => Boolean = cfg => cfg.getBoolean("enabled", defaultConfig.enabled)
val jmxEnabled : Config => Boolean = cfg => cfg.getBoolean("jmxEnabled", defaultConfig.jmxEnabled)
val keepAliveEnabled : Config => Boolean = cfg => cfg.getBoolean("keepAliveEnabled", defaultConfig.keepAliveEnabled)
@@ -51,7 +51,7 @@ abstract class ConnectionFactoryActivator extends DominoActivator with ActorSyst

val fnEnabled = connectionFactoryEnabled.map(f => f(osgiCfg))

val cfCfg = BlendedJMSConnectionConfig.fromConfig(osgiCfg.idSvc.resolvePropertyString)(
val cfCfg = BlendedJMSConnectionConfig.fromConfig(osgiCfg.ctContext)(
vendor = cfVendor,
provider = cfProvider,
cfg = osgiCfg.config.getConfig("factories").getConfig(cfProvider)
@@ -24,7 +24,7 @@ class FrameworkService(bundleContext : BundleContext, ctContext : ContainerConte

override def restartContainer(reason : String, saveLogs : Boolean) : Unit = {

val cfg = ctContext.containerConfig()
val cfg = ctContext.containerConfig
val saveLogsPath = "blended.saveLogsOnRestart"

try {
@@ -62,7 +62,7 @@ class FrameworkService(bundleContext : BundleContext, ctContext : ContainerConte

val archiveName = s"restart-$timestamp.zip"

val logDir = new File(ctContext.containerLogDirectory())
val logDir = new File(ctContext.containerLogDirectory)
log.info(s"Creating log archive from directory [${logDir.getAbsolutePath()}]")

val logFiles = logDir.list( (dir : File, name : String) => {
@@ -84,7 +84,7 @@ class FrameworkService(bundleContext : BundleContext, ctContext : ContainerConte
out.flush()
out.close()
} catch {
case e : Exception => log.error(e)(s"Error creating log archive (${e.getMessage})")
case NonFatal(e) => log.error(e)(s"Error creating log archive (${e.getMessage})")
}
}
}
@@ -1,6 +1,6 @@
package blended.mgmt.base.internal

import blended.container.context.api.ContainerIdentifierService
import blended.container.context.api.ContainerContext
import blended.util.logging.Logger
import domino.DominoActivator
import javax.management.{MBeanServer, ObjectName}
@@ -10,11 +10,11 @@ class MgmtBaseActivator extends DominoActivator {
private[this] val log = Logger[MgmtBaseActivator]

whenBundleActive {
whenServicePresent[ContainerIdentifierService] { idSvc =>
whenServicePresent[ContainerContext] { ctCtxt =>

log.info("Creating Framework Service instance...")

val fwSvc = new FrameworkService(bundleContext, idSvc.containerContext)
val fwSvc = new FrameworkService(bundleContext, ctCtxt)
fwSvc.providesService[blended.mgmt.base.FrameworkService]

whenServicePresent[MBeanServer] { server =>
@@ -0,0 +1,32 @@
package blended.mgmt.base.internal

import java.io.File

import blended.testsupport.BlendedTestSupport
import blended.testsupport.pojosr.{PojoSrTestHelper, SimplePojoContainerSpec}
import blended.testsupport.scalatest.LoggingFreeSpecLike
import org.osgi.framework.BundleActivator
import org.scalatest.Matchers
import scala.concurrent.duration._

class MgmtBaseActivatorSpec extends SimplePojoContainerSpec
with LoggingFreeSpecLike
with PojoSrTestHelper
with Matchers {

override def bundles : Seq[(String, BundleActivator)] = Seq(
"blended.mgmt.base" -> new MgmtBaseActivator()
)

override def baseDir : String = new File(BlendedTestSupport.projectTestOutput, "container").getAbsolutePath()

"The Mgmt base activator" - {

"should register the framework as OSGi service" in {

implicit val timeout : FiniteDuration = 3.seconds
mandatoryService[blended.mgmt.base.FrameworkService](registry)(None)

}
}
}
@@ -3,40 +3,25 @@ package blended.testsupport.pojosr
import java.io.File
import java.util.Properties

import blended.container.context.api.ContainerContext
import blended.security.crypto.{BlendedCryptoSupport, ContainerCryptoSupport}
import blended.container.context.impl.internal.AbstractContainerContextImpl
import com.typesafe.config.impl.Parseable
import com.typesafe.config.{Config, ConfigFactory, ConfigObject, ConfigParseOptions}

class MockContainerContext(baseDir : String) extends ContainerContext {
class MockContainerContext(baseDir : String) extends AbstractContainerContextImpl {

private val SECRET_FILE_PATH : String = "blended.security.secretFile"

override val containerDirectory : String = baseDir
override lazy val containerDirectory : String = baseDir

override val containerConfigDirectory : String = containerDirectory + "/etc"
override lazy val containerConfigDirectory : String = containerDirectory + "/etc"

override val containerLogDirectory : String = baseDir
override lazy val containerLogDirectory : String = baseDir

override val profileDirectory : String = containerDirectory
override lazy val profileDirectory : String = containerDirectory

override val profileConfigDirectory : String = containerConfigDirectory
override lazy val profileConfigDirectory : String = containerConfigDirectory

override val containerHostname : String = "localhost"

override val cryptoSupport : ContainerCryptoSupport = {
val ctConfig : Config = containerConfig

val cipherSecretFile : String = if (ctConfig.hasPath(SECRET_FILE_PATH)) {
ctConfig.getString(SECRET_FILE_PATH)
} else {
"secret"
}

BlendedCryptoSupport.initCryptoSupport(
new File(containerConfigDirectory, cipherSecretFile).getAbsolutePath()
)
}
override lazy val containerHostname : String = "localhost"

private def getSystemProperties() : Properties = {
// Avoid ConcurrentModificationException due to parallel setting of system properties by copying properties
@@ -55,7 +40,7 @@ class MockContainerContext(baseDir : String) extends ContainerContext {
.parse()
}

override val containerConfig : Config = {
override lazy val containerConfig : Config = {
val sysProps = loadSystemProperties()
val envProps = ConfigFactory.systemEnvironment()

@@ -68,6 +53,4 @@ class MockContainerContext(baseDir : String) extends ContainerContext {
.withFallback(envProps)
.resolve()
}


}
@@ -2,8 +2,8 @@ package blended.testsupport.pojosr

import java.io.File

import blended.container.context.api.ContainerIdentifierService
import blended.container.context.impl.internal.ContainerIdentifierServiceImpl
import blended.container.context.api.{ContainerContext, ContainerIdentifierService}
import blended.container.context.impl.internal.{ContainerContextImpl, ContainerIdentifierServiceImpl}
import blended.util.logging.Logger
import domino.DominoActivator
import org.osgi.framework.{BundleActivator, ServiceReference}
@@ -42,7 +42,7 @@ trait PojoSrTestHelper {
}
}

def idSvcActivator(
def contextActivator(
mandatoryProperties : Option[String] = None
) : BundleActivator = {
new DominoActivator {
@@ -51,11 +51,7 @@ trait PojoSrTestHelper {
System.setProperty("blended.updater.profile.properties.keys", s))

whenBundleActive {
val ctCtxt = new MockContainerContext(baseDir)
// This needs to be a fixed uuid as some tests might be for restarts and require the same id
new ContainerIdentifierServiceImpl(ctCtxt) {
override lazy val uuid : String = pojoUuid
}.providesService[ContainerIdentifierService]
new MockContainerContext(baseDir).providesService[ContainerContext]
}
}
}
@@ -80,7 +76,7 @@ trait PojoSrTestHelper {
System.setProperty("blended.container.home", baseDir)
sysProperties.foreach { case (k, v) => System.setProperty(k, v) }
startBundle(createRegistry().get)(
classOf[ContainerIdentifierServiceImpl].getPackage().getName(), idSvcActivator(Some(mandatoryProperties.mkString(",")))
classOf[ContainerContext].getPackage().getName(), contextActivator(Some(mandatoryProperties.mkString(",")))
).get._2
}

@@ -0,0 +1,6 @@
## Container Context API migration


[ ] Remove blended evaluation from bundle config code

[ ] Add test case for config evaluation and activator to jms utils.
@@ -23,7 +23,10 @@ object BlendedMgmtBase extends ProjectFactory {
BlendedDomino.project,
BlendedContainerContextApi.project,
BlendedUtil.project,
BlendedUtilLogging.project
BlendedUtilLogging.project,

BlendedTestsupport.project % Test,
BlendedTestsupportPojosr.project % Test
)
}
}

0 comments on commit 6aba8b1

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