From 7628831bdb924d3764a8b146ee45a5448fc3eb17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bon=C3=A9r?= Date: Tue, 16 Feb 2010 17:10:32 +0100 Subject: [PATCH] added some methods to the AspectRegistry --- .../src/main/scala/actor/ActorRegistry.scala | 30 ++++++++++++++++++- .../ServerInitiatedRemoteActorTest.scala | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/akka-core/src/main/scala/actor/ActorRegistry.scala b/akka-core/src/main/scala/actor/ActorRegistry.scala index 9a6dafcc58..63314ae051 100644 --- a/akka-core/src/main/scala/actor/ActorRegistry.scala +++ b/akka-core/src/main/scala/actor/ActorRegistry.scala @@ -6,7 +6,8 @@ package se.scalablesolutions.akka.actor import se.scalablesolutions.akka.util.Logging -import scala.collection.mutable.HashMap +import scala.collection.mutable.{ListBuffer, HashMap} +import scala.reflect.Manifest /** * Registry holding all actor instances, mapped by class and the actor's id field (which can be set by user-code). @@ -17,6 +18,30 @@ object ActorRegistry extends Logging { private val actorsByClassName = new HashMap[String, List[Actor]] private val actorsById = new HashMap[String, List[Actor]] + /** + * Returns all actors in the system. + */ + def actors: List[Actor] = synchronized { + val all = new ListBuffer[Actor] + actorsById.values.foreach(all ++= _) + all.toList + } + + /** + * Invokes a function for all actors. + */ + def foreach(f: (Actor) => Unit) = actors.foreach(f) + + /** + * Finds all actors that are subtypes of the class passed in as the Manifest argument. + */ + def actorsFor[T <: Actor](implicit manifest: Manifest[T]): List[T] = synchronized { + for (actor <- actors; if manifest.erasure.isAssignableFrom(actor.getClass)) yield actor.asInstanceOf[T] + } + + /** + * Finds all actors of the exact type specified by the class passed in as the Class argument. + */ def actorsFor[T <: Actor](clazz: Class[T]): List[T] = synchronized { actorsByClassName.get(clazz.getName) match { case None => Nil @@ -24,6 +49,9 @@ object ActorRegistry extends Logging { } } + /** + * Finds all actors that has a specific id. + */ def actorsFor(id : String): List[Actor] = synchronized { actorsById.get(id) match { case None => Nil diff --git a/akka-core/src/test/scala/ServerInitiatedRemoteActorTest.scala b/akka-core/src/test/scala/ServerInitiatedRemoteActorTest.scala index 232e8e539b..2f1ef161c8 100644 --- a/akka-core/src/test/scala/ServerInitiatedRemoteActorTest.scala +++ b/akka-core/src/test/scala/ServerInitiatedRemoteActorTest.scala @@ -142,3 +142,4 @@ class ServerInitiatedRemoteActorTest extends JUnitSuite { actor.stop } } + \ No newline at end of file