Skip to content

Instantly share code, notes, and snippets.

@kevinwright
Created November 28, 2011 19:30
Show Gist options
  • Save kevinwright/1401655 to your computer and use it in GitHub Desktop.
Save kevinwright/1401655 to your computer and use it in GitHub Desktop.
Scalate in Akka
...
lazy val engine: TemplateEngine = {
val ctx = CaptureContext.context.get
val srcDirectories = ctx.getRealPath("/") match {
case path: String => List(new File(path))
case null => List()
}
println("Scalate got src directories " + srcDirectories)
new TemplateEngine(srcDirectories)
}
...
package web.listeners
import akka.dispatch.{DefaultCompletableFuture, Future}
import javax.servlet.{ServletContext, ServletContextEvent, ServletContextListener}
import org.slf4j.LoggerFactory
object CaptureContext {
private[listeners] val completableContext = new DefaultCompletableFuture[ServletContext]
def context: Future[ServletContext] = completableContext
}
class CaptureContext extends ServletContextListener {
val log = LoggerFactory.getLogger(this.getClass)
import CaptureContext.completableContext.{isCompleted, complete}
def contextInitialized(evt: ServletContextEvent) {
if (!isCompleted) {
try {
val ctx = evt.getServletContext
log.info("Captured context " + ctx)
complete(Right(ctx))
} catch {
case x: Exception => complete(Left(x))
}
}
}
def contextDestroyed(e: ServletContextEvent) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment