WIP: IMEventModel end-to-end chain
[aquarium] / src / main / scala / gr / grnet / aquarium / actor / ReflectiveActor.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35
36 package gr.grnet.aquarium
37 package actor
38
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}
43
44 /**
45  * An actor who dispatches to particular methods based on the type of the received message.
46  *
47  * Do not overload message class names in different packages or else it will not recognize them!
48  *
49  * @author Christos KK Loverdos <loverdos@gmail.com>.
50  */
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))
55
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 {
60         case Just(method) =>
61           method.setAccessible(true)
62           (knownMessageClass, method)
63
64         case Failed(e) =>
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)
68       }
69     }
70
71     Map(classMethodPairs.toSeq: _*)
72   }
73
74   /**
75    * Handles an exception that occurred while servicing a message.
76    *
77    * @param t
78    *          The exception.
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
82    *          is an NPE.
83    */
84   protected def onThrowable(t: Throwable, servicingMessage: AnyRef): Unit = {
85     throw t
86   }
87
88   def knownMessageTypes: Set[Class[_]]// = role.knownMessageTypes
89
90   final protected def receive: Receive = {
91     case null =>
92       onThrowable(new NullPointerException("Received null message"), null)
93
94     case message: AnyRef if messageMethodMap.contains(message.getClass) ⇒
95       try messageMethodMap(message.getClass).invoke(this, message)
96       catch {
97         case e: InvocationTargetException ⇒
98           onThrowable(e.getTargetException, message)
99
100         case e ⇒
101           onThrowable(e, message)
102       }
103   }
104 }