2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2011-12-21 11:25:40 +01:00
|
|
|
*/
|
|
|
|
|
|
2012-09-17 12:54:08 +02:00
|
|
|
package akka.remote.serialization
|
2011-12-21 11:25:40 +01:00
|
|
|
|
2014-11-29 17:10:50 +08:00
|
|
|
import java.lang.reflect.Method
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
|
|
|
|
|
|
|
|
|
import akka.actor.{ ActorRef, ExtendedActorSystem }
|
2013-03-27 17:47:56 +01:00
|
|
|
import akka.remote.WireFormats.ActorRefData
|
2015-03-05 11:55:05 -06:00
|
|
|
import akka.serialization.{ Serialization, BaseSerializer }
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2014-11-29 17:10:50 +08:00
|
|
|
import scala.annotation.tailrec
|
|
|
|
|
|
2012-05-15 09:40:13 +02:00
|
|
|
object ProtobufSerializer {
|
2014-11-29 17:10:50 +08:00
|
|
|
private val ARRAY_OF_BYTE_ARRAY = Array[Class[_]](classOf[Array[Byte]])
|
2012-05-15 17:16:46 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper to serialize an [[akka.actor.ActorRef]] to Akka's
|
|
|
|
|
* protobuf representation.
|
|
|
|
|
*/
|
2013-03-27 17:47:56 +01:00
|
|
|
def serializeActorRef(ref: ActorRef): ActorRefData = {
|
|
|
|
|
ActorRefData.newBuilder.setPath(Serialization.serializedActorPath(ref)).build
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-15 17:16:46 +02:00
|
|
|
/**
|
|
|
|
|
* Helper to materialize (lookup) an [[akka.actor.ActorRef]]
|
|
|
|
|
* from Akka's protobuf representation in the supplied
|
2012-06-05 18:19:46 +02:00
|
|
|
* [[akka.actor.ActorSystem]].
|
2012-05-15 17:16:46 +02:00
|
|
|
*/
|
2013-03-27 17:47:56 +01:00
|
|
|
def deserializeActorRef(system: ExtendedActorSystem, refProtocol: ActorRefData): ActorRef =
|
2013-03-26 18:17:50 +01:00
|
|
|
system.provider.resolveActorRef(refProtocol.getPath)
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
2011-12-21 11:25:40 +01:00
|
|
|
|
2011-12-29 16:17:19 +01:00
|
|
|
/**
|
2015-08-31 12:38:07 +02:00
|
|
|
* This Serializer serializes `akka.protobuf.Message` and `com.google.protobuf.Message`
|
|
|
|
|
* It is using reflection to find the `parseFrom` and `toByteArray` methods to avoid
|
|
|
|
|
* dependency to `com.google.protobuf`.
|
2011-12-29 16:17:19 +01:00
|
|
|
*/
|
2015-03-05 11:55:05 -06:00
|
|
|
class ProtobufSerializer(val system: ExtendedActorSystem) extends BaseSerializer {
|
2011-12-21 11:25:40 +01:00
|
|
|
|
2014-11-29 17:10:50 +08:00
|
|
|
private val parsingMethodBindingRef = new AtomicReference[Map[Class[_], Method]](Map.empty)
|
2015-08-31 12:38:07 +02:00
|
|
|
private val toByteArrayMethodBindingRef = new AtomicReference[Map[Class[_], Method]](Map.empty)
|
2014-11-29 17:10:50 +08:00
|
|
|
|
|
|
|
|
override def includeManifest: Boolean = true
|
2011-12-21 11:25:40 +01:00
|
|
|
|
2014-11-29 17:10:50 +08:00
|
|
|
override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = {
|
|
|
|
|
manifest match {
|
|
|
|
|
case Some(clazz) ⇒
|
|
|
|
|
@tailrec
|
|
|
|
|
def parsingMethod(method: Method = null): Method = {
|
|
|
|
|
val parsingMethodBinding = parsingMethodBindingRef.get()
|
|
|
|
|
parsingMethodBinding.get(clazz) match {
|
|
|
|
|
case Some(cachedParsingMethod) ⇒ cachedParsingMethod
|
|
|
|
|
case None ⇒
|
2015-04-30 09:23:18 +02:00
|
|
|
val unCachedParsingMethod =
|
|
|
|
|
if (method eq null) clazz.getDeclaredMethod("parseFrom", ProtobufSerializer.ARRAY_OF_BYTE_ARRAY: _*)
|
|
|
|
|
else method
|
2014-11-29 17:10:50 +08:00
|
|
|
if (parsingMethodBindingRef.compareAndSet(parsingMethodBinding, parsingMethodBinding.updated(clazz, unCachedParsingMethod)))
|
|
|
|
|
unCachedParsingMethod
|
|
|
|
|
else
|
|
|
|
|
parsingMethod(unCachedParsingMethod)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-31 12:38:07 +02:00
|
|
|
parsingMethod().invoke(null, bytes)
|
2014-11-29 17:10:50 +08:00
|
|
|
|
|
|
|
|
case None ⇒ throw new IllegalArgumentException("Need a protobuf message class to be able to serialize bytes using protobuf")
|
2011-12-30 19:20:24 +01:00
|
|
|
}
|
2014-11-29 17:10:50 +08:00
|
|
|
}
|
|
|
|
|
|
2015-08-31 12:38:07 +02:00
|
|
|
override def toBinary(obj: AnyRef): Array[Byte] = {
|
|
|
|
|
val clazz = obj.getClass
|
|
|
|
|
@tailrec
|
|
|
|
|
def toByteArrayMethod(method: Method = null): Method = {
|
|
|
|
|
val toByteArrayMethodBinding = toByteArrayMethodBindingRef.get()
|
|
|
|
|
toByteArrayMethodBinding.get(clazz) match {
|
|
|
|
|
case Some(cachedtoByteArrayMethod) ⇒ cachedtoByteArrayMethod
|
|
|
|
|
case None ⇒
|
|
|
|
|
val unCachedtoByteArrayMethod =
|
|
|
|
|
if (method eq null) clazz.getMethod("toByteArray")
|
|
|
|
|
else method
|
|
|
|
|
if (toByteArrayMethodBindingRef.compareAndSet(toByteArrayMethodBinding, toByteArrayMethodBinding.updated(clazz, unCachedtoByteArrayMethod)))
|
|
|
|
|
unCachedtoByteArrayMethod
|
|
|
|
|
else
|
|
|
|
|
toByteArrayMethod(unCachedtoByteArrayMethod)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
toByteArrayMethod().invoke(obj).asInstanceOf[Array[Byte]]
|
2014-11-29 17:10:50 +08:00
|
|
|
}
|
2013-01-09 01:47:48 +01:00
|
|
|
}
|