Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
Refactored, added Finalizable and response events.
Browse files Browse the repository at this point in the history
  • Loading branch information
inca committed May 15, 2013
1 parent d76f35b commit c10750b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
22 changes: 3 additions & 19 deletions core/src/main/scala/context.scala
Expand Up @@ -41,24 +41,8 @@ accessed via the `Context.get` method.
*/
class Context
extends HashMap[String, Any]
with KeyValueCoercion {

def finalizers = getAs[Seq[() => Unit]](
"finalizers").getOrElse(Nil)

def enqueueFinalizer(key: String, fn: () => Unit) {
if (!contains("finalizers." + key)) {
update("finalizers." + key, true)
update("finalizers", finalizers ++ Seq(fn))
}
}

def pushFinalizer(key: String, fn: () => Unit) {
if (!contains("finalizers." + key)) {
update("finalizers." + key, true)
update("finalizers", Seq(fn) ++ finalizers)
}
}
with KeyValueCoercion
with Finalizable {

def executeWith[R](params: (String, Any)*)
(actions: => R): R = {
Expand Down Expand Up @@ -106,7 +90,7 @@ object Context {

def destroy() {
if (isLive) {
get().finalizers.foreach(_.apply())
get().doFinalize()
threadLocal.set(null)
}
}
Expand Down
34 changes: 34 additions & 0 deletions core/src/main/scala/finalizable.scala
@@ -0,0 +1,34 @@
package pro.savant.circumflex
package core

/*! `Finalizable` trait adds abstract finalization functionality
based on collecting functions `() => Unit` in current context.
The implementation then decides when to perform such finalization
(i.e. execute all the functions).
*/
trait Finalizable {

protected def baseName = "finalizers"

def finalizers = ctx.getAs[Seq[() => Unit]](baseName).getOrElse(Nil)

def enqueueFinalizer(key: String, fn: () => Unit) {
if (!ctx.contains(baseName + "." + key)) {
ctx.update(baseName + "." + key, true)
ctx.update(baseName, finalizers ++ Seq(fn))
}
}

def pushFinalizer(key: String, fn: () => Unit) {
if (!ctx.contains(baseName + "." + key)) {
ctx.update(baseName + "." + key, true)
ctx.update(baseName, Seq(fn) ++ finalizers)
}
}

def doFinalize() {
finalizers.foreach(_.apply())
}

}
6 changes: 5 additions & 1 deletion web/src/main/scala/response.scala
@@ -1,6 +1,7 @@
package pro.savant.circumflex
package web

import core._
import javax.servlet.http.HttpServletResponse
import collection.mutable.{ListBuffer, HashMap}
import java.lang.String
Expand All @@ -17,7 +18,8 @@ actual `HttpServletResponse` using the `flush` method.
Since Circumflex is UTF-friendly it will implicitly set character encoding of
response body to `UTF-8`. Feel free to change it if your application requires so.
*/
class HttpResponse(val raw: HttpServletResponse) {
class HttpResponse(val raw: HttpServletResponse)
extends Finalizable {

def flush(): Nothing = {
if (!raw.isCommitted) {
Expand All @@ -33,6 +35,8 @@ class HttpResponse(val raw: HttpServletResponse) {
}
// apply cookies
cookies.foreach(c => raw.addCookie(c.convert()))
// perform finalization before writing body response
doFinalize()
// write response body
body(raw)
// flush
Expand Down

0 comments on commit c10750b

Please sign in to comment.