2011-09-09 13:46:36 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-09-20 21:44:50 +02:00
|
|
|
package akka.remote
|
2011-09-09 13:46:36 +02:00
|
|
|
|
2011-09-20 21:44:50 +02:00
|
|
|
import akka.remote.RemoteProtocol._
|
2011-09-09 13:46:36 +02:00
|
|
|
import akka.serialization.Serialization
|
|
|
|
|
import com.google.protobuf.ByteString
|
2011-11-10 20:08:00 +01:00
|
|
|
import akka.actor.ActorSystem
|
2011-11-22 13:04:10 +01:00
|
|
|
import akka.serialization.SerializationExtension
|
2011-09-09 13:46:36 +02:00
|
|
|
|
|
|
|
|
object MessageSerializer {
|
|
|
|
|
|
2011-11-17 12:36:35 +01:00
|
|
|
def deserialize(system: ActorSystem, messageProtocol: MessageProtocol, classLoader: Option[ClassLoader] = None): AnyRef = {
|
2011-09-09 13:46:36 +02:00
|
|
|
val clazz = loadManifest(classLoader, messageProtocol)
|
2011-11-22 13:04:10 +01:00
|
|
|
SerializationExtension(system).serialization.deserialize(messageProtocol.getMessage.toByteArray,
|
2011-11-03 14:53:38 +01:00
|
|
|
clazz, classLoader).fold(x ⇒ throw x, identity)
|
2011-09-09 13:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
2011-11-17 12:36:35 +01:00
|
|
|
def serialize(system: ActorSystem, message: AnyRef): MessageProtocol = {
|
2011-09-09 13:46:36 +02:00
|
|
|
val builder = MessageProtocol.newBuilder
|
2011-11-22 13:04:10 +01:00
|
|
|
val bytes = SerializationExtension(system).serialization.serialize(message).fold(x ⇒ throw x, identity)
|
2011-09-09 13:46:36 +02:00
|
|
|
builder.setMessage(ByteString.copyFrom(bytes))
|
|
|
|
|
builder.setMessageManifest(ByteString.copyFromUtf8(message.getClass.getName))
|
|
|
|
|
builder.build
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def loadManifest(classLoader: Option[ClassLoader], messageProtocol: MessageProtocol): Class[_] = {
|
|
|
|
|
val manifest = messageProtocol.getMessageManifest.toStringUtf8
|
|
|
|
|
classLoader map (_.loadClass(manifest)) getOrElse (Class.forName(manifest))
|
|
|
|
|
}
|
|
|
|
|
}
|