2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.aquarium
39 import java.lang.reflect.InvocationTargetException
40 import com.ckkloverdos.maybe.{Failed, Just, MaybeEither}
41 import akka.actor.Actor
42 import gr.grnet.aquarium.util.{Loggable, simpleNameOfClass}
45 * An actor who dispatches to particular methods based on the type of the received message.
47 * Do not overload message class names in different packages or else it will not recognize them!
49 * @author Christos KK Loverdos <loverdos@gmail.com>.
51 trait ReflectiveActor extends Actor with Loggable {
52 private val messageMethodMap: Map[Class[_], java.lang.reflect.Method] = {
53 val classMethodPairs = for(knownMessageClass <- knownMessageTypes) yield {
54 require(knownMessageClass ne null, "Null in knownMessageTypes of %s".format(this.getClass))
56 val methodName = "on%s".format(simpleNameOfClass(knownMessageClass))
57 // For each class MethodClass we expect a method with the following signature:
58 // def onMethodClass(message: MethodClass): Unit
59 MaybeEither(this.getClass.getMethod(methodName, knownMessageClass)) match {
61 method.setAccessible(true)
62 (knownMessageClass, method)
65 val errMsg = "Reflective actor %s does not know how to process message %s".format(this.getClass, knownMessageClass)
66 logger.error(errMsg, e)
67 throw new AquariumException(errMsg, e)
71 Map(classMethodPairs.toSeq: _*)
75 * Handles an exception that occurred while servicing a message.
79 * @param servicingMessage
80 * The message that was being served while the exception happened.
81 * Note that the message can be `null`, in which case the exception
84 protected def onThrowable(t: Throwable, servicingMessage: AnyRef): Unit = {
88 def knownMessageTypes: Set[Class[_]]// = role.knownMessageTypes
90 final protected def receive: Receive = {
92 onThrowable(new NullPointerException("Received null message"), null)
94 case message: AnyRef if messageMethodMap.contains(message.getClass) ⇒
95 try messageMethodMap(message.getClass).invoke(this, message)
97 case e: InvocationTargetException ⇒
98 onThrowable(e.getTargetException, message)
101 onThrowable(e, message)