Provides Slick Session to plug-ins via ThreadLocal

This commit is contained in:
Naoki Takezoe
2015-02-16 13:14:52 +09:00
parent f6e7401d1b
commit 8c588cbd66
2 changed files with 35 additions and 19 deletions

View File

@@ -0,0 +1,11 @@
package plugin
import slick.jdbc.JdbcBackend.Session
/**
* Provides Slick Session to Plug-ins.
*/
object Sessions {
val sessions = new ThreadLocal[Session]
implicit def session: Session = sessions.get()
}

View File

@@ -10,6 +10,7 @@ import service.SystemSettingsService
import util.Keys
import app.Context
import plugin.Results._
import plugin.Sessions._
class PluginActionFilter extends Filter with SystemSettingsService {
@@ -27,7 +28,8 @@ class PluginActionFilter extends Filter with SystemSettingsService {
// Create Context
val loginAccount = req.getSession.getAttribute(Keys.Session.LoginAccount).asInstanceOf[Account]
implicit val context = Context(loadSystemSettings(), Option(loginAccount), req)
sessions.set(Database.getSession(req))
try {
// Invoke global action
action(req, res, context) match {
// TODO to be type classes?
@@ -46,6 +48,9 @@ class PluginActionFilter extends Filter with SystemSettingsService {
res.getWriter.write(x.body)
res.getWriter.flush()
}
} finally {
sessions.remove()
}
}.getOrElse {
chain.doFilter(req, res)
}