<commit_before>package bootstrap.liftweb

import scala.xml.{Text,NodeSeq}
import net.liftweb.common.{LazyLoggable,Box,Full}
import net.liftweb.actor.LiftActor
import net.liftweb.util.{Helpers,Props}
import net.liftweb.http.{LiftRules,S,Req,GetRequest,LiftSession,
  SessionMaster,SessionWatcherInfo,RequestVar,LiftResponse}
import net.liftweb.sitemap.{SiteMap,Menu,Loc}

// extended sessions
import net.liftweb.mapper.{StandardDBVendor,MapperRules,DefaultConnectionIdentifier,DB,Schemifier}
import sample.model.{User,ExtendedSession}

// jmx monitoring
import com.twitter.ostrich.{RuntimeEnvironment, ServiceTracker, Stats, StatsMBean, Service}
import net.lag.configgy.Config

class Boot extends LazyLoggable with Service {
  def boot {
    // http
    LiftRules.addToPackages("sample")
    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
    LiftRules.setSiteMap(SiteMap(Application.sitemap:_*))
    
    DefaultConnectionIdentifier.jndiName = "jdbc/liftinaction"
    if (!DB.jndiJdbcConnAvailable_?){
      DB.defineConnectionManager(DefaultConnectionIdentifier, Application.database)
      LiftRules.unloadHooks.append(() => Application.database.closeAllConnections_!()) 
    }
    
    logger.info("About to schemify...")
    Schemifier.schemify(true, Schemifier.infoF _, User, ExtendedSession)
    S.addAround(DB.buildLoanWrapper)
    
    // extended sessions
    S.addAround(ExtendedSession.requestLoans)
    LiftRules.liftRequest.append { 
      case Req("classpath" :: _, _, _) => true
      case Req("favicon" :: Nil, "ico", GetRequest) => false
      case Req(_, "css", GetRequest) => false 
      case Req(_, "js", GetRequest) => false 
    }
    
    // comet configuration
    // import net.liftweb.http.provider.servlet.containers.Jetty7AsyncProvider
    // LiftRules.servletAsyncProvider = new Jetty7AsyncProvider(_)
    
    StatsMBean("manning.lia.sample")
    
    // jmx monitoring
    //if (Props.getBool("jmx.enable", false))
    logger.info("Booting Ostrich...")
    val runtime = new RuntimeEnvironment(getClass)
    var config = new Config
    config("admin_http_port") = 9990
    ServiceTracker.register(this)
    ServiceTracker.startAdmin(config, runtime)
    
    Stats.makeGauge("current_session_count"){ 
      SessionMonitor.count.toDouble 
    }
    
    // session gauge
    SessionMaster.sessionWatchers = SessionMonitor :: SessionMaster.sessionWatchers
    
    // request timer
    LiftSession.onBeginServicing = RequestTimer.beginServicing _ ::
      LiftSession.onBeginServicing
    
    LiftSession.onEndServicing = RequestTimer.endServicing _ ::
      LiftSession.onEndServicing
    
  }
  def shutdown(){}
  def quiesce(){}
}

object SessionMonitor extends LiftActor {
  private var sessionSize = 0
  protected def messageHandler = {
    case SessionWatcherInfo(sessions) => sessionSize = sessions.size
  }
  def count = sessionSize
}

object RequestTimer {
  object startTime extends RequestVar(0L)
  
  def beginServicing(session: LiftSession, req: Req){
    startTime(Helpers.millis)
  }
  
  def endServicing(session: LiftSession, req: Req, response: Box[LiftResponse]) {
    val delta = Helpers.millis - startTime.is
    Stats.addTiming("request_duration", delta.toInt)
  }
}

object Application {
  import Loc.ExtLink
  val sitemap = List(
    Menu("Home") / "index",
    Menu("Distributed Words") / "words",
    Menu(Loc("ostrich.graphs", ExtLink("http://127.0.0.1:9990/graph/"), "Ostrich: Graphs")) 
  ) ::: User.menus
  
  lazy val database = DBVendor
  object DBVendor extends StandardDBVendor(
    Props.get("db.class").openOr("org.h2.Driver"),
    Props.get("db.url").openOr("jdbc:h2:mem:chapter_fourteen;DB_CLOSE_DELAY=-1"),
    Props.get("db.user"),
    Props.get("db.pass"))
}


<commit_msg>Added exception handler to chap 14<commit_after>package bootstrap.liftweb

