2011-12-21 11:25:40 +01:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.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
|
|
|
|
2012-09-17 12:54:08 +02:00
|
|
|
import akka.serialization.{ Serializer, Serialization }
|
2011-12-21 11:25:40 +01:00
|
|
|
import com.google.protobuf.Message
|
2012-02-10 11:36:23 +01:00
|
|
|
import akka.actor.DynamicAccess
|
2012-05-15 09:40:13 +02:00
|
|
|
import akka.remote.RemoteProtocol.ActorRefProtocol
|
|
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import akka.actor.ActorRef
|
|
|
|
|
|
|
|
|
|
object ProtobufSerializer {
|
2012-05-15 17:16:46 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper to serialize an [[akka.actor.ActorRef]] to Akka's
|
|
|
|
|
* protobuf representation.
|
|
|
|
|
*/
|
2012-05-15 09:40:13 +02:00
|
|
|
def serializeActorRef(ref: ActorRef): ActorRefProtocol = {
|
|
|
|
|
val identifier: String = Serialization.currentTransportAddress.value match {
|
2013-03-13 16:01:57 +01:00
|
|
|
case null ⇒ ref.path.toSerializationFormat
|
|
|
|
|
case address ⇒ ref.path.toSerializationFormatWithAddress(address)
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
ActorRefProtocol.newBuilder.setPath(identifier).build
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2012-05-15 09:40:13 +02:00
|
|
|
def deserializeActorRef(system: ActorSystem, refProtocol: ActorRefProtocol): ActorRef =
|
|
|
|
|
system.actorFor(refProtocol.getPath)
|
|
|
|
|
}
|
2011-12-21 11:25:40 +01:00
|
|
|
|
2011-12-29 16:17:19 +01:00
|
|
|
/**
|
|
|
|
|
* This Serializer serializes `com.google.protobuf.Message`s
|
|
|
|
|
*/
|
2011-12-21 11:25:40 +01:00
|
|
|
class ProtobufSerializer extends Serializer {
|
|
|
|
|
val ARRAY_OF_BYTE_ARRAY = Array[Class[_]](classOf[Array[Byte]])
|
2011-12-29 16:17:19 +01:00
|
|
|
def includeManifest: Boolean = true
|
2011-12-30 22:00:49 +01:00
|
|
|
def identifier = 2
|
2011-12-21 11:25:40 +01:00
|
|
|
|
2011-12-30 19:20:24 +01:00
|
|
|
def toBinary(obj: AnyRef): Array[Byte] = obj match {
|
|
|
|
|
case m: Message ⇒ m.toByteArray
|
|
|
|
|
case _ ⇒ throw new IllegalArgumentException("Can't serialize a non-protobuf message using protobuf [" + obj + "]")
|
2011-12-21 11:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
Bye-bye ReflectiveAccess, introducing PropertyMaster, see #1750
- PropertyMaster is the only place in Akka which calls
ClassLoader.getClass (apart from kernel, which might be special)
- all PropertyMaster methods (there are only three) take a ClassManifest
of what is to be constructed, and they verify that the obtained object
is actually compatible with the required type
Other stuff:
- noticed that I had forgotten to change to ExtendedActorSystem when
constructing Extensions by ExtensionKey (damn you, reflection!)
- moved Serializer.currentSystem into JavaSerializer, because that’s the
only one needing it (it’s only used in readResolve() methods)
- Serializers are constructed now with one-arg constructor taking
ExtendedActorSystem (if that exists, otherwise no-arg as before), to
allow JavaSerializer to do its magic; possibly necessary for others as
well
- Removed all Option[ClassLoader] signatures
- made it so that the ActorSystem will try context class loader, then
the class loader which loaded the class actually calling into
ActorSystem.apply, then the loader which loaded ActorSystemImpl
- for the second of the above I added a (reflectively accessed hopefully
safe) facility for getting caller Class[_] objects by using
sun.reflect.Reflection; this is optional an defaults to None, e.g. on
Android, which means that getting the caller’s classloader is done on
a best effort basis (there’s nothing we can do because a StackTrace
does not contain actual Class[_] objects).
- refactored DurableMailbox to contain the owner val and use that
instead of declaring that in all subclasses
2012-02-09 11:56:43 +01:00
|
|
|
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef =
|
2011-12-30 19:20:24 +01:00
|
|
|
clazz match {
|
|
|
|
|
case None ⇒ throw new IllegalArgumentException("Need a protobuf message class to be able to serialize bytes using protobuf")
|
|
|
|
|
case Some(c) ⇒ c.getDeclaredMethod("parseFrom", ARRAY_OF_BYTE_ARRAY: _*).invoke(null, bytes).asInstanceOf[Message]
|
|
|
|
|
}
|
2013-01-09 01:47:48 +01:00
|
|
|
}
|