Lift当中的AnyVarTrati源码解析
生活随笔
收集整理的這篇文章主要介紹了
Lift当中的AnyVarTrati源码解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Lift2.0當中,AnyVarTrait是一個非常重要的trait,很多重要的像RequestVar,SessionVar都是繼續該接口的,
首先看下大致接口代碼
abstract class AnyVar[T, MyType <: AnyVar[T, MyType]](dflt: => T) extends AnyVarTrait[T, MyType] {self: MyType =>protected def calcDefaultValue: T = dflt }/*** Abstract a request or a session scoped variable.*/ trait AnyVarTrait[T, MyType <: AnyVarTrait[T, MyType]] extends PSettableValueHolder[T] with HasCalcDefaultValue[T] { AnyVarTrait繼承了PSettableValueHolder,它的接口是: trait ValueHolder {type ValueTypedef is: ValueTypedef get: ValueType }trait SettableValueHolder extends ValueHolder {def set(in: ValueType): ValueType }trait PValueHolder[T] extends ValueHolder {type ValueType = T// def manifest: Manifest[T] }trait PSettableValueHolder[T] extends PValueHolder[T] with SettableValueHolder} }?
PSettableValueHolder主要是實現了類似于get,set的這樣的方法,比較簡單,寫一個比較簡單的實現PSettableValueHolder的例子:
class Exampletrait ValueHolder{type ValueTypedef is: ValueTypedef get: ValueType }trait SettableValueHolder extends ValueHolder{def set(in: ValueType): ValueType }trait PValueHolder[T] extends ValueHolder{type ValueType = T }trait PSettableValueHolder[T] extends PValueHolder[T] with SettableValueHolderclass TestPSettableValueHolder[T] extends PSettableValueHolder[T] {protected var testVal: T=_def set(in: T):T ={testVal=inin}def get:T={testVal}def is:T={testVal} }object Test{def main(args: Array[String]){val test=new TestPSettableValueHolder[String]test.set("Test22")println(test.get)} }需要重載的方法:
protected lazy val name = VarConstants.varPrefix+getClass.getName+"_"+__nameSaltprotected def findFunc(name: String): Box[T]protected def setFunc(name: String, value: T): Unitprotected def clearFunc(name: String): Unitprotected def wasInitialized(name: String): Booleanprotected def calcDefaultValue: T/*** A non-side-effecting test if the value was initialized*/protected def testWasSet(name: String): Booleanprotected def __nameSalt = ""type CleanUpParam/*** Different Vars require different mechanisms for synchronization. This method implements* the Var specific synchronization mechanism*/def doSync[F](f: => F): F?
實現了PSettableValueHolder[T] trait的方法:
?
/*** The current value of the variable*/def is: T = doSync {findFunc(name) match {case Full(v) => vcase _ => val ret = calcDefaultValuetestInitializedapply(ret)// Use findFunc so that we clear the "unread" flagfindFunc(name) match {case Full(v) => vcase _ => ret}}}private def testInitialized: Unit = doSync {if (!wasInitialized(name)) {registerCleanupFunc(_onShutdown _)}}/*** Shadow of the 'is' method*/def get: T = is/*** Shadow of the apply method*/def set(what: T): T = apply(what)該trait的apply實現:
/*** Set the session variable** @param what -- the value to set the session variable to*/def apply(what: T): T = {testInitializedsetFunc(name, what)what}?
最后一個該trait非常重要的方法 :在該var的生命周期內去修改它的Value:
/*** Change the value of the Var for the lifespan of the function*/def doWith[F](newVal: T)(f: => F): F = {val old = findFunc(name)setFunc(name, newVal)try {f} finally {old match {case Full(t) => setFunc(name, t)case _ => clearFunc(name)}}}再來看下該trait的具體實現:SessionVar和RequestVar
SessionVar,看下他的幾個主要實現AnyVarTrait的實現:
override protected def findFunc(name: String): Box[T] = S.session match {case Full(s) => s.get(name)case _ =>if (showWarningWhenAccessedOutOfSessionScope_?)logger.warn("Getting a SessionVar "+name+" outside session scope") // added warning per issue 188Empty}override protected def setFunc(name: String, value: T): Unit = S.session match {case Full(s) => s.set(name, value)case _ =>if (showWarningWhenAccessedOutOfSessionScope_?)logger.warn("Setting a SessionVar "+name+" to "+value+" outside session scope") // added warning per issue 188}/*** Different Vars require different mechanisms for synchronization. This method implements* the Var specific synchronization mechanism*/def doSync[F](f: => F): F = S.session match {case Full(s) =>// lock the session while the Var-specific lock object is found/createdval lockName = name + VarConstants.lockSuffixval lockObj = s.synchronized {s.get[AnyRef](lockName) match {case Full(lock) => lockcase _ => val lock = new AnyRefs.set(lockName, lock)lock}}// execute the query in the scope of the lock objlockObj.synchronized {f}case _ => f}RequestVar的實現:
override protected def findFunc(name: String): Box[T] = RequestVarHandler.get(name)override protected def setFunc(name: String, value: T): Unit = RequestVarHandler.set(name, this, value)override protected def clearFunc(name: String): Unit = RequestVarHandler.clear(name)override protected def wasInitialized(name: String): Boolean = {val bn = name + VarConstants.initedSuffixval old: Boolean = RequestVarHandler.get(bn) openOr falseRequestVarHandler.set(bn, this, true)old}/*** Different Vars require different mechanisms for synchronization. This method implements* the Var specific synchronization mechanism*/def doSync[F](f: => F): F = f // no sync necessary for RequestVars... always on the same thread轉載于:https://www.cnblogs.com/zoujiaxue/archive/2010/08/24/1807305.html
總結
以上是生活随笔為你收集整理的Lift当中的AnyVarTrati源码解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拖动无标题栏的窗体,需要处理的三个事件
- 下一篇: 数据库的小细节