Browse files

Closes #830. Optional exception thrown when Session and RequestVar ac…

…cess out of scope
  • Loading branch information...
1 parent 1c6d4dd commit e258f20944a78faad226c24a6a74ba7e4b3454fb @dpp dpp committed Mar 16, 2011
View
6 web/webkit/src/main/scala/net/liftweb/http/LiftRules.scala
@@ -980,6 +980,12 @@ trait LiftRules extends Factory with FormVendor with LazyLoggable {
@volatile private var _context: HTTPContext = _
/**
+ * Should an exception be thrown on out of scope Session and RequestVar
+ * access. By default, no.
+ */
+ @volatile var throwOnOutOfScopeVarAccess: Boolean = false
+
+ /**
* Returns the HTTPContext
*/
def context: HTTPContext = synchronized {_context}
View
32 web/webkit/src/main/scala/net/liftweb/http/Vars.scala
@@ -67,6 +67,10 @@ abstract class SessionVar[T](dflt: => T) extends AnyVar[T, SessionVar[T]](dflt)
override protected def findFunc(name: String): Box[T] = S.session match {
case Full(s) => s.get(name)
case _ =>
+ if (LiftRules.throwOnOutOfScopeVarAccess) {
+ throw new IllegalAccessException("Access to SessionVar outside a request or comet actor scope")
+ }
+
if (showWarningWhenAccessedOutOfSessionScope_?)
logger.warn("Getting a SessionVar "+name+" outside session scope") // added warning per issue 188
@@ -89,6 +93,10 @@ abstract class SessionVar[T](dflt: => T) extends AnyVar[T, SessionVar[T]](dflt)
case Full(s) => s.set(name, value)
case _ =>
+ if (LiftRules.throwOnOutOfScopeVarAccess) {
+ throw new IllegalAccessException("Access to SessionVar outside a request or comet actor scope")
+ }
+
if (showWarningWhenAccessedOutOfSessionScope_?)
logger.warn("Setting a SessionVar "+name+" to "+value+" outside session scope") // added warning per issue 188
}
@@ -133,10 +141,6 @@ abstract class SessionVar[T](dflt: => T) extends AnyVar[T, SessionVar[T]](dflt)
S.session.flatMap(_.get(name)).isDefined || (S.session.flatMap(_.get(bn)) openOr false)
}
-
-
-
-
protected override def registerCleanupFunc(in: LiftSession => Unit): Unit =
S.session.foreach(_.addSessionCleanup(in))
@@ -450,7 +454,7 @@ private[http] object TransientRequestVarHandler extends CoreRequestVarHandler {
type MyType = TransientRequestVar[_]
}
-private[http] trait CoreRequestVarHandler {
+private[http] trait CoreRequestVarHandler extends Logger {
type MyType <: HasLogUnreadVal
private val logger = Logger(classOf[CoreRequestVarHandler])
@@ -483,9 +487,21 @@ private[http] trait CoreRequestVarHandler {
)
}
+ private def backingStore: Box[HashMap[String, (MyType, Any, Boolean)]] =
+ vals.value match {
+ case null =>
+ if (LiftRules.throwOnOutOfScopeVarAccess) {
+ throw new IllegalAccessException("Access to Var outside a request or comet actor scope")
+ } else {
+ logger.warn("Access to Var outside a request or comet actor scope")
+ }
+ None
+ case x => Full(x)
+ }
+
private[http] def get[T](name: String): Box[T] =
for {
- ht <- Box.legacyNullTest(vals.value)
+ ht <- backingStore
(rvInstance,value,unread) <- ht.get(name)
} yield {
if (unread) {
@@ -496,11 +512,11 @@ private[http] trait CoreRequestVarHandler {
}
private[http] def set[T](name: String, from: MyType, value: T): Unit =
- for (ht <- Box.legacyNullTest(vals.value))
+ for (ht <- backingStore)
ht(name) = (from, value, true)
private[http] def clear(name: String): Unit =
- for (ht <- Box.legacyNullTest(vals.value))
+ for (ht <- backingStore)
ht -= name
private[http] def addCleanupFunc(f: Box[LiftSession] => Unit): Unit =

0 comments on commit e258f20

Please sign in to comment.