From b2211a76a6e537cf2e404d424cb643f171818e92 Mon Sep 17 00:00:00 2001 From: phaller Date: Sat, 29 Sep 2012 18:15:37 +0200 Subject: SI-6442 - Add ActorDSL object for actor migration kit Removes MigrationSystem, since ActorDSL replaces it. --- .../scala/actors/migration/ActorDSL.scala | 56 ++++++++++++++++++++++ .../scala/actors/migration/MigrationSystem.scala | 37 -------------- .../scala/actors/migration/StashingActor.scala | 10 ++-- 3 files changed, 61 insertions(+), 42 deletions(-) create mode 100644 src/actors-migration/scala/actors/migration/ActorDSL.scala delete mode 100644 src/actors-migration/scala/actors/migration/MigrationSystem.scala (limited to 'src/actors-migration') 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; do not make the generated object accessible to code + * outside and do not return the same object upon subsequent invocations. + */ + 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 d0a1432e72..c628035891 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)) } } -- cgit v1.2.3