diff --git a/build.gradle b/build.gradle index 39b4387..d2aacc6 100644 --- a/build.gradle +++ b/build.gradle @@ -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 () diff --git a/src/org/mbte/gretty/httpserver/GrettyContext.groovy b/src/org/mbte/gretty/httpserver/GrettyContext.groovy index c2f7649..e320537 100644 --- a/src/org/mbte/gretty/httpserver/GrettyContext.groovy +++ b/src/org/mbte/gretty/httpserver/GrettyContext.groovy @@ -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 + } } } } diff --git a/src/org/mbte/gretty/httpserver/template/GrettyTemplateEngine.groovy b/src/org/mbte/gretty/httpserver/template/GrettyTemplateEngine.groovy index ee606b5..d9c6f3b 100644 --- a/src/org/mbte/gretty/httpserver/template/GrettyTemplateEngine.groovy +++ b/src/org/mbte/gretty/httpserver/template/GrettyTemplateEngine.groovy @@ -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 { @@ -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}" + } } diff --git a/tests/org/mbte/gretty/httpserver/TemplateTest.groovy b/tests/org/mbte/gretty/httpserver/TemplateTest.groovy index 5a46785..caef0f7 100644 --- a/tests/org/mbte/gretty/httpserver/TemplateTest.groovy +++ b/tests/org/mbte/gretty/httpserver/TemplateTest.groovy @@ -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() }\ @@ -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}") + } + } + } }