diff --git a/akka-docs/additional/code/osgi/Activator.scala b/akka-docs/additional/code/osgi/Activator.scala index 06a538d242..34e83fcf77 100644 --- a/akka-docs/additional/code/osgi/Activator.scala +++ b/akka-docs/additional/code/osgi/Activator.scala @@ -6,7 +6,10 @@ import org.apache.servicemix.examples.akka.Master //#Activator class Activator extends ActorSystemActivator("PiSystem") { - def configure(system: ActorSystem) { + def configure(context: BundleContext, system: ActorSystem) { + // optionally register the ActorSystem in the OSGi Service Registry + registerService(context, system) + val listener = system.actorOf(Props[Listener], name = "listener") val master = system.actorOf(Props(new Master(4, 10000, 10000, listener)), name = "master") master ! Calculate diff --git a/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala b/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala index 7f60aebccc..546ff8c2c4 100644 --- a/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala +++ b/akka-osgi/src/main/scala/akka/osgi/ActorSystemActivator.scala @@ -2,8 +2,8 @@ package akka.osgi import com.typesafe.config.{ Config, ConfigFactory } import akka.actor.ActorSystem -import org.osgi.framework.{ BundleContext, BundleActivator } -import java.util.Properties +import java.util.{ Dictionary, Properties } +import org.osgi.framework.{ ServiceRegistration, BundleContext, BundleActivator } /** * Abstract {@link BundleActivator} implementation to bootstrap and configure an {@link ActorSystem} in an @@ -14,14 +14,16 @@ abstract class ActorSystemActivator(nameFor: (BundleContext) ⇒ Option[String]) def this() = this({ context: BundleContext ⇒ None }) def this(name: String) = this({ context: BundleContext ⇒ Some(name) }) - var system: ActorSystem = null + var system: Option[ActorSystem] = None + var registration: Option[ServiceRegistration] = None /** * Implement this method to add your own actors to the ActorSystem * + * @param context the bundle context * @param system the ActorSystem that was created by the activator */ - def configure(system: ActorSystem) + def configure(context: BundleContext, system: ActorSystem) /** * Sets up a new ActorSystem and registers it in the OSGi Service Registry @@ -29,20 +31,31 @@ abstract class ActorSystemActivator(nameFor: (BundleContext) ⇒ Option[String]) * @param context the BundleContext */ def start(context: BundleContext) { - system = OsgiActorSystemFactory(context).createActorSystem(nameFor(context)) - configure(system) + system = Some(OsgiActorSystemFactory(context).createActorSystem(nameFor(context))) + system.foreach(configure(context, _)) } /** - * Shuts down the ActorSystem when the bundle is stopped. + * Shuts down the ActorSystem when the bundle is stopped and, if necessary, unregisters a service registration * * @param context the BundleContext */ def stop(context: BundleContext) { - if (system != null) { - system.shutdown() - system = null - } + registration.foreach(_.unregister()) + system.foreach(_.shutdown()) + } + + /** + * Register the actor system in the OSGi service registry + * + * @param context the bundle context + * @param system the actor system + */ + def registerService(context: BundleContext, system: ActorSystem) { + val properties = new Properties() + properties.put("name", system.name) + registration = Some(context.registerService(classOf[ActorSystem].getName, system, + properties.asInstanceOf[Dictionary[String, Any]])) } } diff --git a/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala b/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala index cddf797d07..2c5a6eca14 100644 --- a/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala +++ b/akka-osgi/src/main/scala/akka/osgi/OsgiActorSystemFactory.scala @@ -1,10 +1,9 @@ package akka.osgi import impl.BundleDelegatingClassLoader -import org.osgi.framework.BundleContext import akka.actor.ActorSystem import com.typesafe.config.{ ConfigFactory, Config } -import java.util.{ Dictionary, Properties } +import org.osgi.framework.BundleContext /** * Factory class to create ActorSystem implementations in an OSGi environment. This mainly involves dealing with @@ -22,17 +21,8 @@ class OsgiActorSystemFactory(val context: BundleContext) { */ def createActorSystem(name: String): ActorSystem = createActorSystem(Option(name)) - def createActorSystem(name: Option[String]): ActorSystem = { - val system = ActorSystem(actorSystemName(name), actorSystemConfig(context), classloader) - registerService(system) - system - } - - def registerService(system: ActorSystem) { - val properties = new Properties() - properties.put("name", system.name) - context.registerService(classOf[ActorSystem].getName, system, properties.asInstanceOf[Dictionary[String, Any]]) - } + def createActorSystem(name: Option[String]): ActorSystem = + ActorSystem(actorSystemName(name), actorSystemConfig(context), classloader) /** * Strategy method to create the Config for the ActorSystem, ensuring that the default/reference configuration is diff --git a/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/config.xml b/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/config.xml index 6bd3d49c9d..ce9f48c551 100644 --- a/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/config.xml +++ b/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/config.xml @@ -2,7 +2,9 @@ - + + + some.config { key=value diff --git a/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/simple.xml b/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/simple.xml index a46834f74b..2ac6552f80 100644 --- a/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/simple.xml +++ b/akka-osgi/src/test/resources/akka/osgi/blueprint/aries/simple.xml @@ -2,6 +2,8 @@ - + + + diff --git a/akka-osgi/src/test/scala/akka/osgi/test/TestActorSystemActivator.scala b/akka-osgi/src/test/scala/akka/osgi/test/TestActorSystemActivator.scala index 2a44e91e4a..90305bc663 100644 --- a/akka-osgi/src/test/scala/akka/osgi/test/TestActorSystemActivator.scala +++ b/akka-osgi/src/test/scala/akka/osgi/test/TestActorSystemActivator.scala @@ -3,14 +3,16 @@ package akka.osgi.test import akka.osgi.ActorSystemActivator import akka.actor.{ Props, ActorSystem } import PingPong._ +import org.osgi.framework.BundleContext /** * Sample ActorSystemActivator implementation used for testing purposes */ class TestActorSystemActivator extends ActorSystemActivator { - def configure(system: ActorSystem) { + def configure(context: BundleContext, system: ActorSystem) { system.actorOf(Props(new PongActor), name = "pong") + registerService(context, system) } }