Removing the intermediate InvocationTargetException and harmonizing creation of Actor instances

This commit is contained in:
Viktor Klang 2011-05-03 16:26:30 +02:00
parent d97e0c1672
commit c81748c95c
3 changed files with 37 additions and 33 deletions

View file

@ -19,11 +19,8 @@ import java.net.{InetAddress, UnknownHostException}
class AkkaException(message: String = "", cause: Throwable = null) extends RuntimeException(message, cause) with Serializable { class AkkaException(message: String = "", cause: Throwable = null) extends RuntimeException(message, cause) with Serializable {
val uuid = "%s_%s".format(AkkaException.hostname, newUuid) val uuid = "%s_%s".format(AkkaException.hostname, newUuid)
override lazy val toString = { override lazy val toString =
val name = getClass.getName "%s: %s\n[%s]\n%s".format(getClass.getName, message, uuid, stackTraceToString)
val trace = stackTraceToString
"%s: %s\n[%s]\n%s".format(name, message, uuid, trace)
}
def stackTraceToString = { def stackTraceToString = {
val trace = getStackTrace val trace = getStackTrace

View file

@ -14,6 +14,7 @@ import scala.reflect.BeanProperty
import akka.util. {ReflectiveAccess, Duration} import akka.util. {ReflectiveAccess, Duration}
import akka.remoteinterface.RemoteSupport import akka.remoteinterface.RemoteSupport
import akka.japi. {Creator, Procedure} import akka.japi. {Creator, Procedure}
import java.lang.reflect.InvocationTargetException
/** /**
* Life-cycle messages for the Actors * Life-cycle messages for the Actors
@ -162,12 +163,18 @@ object Actor extends ListenerManagement {
def actorOf(clazz: Class[_ <: Actor]): ActorRef = new LocalActorRef(() => { def actorOf(clazz: Class[_ <: Actor]): ActorRef = new LocalActorRef(() => {
import ReflectiveAccess.{ createInstance, noParams, noArgs } import ReflectiveAccess.{ createInstance, noParams, noArgs }
createInstance[Actor](clazz.asInstanceOf[Class[_]], noParams, noArgs) match { createInstance[Actor](clazz.asInstanceOf[Class[_]], noParams, noArgs) match {
case r: Right[Exception, Actor] => r.b case Right(actor) => actor
case l: Left[Exception, Actor] => throw new ActorInitializationException( case Left(exception) =>
"Could not instantiate Actor of " + clazz + val cause = exception match {
"\nMake sure Actor is NOT defined inside a class/trait," + case i: InvocationTargetException => i.getTargetException
"\nif so put it outside the class/trait, f.e. in a companion object," + case _ => exception
"\nOR try to change: 'actorOf[MyActor]' to 'actorOf(new MyActor)'.", l.a) }
throw new ActorInitializationException(
"Could not instantiate Actor of " + clazz +
"\nMake sure Actor is NOT defined inside a class/trait," +
"\nif so put it outside the class/trait, f.e. in a companion object," +
"\nOR try to change: 'actorOf[MyActor]' to 'actorOf(new MyActor)'.", cause)
} }
}, None) }, None)

View file

@ -15,6 +15,7 @@ import scala.reflect.BeanProperty
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.io.{PrintWriter, PrintStream} import java.io.{PrintWriter, PrintStream}
import java.lang.reflect.InvocationTargetException
trait RemoteModule { trait RemoteModule {
val UUID_PREFIX = "uuid:".intern val UUID_PREFIX = "uuid:".intern
@ -186,18 +187,8 @@ abstract class RemoteSupport extends ListenerManagement with RemoteServerModule
* </pre> * </pre>
*/ */
@deprecated("Will be removed after 1.1") @deprecated("Will be removed after 1.1")
def actorOf(clazz: Class[_ <: Actor], host: String, port: Int): ActorRef = { def actorOf(clazz: Class[_ <: Actor], host: String, port: Int): ActorRef =
import ReflectiveAccess.{ createInstance, noParams, noArgs } clientManagedActorOf(() => createActorFromClass(clazz), host, port)
clientManagedActorOf(() =>
createInstance[Actor](clazz.asInstanceOf[Class[_]], noParams, noArgs) match {
case r: Right[_, Actor] => r.b
case l: Left[Exception, _] => throw new ActorInitializationException(
"Could not instantiate Actor" +
"\nMake sure Actor is NOT defined inside a class/trait," +
"\nif so put it outside the class/trait, f.e. in a companion object," +
"\nOR try to change: 'actorOf[MyActor]' to 'actorOf(new MyActor)'.", l.a)
}, host, port)
}
/** /**
* Creates a Client-managed ActorRef out of the Actor of the specified Class. * Creates a Client-managed ActorRef out of the Actor of the specified Class.
@ -215,17 +206,26 @@ abstract class RemoteSupport extends ListenerManagement with RemoteServerModule
* </pre> * </pre>
*/ */
@deprecated("Will be removed after 1.1") @deprecated("Will be removed after 1.1")
def actorOf[T <: Actor : Manifest](host: String, port: Int): ActorRef = { def actorOf[T <: Actor : Manifest](host: String, port: Int): ActorRef =
clientManagedActorOf(() => createActorFromClass(manifest.erasure), host, port)
protected def createActorFromClass(clazz: Class[_]): Actor = {
import ReflectiveAccess.{ createInstance, noParams, noArgs } import ReflectiveAccess.{ createInstance, noParams, noArgs }
clientManagedActorOf(() => createInstance[Actor](clazz, noParams, noArgs) match {
createInstance[Actor](manifest[T].erasure.asInstanceOf[Class[_]], noParams, noArgs) match { case Right(actor) => actor
case r: Right[_, Actor] => r.b case Left(exception) =>
case l: Left[Exception, _] => throw new ActorInitializationException( val cause = exception match {
"Could not instantiate Actor" + case i: InvocationTargetException => i.getTargetException
"\nMake sure Actor is NOT defined inside a class/trait," + case _ => exception
"\nif so put it outside the class/trait, f.e. in a companion object," + }
"\nOR try to change: 'actorOf[MyActor]' to 'actorOf(new MyActor)'.", l.a)
}, host, port) throw new ActorInitializationException(
"Could not instantiate Actor of " + clazz +
"\nMake sure Actor is NOT defined inside a class/trait," +
"\nif so put it outside the class/trait, f.e. in a companion object," +
"\nOR try to change: 'actorOf[MyActor]' to 'actorOf(new MyActor)'.", cause)
}
} }
protected override def manageLifeCycleOfListeners = false protected override def manageLifeCycleOfListeners = false