Whistle-­‐stop	
  tour	
  of	
  the	
  Li#	
  web	
  framework	
  and	
  
                           demonstra:ng	
  why	
  the	
  real-­‐:me	
  web	
  	
  
                                         should	
  ma=er	
  to	
  you	
  
www.devoxx.com	
  
Agenda	
  
                     !    Overview	
  of	
  Scala	
  
                     !     Li#’s	
  Ra:onale	
  
                     !      The	
  real-­‐:me	
  web	
  
                     !       Li#’s	
  awesome	
  feature	
  set	
  
                     !        Why	
  Scala	
  makes	
  Li#	
  possible	
  
www.devoxx.com	
  
About	
  Me	
  
                     !  Project	
  commi=er	
  on	
  Li#	
  
                     !  Manufacturing	
  and	
  marke:ng	
  automa:on	
  is	
  
                       my	
  day-­‐job	
  (industrial	
  systems)	
  
                     !  Whole	
  raH	
  of	
  experience	
  with	
  both	
  dynamic	
  
                       and	
  sta:c	
  languages	
  
                     !  Desktop	
  and	
  Web	
  applica:on	
  development	
  
www.devoxx.com	
  




                     !  Coding	
  Scala	
  and	
  LiH	
  since	
  end	
  of	
  2007	
  
What	
  is	
  Scala?	
  (10,000	
  foot	
  view!)	
  
                     !    Scala	
  is	
  func:onal	
  
                     !     Scala	
  is	
  object	
  orientated	
  
                     !      Scala	
  is	
  mul:-­‐paradigm!	
  OO	
  and	
  FP	
  hybrid...	
  
                     !       Designed	
  by	
  Mar:n	
  Odersky	
  (Java	
  generics)	
  
                     !        Runs	
  as	
  fast	
  as	
  na:ve	
  Java	
  code	
  
                     !         Fully	
  interoperable	
  with	
  exis:ng	
  Java	
  libraries	
  
www.devoxx.com	
  




                     !          More	
  type-­‐safe	
  than	
  Java	
  
Scala	
  in	
  the	
  wild?	
  
www.devoxx.com	
  
Scala	
  in	
  breif….	
  
                     !  What	
  follows	
  is	
  an	
  overview	
  of	
  core	
  Scala	
  
                       language	
  features	
  
www.devoxx.com	
  
Immutability	
  
                     val example = “Devoxx”	

                     !  Immutable	
  values	
  are	
  assigned	
  with	
  the	
  	
  
                       “val”	
  keyword	
  
                     !  Immutability	
  is	
  your	
  long-­‐lost	
  friend:	
  
                         !  	
  No	
  need	
  to	
  synchronise	
  
                         !  	
  Never	
  need	
  to	
  copy	
  vars	
  “just	
  in	
  case”	
  
                         !  	
  Great	
  for	
  hash	
  keys	
  
www.devoxx.com	
  




                     !  Be=er	
  for	
  garbage	
  collector	
  
Case	
  Classes	
  
                     case class Foo(bar: String, baz: List[Int])	

                     !  Func:onality	
  you	
  get	
  for	
  free:	
  
                         !    	
  “new”	
  keyword	
  is	
  redundant	
  
                         !     	
  bar	
  and	
  baz	
  proper:es	
  (immutable	
  by	
  default)	
  
                         !      	
  toString,	
  hashCode,	
  and	
  equals	
  et	
  al	
  
                         !       	
  Pa=ern	
  matching	
  with	
  parameter	
  extrac:on	
  
www.devoxx.com	
  
Singletons	
  
                     object ChatServer extends Actor	

                     !  ChatServer	
  is	
  a	
  singleton	
  
                     !  One	
  instance	
  per	
  classloader	
  
                     !  Can	
  be	
  passed	
  as	
  parameter…	
  it’s	
  an	
  instance	
  
www.devoxx.com	
  
Composi:on	
  with	
  Traits	
  
                     !  Finely-­‐grained	
  aggrega:on	
  of	
  func:onality	
  
                     !  Mul:ple-­‐inheritance	
  without	
  diamond	
  problem[1]	
  
                     !  Extensively	
  used	
  in	
  Li#	
  
                         object sally extends Person(“Sally”) 	
                           with Female 	
                           with Runner	

                         def womansRun(who: Female with Runner) 	
