2011-12-30 20:46:04 +01:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-30 20:46:04 +01:00
|
|
|
*/
|
|
|
|
|
package akka.docs.serialization
|
|
|
|
|
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
|
|
|
|
import akka.testkit._
|
|
|
|
|
//#imports
|
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
|
|
|
import akka.actor.{ ActorSystem, PropertyMaster }
|
2011-12-30 20:46:04 +01:00
|
|
|
import akka.serialization._
|
2011-12-30 22:00:49 +01:00
|
|
|
import com.typesafe.config.ConfigFactory
|
2011-12-30 20:46:04 +01:00
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
|
|
|
|
|
//#my-own-serializer
|
|
|
|
|
class MyOwnSerializer extends Serializer {
|
|
|
|
|
|
|
|
|
|
// This is whether "fromBinary" requires a "clazz" or not
|
|
|
|
|
def includeManifest: Boolean = false
|
|
|
|
|
|
|
|
|
|
// Pick a unique identifier for your Serializer,
|
|
|
|
|
// you've got a couple of billions to choose from,
|
|
|
|
|
// 0 - 16 is reserved by Akka itself
|
2011-12-30 22:00:49 +01:00
|
|
|
def identifier = 1234567
|
2011-12-30 20:46:04 +01:00
|
|
|
|
|
|
|
|
// "toBinary" serializes the given object to an Array of Bytes
|
|
|
|
|
def toBinary(obj: AnyRef): Array[Byte] = {
|
|
|
|
|
// Put the code that serializes the object here
|
|
|
|
|
//#...
|
|
|
|
|
Array[Byte]()
|
|
|
|
|
//#...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// "fromBinary" deserializes the given array,
|
|
|
|
|
// using the type hint (if any, see "includeManifest" above)
|
|
|
|
|
// into the optionally provided classLoader.
|
|
|
|
|
def fromBinary(bytes: Array[Byte],
|
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
|
|
|
clazz: Option[Class[_]]): AnyRef = {
|
2011-12-30 20:46:04 +01:00
|
|
|
// Put your code that deserializes here
|
|
|
|
|
//#...
|
|
|
|
|
null
|
|
|
|
|
//#...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#my-own-serializer
|
|
|
|
|
|
|
|
|
|
class SerializationDocSpec extends AkkaSpec {
|
|
|
|
|
"demonstrate configuration of serialize messages" in {
|
|
|
|
|
//#serialize-messages-config
|
|
|
|
|
val config = ConfigFactory.parseString("""
|
|
|
|
|
akka {
|
|
|
|
|
actor {
|
|
|
|
|
serialize-messages = on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
//#serialize-messages-config
|
|
|
|
|
val a = ActorSystem("system", config)
|
|
|
|
|
a.settings.SerializeAllMessages must be(true)
|
|
|
|
|
a.shutdown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"demonstrate configuration of serialize creators" in {
|
|
|
|
|
//#serialize-creators-config
|
|
|
|
|
val config = ConfigFactory.parseString("""
|
|
|
|
|
akka {
|
|
|
|
|
actor {
|
|
|
|
|
serialize-creators = on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
//#serialize-creators-config
|
|
|
|
|
val a = ActorSystem("system", config)
|
|
|
|
|
a.settings.SerializeAllCreators must be(true)
|
|
|
|
|
a.shutdown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"demonstrate configuration of serializers" in {
|
|
|
|
|
//#serialize-serializers-config
|
|
|
|
|
val config = ConfigFactory.parseString("""
|
|
|
|
|
akka {
|
|
|
|
|
actor {
|
|
|
|
|
serializers {
|
|
|
|
|
default = "akka.serialization.JavaSerializer"
|
|
|
|
|
|
|
|
|
|
myown = "akka.docs.serialization.MyOwnSerializer"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
//#serialize-serializers-config
|
|
|
|
|
val a = ActorSystem("system", config)
|
|
|
|
|
SerializationExtension(a).serializers("default").getClass.getName must equal("akka.serialization.JavaSerializer")
|
|
|
|
|
SerializationExtension(a).serializers("myown").getClass.getName must equal("akka.docs.serialization.MyOwnSerializer")
|
|
|
|
|
a.shutdown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"demonstrate configuration of serialization-bindings" in {
|
|
|
|
|
//#serialization-bindings-config
|
|
|
|
|
val config = ConfigFactory.parseString("""
|
|
|
|
|
akka {
|
|
|
|
|
actor {
|
|
|
|
|
serializers {
|
|
|
|
|
default = "akka.serialization.JavaSerializer"
|
|
|
|
|
java = "akka.serialization.JavaSerializer"
|
2012-02-03 17:32:32 +01:00
|
|
|
proto = "akka.serialization.ProtobufSerializer"
|
2011-12-30 20:46:04 +01:00
|
|
|
myown = "akka.docs.serialization.MyOwnSerializer"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serialization-bindings {
|
|
|
|
|
java = ["java.lang.String",
|
|
|
|
|
"app.my.Customer"]
|
2012-02-03 17:32:32 +01:00
|
|
|
proto = ["com.google.protobuf.Message"]
|
2011-12-30 20:46:04 +01:00
|
|
|
myown = ["my.own.BusinessObject",
|
|
|
|
|
"something.equally.Awesome",
|
2012-02-03 17:32:32 +01:00
|
|
|
"akka.docs.serialization.MyOwnSerializable"
|
2011-12-30 20:46:04 +01:00
|
|
|
"java.lang.Boolean"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
//#serialization-bindings-config
|
|
|
|
|
val a = ActorSystem("system", config)
|
|
|
|
|
SerializationExtension(a).serializers("default").getClass.getName must equal("akka.serialization.JavaSerializer")
|
|
|
|
|
SerializationExtension(a).serializers("java").getClass.getName must equal("akka.serialization.JavaSerializer")
|
|
|
|
|
SerializationExtension(a).serializers("myown").getClass.getName must equal("akka.docs.serialization.MyOwnSerializer")
|
|
|
|
|
SerializationExtension(a).serializerFor(classOf[String]).getClass.getName must equal("akka.serialization.JavaSerializer")
|
|
|
|
|
SerializationExtension(a).serializerFor(classOf[java.lang.Boolean]).getClass.getName must equal("akka.docs.serialization.MyOwnSerializer")
|
|
|
|
|
a.shutdown()
|
|
|
|
|
}
|
2011-12-30 20:57:40 +01:00
|
|
|
|
|
|
|
|
"demonstrate the programmatic API" in {
|
|
|
|
|
//#programmatic
|
|
|
|
|
val system = ActorSystem("example")
|
|
|
|
|
|
|
|
|
|
// Get the Serialization Extension
|
|
|
|
|
val serialization = SerializationExtension(system)
|
|
|
|
|
|
|
|
|
|
// Have something to serialize
|
|
|
|
|
val original = "woohoo"
|
|
|
|
|
|
|
|
|
|
// Find the Serializer for it
|
|
|
|
|
val serializer = serialization.findSerializerFor(original)
|
|
|
|
|
|
|
|
|
|
// Turn it into bytes
|
|
|
|
|
val bytes = serializer.toBinary(original)
|
|
|
|
|
|
|
|
|
|
// Turn it back into an object
|
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
|
|
|
val back = serializer.fromBinary(bytes, manifest = None)
|
2011-12-30 20:57:40 +01:00
|
|
|
|
|
|
|
|
// Voilá!
|
|
|
|
|
back must equal(original)
|
|
|
|
|
|
|
|
|
|
//#programmatic
|
|
|
|
|
system.shutdown()
|
|
|
|
|
}
|
2011-12-30 20:46:04 +01:00
|
|
|
}
|