/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. */
  5. package com.razie.pub.draw
  6. import com.razie.pub.comms._
  7. import razie.base.ActionItem
  8. import com.razie.pub.base._
  9. //import com.razie.pub.agent._
  10. import com.razie.pub.base.log._
  11. import razie.base._
  12. import razie.draw._
  13. /**
  14. * often you want different screens pre-built and invoked at a later time.
  15. * This class allows you to register a pre-built screen and you get an invocable URL for that screen
  16. *
  17. * you can either pre-build a Drawable3 or inject a function that will build it dynamically
  18. */
  19. object DrawCallback {
  20. type CallbackFun = (AttrAccess) => Drawable
  21. def apply (screen:Drawable, validSec:Int) = DrawCallback1 (screen, validSec)
  22. def apply (screen:CallbackFun, validSec:Int) = DrawCallback2 (screen, validSec)
  23. }
  24. /** TODO FIXME this is not multi-thread safe... */
  25. protected object MyCache {
  26. final val MAXTIMEMSEC = 5 * 60 * 1000
  27. var counter:Int = 1
  28. var cache : collection.mutable.Map[String,(AnyRef,Int, Long)] =
  29. new collection.mutable.HashMap[String, (AnyRef,Int, Long)]()
  30. def put (screen:Drawable, validSec:Int) : String =
  31. iput (screen,validSec)
  32. def put (screen:DrawCallback.CallbackFun, validSec:Int) : String =
  33. iput (screen,validSec)
  34. private def iput (screen:AnyRef, validSec:Int) : String = {
  35. clean
  36. val url:String = counter.toString
  37. counter+=1
  38. cache.put (url,(screen,validSec, System.currentTimeMillis()))
  39. url
  40. }
  41. /** the return is either a Drawable3 or a CallbackFun */
  42. def get (url:String) : Option[AnyRef] = {
  43. clean
  44. cache.get(url) match {
  45. case Some((d, _, _)) => new Some(d)
  46. case None => None
  47. }
  48. }
  49. // TODO unit-test this
  50. def clean () = {
  51. // TODO only do this every like 1 min - or better, based on size, not more often, eh?
  52. val curt = System.currentTimeMillis
  53. cache.retain((x,y) => (curt - y._3 < MAXTIMEMSEC))
  54. }
  55. def clear () = cache.clear
  56. def keys = cache.keys
  57. }
  58. /** often you want different screens pre-built and invoked at a later time.
  59. * This class allows you to register a pre-built screen and you get an invocable URL for that screen
  60. */
  61. abstract class DrawCallback (screen:AnyRef, validSec:Int, url:String)
  62. extends ServiceActionToInvoke (classOf[DrawScreenService].getSimpleName,
  63. DrawScreenServiceStatic.DRAW, "screen",
  64. url) {
  65. DrawScreenServiceStatic.autoRegister
  66. }
  67. case class DrawCallback1 (screen:Drawable, validSec:Int) extends DrawCallback(screen, validSec, MyCache.put (screen,validSec))
  68. case class DrawCallback2 (screen:DrawCallback.CallbackFun, validSec:Int) extends DrawCallback(screen, validSec, MyCache.put (screen,validSec))
  69. class InternalDrawCallback (screenNo:String)
  70. extends ServiceActionToInvoke (classOf[DrawScreenService].getSimpleName,
  71. DrawScreenServiceStatic.DRAW, "screen",
  72. screenNo) {
  73. DrawScreenServiceStatic.autoRegister
  74. }