PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/razpub/src/com/razie/pub/webui/DrawCallback.scala

http://razpub.googlecode.com/
Scala | 90 lines | 56 code | 16 blank | 18 comment | 0 complexity | 834207eab20129b79b449f83b2e80de1 MD5 | raw file
 1/**
 2 * Razvan's public code. Copyright 2008 based on Apache license (share alike) see LICENSE.txt for
 3 * details. No warranty implied nor any liability assumed for this code.
 4 */
 5package com.razie.pub.draw
 6
 7import com.razie.pub.comms._
 8import razie.base.ActionItem
 9import com.razie.pub.base._
10//import com.razie.pub.agent._
11import com.razie.pub.base.log._
12import razie.base._
13import razie.draw._
14
15/** 
16 * often you want different screens pre-built and invoked at a later time. 
17 * This class allows you to register a pre-built screen and you get an invocable URL for that screen
18 * 
19 * you can either pre-build a Drawable3 or inject a function that will build it dynamically
20 */
21object DrawCallback {
22   type CallbackFun = (AttrAccess) => Drawable
23   
24   def apply (screen:Drawable, validSec:Int) = DrawCallback1 (screen, validSec)
25   def apply (screen:CallbackFun, validSec:Int) = DrawCallback2 (screen, validSec)
26}
27
28/** TODO FIXME this is not multi-thread safe... */
29protected object MyCache {
30   final val MAXTIMEMSEC = 5 * 60 * 1000
31   
32   var counter:Int = 1
33   
34   var cache : collection.mutable.Map[String,(AnyRef,Int, Long)] = 
35     new collection.mutable.HashMap[String, (AnyRef,Int, Long)]()
36
37   def put (screen:Drawable, validSec:Int) : String = 
38      iput (screen,validSec)
39      
40   def put (screen:DrawCallback.CallbackFun, validSec:Int) : String = 
41      iput (screen,validSec)
42   
43   private def iput (screen:AnyRef, validSec:Int) : String = {
44     clean
45     val url:String = counter.toString
46     counter+=1
47     cache.put (url,(screen,validSec, System.currentTimeMillis()))
48     url
49     }
50  
51   /** the return is either a Drawable3 or a CallbackFun */
52   def get (url:String) : Option[AnyRef] = {
53      clean
54      cache.get(url) match {
55        case Some((d, _, _)) => new Some(d)
56        case None => None
57        }
58     }
59    
60   // TODO unit-test this
61   def clean () = {
62     // TODO only do this every like 1 min - or better, based on size, not more often, eh?
63     val curt = System.currentTimeMillis
64     cache.retain((x,y) => (curt - y._3 < MAXTIMEMSEC))
65   }
66   
67   def clear () = cache.clear
68   
69   def keys = cache.keys
70}
71
72/** often you want different screens pre-built and invoked at a later time. 
73 * This class allows you to register a pre-built screen and you get an invocable URL for that screen
74 */
75abstract class DrawCallback (screen:AnyRef, validSec:Int, url:String) 
76   extends ServiceActionToInvoke (classOf[DrawScreenService].getSimpleName, 
77                                  DrawScreenServiceStatic.DRAW, "screen", 
78                                  url) {
79     DrawScreenServiceStatic.autoRegister
80}
81
82case class DrawCallback1 (screen:Drawable, validSec:Int) extends DrawCallback(screen, validSec, MyCache.put (screen,validSec))
83case class DrawCallback2 (screen:DrawCallback.CallbackFun, validSec:Int) extends DrawCallback(screen, validSec, MyCache.put (screen,validSec))
84
85class InternalDrawCallback (screenNo:String) 
86   extends ServiceActionToInvoke (classOf[DrawScreenService].getSimpleName, 
87                                  DrawScreenServiceStatic.DRAW, "screen", 
88                                  screenNo) {
89     DrawScreenServiceStatic.autoRegister
90}