Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

LiftTemplateEngine.scala

By: a guest on Feb 27th, 2012  |  syntax: Scala  |  size: 4.35 KB  |  views: 87  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * Copyright 2010-2011 WorldWide Conferencing, LLC
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package code
  18. package lib
  19.  
  20. import java.io.File
  21.  
  22. import tools.nsc.Global
  23.  
  24. import org.fusesource.scalate.layout.DefaultLayoutStrategy
  25. import org.fusesource.scalate.util.{ClassPathBuilder, FileResourceLoader}
  26. import org.fusesource.scalate.{DefaultRenderContext, ResourceNotFoundException, Binding, TemplateEngine}
  27.  
  28. import net.liftweb.common._
  29. import net.liftweb.http.LiftRules
  30. import net.liftweb.http.provider.servlet.HTTPServletContext
  31.  
  32.  
  33. /**
  34.  * A TemplateEngine using the Lift web abstractions.
  35.  */
  36. class LiftTemplateEngine extends TemplateEngine with Loggable {
  37.   bindings = List(Binding("context", classOf[DefaultRenderContext].getName, true, isImplicit = true))
  38.  
  39.   if (useWebInfWorkingDirectory) {
  40.     val path = realPath("WEB-INF")
  41.     if (path != null) {
  42.       workingDirectory = new File(path, "_scalate")
  43.       workingDirectory.mkdirs
  44.     }
  45.   }
  46.   classpath = buildClassPath
  47.   resourceLoader = new LiftResourceLoader(this)
  48.   layoutStrategy = new DefaultLayoutStrategy(this, "/WEB-INF/scalate/layouts/default.scaml", "/WEB-INF/scalate/layouts/default.ssp", "/WEB-INF/scalate/layouts/default.jade")
  49.  
  50.   private def buildClassPath(): String = {
  51.     val builder = new ClassPathBuilder
  52.  
  53.     // Add containers class path
  54.     builder.addPathFrom(getClass)
  55.             .addPathFrom(classOf[TemplateEngine])
  56.             .addPathFrom(classOf[Product])
  57.             .addPathFrom(classOf[Global])
  58.  
  59.     // Always include WEB-INF/classes and all the JARs in WEB-INF/lib just in case
  60.     builder.addClassesDir(realPath("/WEB-INF/classes"))
  61.             .addLibDir(realPath("/WEB-INF/lib"))
  62.  
  63.     builder.classPath
  64.   }
  65.  
  66.   def useWebInfWorkingDirectory = {
  67.     val customWorkDir = System.getProperty("scalate.workingdir", "")
  68.     val property = System.getProperty("scalate.temp.workingdir", "")
  69.     println("using scalate.temp.workingdir: " + property)
  70.     property.toLowerCase != "true" && customWorkDir.length <= 0
  71.   }
  72.  
  73.   def realPath(uri: String): String = {
  74.     LiftRules.context match {
  75.       case http: HTTPServletContext => http.ctx.getRealPath(uri)
  76.       case c => logger.warn("Do not know how to get the real path of: " + uri + " for context: " + c); uri
  77.     }
  78.   }
  79.  
  80.   class LiftResourceLoader(context: LiftTemplateEngine) extends FileResourceLoader {
  81.     override protected def toFile(uri: String) = {
  82.       realFile(uri)
  83.     }
  84.  
  85.     protected def toFileOrFail(uri: String): File = {
  86.       val file = realFile(uri)
  87.       if (file == null) {
  88.         throw new ResourceNotFoundException(resource = uri, root = context.realPath("/"))
  89.       }
  90.       file
  91.     }
  92.  
  93.     /**
  94.      * Returns the real path for the given uri
  95.      */
  96.     def realPath(uri: String): String = {
  97.       val file = realFile(uri)
  98.       if (file != null) file.getPath else null
  99.     }
  100.  
  101.     /**
  102.      * Returns the File for the given uri
  103.      */
  104.     def realFile(uri: String): File = {
  105.       def findFile(uri: String): File = {
  106.         /*
  107.                 val url = LiftRules.context.resource(uri)
  108.                 if (url != null) {
  109.                   url.toFile
  110.                 }
  111.                 else {
  112.                   null
  113.                 }
  114.         */
  115.         val path = context.realPath(uri)
  116.         logger.debug("realPath for: " + uri + " is: " + path)
  117.  
  118.         var answer: File = null
  119.         if (path != null) {
  120.           val file = new File(path)
  121.           logger.debug("file from realPath for: " + uri + " is: " + file)
  122.           if (file.canRead) {answer = file}
  123.         }
  124.         answer
  125.       }
  126.  
  127.       findFile(uri) match {
  128.         case file: File => file
  129.         case _ => if (uri.startsWith("/") && !uri.startsWith("/WEB-INF")) {
  130.           findFile("/WEB-INF" + uri)
  131.         }
  132.         else {
  133.           null
  134.         }
  135.       }
  136.     }
  137.  
  138.   }
  139.  
  140. }