diff options
author | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2012-10-02 04:42:58 -0700 |
---|---|---|
committer | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2012-10-02 04:42:58 -0700 |
commit | 60a6e245d73a2fc9844cff9993b4bc7d4eea61a4 (patch) | |
tree | bfc90cf8060c91fa829bdd9f67528ded571056f5 /src/actors-migration | |
parent | 1088af1236b64b89118e566ce3856b8101b391a9 (diff) | |
parent | b2211a76a6e537cf2e404d424cb643f171818e92 (diff) | |
download | scala-60a6e245d73a2fc9844cff9993b4bc7d4eea61a4.tar.gz scala-60a6e245d73a2fc9844cff9993b4bc7d4eea61a4.tar.bz2 scala-60a6e245d73a2fc9844cff9993b4bc7d4eea61a4.zip |
Merge pull request #1425 from phaller/issue/6442
SI-6442 - Add ActorDSL object for actor migration kit
Diffstat (limited to 'src/actors-migration')
3 files changed, 61 insertions, 42 deletions
diff --git a/src/actors-migration/scala/actors/migration/ActorDSL.scala b/src/actors-migration/scala/actors/migration/ActorDSL.scala new file mode 100644 index 0000000000..b8cb8ec998 --- /dev/null +++ b/src/actors-migration/scala/actors/migration/ActorDSL.scala @@ -0,0 +1,56 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2005-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.actors +package migration + +import scala.actors.{ Actor, ActorRef, InternalActorRef } +import scala.collection.immutable +import scala.reflect.ClassTag + +object ActorDSL { + + private[migration] val contextStack = new ThreadLocal[immutable.Stack[Boolean]] { + override def initialValue() = immutable.Stack[Boolean]() + } + + private[this] def withCleanContext(block: => ActorRef): ActorRef = { + // push clean marker + val old = contextStack.get + contextStack.set(old.push(true)) + try { + val instance = block + + if (instance eq null) + throw new Exception("ActorRef can't be 'null'") + + instance + } finally { + val stackAfter = contextStack.get + if (stackAfter.nonEmpty) + contextStack.set(if (!stackAfter.head) stackAfter.pop.pop else stackAfter.pop) + } + } + + /** + * Create an actor from the given thunk which must produce an [[scala.actors.Actor]]. + * + * @param ctor is a by-name argument which captures an [[scala.actors.Actor]] + * factory; <b>do not make the generated object accessible to code + * outside and do not return the same object upon subsequent invocations.</b> + */ + def actor[T <: InternalActor: ClassTag](ctor: ⇒ T): ActorRef = { + withCleanContext { + val newActor = ctor + val newRef = new InternalActorRef(newActor) + newActor.start() + newRef + } + } + +} diff --git a/src/actors-migration/scala/actors/migration/MigrationSystem.scala b/src/actors-migration/scala/actors/migration/MigrationSystem.scala deleted file mode 100644 index 3dcb38e634..0000000000 --- a/src/actors-migration/scala/actors/migration/MigrationSystem.scala +++ /dev/null @@ -1,37 +0,0 @@ -package scala.actors.migration - -import scala.actors._ -import scala.collection._ - -object MigrationSystem { - - private[migration] val contextStack = new ThreadLocal[immutable.Stack[Boolean]] { - override def initialValue() = immutable.Stack[Boolean]() - } - - private[this] def withCleanContext(block: => ActorRef): ActorRef = { - // push clean marker - val old = contextStack.get - contextStack.set(old.push(true)) - try { - val instance = block - - if (instance eq null) - throw new Exception("Actor instance passed to actorOf can't be 'null'") - - instance - } finally { - val stackAfter = contextStack.get - if (stackAfter.nonEmpty) - contextStack.set(if (!stackAfter.head) stackAfter.pop.pop else stackAfter.pop) - } - } - - def actorOf(props: Props): ActorRef = withCleanContext { - val creator = props.creator() - val r = new InternalActorRef(creator) - creator.start() - r - } - -}
\ No newline at end of file diff --git a/src/actors-migration/scala/actors/migration/StashingActor.scala b/src/actors-migration/scala/actors/migration/StashingActor.scala index 2a6351f120..12bad2ed1c 100644 --- a/src/actors-migration/scala/actors/migration/StashingActor.scala +++ b/src/actors-migration/scala/actors/migration/StashingActor.scala @@ -110,18 +110,18 @@ trait StashingActor extends InternalActor { private[actors] var behaviorStack = immutable.Stack[PartialFunction[Any, Unit]]() /* - * Checks that StashingActor can be created only by MigrationSystem.actorOf method. + * Checks that StashingActor instances can only be created using the ActorDSL. */ private[this] def creationCheck(): Unit = { // creation check (see ActorRef) - val context = MigrationSystem.contextStack.get + val context = ActorDSL.contextStack.get if (context.isEmpty) - throw new RuntimeException("In order to create StashingActor one must use actorOf.") + throw new RuntimeException("In order to create a StashingActor one must use the ActorDSL object") else { if (!context.head) - throw new RuntimeException("Only one actor can be created per actorOf call.") + throw new RuntimeException("Cannot create more than one actor") else - MigrationSystem.contextStack.set(context.push(false)) + ActorDSL.contextStack.set(context.push(false)) } } |