www.devoxx.com	
  




                           //=> womansRun(sally)	
                         [1]	
  h=p://en.wikipedia.org/wiki/Diamond_problem	
  
Func:on	
  Passing	
  
                     !  Encapsulates	
  logic	
  concisely	
  
                     !  Wildcard	
  shorthand	
  keeps	
  things	
  simple	
  
                     !  Func:ons	
  are	
  instances	
  that	
  can	
  be	
  passed	
  
                     // make x a function that lower cases input	
                     val x: (String) => String = (str) => str.toLowerCase	
www.devoxx.com	
  




                     // alter the request encoding	
                     LiftRules.early { _.setCharacterEncoding(“UTF-8”) }
Pa=ern	
  Matching	
  
                     !  Match	
  against	
  case	
  classes	
  
                        !  	
  Parameter	
  extrac:on	
  
                        !  	
  Great	
  for	
  message	
  /	
  event	
  handling	
  
                        !  	
  Test	
  against	
  parameters:	
  case Person(name,     35) =>	

                     !  Declara:ve	
  expression	
  of	
  logic	
  
                         case Messages(m) => msgs = m	
www.devoxx.com	
  




                         case s: String if s.length > 0 => msgs ::= s
XML	
  Literals	
  	
  
                     !  XML	
  is	
  first-­‐class	
  
                     !  Ripe	
  for	
  templa:ng	
  abuse!	
  
                     !  Very	
  handy	
  for	
  development	
  but	
  Li#	
  provides	
  
                       strong	
  abstrac:ons	
  for	
  templates	
  
                       val links = (“Link A”, http://…) :: 	
                          	(“Link B”, “http://…”) :: (“Link C”, https://…) :: Nil	
www.devoxx.com	
  




                       val myLinkList = 	
                         <ul>{ links.flatMap({case (name,url) =>	
                          	<li><a href={url}>{name}</a></li>}) }	
                         </ul>
Actors	
  
                     !    Being	
  “real-­‐:me”	
  means	
  events	
  
                     !     Lightweight	
  concurrency	
  model	
  	
  
                     !      With	
  very	
  nice	
  syntax	
  
                     !       Implemented	
  as	
  a	
  library!	
  
                         object DevoxxActor extends Actor { …… }	
                         case class Talk(name: String)	
www.devoxx.com	
  




                         // asyncronusly send the talk message to the actor	
                         DevoxxActor ! Talk(“Lift web framework”)
What	
  is	
  LiH?	
  
                     !    Web	
  framework	
  wri=en	
  in	
  Scala	
  
                     !     3	
  years	
  old,	
  founded	
  by	
  David	
  Pollak	
  
                     !      Leverages	
  the	
  best	
  of	
  Scala	
  language	
  features	
  
                     !       Takes	
  the	
  best	
  ideas	
  from	
  other	
  frameworks	
  
                     !        Makes	
  real-­‐:me	
  web	
  applica:ons	
  accessible	
  
                     !         Massively	
  scalable	
  
www.devoxx.com	
  
Why	
  another	
  framework?	
  
                     !  Developers	
  needn't	
  be	
  plumbers	
  
                     !  Unifies	
  the	
  best	
  of	
  other	
  framework	
  concepts	
  
                         !  	
  Granular	
  sessions	
  and	
  security	
  a	
  la	
  Seaside	
  
                         !  	
  Flash-­‐to-­‐bang	
  produc:vity	
  a	
  la	
  Rails	
  
                         !  	
  Designer	
  friendly	
  templates	
  a	
  la	
  Wicket	
  
                     !  Advanced	
  features	
  like	
  comet	
  work	
  OOTB	
  
www.devoxx.com	
  




                     !  Tough	
  to	
  be	
  truly	
  real-­‐:me	
  with	
  exis:ng	
  tech	
  
Web	
  Beginnings	
  
                     !  Very	
  person	
  -­‐>	
  machine	
  affair	
  
                     !  Primary	
  uses:	
  	
  
                         !  	
  Shopping	
  
                         !  	
  Banking	
  
                         !  	
  CRUD	
  opera:ons	
  
                     !  56k	
  modems	
  (yikes!)	
  
www.devoxx.com	
  
Web	
  2.0	
  
                     !  Behaviour	
  shiH	
  -­‐>	
  more	
  social	
  
                     !  Different	
  usage	
  models:	
  
                         !  	
  Person	
  -­‐>	
  Machine	
  -­‐>	
  Machine	
  (mashups)	
  
                         !  	
  Person	
  -­‐>	
  Person	
  (Facebook,	
  Twi=er)	
  
                         !  	
  Machine	
  -­‐>	
  Machine	
  -­‐>	
  Person	
  (micro-­‐formats)	
  
                     !  Internet	
  becomes	
  society	
  cornerstone	
  	
  
www.devoxx.com	
  
Real	
  Time	
  Web	
  
                     !    Users	
  demand	
  instant	
  gra:fica:on	
  
                     !     Peer-­‐to-­‐peer	
  news	
  /	
  informa:on	
  streams	
  
                     !      Business	
  informa:on	
  in	
  constant	
  flux	
  
                     !       Online	
  interac:ve	
  games	
  
                     !        Real	
  Time	
  Web	
  is	
  the	
  next	
  evolu2onary	
  step...	
  
www.devoxx.com	
  
View	
  First	
  
                     !  Its	
  not	
  MVC	
  
                     !  Your	
  typical	
  page	
  has	
  more	
  than	
  a	
  single	
  piece	
  
                       of	
  dynamic	
  content	
  
                     !  Code	
  in	
  the	
  view	
  usually	
  means	
  some	
  “seep”	
  
                       of	
  business	
  logic:	
  thus,	
  NO	
  code	
  in	
  the	
  view	
  
                     !  Only	
  well-­‐formed	
  XHTML;	
  no	
  string	
  
                       replacement	
  –	
  e.g.	
  {{myvar}}	
  
www.devoxx.com	
  




                     !  Designer	
  friendly	
  dynamic	
  content	
  markup	
  
Snippets	
  
                     !  A	
  “snippet”	
  in	
  Li#	
  represents	
  a	
  collec:on	
  of	
  
                       applica:on	
  logic	
  
                     !  One	
  snippet	
  can	
  be	
  called	
  on	
  n	
  number	
  of	
  
                       pages,	
  n	
  number	
  of	
  :mes	
  
                     !  A	
  snippet	
  is	
  a	
  normal	
  Scala	
  class	
  or	
  object:	
  
                       there	
  is	
  no	
  black	
  magic	
  
www.devoxx.com	
  




                     !  Lookup	
  via	
  reflec:on	
  or	
  manual	
  wiring	
  
What	
  a	
  bind…	
  
                     class Example {	
                        def nameForm(xhtml: NodeSeq) = {	
                           var name = “”	
                           def doSubmit { println(“Submitted…”); }	
                           bind("f", xhtml, 	
                              "name" -> SHtml.text(name, name = _),	
                              "submit" -> SHtml.submit(doSubmit _)	
                           )	
                        }	
                     }	
                     <lift:example.name_form>	
                       <p><f:name /></p>	
www.devoxx.com	
  




                       <p><f:submit /></p>	
                     </lift:example.name_form>
Security	
  
                     !  Page	
  elements	
  have	
  session	
  specific	
  opaque	
  
                       GUIDs	
  to	
  reference	
  server	
  components	
  
                       (impossible	
  to	
  do	
  CSRF[1])	
  
                       <input name=“F677071620957IFD” type=“text” />	

                     !  Callback	
  func:ons	
  also	
  use	
  similar	
  GUIDs	
  so	
  
                       that	
  sensi:ve	
  IDs	
  etc	
  are	
  not	
  exposed	
  	
  
www.devoxx.com	
  




                       /somepath?F677071620979LIK=_	

                       [1]	
  h=p://en.wikipedia.org/wiki/Cross-­‐site_request_forgery	
  
Site	
  Map	
  
                     !  	
  Declara:ve	
  and	
  granular	
  defini:on	
  of	
  page	
  
                       and	
  UI	
  access	
  rules	
  
                     !  Enforced	
  long	
  before	
  the	
  page	
  content	
  is	
  
                       processed	
  or	
  rendered	
  –	
  its	
  very	
  fast.	
  
                     !  Can	
  generate	
  site	
  menus	
  even	
  with	
  complex	
  
                       rules	
  on	
  what	
  different	
  users	
  see	
  
www.devoxx.com	
  




                     !  Unified	
  access	
  control	
  model	
  
Persistence	
  
                     !  Li#	
  includes	
  “Mapper”	
  which	
  is	
  a	
  lightweight	
  
                       ORM	
  of	
  similar	
  design	
  to	
  Ac:veRecord	
  in	
  Rails	
  
                     !  Use	
  any	
  backend	
  persistence	
  system	
  you	
  
                       want	
  (Hibernate,	
  JPA	
  etc)	
  
                     !  Write	
  custom	
  back-­‐ends	
  using	
  Li#	
  Record	
  –	
  
                       provides	
  OOTB	
  valida:on	
  and	
  CRUD	
  
                       seman:cs;	
  you	
  provide	
  the	
  implementa:on	
  
www.devoxx.com	
  
REST	
  and	
  WebServices	
  
                     !  REST	
  via	
  state(full/less)	
  dispatching	
  
                     !  Declara:ve	
  rules	
  for	
  URI	
  -­‐>	
  handler	
  dispatch	
  
                     !  LiH	
  has	
  awesome	
  support	
  for	
  JSON:	
  
                         !  	
  Slick	
  DSL	
  for	
  construc:on	
  /	
  parsing	
  
                         !  	
  ~300	
  :mes	
  faster	
  than	
  Scala	
  JSON	
  parser	
  
                         !  	
  Faster	
  deseriliza:on	
  than	
  Java	
  
www.devoxx.com	
  




                     !  Na:ve,	
  first-­‐class	
  XML	
  support	
  in	
  Scala	
  
Technology	
  Support	
  
                     !    AMQP	
  
                     !     Tex:le	
  
                     !      XMPP	
  
                     !       OpenID	
  
                     !        JTA	
  
                     !         PayPal	
  
www.devoxx.com	
  




                     !          …and	
  lots	
  more!	
  
Summary	
  
                     !    Scala’s	
  object	
  model	
  is	
  a	
  superset	
  of	
  Java’s	
  
                     !     Scala	
  traits:	
  powerful	
  and	
  granular	
  composi:on	
  
                     !      Scala	
  is	
  syntac:cally	
  simpler	
  than	
  Java	
  or	
  Ruby	
  
                     !       Scala’s	
  design	
  led	
  to	
  Li#’s	
  design	
  
                     !        Li#	
  makes	
  the	
  real-­‐:me	
  web	
  simple	
  
                     !         Li#	
  is	
  the	
  culmina:on	
  of	
  many	
  great	
  ideas	
  
www.devoxx.com	
  
Devoxx 2009: The Lift Framework

Devoxx 2009: The Lift Framework

  • 2.
    Whistle-­‐stop  tour  of  the  Li#  web  framework  and   demonstra:ng  why  the  real-­‐:me  web     should  ma=er  to  you   www.devoxx.com  
  • 3.
    Agenda   !  Overview  of  Scala   !  Li#’s  Ra:onale   !  The  real-­‐:me  web   !  Li#’s  awesome  feature  set   !  Why  Scala  makes  Li#  possible   www.devoxx.com  
  • 4.
    About  Me   !  Project  commi=er  on  Li#   !  Manufacturing  and  marke:ng  automa:on  is   my  day-­‐job  (industrial  systems)   !  Whole  raH  of  experience  with  both  dynamic   and  sta:c  languages   !  Desktop  and  Web  applica:on  development   www.devoxx.com   !  Coding  Scala  and  LiH  since  end  of  2007  
  • 5.
    What  is  Scala?  (10,000  foot  view!)   !  Scala  is  func:onal   !  Scala  is  object  orientated   !  Scala  is  mul:-­‐paradigm!  OO  and  FP  hybrid...   !  Designed  by  Mar:n  Odersky  (Java  generics)   !  Runs  as  fast  as  na:ve  Java  code   !  Fully  interoperable  with  exis:ng  Java  libraries   www.devoxx.com   !  More  type-­‐safe  than  Java  
  • 6.
    Scala  in  the  wild?   www.devoxx.com  
  • 7.
    Scala  in  breif….   !  What  follows  is  an  overview  of  core  Scala   language  features   www.devoxx.com  
  • 8.
    Immutability   val example = “Devoxx” !  Immutable  values  are  assigned  with  the     “val”  keyword   !  Immutability  is  your  long-­‐lost  friend:   !    No  need  to  synchronise   !    Never  need  to  copy  vars  “just  in  case”   !    Great  for  hash  keys   www.devoxx.com   !  Be=er  for  garbage  collector  
  • 9.
    Case  Classes   case class Foo(bar: String, baz: List[Int]) !  Func:onality  you  get  for  free:   !    “new”  keyword  is  redundant   !    bar  and  baz  proper:es  (immutable  by  default)   !    toString,  hashCode,  and  equals  et  al   !    Pa=ern  matching  with  parameter  extrac:on   www.devoxx.com  
  • 10.
    Singletons   object ChatServer extends Actor !  ChatServer  is  a  singleton   !  One  instance  per  classloader   !  Can  be  passed  as  parameter…  it’s  an  instance   www.devoxx.com  
  • 11.
    Composi:on  with  Traits   !  Finely-­‐grained  aggrega:on  of  func:onality   !  Mul:ple-­‐inheritance  without  diamond  problem[1]   !  Extensively  used  in  Li#   object sally extends Person(“Sally”) with Female with Runner def womansRun(who: Female with Runner) www.devoxx.com   //=> womansRun(sally) [1]  h=p://en.wikipedia.org/wiki/Diamond_problem  
  • 12.
    Func:on  Passing   !  Encapsulates  logic  concisely   !  Wildcard  shorthand  keeps  things  simple   !  Func:ons  are  instances  that  can  be  passed   // make x a function that lower cases input val x: (String) => String = (str) => str.toLowerCase www.devoxx.com   // alter the request encoding LiftRules.early { _.setCharacterEncoding(“UTF-8”) }
  • 13.
    Pa=ern  Matching   !  Match  against  case  classes   !    Parameter  extrac:on   !    Great  for  message  /  event  handling   !    Test  against  parameters:  case Person(name, 35) => !  Declara:ve  expression  of  logic   case Messages(m) => msgs = m www.devoxx.com   case s: String if s.length > 0 => msgs ::= s
  • 14.
    XML  Literals     !  XML  is  first-­‐class   !  Ripe  for  templa:ng  abuse!   !  Very  handy  for  development  but  Li#  provides   strong  abstrac:ons  for  templates   val links = (“Link A”, http://…) :: (“Link B”, “http://…”) :: (“Link C”, https://…) :: Nil www.devoxx.com   val myLinkList = <ul>{ links.flatMap({case (name,url) => <li><a href={url}>{name}</a></li>}) } </ul>
  • 15.
    Actors   !  Being  “real-­‐:me”  means  events   !  Lightweight  concurrency  model     !  With  very  nice  syntax   !  Implemented  as  a  library!   object DevoxxActor extends Actor { …… } case class Talk(name: String) www.devoxx.com   // asyncronusly send the talk message to the actor DevoxxActor ! Talk(“Lift web framework”)
  • 16.
    What  is  LiH?   !  Web  framework  wri=en  in  Scala   !  3  years  old,  founded  by  David  Pollak   !  Leverages  the  best  of  Scala  language  features   !  Takes  the  best  ideas  from  other  frameworks   !  Makes  real-­‐:me  web  applica:ons  accessible   !  Massively  scalable   www.devoxx.com  
  • 17.
    Why  another  framework?   !  Developers  needn't  be  plumbers   !  Unifies  the  best  of  other  framework  concepts   !    Granular  sessions  and  security  a  la  Seaside   !    Flash-­‐to-­‐bang  produc:vity  a  la  Rails   !    Designer  friendly  templates  a  la  Wicket   !  Advanced  features  like  comet  work  OOTB   www.devoxx.com   !  Tough  to  be  truly  real-­‐:me  with  exis:ng  tech  
  • 18.
    Web  Beginnings   !  Very  person  -­‐>  machine  affair   !  Primary  uses:     !    Shopping   !    Banking   !    CRUD  opera:ons   !  56k  modems  (yikes!)   www.devoxx.com  
  • 19.
    Web  2.0   !  Behaviour  shiH  -­‐>  more  social   !  Different  usage  models:   !    Person  -­‐>  Machine  -­‐>  Machine  (mashups)   !    Person  -­‐>  Person  (Facebook,  Twi=er)   !    Machine  -­‐>  Machine  -­‐>  Person  (micro-­‐formats)   !  Internet  becomes  society  cornerstone     www.devoxx.com  
  • 20.
    Real  Time  Web   !  Users  demand  instant  gra:fica:on   !  Peer-­‐to-­‐peer  news  /  informa:on  streams   !  Business  informa:on  in  constant  flux   !  Online  interac:ve  games   !  Real  Time  Web  is  the  next  evolu2onary  step...   www.devoxx.com  
  • 22.
    View  First   !  Its  not  MVC   !  Your  typical  page  has  more  than  a  single  piece   of  dynamic  content   !  Code  in  the  view  usually  means  some  “seep”   of  business  logic:  thus,  NO  code  in  the  view   !  Only  well-­‐formed  XHTML;  no  string   replacement  –  e.g.  {{myvar}}   www.devoxx.com   !  Designer  friendly  dynamic  content  markup  
  • 23.
    Snippets   !  A  “snippet”  in  Li#  represents  a  collec:on  of   applica:on  logic   !  One  snippet  can  be  called  on  n  number  of   pages,  n  number  of  :mes   !  A  snippet  is  a  normal  Scala  class  or  object:   there  is  no  black  magic   www.devoxx.com   !  Lookup  via  reflec:on  or  manual  wiring  
  • 24.
    What  a  bind…   class Example { def nameForm(xhtml: NodeSeq) = { var name = “” def doSubmit { println(“Submitted…”); } bind("f", xhtml, "name" -> SHtml.text(name, name = _), "submit" -> SHtml.submit(doSubmit _) ) } } <lift:example.name_form> <p><f:name /></p> www.devoxx.com   <p><f:submit /></p> </lift:example.name_form>
  • 25.
    Security   !  Page  elements  have  session  specific  opaque   GUIDs  to  reference  server  components   (impossible  to  do  CSRF[1])   <input name=“F677071620957IFD” type=“text” /> !  Callback  func:ons  also  use  similar  GUIDs  so   that  sensi:ve  IDs  etc  are  not  exposed     www.devoxx.com   /somepath?F677071620979LIK=_ [1]  h=p://en.wikipedia.org/wiki/Cross-­‐site_request_forgery  
  • 26.
    Site  Map   !    Declara:ve  and  granular  defini:on  of  page   and  UI  access  rules   !  Enforced  long  before  the  page  content  is   processed  or  rendered  –  its  very  fast.   !  Can  generate  site  menus  even  with  complex   rules  on  what  different  users  see   www.devoxx.com   !  Unified  access  control  model  
  • 27.
    Persistence   !  Li#  includes  “Mapper”  which  is  a  lightweight   ORM  of  similar  design  to  Ac:veRecord  in  Rails   !  Use  any  backend  persistence  system  you   want  (Hibernate,  JPA  etc)   !  Write  custom  back-­‐ends  using  Li#  Record  –   provides  OOTB  valida:on  and  CRUD   seman:cs;  you  provide  the  implementa:on   www.devoxx.com  
  • 28.
    REST  and  WebServices   !  REST  via  state(full/less)  dispatching   !  Declara:ve  rules  for  URI  -­‐>  handler  dispatch   !  LiH  has  awesome  support  for  JSON:   !    Slick  DSL  for  construc:on  /  parsing   !    ~300  :mes  faster  than  Scala  JSON  parser   !    Faster  deseriliza:on  than  Java   www.devoxx.com   !  Na:ve,  first-­‐class  XML  support  in  Scala  
  • 29.
    Technology  Support   !  AMQP   !  Tex:le   !  XMPP   !  OpenID   !  JTA   !  PayPal   www.devoxx.com   !  …and  lots  more!  
  • 30.
    Summary   !  Scala’s  object  model  is  a  superset  of  Java’s   !  Scala  traits:  powerful  and  granular  composi:on   !  Scala  is  syntac:cally  simpler  than  Java  or  Ruby   !  Scala’s  design  led  to  Li#’s  design   !  Li#  makes  the  real-­‐:me  web  simple   !  Li#  is  the  culmina:on  of  many  great  ideas   www.devoxx.com