2011-06-07 06:36:21 +05:30
|
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-06-07 06:36:21 +05:30
|
|
|
|
*/
|
|
|
|
|
|
|
2011-06-22 09:59:00 +02:00
|
|
|
|
package akka.serialization
|
|
|
|
|
|
|
|
|
|
|
|
import akka.AkkaException
|
2011-10-06 21:19:46 +02:00
|
|
|
|
import scala.util.DynamicVariable
|
2011-11-29 11:50:22 +01:00
|
|
|
|
import com.typesafe.config.Config
|
2011-11-24 18:53:18 +01:00
|
|
|
|
import akka.config.ConfigurationException
|
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.{ Extension, ExtendedActorSystem, Address }
|
2012-02-03 17:32:32 +01:00
|
|
|
|
import java.util.concurrent.ConcurrentHashMap
|
|
|
|
|
|
import akka.event.Logging
|
2012-02-09 19:26:02 +01:00
|
|
|
|
import akka.util.NonFatal
|
2011-06-07 06:36:21 +05:30
|
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
|
case class NoSerializerFoundException(m: String) extends AkkaException(m)
|
|
|
|
|
|
|
2011-11-24 18:53:18 +01:00
|
|
|
|
object Serialization {
|
2012-01-27 12:14:28 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* This holds a reference to the current transport address to be inserted
|
|
|
|
|
|
* into local actor refs during serialization.
|
|
|
|
|
|
*/
|
|
|
|
|
|
val currentTransportAddress = new DynamicVariable[Address](null)
|
|
|
|
|
|
|
2011-11-29 11:50:22 +01:00
|
|
|
|
class Settings(val config: Config) {
|
2011-11-24 18:53:18 +01:00
|
|
|
|
|
|
|
|
|
|
import scala.collection.JavaConverters._
|
|
|
|
|
|
import config._
|
|
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
val Serializers: Map[String, String] =
|
|
|
|
|
|
getConfig("akka.actor.serializers").root.unwrapped.asScala.toMap.map { case (k, v) ⇒ (k, v.toString) }
|
2011-11-24 18:53:18 +01:00
|
|
|
|
|
|
|
|
|
|
val SerializationBindings: Map[String, Seq[String]] = {
|
|
|
|
|
|
val configPath = "akka.actor.serialization-bindings"
|
|
|
|
|
|
hasPath(configPath) match {
|
|
|
|
|
|
case false ⇒ Map()
|
|
|
|
|
|
case true ⇒
|
2011-12-02 08:51:51 +01:00
|
|
|
|
val serializationBindings: Map[String, Seq[String]] = getConfig(configPath).root.unwrapped.asScala.toMap.map {
|
2011-11-24 18:53:18 +01:00
|
|
|
|
case (k: String, v: java.util.Collection[_]) ⇒ (k -> v.asScala.toSeq.asInstanceOf[Seq[String]])
|
|
|
|
|
|
case invalid ⇒ throw new ConfigurationException("Invalid serialization-bindings [%s]".format(invalid))
|
|
|
|
|
|
}
|
|
|
|
|
|
serializationBindings
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-06-22 09:59:00 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Serialization module. Contains methods for serialization and deserialization as well as
|
|
|
|
|
|
* locating a Serializer for a particular class as defined in the mapping in the 'akka.conf' file.
|
|
|
|
|
|
*/
|
2012-01-24 11:33:40 +01:00
|
|
|
|
class Serialization(val system: ExtendedActorSystem) extends Extension {
|
2011-11-24 18:53:18 +01:00
|
|
|
|
import Serialization._
|
|
|
|
|
|
|
2011-11-29 11:50:22 +01:00
|
|
|
|
val settings = new Settings(system.settings.config)
|
2012-02-03 17:32:32 +01:00
|
|
|
|
val log = Logging(system, getClass.getName)
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Serializes the given AnyRef/java.lang.Object according to the Serialization configuration
|
|
|
|
|
|
* to either an Array of Bytes or an Exception if one was thrown.
|
|
|
|
|
|
*/
|
2012-02-09 19:26:02 +01:00
|
|
|
|
def serialize(o: AnyRef): Either[Throwable, 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
|
|
|
|
try Right(findSerializerFor(o).toBinary(o))
|
2012-02-09 19:26:02 +01:00
|
|
|
|
catch { case NonFatal(e) ⇒ Left(e) }
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Deserializes the given array of bytes using the specified serializer id,
|
|
|
|
|
|
* using the optional type hint to the Serializer and the optional ClassLoader ot load it into.
|
|
|
|
|
|
* Returns either the resulting object or an Exception if one was thrown.
|
|
|
|
|
|
*/
|
|
|
|
|
|
def deserialize(bytes: Array[Byte],
|
2011-12-30 22:00:49 +01:00
|
|
|
|
serializerId: Int,
|
2012-02-09 19:26:02 +01:00
|
|
|
|
clazz: Option[Class[_]]): Either[Throwable, AnyRef] =
|
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
|
|
|
|
try Right(serializerByIdentity(serializerId).fromBinary(bytes, clazz))
|
2012-02-09 19:26:02 +01:00
|
|
|
|
catch { case NonFatal(e) ⇒ Left(e) }
|
2011-12-29 16:11:56 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Deserializes the given array of bytes using the specified type to look up what Serializer should be used.
|
|
|
|
|
|
* You can specify an optional ClassLoader to load the object into.
|
|
|
|
|
|
* Returns either the resulting object or an Exception if one was thrown.
|
|
|
|
|
|
*/
|
2012-02-09 19:26:02 +01:00
|
|
|
|
def deserialize(bytes: Array[Byte], clazz: Class[_]): Either[Throwable, AnyRef] =
|
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
|
|
|
|
try Right(serializerFor(clazz).fromBinary(bytes, Some(clazz)))
|
2012-02-09 19:26:02 +01:00
|
|
|
|
catch { case NonFatal(e) ⇒ Left(e) }
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
/**
|
2011-12-30 15:09:34 +01:00
|
|
|
|
* Returns the Serializer configured for the given object, returns the NullSerializer if it's null,
|
|
|
|
|
|
* falls back to the Serializer named "default"
|
2011-12-29 16:11:56 +01:00
|
|
|
|
*/
|
2011-07-19 14:20:18 +02:00
|
|
|
|
def findSerializerFor(o: AnyRef): Serializer = o match {
|
2011-07-26 18:33:59 +12:00
|
|
|
|
case null ⇒ NullSerializer
|
|
|
|
|
|
case other ⇒ serializerFor(other.getClass)
|
2011-07-19 14:20:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
|
/**
|
2012-02-03 17:32:32 +01:00
|
|
|
|
* Returns the configured Serializer for the given Class, falls back to the Serializer named "default".
|
|
|
|
|
|
* It traverses interfaces and super classes to find any configured Serializer that match
|
|
|
|
|
|
* the class name.
|
2011-12-29 16:11:56 +01:00
|
|
|
|
*/
|
2012-02-04 17:35:39 +01:00
|
|
|
|
def serializerFor(clazz: Class[_]): Serializer =
|
2012-02-03 17:32:32 +01:00
|
|
|
|
if (bindings.isEmpty) {
|
|
|
|
|
|
// quick path to default when no bindings are registered
|
|
|
|
|
|
serializers("default")
|
|
|
|
|
|
} else {
|
2012-02-04 17:35:39 +01:00
|
|
|
|
|
|
|
|
|
|
def resolve(c: Class[_]): Option[Serializer] =
|
|
|
|
|
|
serializerMap.get(c.getName) match {
|
|
|
|
|
|
case null ⇒
|
|
|
|
|
|
val classes = c.getInterfaces ++ Option(c.getSuperclass)
|
|
|
|
|
|
classes.view map resolve collectFirst { case Some(x) ⇒ x }
|
|
|
|
|
|
case x ⇒ Some(x)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-02-03 21:42:33 +01:00
|
|
|
|
serializerMap.get(clazz.getName) match {
|
2012-02-03 17:32:32 +01:00
|
|
|
|
case null ⇒
|
|
|
|
|
|
val ser = resolve(clazz).getOrElse(serializers("default"))
|
|
|
|
|
|
// memorize the lookups for performance
|
2012-02-03 21:42:33 +01:00
|
|
|
|
serializerMap.putIfAbsent(clazz.getName, ser) match {
|
|
|
|
|
|
case null ⇒
|
|
|
|
|
|
log.debug("Using serializer[{}] for message [{}]", ser.getClass.getName, clazz.getName)
|
|
|
|
|
|
ser
|
|
|
|
|
|
case some ⇒ some
|
|
|
|
|
|
}
|
2012-02-03 17:32:32 +01:00
|
|
|
|
case ser ⇒ ser
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
|
|
2011-07-15 16:21:45 +02:00
|
|
|
|
/**
|
2012-02-09 19:26:02 +01:00
|
|
|
|
* Tries to load the specified Serializer by the fully-qualified name; the actual
|
|
|
|
|
|
* loading is performed by the system’s [[akka.actor.PropertyMaster]].
|
2011-07-15 16:21:45 +02: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 serializerOf(serializerFQN: String): Either[Throwable, Serializer] = {
|
|
|
|
|
|
val pm = system.propertyMaster
|
|
|
|
|
|
pm.getInstanceFor[Serializer](serializerFQN, Seq(classOf[ExtendedActorSystem] -> system))
|
|
|
|
|
|
.fold(_ ⇒ pm.getInstanceFor[Serializer](serializerFQN, Seq()), Right(_))
|
|
|
|
|
|
}
|
2011-07-15 16:21:45 +02:00
|
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
|
/**
|
2011-07-19 14:20:18 +02:00
|
|
|
|
* A Map of serializer from alias to implementation (class implementing akka.serialization.Serializer)
|
|
|
|
|
|
* By default always contains the following mapping: "default" -> akka.serialization.JavaSerializer
|
2011-07-14 11:25:40 +02:00
|
|
|
|
* But "default" can be overridden in config
|
|
|
|
|
|
*/
|
2011-11-22 13:04:10 +01:00
|
|
|
|
lazy val serializers: Map[String, Serializer] = {
|
2011-11-24 18:53:18 +01:00
|
|
|
|
val serializersConf = settings.Serializers
|
2011-11-15 11:34:39 +01:00
|
|
|
|
for ((k: String, v: String) ← serializersConf)
|
|
|
|
|
|
yield k -> serializerOf(v).fold(throw _, identity)
|
|
|
|
|
|
}
|
2011-06-29 21:25:17 +02:00
|
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* bindings is a Map whose keys = FQN of class that is serializable and values = the alias of the serializer to be used
|
|
|
|
|
|
*/
|
2011-11-22 13:04:10 +01:00
|
|
|
|
lazy val bindings: Map[String, String] = {
|
2011-12-29 16:17:19 +01:00
|
|
|
|
settings.SerializationBindings.foldLeft(Map[String, String]()) {
|
|
|
|
|
|
//All keys which are lists, take the Strings from them and Map them
|
|
|
|
|
|
case (result, (k: String, vs: Seq[_])) ⇒ result ++ (vs collect { case v: String ⇒ (v, k) })
|
|
|
|
|
|
//For any other values, just skip them
|
|
|
|
|
|
case (result, _) ⇒ result
|
2011-11-19 19:55:44 +01:00
|
|
|
|
}
|
2011-11-15 11:34:39 +01:00
|
|
|
|
}
|
2011-06-29 21:25:17 +02:00
|
|
|
|
|
2011-07-14 11:25:40 +02:00
|
|
|
|
/**
|
2012-02-03 17:32:32 +01:00
|
|
|
|
* serializerMap is a Map whose keys = FQN of class that is serializable and values is the serializer to be used for that class
|
2011-07-14 11:25:40 +02:00
|
|
|
|
*/
|
2012-02-03 17:32:32 +01:00
|
|
|
|
private lazy val serializerMap: ConcurrentHashMap[String, Serializer] = {
|
|
|
|
|
|
val serializerMap = new ConcurrentHashMap[String, Serializer]
|
2012-02-03 21:42:33 +01:00
|
|
|
|
for ((k, v) ← bindings) {
|
|
|
|
|
|
serializerMap.put(k, serializers(v))
|
2012-02-03 17:32:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
serializerMap
|
|
|
|
|
|
}
|
2011-07-19 14:20:18 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-30 22:00:49 +01:00
|
|
|
|
* Maps from a Serializer Identity (Int) to a Serializer instance (optimization)
|
2011-07-19 14:20:18 +02:00
|
|
|
|
*/
|
2011-12-30 22:00:49 +01:00
|
|
|
|
lazy val serializerByIdentity: Map[Int, Serializer] =
|
2011-07-26 18:33:59 +12:00
|
|
|
|
Map(NullSerializer.identifier -> NullSerializer) ++ serializers map { case (_, v) ⇒ (v.identifier, v) }
|
2011-06-07 06:36:21 +05:30
|
|
|
|
}
|
2011-10-06 21:19:46 +02:00
|
|
|
|
|