2011-06-07 06:36:21 +05:30
|
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
|
* Copyright (C) 2009-2013 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-11-29 11:50:22 +01:00
|
|
|
|
import com.typesafe.config.Config
|
2013-03-25 08:42:48 +01:00
|
|
|
|
import akka.actor._
|
2012-02-10 12:45:22 +01:00
|
|
|
|
import akka.event.Logging
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap
|
2012-02-06 21:12:26 +01:00
|
|
|
|
import scala.collection.mutable.ArrayBuffer
|
|
|
|
|
|
import java.io.NotSerializableException
|
2012-10-30 15:08:41 +01:00
|
|
|
|
import scala.util.{ Try, DynamicVariable }
|
|
|
|
|
|
import scala.collection.immutable
|
2011-06-07 06:36:21 +05:30
|
|
|
|
|
2011-11-24 18:53:18 +01:00
|
|
|
|
object Serialization {
|
2012-02-06 21:12:26 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Tuple that represents mapping from Class to Serializer
|
|
|
|
|
|
*/
|
|
|
|
|
|
type ClassSerializer = (Class[_], Serializer)
|
|
|
|
|
|
|
2012-01-27 12:14:28 +01:00
|
|
|
|
/**
|
2013-03-25 08:42:48 +01:00
|
|
|
|
* This holds a reference to the current transport serialization information used for
|
|
|
|
|
|
* serializing local actor refs.
|
2012-01-27 12:14:28 +01:00
|
|
|
|
*/
|
2013-03-25 08:42:48 +01:00
|
|
|
|
val currentTransportInformation = new DynamicVariable[SerializationInformation](null)
|
2012-01-27 12:14:28 +01:00
|
|
|
|
|
2011-11-29 11:50:22 +01:00
|
|
|
|
class Settings(val config: Config) {
|
2012-11-07 16:35:14 +01:00
|
|
|
|
val Serializers: Map[String, String] = configToMap("akka.actor.serializers")
|
|
|
|
|
|
val SerializationBindings: Map[String, String] = configToMap("akka.actor.serialization-bindings")
|
2011-11-24 18:53:18 +01:00
|
|
|
|
|
2012-11-07 16:35:14 +01:00
|
|
|
|
private final def configToMap(path: String): Map[String, String] = {
|
|
|
|
|
|
import scala.collection.JavaConverters._
|
2012-11-15 12:48:13 +01:00
|
|
|
|
config.getConfig(path).root.unwrapped.asScala.toMap map { case (k, v) ⇒ (k -> v.toString) }
|
2012-11-07 16:35:14 +01:00
|
|
|
|
}
|
2011-11-24 18:53:18 +01:00
|
|
|
|
}
|
2013-03-25 08:42:48 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The serialized path of an actorRef, based on the current transport serialization information.
|
|
|
|
|
|
*/
|
|
|
|
|
|
def serializedActorPath(actorRef: ActorRef): String = {
|
|
|
|
|
|
val path = actorRef.path
|
|
|
|
|
|
val originalSystem: ActorSystemImpl = actorRef match {
|
|
|
|
|
|
case a: ActorRefWithCell ⇒ a.underlying.systemImpl
|
|
|
|
|
|
case _ ⇒ null
|
|
|
|
|
|
}
|
|
|
|
|
|
Serialization.currentTransportInformation.value match {
|
|
|
|
|
|
case null ⇒ path.toSerializationFormat
|
|
|
|
|
|
case SerializationInformation(address, system) ⇒
|
|
|
|
|
|
if (originalSystem == null || originalSystem == system)
|
|
|
|
|
|
path.toSerializationFormatWithAddress(address)
|
|
|
|
|
|
else {
|
|
|
|
|
|
val provider = originalSystem.provider
|
|
|
|
|
|
path.toSerializationFormatWithAddress(provider.getExternalAddressFor(address).getOrElse(provider.getDefaultAddress))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-11-24 18:53:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-03-25 08:42:48 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Serialization information needed for serializing local actor refs.
|
|
|
|
|
|
*/
|
|
|
|
|
|
case class SerializationInformation(val address: Address, val system: ActorSystem)
|
|
|
|
|
|
|
2011-06-22 09:59:00 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Serialization module. Contains methods for serialization and deserialization as well as
|
2012-02-06 21:12:26 +01:00
|
|
|
|
* locating a Serializer for a particular class as defined in the mapping in the configuration.
|
2011-06-22 09:59:00 +02:00
|
|
|
|
*/
|
2012-01-24 11:33:40 +01:00
|
|
|
|
class Serialization(val system: ExtendedActorSystem) extends Extension {
|
2011-11-24 18:53:18 +01:00
|
|
|
|
import Serialization._
|
|
|
|
|
|
|
2011-11-29 11:50:22 +01:00
|
|
|
|
val settings = new Settings(system.settings.config)
|
2012-02-03 17:32:32 +01:00
|
|
|
|
val log = Logging(system, getClass.getName)
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Serializes the given AnyRef/java.lang.Object according to the Serialization configuration
|
|
|
|
|
|
* to either an Array of Bytes or an Exception if one was thrown.
|
|
|
|
|
|
*/
|
2012-09-06 03:17:51 +02:00
|
|
|
|
def serialize(o: AnyRef): Try[Array[Byte]] = Try(findSerializerFor(o).toBinary(o))
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Deserializes the given array of bytes using the specified serializer id,
|
|
|
|
|
|
* using the optional type hint to the Serializer and the optional ClassLoader ot load it into.
|
|
|
|
|
|
* Returns either the resulting object or an Exception if one was thrown.
|
|
|
|
|
|
*/
|
2012-12-14 18:25:04 +01:00
|
|
|
|
def deserialize[T](bytes: Array[Byte], serializerId: Int, clazz: Option[Class[_ <: T]]): Try[T] =
|
|
|
|
|
|
Try(serializerByIdentity(serializerId).fromBinary(bytes, clazz).asInstanceOf[T])
|
2011-12-29 16:11:56 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Deserializes the given array of bytes using the specified type to look up what Serializer should be used.
|
|
|
|
|
|
* You can specify an optional ClassLoader to load the object into.
|
|
|
|
|
|
* Returns either the resulting object or an Exception if one was thrown.
|
|
|
|
|
|
*/
|
2012-12-14 18:25:04 +01:00
|
|
|
|
def deserialize[T](bytes: Array[Byte], clazz: Class[T]): Try[T] =
|
|
|
|
|
|
Try(serializerFor(clazz).fromBinary(bytes, Some(clazz)).asInstanceOf[T])
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
/**
|
2012-02-06 21:12:26 +01:00
|
|
|
|
* Returns the Serializer configured for the given object, returns the NullSerializer if it's null.
|
|
|
|
|
|
*
|
2012-05-16 17:04:13 +02:00
|
|
|
|
* @throws akka.ConfigurationException if no `serialization-bindings` is configured for the
|
2012-02-06 21:12:26 +01:00
|
|
|
|
* class of the object
|
2011-12-29 16:11:56 +01:00
|
|
|
|
*/
|
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-12-29 16:11:56 +01:00
|
|
|
|
/**
|
2012-02-06 21:12:26 +01:00
|
|
|
|
* Returns the configured Serializer for the given Class. The configured Serializer
|
|
|
|
|
|
* is used if the configured class `isAssignableFrom` from the `clazz`, i.e.
|
|
|
|
|
|
* the configured class is a super class or implemented interface. In case of
|
|
|
|
|
|
* ambiguity it is primarily using the most specific configured class,
|
|
|
|
|
|
* and secondly the entry configured first.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @throws java.io.NotSerializableException if no `serialization-bindings` is configured for the class
|
2011-12-29 16:11:56 +01:00
|
|
|
|
*/
|
2012-02-04 17:35:39 +01:00
|
|
|
|
def serializerFor(clazz: Class[_]): Serializer =
|
2012-02-06 21:12:26 +01:00
|
|
|
|
serializerMap.get(clazz) match {
|
2012-11-07 16:35:14 +01:00
|
|
|
|
case null ⇒ // bindings are ordered from most specific to least specific
|
2012-10-30 15:08:41 +01:00
|
|
|
|
def unique(possibilities: immutable.Seq[(Class[_], Serializer)]): Boolean =
|
2012-02-07 16:21:48 +01:00
|
|
|
|
possibilities.size == 1 ||
|
2012-09-18 23:57:25 +02:00
|
|
|
|
(possibilities forall (_._1 isAssignableFrom possibilities(0)._1)) ||
|
|
|
|
|
|
(possibilities forall (_._2 == possibilities(0)._2))
|
2012-02-07 15:11:16 +01:00
|
|
|
|
|
2012-02-07 16:21:48 +01:00
|
|
|
|
val ser = bindings filter { _._1 isAssignableFrom clazz } match {
|
|
|
|
|
|
case Seq() ⇒
|
2012-02-07 15:11:16 +01:00
|
|
|
|
throw new NotSerializableException("No configured serialization-bindings for class [%s]" format clazz.getName)
|
2012-02-07 16:21:48 +01:00
|
|
|
|
case possibilities ⇒
|
|
|
|
|
|
if (!unique(possibilities))
|
|
|
|
|
|
log.warning("Multiple serializers found for " + clazz + ", choosing first: " + possibilities)
|
|
|
|
|
|
possibilities(0)._2
|
2012-02-07 15:51:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
serializerMap.putIfAbsent(clazz, ser) match {
|
2012-05-18 19:25:43 +02:00
|
|
|
|
case null ⇒ log.debug("Using serializer[{}] for message [{}]", ser.getClass.getName, clazz.getName); ser
|
2012-02-07 15:51:41 +01:00
|
|
|
|
case some ⇒ some
|
2012-02-04 17:35:39 +01:00
|
|
|
|
}
|
2012-02-06 21:12:26 +01:00
|
|
|
|
case ser ⇒ ser
|
2012-02-03 17:32:32 +01:00
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
|
|
2011-07-15 16:21:45 +02:00
|
|
|
|
/**
|
2012-02-09 19:26:02 +01:00
|
|
|
|
* Tries to load the specified Serializer by the fully-qualified name; the actual
|
2012-02-10 11:36:23 +01:00
|
|
|
|
* loading is performed by the system’s [[akka.actor.DynamicAccess]].
|
2011-07-15 16:21:45 +02:00
|
|
|
|
*/
|
2012-09-06 03:17:51 +02:00
|
|
|
|
def serializerOf(serializerFQN: String): Try[Serializer] =
|
2012-10-30 15:08:41 +01:00
|
|
|
|
system.dynamicAccess.createInstanceFor[Serializer](serializerFQN, List(classOf[ExtendedActorSystem] -> system)) recoverWith {
|
|
|
|
|
|
case _ ⇒ system.dynamicAccess.createInstanceFor[Serializer](serializerFQN, Nil)
|
2012-09-06 03:17:51 +02:00
|
|
|
|
}
|
2011-07-15 16:21:45 +02:00
|
|
|
|
|
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)
|
2012-02-06 21:12:26 +01:00
|
|
|
|
* By default always contains the following mapping: "java" -> akka.serialization.JavaSerializer
|
2011-07-14 11:25:40 +02:00
|
|
|
|
*/
|
2012-05-18 19:25:43 +02:00
|
|
|
|
private val serializers: Map[String, Serializer] =
|
2012-09-06 03:17:51 +02:00
|
|
|
|
for ((k: String, v: String) ← settings.Serializers) yield k -> serializerOf(v).get
|
2011-06-29 21:25:17 +02:00
|
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
|
/**
|
2012-02-06 21:12:26 +01:00
|
|
|
|
* bindings is a Seq of tuple representing the mapping from Class to Serializer.
|
|
|
|
|
|
* It is primarily ordered by the most specific classes first, and secondly in the configured order.
|
2011-07-14 11:25:40 +02:00
|
|
|
|
*/
|
2012-10-30 15:08:41 +01:00
|
|
|
|
private[akka] val bindings: immutable.Seq[ClassSerializer] =
|
|
|
|
|
|
sort(for ((k: String, v: String) ← settings.SerializationBindings if v != "none") yield (system.dynamicAccess.getClassFor[Any](k).get, serializers(v))).to[immutable.Seq]
|
2011-06-29 21:25:17 +02:00
|
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
|
/**
|
2012-02-06 21:12:26 +01:00
|
|
|
|
* Sort so that subtypes always precede their supertypes, but without
|
|
|
|
|
|
* obeying any order between unrelated subtypes (insert sort).
|
2011-07-14 11:25:40 +02:00
|
|
|
|
*/
|
2012-10-30 15:08:41 +01:00
|
|
|
|
private def sort(in: Iterable[ClassSerializer]): immutable.Seq[ClassSerializer] =
|
|
|
|
|
|
((new ArrayBuffer[ClassSerializer](in.size) /: in) { (buf, ca) ⇒
|
2012-02-06 21:12:26 +01:00
|
|
|
|
buf.indexWhere(_._1 isAssignableFrom ca._1) match {
|
|
|
|
|
|
case -1 ⇒ buf append ca
|
|
|
|
|
|
case x ⇒ buf insert (x, ca)
|
|
|
|
|
|
}
|
|
|
|
|
|
buf
|
2012-10-30 15:08:41 +01:00
|
|
|
|
}).to[immutable.Seq]
|
2012-02-06 21:12:26 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* serializerMap is a Map whose keys is the class that is serializable and values is the serializer
|
|
|
|
|
|
* to be used for that class.
|
|
|
|
|
|
*/
|
2012-06-25 17:09:00 +02:00
|
|
|
|
private val serializerMap: ConcurrentHashMap[Class[_], Serializer] =
|
|
|
|
|
|
(new ConcurrentHashMap[Class[_], Serializer] /: bindings) { case (map, (c, s)) ⇒ map.put(c, s); map }
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-30 22:00:49 +01:00
|
|
|
|
* Maps from a Serializer Identity (Int) to a Serializer instance (optimization)
|
2011-07-19 14:20:18 +02:00
|
|
|
|
*/
|
2012-02-06 21:12:26 +01:00
|
|
|
|
val serializerByIdentity: Map[Int, 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
|
|
|
|
}
|
2011-10-06 21:19:46 +02:00
|
|
|
|
|