import scala.xml.{Text,NodeSeq}
import net.liftweb.common.{LazyLoggable,Box,Full}
import net.liftweb.actor.LiftActor
import net.liftweb.util.{Helpers,Props}
import net.liftweb.http.{LiftRules,S,Req,GetRequest,LiftSession,
  SessionMaster,SessionWatcherInfo,RequestVar,LiftResponse,RedirectResponse}
import net.liftweb.sitemap.{SiteMap,Menu,Loc}

// extended sessions
import net.liftweb.mapper.{StandardDBVendor,MapperRules,DefaultConnectionIdentifier,DB,Schemifier}
import sample.model.{User,ExtendedSession}

// jmx monitoring
import com.twitter.ostrich.{RuntimeEnvironment, ServiceTracker, Stats, StatsMBean, Service}
import net.lag.configgy.Config

class Boot extends LazyLoggable {
  def boot {
    // http
    LiftRules.addToPackages("sample")
    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
    LiftRules.setSiteMap(SiteMap(Application.sitemap:_*))
    
    DefaultConnectionIdentifier.jndiName = "jdbc/liftinaction"
    if (!DB.jndiJdbcConnAvailable_?){
      DB.defineConnectionManager(DefaultConnectionIdentifier, Application.database)
      LiftRules.unloadHooks.append(() => Application.database.closeAllConnections_!()) 
    }
    
    logger.info("About to schemify...")
    Schemifier.schemify(true, Schemifier.infoF _, User, ExtendedSession)
    S.addAround(DB.buildLoanWrapper)
    
    // extended sessions
    S.addAround(ExtendedSession.requestLoans)
    LiftRules.liftRequest.append { 
      case Req("classpath" :: _, _, _) => true
      case Req("favicon" :: Nil, "ico", GetRequest) => false
      case Req(_, "css", GetRequest) => false 
      case Req(_, "js", GetRequest) => false 
    }
    
    // comet configuration
    // import net.liftweb.http.provider.servlet.containers.Jetty7AsyncProvider
    // LiftRules.servletAsyncProvider = new Jetty7AsyncProvider(_)
    
    // exception handler
    LiftRules.exceptionHandler.prepend {
      case (Props.RunModes.Production, _, exception) => RedirectResponse("/error")
    }
    
    StatsMBean("manning.lia.sample")
    
    // jmx monitoring
    //if (Props.getBool("jmx.enable", false))
    logger.info("Booting Ostrich...")
    val runtime = new RuntimeEnvironment(getClass)
    var config = new Config
    config("admin_http_port") = 9990
    ServiceTracker.register(RequestTimer)
    ServiceTracker.startAdmin(config, runtime)
    
    Stats.makeGauge("current_session_count"){ 
      SessionMonitor.count.toDouble 
    }
    
    // session gauge
    SessionMaster.sessionWatchers = SessionMonitor :: SessionMaster.sessionWatchers
    
    // request timer
    LiftSession.onBeginServicing = RequestTimer.beginServicing _ ::
      LiftSession.onBeginServicing
    
    LiftSession.onEndServicing = RequestTimer.endServicing _ ::
      LiftSession.onEndServicing
    
  }

}

object SessionMonitor extends LiftActor {
  private var sessionSize = 0
  protected def messageHandler = {
    case SessionWatcherInfo(sessions) => sessionSize = sessions.size
  }
  def count = sessionSize
}

object RequestTimer extends Service {
  object startTime extends RequestVar(0L)
  
  def beginServicing(session: LiftSession, req: Req){
    startTime(Helpers.millis)
  }
  
  def endServicing(session: LiftSession, req: Req, response: Box[LiftResponse]) {
    val delta = Helpers.millis - startTime.is
    Stats.addTiming("request_duration", delta.toInt)
  }
  def shutdown(){}
  def quiesce(){}
}

object Application {
  import Loc.ExtLink
  val sitemap = List(
    Menu("Home") / "index",
    Menu("Distributed Words") / "words",
    Menu(Loc("ostrich.graphs", ExtLink("http://127.0.0.1:9990/graph/"), "Ostrich: Graphs")) 
  ) ::: User.menus
  
  lazy val database = DBVendor
  object DBVendor extends StandardDBVendor(
    Props.get("db.class").openOr("org.h2.Driver"),
    Props.get("db.url").openOr("jdbc:h2:mem:chapter_fourteen;DB_CLOSE_DELAY=-1"),
    Props.get("db.user"),
    Props.get("db.pass"))
}



{'fraction_non_alphanumeric': 0.11813471502590674, 'fraction_numerical': 0.005958549222797927, 'mean_word_length': 3.958895311496468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491487', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2433, 'n_words': 414}