user configuration for OSGi application bundle

This commit is contained in:
Christophe Pache 2013-01-29 10:17:06 +01:00
parent 2ee4198fb2
commit 240981d4d5
2 changed files with 18 additions and 12 deletions

View file

@ -7,6 +7,7 @@ import akka.actor.ActorSystem
import java.util.{ Dictionary, Properties }
import org.osgi.framework._
import org.osgi.service.log.LogService
import com.typesafe.config.{ ConfigFactory, Config }
/**
* Abstract bundle activator implementation to bootstrap and configure an actor system in an
@ -38,7 +39,7 @@ abstract class ActorSystemActivator extends BundleActivator {
* @param context the BundleContext
*/
def start(context: BundleContext): Unit = {
system = Some(OsgiActorSystemFactory(context).createActorSystem(Option(getActorSystemName(context))))
system = Some(OsgiActorSystemFactory(context, getActorSystemConfiguration(context)).createActorSystem(Option(getActorSystemName(context))))
system foreach (addLogServiceListener(context, _))
system foreach (configure(context, _))
}
@ -109,4 +110,16 @@ abstract class ActorSystemActivator extends BundleActivator {
*/
def getActorSystemName(context: BundleContext): String = null
/**
* Override this method to define a configuration for your [[akka.actor.ActorSystem]] instance.
* This configuration will be merged with fallback on
* the application.conf of your bundle
* the reference.conf of the akka bundles
* the System properties.
*
* @param context the bundle context
* @return the actor system specific configuration, ConfigFactory.empty by default
*/
def getActorSystemConfiguration(context: BundleContext): Config = ConfigFactory.empty
}

View file

@ -13,7 +13,7 @@ import java.io.File
* Factory class to create ActorSystem implementations in an OSGi environment. This mainly involves dealing with
* bundle classloaders appropriately to ensure that configuration files and classes get loaded properly
*/
class OsgiActorSystemFactory(val context: BundleContext, val fallbackClassLoader: Option[ClassLoader]) {
class OsgiActorSystemFactory(val context: BundleContext, val fallbackClassLoader: Option[ClassLoader], config: Config = ConfigFactory.empty) {
/*
* Classloader that delegates to the bundle for which the factory is creating an ActorSystem
@ -34,19 +34,12 @@ class OsgiActorSystemFactory(val context: BundleContext, val fallbackClassLoader
ActorSystem(actorSystemName(name), actorSystemConfig(context), classloader)
/**
* Strategy method to create the Config for the ActorSystem, based on configuration files found in etc directory,
* Strategy method to create the Config for the ActorSystem
* ensuring that the default/reference configuration is loaded from the akka-actor bundle.
* The configuration is based on
* etc/bundle-SYMBOLICNAME, etc/bundle-ID, etc/akka.conf
* in either ".conf", ".properties", ".json" formats
* Configuration files found in akka-actor bundle
*/
def actorSystemConfig(context: BundleContext): Config = {
val bundleSymbolicName = context.getBundle.getSymbolicName
val bundleId = context.getBundle.getBundleId
val acceptedFilePath = List(s"bundle-$bundleSymbolicName", s"bundle-$bundleId", "akka").map(x s"etc/$x")
val applicationConfiguration = acceptedFilePath.foldLeft(ConfigFactory.empty())((x, y) x.withFallback(ConfigFactory.parseFileAnySyntax(new File(y))))
applicationConfiguration.withFallback(ConfigFactory.load(classloader).withFallback(ConfigFactory.defaultReference(OsgiActorSystemFactory.akkaActorClassLoader)))
config.withFallback(ConfigFactory.load(classloader).withFallback(ConfigFactory.defaultReference(OsgiActorSystemFactory.akkaActorClassLoader)))
}
/**
@ -67,5 +60,5 @@ object OsgiActorSystemFactory {
/*
* Create an [[OsgiActorSystemFactory]] instance to set up Akka in an OSGi environment
*/
def apply(context: BundleContext): OsgiActorSystemFactory = new OsgiActorSystemFactory(context, Some(akkaActorClassLoader))
def apply(context: BundleContext, config: Config): OsgiActorSystemFactory = new OsgiActorSystemFactory(context, Some(akkaActorClassLoader), config)
}