From 240981d4d55fb939c4ff5216d539b8b51e7f3965 Mon Sep 17 00:00:00 2001 From: Christophe Pache Date: Tue, 29 Jan 2013 10:17:06 +0100 Subject: [PATCH] user configuration for OSGi application bundle --- .../scala/akka/osgi/ActorSystemActivator.scala | 15 ++++++++++++++- .../scala/akka/osgi/OsgiActorSystemFactory.scala | 15 ++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala b/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala index f569a673a4..dfe55728aa 100644 --- a/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala +++ b/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala @@ -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 + } diff --git a/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala b/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala index 4194afba01..7e434671b2 100644 --- a/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala +++ b/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala @@ -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) }