Created
November 21, 2009 16:08
-
-
Save ArtemGr/240182 to your computer and use it in GitHub Desktop.
Here's how we use Velocity under GAE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.{File, FileInputStream} | |
import java.util.{Collections, Map, HashMap} | |
import org.apache.commons.collections.map.{ReferenceMap, AbstractReferenceMap} | |
import org.apache.velocity.app.VelocityEngine, org.apache.velocity.runtime.resource.{Resource, ResourceManager} | |
import org.apache.velocity.runtime.resource.loader.ResourceLoader | |
import org.apache.velocity.exception.ResourceNotFoundException | |
import org.apache.velocity.runtime.{RuntimeConstants, RuntimeServices}, org.apache.velocity.runtime.log.LogSystem | |
import org.apache.velocity.Template, org.apache.velocity.VelocityContext | |
class VelocityResourceManager extends ResourceManager { | |
import VelocityInit._ | |
var runtime: RuntimeServices = null | |
override def initialize (runtime: RuntimeServices): Unit = {this.runtime = runtime} | |
override def getResource (resourceName: String, resourceType: Int, encoding: String): Resource = { | |
val have = TEMPLATES_CACHE.get (resourceName) | |
if ((have ne null) && !have.isSourceModified) { | |
have.touch; have | |
} else { | |
println ("velocity; loading template: " + resourceName) | |
val template = new Template | |
template.setName (resourceName) | |
template.setEncoding (encoding) | |
template.setResourceLoader (RESOURCE_LOADER) | |
template.setRuntimeServices (runtime) | |
template.setLastModified (RESOURCE_LOADER.getLastModified (template)) | |
template.setModificationCheckInterval (2) | |
template.process | |
TEMPLATES_CACHE.put (resourceName, template); template | |
} } | |
override def getLoaderNameForResource (resourceName: String): String = "VelocityResourceManager" | |
} | |
object VelocityInit { | |
val TEMPLATES_DIR = new java.io.File ("WEB-INF/templates") .getAbsoluteFile | |
val TEMPLATES_CACHE = Collections.synchronizedMap ( | |
new ReferenceMap (AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT).asInstanceOf[Map[String, Template]]) | |
val RESOURCE_LOADER = new ResourceLoader { | |
override def getLastModified (resource: Resource) = new File (TEMPLATES_DIR, resource.getName) .lastModified | |
override def getResourceStream (source: String) = new FileInputStream (new File (TEMPLATES_DIR, source)) | |
override def init (configuration: org.apache.commons.collections.ExtendedProperties) = {} | |
override def isCachingOn = false // Velocity LRU cache is probably too bulky. We use a ReferenceMap instead. | |
override def isSourceModified (resource: Resource) = | |
if (resource.requiresChecking) getLastModified (resource) != resource.getLastModified | |
else false | |
override def resourceExists (resourceName: String) = new File (resourceName) .exists | |
} | |
val VELOCITY_ENGINE = { | |
val ve = new VelocityEngine | |
ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new LogSystem { | |
override def init (services: RuntimeServices) = {} | |
override def logVelocityMessage (i: Int, str: String) = if (i == LogSystem.ERROR_ID) System.err.println (str) | |
}) | |
ve.setProperty (RuntimeConstants.RESOURCE_MANAGER_CLASS, classOf[VelocityResourceManager].getName) | |
ve.setProperty (RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE, "false") | |
ve.setProperty (RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE, "false") | |
ve.setProperty (RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, "false") | |
ve.setProperty (RuntimeConstants.VM_PERM_INLINE_LOCAL, "true") | |
ve.init; ve | |
} | |
val GLOBAL_VELOCITY_CONTEXT = { | |
import org.apache.velocity.tools.generic._ // http://velocity.apache.org/tools/releases/1.4/generic/ | |
val ctx = new VelocityContext | |
ctx.put ("MathTool", new MathTool) | |
ctx.put ("NumberTool", new NumberTool) | |
ctx.put ("DateTool", new DateTool) | |
ctx.put ("SortTool", new SortTool) | |
ctx.put ("EscapeTool", new EscapeTool) | |
ctx | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment