|
Play Framework/Scala example source code file (HttpExecutionContext.scala)
The HttpExecutionContext.scala Play Framework example source code
/*
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package play.core.j
import play.mvc.Http
import scala.concurrent.{ ExecutionContext, ExecutionContextExecutor }
object HttpExecutionContext {
/**
* Create an HttpExecutionContext with values from the current thread.
*/
def fromThread(delegate: ExecutionContext): ExecutionContextExecutor =
new HttpExecutionContext(Thread.currentThread().getContextClassLoader(), Http.Context.current.get(), delegate)
/**
* Create an ExecutionContext that will, when prepared, be created with values from that thread.
*/
def unprepared(delegate: ExecutionContext) = new ExecutionContext {
def execute(runnable: Runnable) = delegate.execute(runnable) // FIXME: Make calling this an error once SI-7383 is fixed
def reportFailure(t: Throwable) = delegate.reportFailure(t)
override def prepare(): ExecutionContext = fromThread(delegate)
}
}
/**
* Manages execution to ensure that the given context ClassLoader and Http.Context are set correctly
* in the current thread. Actual execution is performed by a delegate ExecutionContext.
*/
class HttpExecutionContext(contextClassLoader: ClassLoader, httpContext: Http.Context, delegate: ExecutionContext) extends ExecutionContextExecutor {
def execute(runnable: Runnable) = delegate.execute(new Runnable {
def run() {
val thread = Thread.currentThread()
val oldContextClassLoader = thread.getContextClassLoader()
val oldHttpContext = Http.Context.current.get()
thread.setContextClassLoader(contextClassLoader)
Http.Context.current.set(httpContext)
try {
runnable.run()
} finally {
thread.setContextClassLoader(oldContextClassLoader)
Http.Context.current.set(oldHttpContext)
}
}
})
def reportFailure(t: Throwable) = delegate.reportFailure(t)
}
Other Play Framework source code examplesHere is a short list of links related to this Play Framework HttpExecutionContext.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.