2011-06-07 06:36:21 +05:30
|
|
|
package akka.serialization
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import akka.util.ReflectiveAccess._
|
|
|
|
|
import akka.config.Config
|
|
|
|
|
import akka.config.Config._
|
|
|
|
|
import akka.actor.{ ActorRef, Actor }
|
|
|
|
|
|
|
|
|
|
object Serialization {
|
|
|
|
|
case class NoSerializerFoundException(m: String) extends Exception(m)
|
|
|
|
|
|
|
|
|
|
def serialize(o: AnyRef): Either[Exception, Array[Byte]] =
|
2011-06-14 19:35:18 +02:00
|
|
|
getSerializer(o.getClass).fold((ex) ⇒ Left(ex), (ser) ⇒ Right(ser.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-07 06:36:21 +05:30
|
|
|
getSerializer(clazz)
|
|
|
|
|
.fold((ex) ⇒ Left(ex),
|
|
|
|
|
(ser) ⇒ Right(ser.fromBinary(bytes, Some(clazz), classLoader)))
|
|
|
|
|
|
|
|
|
|
def getSerializer(clazz: Class[_]): Either[Exception, Serializer] = {
|
|
|
|
|
Config.serializerMap.get(clazz.getName) match {
|
|
|
|
|
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 _ ⇒
|
|
|
|
|
getDefaultSerializer match {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def getDefaultSerializer = {
|
|
|
|
|
Config.serializers.get("default") match {
|
|
|
|
|
case Some(ser: String) ⇒
|
|
|
|
|
getClassFor(ser) match {
|
2011-06-14 19:35:18 +02:00
|
|
|
case Right(srializer) ⇒ Some(srializer.newInstance.asInstanceOf[Serializer])
|
|
|
|
|
case Left(exception) ⇒ None
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
case None ⇒ None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-14 19:35:18 +02:00
|
|
|
private def getSerializerInstanceForBestMatchClass(
|
|
|
|
|
configMap: collection.mutable.Map[String, String],
|
|
|
|
|
cl: Class[_]) = {
|
2011-06-07 06:36:21 +05:30
|
|
|
configMap
|
|
|
|
|
.find {
|
|
|
|
|
case (clazzName, ser) ⇒
|
|
|
|
|
getClassFor(clazzName) match {
|
2011-06-14 19:35:18 +02:00
|
|
|
case Right(clazz) ⇒ clazz.isAssignableFrom(cl)
|
|
|
|
|
case _ ⇒ false
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.map {
|
|
|
|
|
case (_, ser) ⇒
|
|
|
|
|
getClassFor(ser) match {
|
2011-06-14 19:35:18 +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
|
|
|
}
|
|
|
|
|
}.getOrElse(Left(NoSerializerFoundException("No mapping serializer found for " + cl)))
|
|
|
|
|
}
|
|
|
|
|
}
|