Skip to content

Commit

Permalink
better templates/groovylets
Browse files Browse the repository at this point in the history
  • Loading branch information
alextkachman committed Jun 19, 2011
1 parent 1ac6567 commit e889777
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -19,9 +19,9 @@ apply plugin: 'idea'
apply plugin: 'maven'

group = 'org.mbte.groovypp'
version = '0.4.259'
version = '0.4.261'

def gppVersion = '0.4.259_1.8.0'
def gppVersion = '0.4.261_1.8.0'

repositories {
mavenCentral ()
Expand Down
10 changes: 7 additions & 3 deletions src/org/mbte/gretty/httpserver/GrettyContext.groovy
Expand Up @@ -280,12 +280,16 @@ import groovypp.text.FastStringWriter

def file = new File(fileUri).canonicalFile
if (!file.exists() || !file.file) {
fileUri = "$groovletFiles/${uri}.gpptl"
fileUri = "$groovletFiles/${uri}.gpp"
file = new File(fileUri).canonicalFile
if (!file.exists() || !file.file) {
file = new File(file.parentFile, "default.groovy").canonicalFile
fileUri = "$groovletFiles/${uri}.gpptl"
file = new File(fileUri).canonicalFile
if (!file.exists() || !file.file) {
return null
file = new File(file.parentFile, "default.groovy").canonicalFile
if (!file.exists() || !file.file) {
return null
}
}
}
}
Expand Down
Expand Up @@ -17,11 +17,6 @@
package org.mbte.gretty.httpserver.template

import groovypp.text.GppSimpleTemplateEngine
import java.util.concurrent.ConcurrentHashMap
import groovy.text.Template
import org.codehaus.groovy.util.ManagedReference
import org.codehaus.groovy.util.ReferenceManager
import org.codehaus.groovy.control.CompilerConfiguration
import groovypp.text.GppTemplateScript

@Typed class GrettyTemplateEngine extends GppSimpleTemplateEngine {
Expand All @@ -33,7 +28,18 @@ import groovypp.text.GppTemplateScript
if(file.name.endsWith(".gpptl"))
return super.compile(file)

loader.parseClass(file)
logAttemptToCompile(file)
def clazz = loader.parseClass(new GroovyCodeSource(file), false)
cache.put(file, new GppSimpleTemplateEngine.CacheEntry(cache, file, clazz))
clazz
}

protected void logCompilationError(Throwable throwable) {
println "Compilation error"
throwable.printStackTrace()
}

protected void logAttemptToCompile(File file) {
println "Trying to compile ${file.canonicalPath}"
}
}
46 changes: 44 additions & 2 deletions tests/org/mbte/gretty/httpserver/TemplateTest.groovy
Expand Up @@ -22,10 +22,9 @@ import sun.misc.Request
@Typed class TemplateTest extends GrettyServerTestCase {

File scriptFile, templateFile
File root = new File(".").canonicalFile

protected void buildServer() {
File root = ["."]
root = root.canonicalFile
templateFile = File.createTempFile("temp_", "_script.gpptl", root)
templateFile.text = """\
Request path: \${request.uri} URI: \${uri ?: request.uri.toUpperCase() }\
Expand Down Expand Up @@ -79,4 +78,47 @@ Request path: \${request.uri} URI: \${uri ?: request.uri.toUpperCase() }\
assert response.contentText == "issue: 239 reporter: wilson"
}
}

void testChangingTemlate() {
def file = File.createTempFile("temp_", "_script.gpptl", root)
file.deleteOnExit()

for (i in 0..<10) {
def text = "<% Integer param = Integer.parseInt(request.parameters.value[0].toString())%>param: \$param const: $i value: <%= param + $i %>"
file.text = text
file.setLastModified(System.currentTimeMillis())
Thread.sleep(1000)

def path = file.name.substring(0, file.name.length() - 6)

GrettyHttpRequest req = [method:HttpMethod.GET, uri:"/${path}?value=$i"]
doTest(req) { GrettyHttpResponse response ->
println response.contentText
assert response.contentText.startsWith("param: $i const: $i value: ${2*i}")
}
}
}

void testChangingScript() {
def file = File.createTempFile("temp_", "_script.gpp", root)
file.deleteOnExit()

for (i in 0..<10) {
def text = """
def param = Integer.parseInt(request.parameters.value[0].toString())
println "param: \$param const: $i value: \${param + $i}"
"""
file.text = text
file.setLastModified(System.currentTimeMillis())
Thread.sleep(1000)

def path = file.name.substring(0, file.name.length() - 4)

GrettyHttpRequest req = [method:HttpMethod.GET, uri:"/${path}?value=$i"]
doTest(req) { GrettyHttpResponse response ->
println response.contentText
assert response.contentText.startsWith("param: $i const: $i value: ${2*i}")
}
}
}
}

0 comments on commit e889777

Please sign in to comment.