diff options
author | Diego Parra <diegolparra@gmail.com> | 2017-11-21 21:23:57 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-21 21:23:57 -0300 |
commit | d38af7d18a4f68a3048f19209af43243d7412f68 (patch) | |
tree | 3dfb34b686b5673ca7fbf2b95b15208780a9a426 /kamon-core/src/main/scala | |
parent | 2162a62802dc4ed89f61fb98c5321a4029766503 (diff) | |
parent | 6ae5dba86d260e6cceffb9a72e9d20c0d3270192 (diff) | |
download | Kamon-d38af7d18a4f68a3048f19209af43243d7412f68.tar.gz Kamon-d38af7d18a4f68a3048f19209af43243d7412f68.tar.bz2 Kamon-d38af7d18a4f68a3048f19209af43243d7412f68.zip |
Merge pull request #491 from mladens/kamon-1.0-develop
Serializable Context
Diffstat (limited to 'kamon-core/src/main/scala')
-rw-r--r-- | kamon-core/src/main/scala/kamon/context/Context.scala | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/kamon-core/src/main/scala/kamon/context/Context.scala b/kamon-core/src/main/scala/kamon/context/Context.scala index 85c80d50..340a7803 100644 --- a/kamon-core/src/main/scala/kamon/context/Context.scala +++ b/kamon-core/src/main/scala/kamon/context/Context.scala @@ -15,15 +15,48 @@ package kamon.context -class Context private (private[context] val entries: Map[Key[_], Any]) { +import java.io._ +import java.nio.ByteBuffer + +import kamon.Kamon + +class Context private (private[context] val entries: Map[Key[_], Any]) extends scala.Serializable { def get[T](key: Key[T]): T = entries.getOrElse(key, key.emptyValue).asInstanceOf[T] def withKey[T](key: Key[T], value: T): Context = new Context(entries.updated(key, value)) + + var _deserializedEntries: Map[Key[_], Any] = Map.empty + + @throws[IOException] + private def writeObject(out: ObjectOutputStream): Unit = out.write( + Kamon.contextCodec().Binary.encode(this).array() + ) + + @throws[IOException] + @throws[ClassNotFoundException] + private def readObject(in: ObjectInputStream): Unit = { + val buf = new Array[Byte](in.available()) + in.readFully(buf) + _deserializedEntries = Kamon.contextCodec().Binary.decode(ByteBuffer.wrap(buf)).entries + } + + def readResolve(): AnyRef = new Context(_deserializedEntries) + + override def equals(obj: scala.Any): Boolean = { + obj != null && + obj.isInstanceOf[Context] && + obj.asInstanceOf[Context].entries != null && + obj.asInstanceOf[Context].entries == this.entries + } + + override def hashCode(): Int = entries.hashCode() + } object Context { + val Empty = new Context(Map.empty) def apply(): Context = @@ -37,6 +70,7 @@ object Context { def create[T](key: Key[T], value: T): Context = apply(key, value) + } |