alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (Comet.scala)

This example Lift Framework source code file (Comet.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

box, comet, comet, cometfailureexception, cometfailureexception, cometnotfoundexception, comettimeoutexception, full, nodeseq, nodeseq, null, snippetfailureexception, string, string

The Lift Framework Comet.scala source code

/*
 * Copyright 2007-2011 WorldWide Conferencing, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.liftweb
package builtin
package snippet

import scala.xml._
import net.liftweb.http._
import net.liftweb.util._
import net.liftweb.common._

import Helpers._

object Comet extends DispatchSnippet with LazyLoggable {
  def dispatch : DispatchIt = {
    case _ => render _
  }

  def render(kids: NodeSeq) : NodeSeq = {

    Props.inGAE match {
      case true => Text("Comet Disabled in Google App Engine")
      case _ =>  buildComet(kids)
    }
  }

  private def buildSpan(timeb: Box[Long], xml: NodeSeq, cometActor: LiftCometActor, spanId: String): NodeSeq =
  Elem(cometActor.parentTag.prefix, cometActor.parentTag.label, cometActor.parentTag.attributes,
       cometActor.parentTag.scope, Group(xml)) %
  (new UnprefixedAttribute("id", Text(spanId), Null)) %
  (timeb.filter(_ > 0L).map(time => (new PrefixedAttribute("lift", "when", Text(time.toString), Null))) openOr Null)
    
  private def buildComet(kids: NodeSeq) : NodeSeq = {
    val theType: Box[String] = S.attr.~("type").map(_.text)
    val name: Box[String] = S.currentAttr("name") or
    S.currentAttr("metaname").flatMap(S.param) or
    S.currentAttr("randomname").map(ignore => Helpers.nextFuncName)


    (for {ctx <- S.session} yield {
      if (!ctx.stateful_?) 
        throw new StateInStatelessException(
          "Lift does not support Comet for stateless requests")

       try {
         ctx.findComet(theType, name, kids, S.attrsFlattenToMap).map {
           c => {
             // Update the view on each page load in dev mode
             // this make development easier
             if (Props.devMode) {
               c ! UpdateDefaultXml(kids)
             }
             
             (c.!?(26600L, AskRender)) match {
               case Full(AnswerRender(response, _, when, _)) if c.hasOuter =>
                 buildSpan(Empty, c.buildSpan(when, response.inSpan) ++ response.outSpan, c, c.uniqueId+"_outer")
               
               case Full(AnswerRender(response, _, when, _)) =>
                 c.buildSpan(when, response.inSpan)
               
               case _ => 
                 throw new CometTimeoutException("type: "+theType+" name: "+name)
             }}} openOr {
               throw new CometNotFoundException("type: "+theType+" name: "+name)
             }
         
       } catch {
         case e: SnippetFailureException => throw e
         case e: Exception => logger.error("Failed to find a comet actor", e); kids
       }
    }) openOr {
      throw new CometNotFoundException("Session not found. type: "+theType+" name: "+name)
    }
  }
}

abstract class CometFailureException(msg: String) extends SnippetFailureException(msg) {
   override def buildStackTrace: NodeSeq = <div>{msg}
++ super.buildStackTrace } class CometTimeoutException(msg: String) extends CometFailureException(msg) { def snippetFailure: LiftRules.SnippetFailures.Value = LiftRules.SnippetFailures.CometTimeout } class CometNotFoundException(msg: String) extends CometFailureException(msg) { def snippetFailure: LiftRules.SnippetFailures.Value = LiftRules.SnippetFailures.CometNotFound }

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework Comet.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.