Fast Groovlet Dev Cycle

View: New views
1 Messages — Rating Filter:   Alert me  

Fast Groovlet Dev Cycle

by akiliner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Grails too heavy for me, so I'm begin constructing web site with Groovlet.

Groovlet have another problem.
It doen not reload groovy file called Groovlet.

Here is my sloopy solution. :)

story is,

In development time, check WEB-INF/groovy directory,
if something updated, remake GroovyScriptEngine instance,
so every groovy file can be reloaded.

In production time, there are no directory checking,
but you can reset ScriptEngine manually.

does not need restart grails,
does not need web container,
there are no class reloading problesm,
and it's light speed. :)

*

package fa

import groovy.util.GroovyScriptEngine
import groovy.servlet.ServletBinding
import groovy.servlet.AbstractHttpServlet

import javax.servlet.ServletException
import javax.servlet.ServletConfig
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class GroovyServlet extends AbstractHttpServlet {
        private scriptEngine
        private inDev
        private codeSheet
       
        void init(ServletConfig config) throws ServletException {
                super.init(config)
                resetScriptEngine()
                inDev = InetAddress.localHost.hostName == "FA"
                log("GroovyServlet initialized")
        }
       
        def resetScriptEngine() {
                def urls = [
                        servletContext.getRealPath("/WEB-INF/groovy"),
                        servletContext.getRealPath("/WEB-INF/groovy/fa")
                ]
                scriptEngine = new GroovyScriptEngine((String[])urls, this.class.classLoader)
                log("New ScriptEngine initialized")
        }
       
        def checkCodeUpdate() {
                def newCodeSheet = [:]
                new File(servletContext.getRealPath("/WEB-INF/groovy")).eachFileRecurse { newCodeSheet[it.path] = it.lastModified() }
                if (codeSheet && codeSheet != newCodeSheet) {
                        resetScriptEngine()
                }
                codeSheet = newCodeSheet
        }
       
        void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
                def scriptName = request.requestURI.tokenize("/")[-1]
                def binding = new ServletBinding(request, response, servletContext)
               
                if (inDev) {
                        checkCodeUpdate()
                }
                binding.inDev = inDev
                binding.servlet = this
                response.setContentType("text/html; charset="+encoding)
                try {
                        scriptEngine.run(scriptName, binding)
                        response.setStatus(HttpServletResponse.SC_OK)
                } catch (ResourceException e) {
                        log("$scriptUri: script not found")
                        response.sendError(HttpServletResponse.SC_NOT_FOUND)
                } catch (Exception e) {
                        e.printStackTrace()
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.message)
                } finally {
                        response.flushBuffer()
                }
                //System.exit(1)
        }
}