Adding more docs to Serialization and making sure that the Protobuf stuff gets its manifests

This commit is contained in:
Viktor Klang 2011-12-29 16:17:19 +01:00
parent cffe60bf43
commit e78739f7e5
5 changed files with 29 additions and 13 deletions

View file

@ -16,7 +16,7 @@ import com.google.protobuf.Message
class ProtobufSerializer extends Serializer { class ProtobufSerializer extends Serializer {
val ARRAY_OF_BYTE_ARRAY = Array[Class[_]](classOf[Array[Byte]]) val ARRAY_OF_BYTE_ARRAY = Array[Class[_]](classOf[Array[Byte]])
def includeManifest: Boolean = false def includeManifest: Boolean = true
def identifier = 2: Serializer.Identifier def identifier = 2: Serializer.Identifier
def toBinary(obj: AnyRef): Array[Byte] = { def toBinary(obj: AnyRef): Array[Byte] = {

View file

@ -184,7 +184,7 @@ object TypedActor extends ExtensionId[TypedActorExtension] with ExtensionIdProvi
* @throws the underlying exception if there's an InvocationTargetException thrown on the invocation * @throws the underlying exception if there's an InvocationTargetException thrown on the invocation
*/ */
def apply(instance: AnyRef): AnyRef = try { def apply(instance: AnyRef): AnyRef = try {
parameters match { //TODO: We do not yet obey Actor.SERIALIZE_MESSAGES parameters match {
case null method.invoke(instance) case null method.invoke(instance)
case args if args.length == 0 method.invoke(instance) case args if args.length == 0 method.invoke(instance)
case args method.invoke(instance, args: _*) case args method.invoke(instance, args: _*)

View file

@ -14,8 +14,18 @@ import akka.actor.{ Extension, ActorSystem, ActorSystemImpl }
case class NoSerializerFoundException(m: String) extends AkkaException(m) case class NoSerializerFoundException(m: String) extends AkkaException(m)
object Serialization { object Serialization {
// TODO ensure that these are always set (i.e. withValue()) when doing deserialization /**
val currentSystem = new DynamicVariable[ActorSystemImpl](null) * This holds a reference to the current ActorSystem (the surrounding context)
* during serialization and deserialization.
*
* If you are using Serializers yourself, outside of SerializationExtension,
* you'll need to surround the serialization/deserialization with:
*
* currentSystem.withValue(system) {
* ...code...
* }
*/
val currentSystem = new DynamicVariable[ActorSystem](null)
class Settings(val config: Config) { class Settings(val config: Config) {
@ -139,14 +149,11 @@ class Serialization(val system: ActorSystemImpl) extends Extension {
* bindings is a Map whose keys = FQN of class that is serializable and values = the alias of the serializer to be used * bindings is a Map whose keys = FQN of class that is serializable and values = the alias of the serializer to be used
*/ */
lazy val bindings: Map[String, String] = { lazy val bindings: Map[String, String] = {
val configBindings = settings.SerializationBindings settings.SerializationBindings.foldLeft(Map[String, String]()) {
configBindings.foldLeft(Map[String, String]()) {
case (result, (k: String, vs: Seq[_]))
//All keys which are lists, take the Strings from them and Map them //All keys which are lists, take the Strings from them and Map them
result ++ (vs collect { case v: String (v, k) }) case (result, (k: String, vs: Seq[_])) result ++ (vs collect { case v: String (v, k) })
case (result, x)
//For any other values, just skip them //For any other values, just skip them
result case (result, _) result
} }
} }

View file

@ -40,6 +40,9 @@ trait Serializer extends scala.Serializable {
object JavaSerializer extends JavaSerializer object JavaSerializer extends JavaSerializer
object NullSerializer extends NullSerializer object NullSerializer extends NullSerializer
/**
* This Serializer uses standard Java Serialization
*/
class JavaSerializer extends Serializer { class JavaSerializer extends Serializer {
def includeManifest: Boolean = false def includeManifest: Boolean = false
@ -65,6 +68,9 @@ class JavaSerializer extends Serializer {
} }
} }
/**
* This is a special Serializer that Serializes and deserializes nulls only
*/
class NullSerializer extends Serializer { class NullSerializer extends Serializer {
val nullAsBytes = Array[Byte]() val nullAsBytes = Array[Byte]()
def includeManifest: Boolean = false def includeManifest: Boolean = false

View file

@ -7,9 +7,12 @@ package akka.serialization
import akka.serialization.Serializer import akka.serialization.Serializer
import com.google.protobuf.Message import com.google.protobuf.Message
/**
* This Serializer serializes `com.google.protobuf.Message`s
*/
class ProtobufSerializer extends Serializer { class ProtobufSerializer extends Serializer {
val ARRAY_OF_BYTE_ARRAY = Array[Class[_]](classOf[Array[Byte]]) val ARRAY_OF_BYTE_ARRAY = Array[Class[_]](classOf[Array[Byte]])
def includeManifest: Boolean = false def includeManifest: Boolean = true
def identifier = 2: Serializer.Identifier def identifier = 2: Serializer.Identifier
def toBinary(obj: AnyRef): Array[Byte] = { def toBinary(obj: AnyRef): Array[Byte] = {