2011-06-07 06:36:21 +05:30
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-06-22 09:59:00 +02:00
|
|
|
package akka.serialization
|
|
|
|
|
|
2011-06-07 06:36:21 +05:30
|
|
|
import akka.util.ReflectiveAccess._
|
|
|
|
|
import akka.config.Config
|
|
|
|
|
import akka.config.Config._
|
|
|
|
|
import akka.actor.{ ActorRef, Actor }
|
2011-06-22 09:59:00 +02:00
|
|
|
import akka.AkkaException
|
2011-06-07 06:36:21 +05:30
|
|
|
|
2011-06-22 09:59:00 +02:00
|
|
|
/**
|
|
|
|
|
* Serialization module. Contains methods for serialization and deserialization as well as
|
|
|
|
|
* locating a Serializer for a particular class as defined in the mapping in the 'akka.conf' file.
|
|
|
|
|
*/
|
2011-06-07 06:36:21 +05:30
|
|
|
object Serialization {
|
2011-06-22 09:59:00 +02:00
|
|
|
case class NoSerializerFoundException(m: String) extends AkkaException(m)
|
2011-06-07 06:36:21 +05:30
|
|
|
|
2011-06-29 21:25:17 +02:00
|
|
|
def serialize(o: AnyRef): Either[Exception, Array[Byte]] = serializerFor(o.getClass) match {
|
|
|
|
|
case Left(ex) ⇒ Left(ex)
|
|
|
|
|
case Right(serializer) ⇒ Right(serializer.toBinary(o))
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
|
2011-06-14 19:35:18 +02:00
|
|
|
def deserialize(
|
|
|
|
|
bytes: Array[Byte],
|
|
|
|
|
clazz: Class[_],
|
|
|
|
|
classLoader: Option[ClassLoader]): Either[Exception, AnyRef] =
|
2011-06-29 21:25:17 +02:00
|
|
|
serializerFor(clazz) match {
|
|
|
|
|
case Left(ex) ⇒ Left(ex)
|
|
|
|
|
case Right(serializer) ⇒ Right(serializer.fromBinary(bytes, Some(clazz), classLoader))
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
|
2011-06-22 09:59:00 +02:00
|
|
|
def serializerFor(clazz: Class[_]): Either[Exception, Serializer] = {
|
2011-06-29 21:25:17 +02:00
|
|
|
serializerMap.get(clazz.getName) match {
|
2011-06-07 06:36:21 +05:30
|
|
|
case Some(serializerName: String) ⇒
|
|
|
|
|
getClassFor(serializerName) match {
|
2011-06-14 19:35:18 +02:00
|
|
|
case Right(serializer) ⇒ Right(serializer.newInstance.asInstanceOf[Serializer])
|
|
|
|
|
case Left(exception) ⇒ Left(exception)
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
case _ ⇒
|
2011-06-22 09:59:00 +02:00
|
|
|
defaultSerializer match {
|
2011-06-07 06:36:21 +05:30
|
|
|
case Some(s: Serializer) ⇒ Right(s)
|
2011-06-15 17:14:11 +02:00
|
|
|
case None ⇒ Left(NoSerializerFoundException("No default serializer found for " + clazz))
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-29 21:25:17 +02:00
|
|
|
private def defaultSerializer = serializers.get("default") match {
|
|
|
|
|
case Some(ser: String) ⇒
|
|
|
|
|
getClassFor(ser) match {
|
|
|
|
|
case Right(serializer) ⇒ Some(serializer.newInstance.asInstanceOf[Serializer])
|
|
|
|
|
case Left(exception) ⇒ None
|
|
|
|
|
}
|
|
|
|
|
case None ⇒ None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def getSerializerInstanceForBestMatchClass(cl: Class[_]) = bindings match {
|
|
|
|
|
case Some(mappings) ⇒ mappings find {
|
|
|
|
|
case (clazzName, ser) ⇒
|
|
|
|
|
getClassFor(clazzName) match {
|
|
|
|
|
case Right(clazz) ⇒ clazz.isAssignableFrom(cl)
|
|
|
|
|
case _ ⇒ false
|
|
|
|
|
}
|
|
|
|
|
} map {
|
|
|
|
|
case (_, ser) ⇒
|
2011-06-07 06:36:21 +05:30
|
|
|
getClassFor(ser) match {
|
2011-06-29 21:25:17 +02:00
|
|
|
case Right(s) ⇒ Right(s.newInstance.asInstanceOf[Serializer])
|
|
|
|
|
case _ ⇒ Left(new Exception("Error instantiating " + ser))
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
2011-06-29 21:25:17 +02:00
|
|
|
} getOrElse Left(NoSerializerFoundException("No mapping serializer found for " + cl))
|
|
|
|
|
case None ⇒ Left(NoSerializerFoundException("No mapping serializer found for " + cl))
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
|
2011-06-29 21:25:17 +02:00
|
|
|
//TODO: Add type and docs
|
|
|
|
|
val serializers = config.getSection("akka.actor.serializers").map(_.map).getOrElse(Map("default" -> "akka.serialization.JavaSerializer"))
|
|
|
|
|
|
|
|
|
|
//TODO: Add type and docs
|
|
|
|
|
val bindings = config.getSection("akka.actor.serialization-bindings")
|
|
|
|
|
.map(_.map)
|
|
|
|
|
.map(m ⇒ Map() ++ m.map { case (k, v: List[String]) ⇒ Map() ++ v.map((_, k)) }.flatten)
|
|
|
|
|
|
|
|
|
|
//TODO: Add type and docs
|
|
|
|
|
val serializerMap = bindings.map(m ⇒ m.map { case (k, v: String) ⇒ (k, serializers(v)) }).getOrElse(Map())
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|