2011-06-07 06:36:21 +05:30
|
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.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
|
2013-07-03 11:26:50 +02:00
|
|
|
|
import scala.util.{ Try, DynamicVariable, Failure }
|
2012-10-30 15:08:41 +01:00
|
|
|
|
import scala.collection.immutable
|
2013-06-02 14:53:31 +02:00
|
|
|
|
import scala.util.control.NonFatal
|
2015-05-28 18:42:22 +02:00
|
|
|
|
import scala.util.Success
|
2016-05-26 11:58:13 +02:00
|
|
|
|
import java.nio.ByteBuffer
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
|
|
|
|
|
import scala.annotation.tailrec
|
2016-06-04 21:44:13 +02:00
|
|
|
|
import java.util.NoSuchElementException
|
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.
|
2013-03-26 11:25:09 +01:00
|
|
|
|
* INTERNAL API
|
2012-01-27 12:14:28 +01:00
|
|
|
|
*/
|
2013-03-26 11:25:09 +01:00
|
|
|
|
private[akka] val currentTransportInformation = new DynamicVariable[Information](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._
|
2016-06-02 14:06:57 +02: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
|
|
|
|
|
2013-03-26 11:25:09 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Serialization information needed for serializing local actor refs.
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
2014-03-07 13:20:01 +01:00
|
|
|
|
private[akka] final case class Information(address: Address, system: ActorSystem)
|
2013-03-26 11:25:09 +01:00
|
|
|
|
|
2013-03-25 08:42:48 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* The serialized path of an actorRef, based on the current transport serialization information.
|
2013-03-26 11:25:09 +01:00
|
|
|
|
* If there is no external address available for the requested address then the systems default
|
|
|
|
|
|
* address will be used.
|
2013-03-25 08:42:48 +01:00
|
|
|
|
*/
|
|
|
|
|
|
def serializedActorPath(actorRef: ActorRef): String = {
|
|
|
|
|
|
val path = actorRef.path
|
2013-03-26 11:25:09 +01:00
|
|
|
|
val originalSystem: ExtendedActorSystem = actorRef match {
|
|
|
|
|
|
case a: ActorRefWithCell ⇒ a.underlying.system.asInstanceOf[ExtendedActorSystem]
|
2013-03-25 08:42:48 +01:00
|
|
|
|
case _ ⇒ null
|
|
|
|
|
|
}
|
|
|
|
|
|
Serialization.currentTransportInformation.value match {
|
2013-06-02 14:53:31 +02:00
|
|
|
|
case null ⇒ originalSystem match {
|
|
|
|
|
|
case null ⇒ path.toSerializationFormat
|
|
|
|
|
|
case system ⇒
|
|
|
|
|
|
try path.toSerializationFormatWithAddress(system.provider.getDefaultAddress)
|
|
|
|
|
|
catch { case NonFatal(_) ⇒ path.toSerializationFormat }
|
|
|
|
|
|
}
|
2013-03-26 11:25:09 +01:00
|
|
|
|
case Information(address, system) ⇒
|
2013-03-25 08:42:48 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
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)
|
2016-05-26 11:58:13 +02:00
|
|
|
|
private val manifestCache = new AtomicReference[Map[String, Option[Class[_]]]](Map.empty[String, Option[Class[_]]])
|
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,
|
2015-05-28 18:42:22 +02:00
|
|
|
|
* using the optional type hint to the Serializer.
|
2011-12-29 16:11:56 +01:00
|
|
|
|
* 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] =
|
2013-07-03 11:26:50 +02:00
|
|
|
|
Try {
|
2016-06-04 21:44:13 +02:00
|
|
|
|
val serializer = try getSerializerById(serializerId) catch {
|
2013-07-03 11:26:50 +02:00
|
|
|
|
case _: NoSuchElementException ⇒ throw new NotSerializableException(
|
|
|
|
|
|
s"Cannot find serializer with id [$serializerId]. The most probable reason is that the configuration entry " +
|
|
|
|
|
|
"akka.actor.serializers is not in synch between the two systems.")
|
|
|
|
|
|
}
|
|
|
|
|
|
serializer.fromBinary(bytes, clazz).asInstanceOf[T]
|
|
|
|
|
|
}
|
2011-12-29 16:11:56 +01:00
|
|
|
|
|
2015-05-28 18:42:22 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Deserializes the given array of bytes using the specified serializer id,
|
|
|
|
|
|
* using the optional type hint to the Serializer.
|
|
|
|
|
|
* Returns either the resulting object or an Exception if one was thrown.
|
|
|
|
|
|
*/
|
|
|
|
|
|
def deserialize(bytes: Array[Byte], serializerId: Int, manifest: String): Try[AnyRef] =
|
|
|
|
|
|
Try {
|
2016-06-04 21:44:13 +02:00
|
|
|
|
val serializer = try getSerializerById(serializerId) catch {
|
2015-05-28 18:42:22 +02:00
|
|
|
|
case _: NoSuchElementException ⇒ throw new NotSerializableException(
|
|
|
|
|
|
s"Cannot find serializer with id [$serializerId]. The most probable reason is that the configuration entry " +
|
|
|
|
|
|
"akka.actor.serializers is not in synch between the two systems.")
|
|
|
|
|
|
}
|
2016-05-26 11:58:13 +02:00
|
|
|
|
deserializeByteArray(bytes, serializer, manifest)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private def deserializeByteArray(bytes: Array[Byte], serializer: Serializer, manifest: String): AnyRef = {
|
|
|
|
|
|
|
|
|
|
|
|
@tailrec def updateCache(cache: Map[String, Option[Class[_]]], key: String, value: Option[Class[_]]): Boolean = {
|
|
|
|
|
|
manifestCache.compareAndSet(cache, cache.updated(key, value)) ||
|
|
|
|
|
|
updateCache(manifestCache.get, key, value) // recursive, try again
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
serializer match {
|
|
|
|
|
|
case s2: SerializerWithStringManifest ⇒ s2.fromBinary(bytes, manifest)
|
|
|
|
|
|
case s1 ⇒
|
|
|
|
|
|
if (manifest == "")
|
|
|
|
|
|
s1.fromBinary(bytes, None)
|
|
|
|
|
|
else {
|
|
|
|
|
|
val cache = manifestCache.get
|
|
|
|
|
|
cache.get(manifest) match {
|
2016-06-03 11:59:00 +02:00
|
|
|
|
case Some(cachedClassManifest) ⇒ s1.fromBinary(bytes, cachedClassManifest)
|
|
|
|
|
|
case None ⇒
|
2016-05-20 10:33:55 +02:00
|
|
|
|
system.dynamicAccess.getClassFor[AnyRef](manifest) match {
|
|
|
|
|
|
case Success(classManifest) ⇒
|
2016-05-26 11:58:13 +02:00
|
|
|
|
val classManifestOption: Option[Class[_]] = Some(classManifest)
|
|
|
|
|
|
updateCache(cache, manifest, classManifestOption)
|
|
|
|
|
|
s1.fromBinary(bytes, classManifestOption)
|
2016-05-20 10:33:55 +02:00
|
|
|
|
case Failure(e) ⇒
|
|
|
|
|
|
throw new NotSerializableException(
|
2016-05-26 11:58:13 +02:00
|
|
|
|
s"Cannot find manifest class [$manifest] for serializer with id [${serializer.identifier}].")
|
2016-05-20 10:33:55 +02:00
|
|
|
|
}
|
2015-05-28 18:42:22 +02:00
|
|
|
|
}
|
2016-05-26 11:58:13 +02:00
|
|
|
|
}
|
2015-05-28 18:42:22 +02:00
|
|
|
|
}
|
2016-05-26 11:58:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Deserializes the given ByteBuffer of bytes using the specified serializer id,
|
|
|
|
|
|
* using the optional type hint to the Serializer.
|
|
|
|
|
|
* Returns either the resulting object or throws an exception if deserialization fails.
|
|
|
|
|
|
*/
|
|
|
|
|
|
def deserializeByteBuffer(buf: ByteBuffer, serializerId: Int, manifest: String): AnyRef = {
|
2016-06-04 21:44:13 +02:00
|
|
|
|
val serializer = try getSerializerById(serializerId) catch {
|
2016-05-26 11:58:13 +02:00
|
|
|
|
case _: NoSuchElementException ⇒ throw new NotSerializableException(
|
|
|
|
|
|
s"Cannot find serializer with id [$serializerId]. The most probable reason is that the configuration entry " +
|
|
|
|
|
|
"akka.actor.serializers is not in synch between the two systems.")
|
|
|
|
|
|
}
|
|
|
|
|
|
serializer match {
|
2016-06-03 11:59:00 +02:00
|
|
|
|
case ser: ByteBufferSerializer ⇒
|
2016-05-26 11:58:13 +02:00
|
|
|
|
ser.fromBinary(buf, manifest)
|
|
|
|
|
|
case _ ⇒
|
|
|
|
|
|
val bytes = Array.ofDim[Byte](buf.remaining())
|
|
|
|
|
|
buf.get(bytes)
|
|
|
|
|
|
deserializeByteArray(bytes, serializer, manifest)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-05-28 18:42:22 +02:00
|
|
|
|
|
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.
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
|
* Throws akka.ConfigurationException if no `serialization-bindings` is configured for the
|
|
|
|
|
|
* class of the object.
|
2011-12-29 16:11:56 +01:00
|
|
|
|
*/
|
2015-05-28 18:42:22 +02:00
|
|
|
|
def findSerializerFor(o: AnyRef): Serializer =
|
|
|
|
|
|
if (o eq null) NullSerializer else serializerFor(o.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.
|
|
|
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
|
* 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 {
|
2013-06-02 14:53:31 +02:00
|
|
|
|
case null ⇒
|
2015-09-24 16:45:51 +02:00
|
|
|
|
if (shouldWarnAboutJavaSerializer(clazz, ser)) {
|
|
|
|
|
|
log.warning("Using the default Java serializer for class [{}] which is not recommended because of " +
|
|
|
|
|
|
"performance implications. Use another serializer or disable this warning using the setting " +
|
|
|
|
|
|
"'akka.actor.warn-about-java-serializer-usage'", clazz.getName)
|
|
|
|
|
|
}
|
2013-06-02 14:53:31 +02:00
|
|
|
|
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] =
|
2016-06-02 14:06:57 +02:00
|
|
|
|
system.dynamicAccess.createInstanceFor[Serializer](serializerFQN, List(classOf[ExtendedActorSystem] → system)) recoverWith {
|
2013-11-22 17:13:24 -08:00
|
|
|
|
case _: NoSuchMethodException ⇒ 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] =
|
2016-06-02 14:06:57 +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] =
|
2015-08-31 12:38:07 +02:00
|
|
|
|
sort(for ((k: String, v: String) ← settings.SerializationBindings if v != "none" && checkGoogleProtobuf(k))
|
|
|
|
|
|
yield (system.dynamicAccess.getClassFor[Any](k).get, serializers(v))).to[immutable.Seq]
|
|
|
|
|
|
|
|
|
|
|
|
// com.google.protobuf serialization binding is only used if the class can be loaded,
|
|
|
|
|
|
// i.e. com.google.protobuf dependency has been added in the application project.
|
|
|
|
|
|
// The reason for this special case is for backwards compatibility so that we still can
|
|
|
|
|
|
// include "com.google.protobuf.GeneratedMessage" = proto in configured serialization-bindings.
|
|
|
|
|
|
private def checkGoogleProtobuf(className: String): Boolean =
|
|
|
|
|
|
(!className.startsWith("com.google.protobuf") || system.dynamicAccess.getClassFor[Any](className).isSuccess)
|
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] =
|
2016-06-02 14:06:57 +02:00
|
|
|
|
Map(NullSerializer.identifier → NullSerializer) ++ serializers map { case (_, v) ⇒ (v.identifier, v) }
|
2015-05-28 18:42:22 +02:00
|
|
|
|
|
2016-06-04 21:44:13 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Serializers with id 0 - 1023 are stored in an array for quick allocation free access
|
|
|
|
|
|
*/
|
|
|
|
|
|
private val quickSerializerByIdentity: Array[Serializer] = {
|
|
|
|
|
|
val size = 1024
|
|
|
|
|
|
val table = Array.ofDim[Serializer](size)
|
|
|
|
|
|
serializerByIdentity.foreach {
|
|
|
|
|
|
case (id, ser) ⇒ if (0 <= id && id < size) table(id) = ser
|
|
|
|
|
|
}
|
|
|
|
|
|
table
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @throws `NoSuchElementException` if no serializer with given `id`
|
|
|
|
|
|
*/
|
|
|
|
|
|
private def getSerializerById(id: Int): Serializer = {
|
|
|
|
|
|
if (0 <= id && id < quickSerializerByIdentity.length) {
|
|
|
|
|
|
quickSerializerByIdentity(id) match {
|
|
|
|
|
|
case null ⇒ throw new NoSuchElementException(s"key not found: $id")
|
|
|
|
|
|
case ser ⇒ ser
|
|
|
|
|
|
}
|
|
|
|
|
|
} else
|
|
|
|
|
|
serializerByIdentity(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-07 20:51:26 +01:00
|
|
|
|
private val isJavaSerializationWarningEnabled = settings.config.getBoolean("akka.actor.warn-about-java-serializer-usage")
|
2016-07-06 18:48:15 +09:00
|
|
|
|
private val isWarningOnNoVerificationEnabled = settings.config.getBoolean("akka.actor.warn-on-no-serialization-verification")
|
2016-03-07 20:51:26 +01:00
|
|
|
|
|
|
|
|
|
|
private def shouldWarnAboutJavaSerializer(serializedClass: Class[_], serializer: Serializer) = {
|
2016-07-06 18:48:15 +09:00
|
|
|
|
|
|
|
|
|
|
def suppressWarningOnNonSerializationVerification(serializedClass: Class[_]) = {
|
|
|
|
|
|
//suppressed, only when warn-on-no-serialization-verification = off, and extending NoSerializationVerificationNeeded
|
|
|
|
|
|
!isWarningOnNoVerificationEnabled && classOf[NoSerializationVerificationNeeded].isAssignableFrom(serializedClass)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-07 20:51:26 +01:00
|
|
|
|
isJavaSerializationWarningEnabled &&
|
2015-09-24 16:45:51 +02:00
|
|
|
|
serializer.isInstanceOf[JavaSerializer] &&
|
2016-03-07 20:51:26 +01:00
|
|
|
|
!serializedClass.getName.startsWith("akka.") &&
|
2016-07-06 18:48:15 +09:00
|
|
|
|
!serializedClass.getName.startsWith("java.lang.") &&
|
|
|
|
|
|
!suppressWarningOnNonSerializationVerification(serializedClass)
|
2016-03-07 20:51:26 +01:00
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
|
}
|
2011-10-06 21:19:46 +02:00
|
|
|
|
|