2011-06-07 06:36:21 +05:30
|
|
|
/**
|
2011-07-14 16:03:08 +02:00
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
2011-06-07 06:36:21 +05:30
|
|
|
*/
|
|
|
|
|
|
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._
|
2011-06-22 09:59:00 +02:00
|
|
|
import akka.AkkaException
|
2011-07-15 16:21:45 +02:00
|
|
|
import akka.util.ReflectiveAccess
|
2011-06-07 06:36:21 +05:30
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
case class NoSerializerFoundException(m: String) extends AkkaException(m)
|
|
|
|
|
|
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-07-19 14:20:18 +02:00
|
|
|
|
2011-07-15 12:38:05 +02:00
|
|
|
//TODO document me
|
2011-07-19 14:20:18 +02:00
|
|
|
def serialize(o: AnyRef): Either[Exception, Array[Byte]] =
|
2011-07-26 18:33:59 +12:00
|
|
|
try { Right(findSerializerFor(o).toBinary(o)) } catch { case e: Exception ⇒ Left(e) }
|
2011-07-19 14:20:18 +02:00
|
|
|
|
2011-07-15 12:38:05 +02:00
|
|
|
//TODO document me
|
2011-06-14 19:35:18 +02:00
|
|
|
def deserialize(
|
|
|
|
|
bytes: Array[Byte],
|
|
|
|
|
clazz: Class[_],
|
|
|
|
|
classLoader: Option[ClassLoader]): Either[Exception, AnyRef] =
|
2011-07-26 18:33:59 +12:00
|
|
|
try { Right(serializerFor(clazz).fromBinary(bytes, Some(clazz), classLoader)) } catch { case e: Exception ⇒ Left(e) }
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
|
|
|
def findSerializerFor(o: AnyRef): Serializer = o match {
|
2011-07-26 18:33:59 +12:00
|
|
|
case null ⇒ NullSerializer
|
|
|
|
|
case other ⇒ serializerFor(other.getClass)
|
2011-07-19 14:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
2011-07-15 12:38:05 +02:00
|
|
|
//TODO document me
|
2011-07-19 14:20:18 +02:00
|
|
|
def serializerFor(clazz: Class[_]): Serializer = //TODO fall back on BestMatchClass THEN default AND memoize the lookups
|
|
|
|
|
serializerMap.get(clazz.getName).getOrElse(serializers("default"))
|
2011-06-07 06:36:21 +05:30
|
|
|
|
2011-07-15 16:21:45 +02:00
|
|
|
/**
|
|
|
|
|
* Tries to load the specified Serializer by the FQN
|
|
|
|
|
*/
|
|
|
|
|
def serializerOf(serializerFQN: String): Either[Exception, Serializer] =
|
|
|
|
|
createInstance(serializerFQN, ReflectiveAccess.emptyParams, ReflectiveAccess.emptyArguments)
|
|
|
|
|
|
2011-07-15 12:38:05 +02:00
|
|
|
private def serializerForBestMatchClass(cl: Class[_]): Either[Exception, Serializer] = {
|
2011-07-14 11:25:40 +02:00
|
|
|
if (bindings.isEmpty)
|
|
|
|
|
Left(NoSerializerFoundException("No mapping serializer found for " + cl))
|
|
|
|
|
else {
|
2011-07-26 18:33:59 +12:00
|
|
|
bindings find {
|
|
|
|
|
case (clazzName, _) ⇒
|
|
|
|
|
getClassFor(clazzName) match {
|
|
|
|
|
case Right(clazz) ⇒ clazz.isAssignableFrom(cl)
|
|
|
|
|
case _ ⇒ false
|
|
|
|
|
}
|
2011-07-14 11:25:40 +02:00
|
|
|
} map {
|
2011-07-15 16:21:45 +02:00
|
|
|
case (_, ser) ⇒ serializerOf(ser)
|
2011-07-14 11:25:40 +02:00
|
|
|
} getOrElse Left(NoSerializerFoundException("No mapping serializer found for " + cl))
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
/**
|
2011-07-19 14:20:18 +02:00
|
|
|
* A Map of serializer from alias to implementation (class implementing akka.serialization.Serializer)
|
|
|
|
|
* By default always contains the following mapping: "default" -> akka.serialization.JavaSerializer
|
2011-07-14 11:25:40 +02:00
|
|
|
* But "default" can be overridden in config
|
|
|
|
|
*/
|
2011-07-19 14:20:18 +02:00
|
|
|
val serializers: Map[String, Serializer] =
|
|
|
|
|
config.getSection("akka.actor.serializers")
|
2011-07-26 18:33:59 +12:00
|
|
|
.map(_.map)
|
|
|
|
|
.getOrElse(Map())
|
|
|
|
|
.foldLeft(Map[String, Serializer]("default" -> akka.serialization.JavaSerializer)) {
|
|
|
|
|
case (result, (k: String, v: String)) ⇒ result + (k -> serializerOf(v).fold(throw _, identity))
|
|
|
|
|
case (result, _) ⇒ result
|
|
|
|
|
}
|
2011-06-29 21:25:17 +02:00
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
/**
|
|
|
|
|
* bindings is a Map whose keys = FQN of class that is serializable and values = the alias of the serializer to be used
|
|
|
|
|
*/
|
|
|
|
|
val bindings: Map[String, String] = config.getSection("akka.actor.serialization-bindings") map {
|
2011-07-26 18:33:59 +12:00
|
|
|
_.map.foldLeft(Map[String, String]()) {
|
|
|
|
|
case (result, (k: String, vs: List[_])) ⇒ result ++ (vs collect { case v: String ⇒ (v, k) }) //All keys which are lists, take the Strings from them and Map them
|
|
|
|
|
case (result, _) ⇒ result //For any other values, just skip them, TODO: print out warnings?
|
2011-07-14 11:25:40 +02:00
|
|
|
}
|
|
|
|
|
} getOrElse Map()
|
2011-06-29 21:25:17 +02:00
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
/**
|
|
|
|
|
* serializerMap is a Map whose keys = FQN of class that is serializable and values = the FQN of the serializer to be used for that class
|
|
|
|
|
*/
|
2011-07-19 14:20:18 +02:00
|
|
|
val serializerMap: Map[String, Serializer] = bindings mapValues serializers
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maps from a Serializer.Identifier (Byte) to a Serializer instance (optimization)
|
|
|
|
|
*/
|
|
|
|
|
val serializerByIdentity: Map[Serializer.Identifier, Serializer] =
|
2011-07-26 18:33:59 +12:00
|
|
|
Map(NullSerializer.identifier -> NullSerializer) ++ serializers map { case (_, v) ⇒ (v.identifier, v) }
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|