This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.scripting.java-2.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-java.git
commit c7eeecd23b4b3d893ecce2f97df1dc7da966216a Author: Carsten Ziegeler <[email protected]> AuthorDate: Fri Mar 12 11:12:41 2010 +0000 SLING-1385 : Recompile java scripts on modifications and avoid periodic check - fix error handling and sync problems. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/java@922216 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/scripting/java/CompilationContext.java | 26 +++-- .../scripting/java/JavaScriptEngineFactory.java | 7 +- .../sling/scripting/java/ServletWrapper.java | 106 +++++++++------------ 3 files changed, 59 insertions(+), 80 deletions(-) diff --git a/src/main/java/org/apache/sling/scripting/java/CompilationContext.java b/src/main/java/org/apache/sling/scripting/java/CompilationContext.java index 764d95b..6efe3f3 100644 --- a/src/main/java/org/apache/sling/scripting/java/CompilationContext.java +++ b/src/main/java/org/apache/sling/scripting/java/CompilationContext.java @@ -16,7 +16,6 @@ */ package org.apache.sling.scripting.java; -import java.io.FileNotFoundException; import java.util.List; import javax.servlet.ServletException; @@ -135,8 +134,8 @@ public class CompilationContext { return this.lastModificationTest; } - public void setLastModificationTest(final long value) { - this.lastModificationTest = value; + public void clearLastModificationTest() { + this.lastModificationTest = 0; } /** @@ -146,8 +145,6 @@ public class CompilationContext { if ( this.lastModificationTest > 0 ) { return false; } - this.lastModificationTest = System.currentTimeMillis(); - final long sourceLastModified = this.ioProvider.lastModified(getSourcePath()); final long targetLastModified = this.ioProvider.lastModified(getCompleteClassPath()); @@ -173,33 +170,32 @@ public class CompilationContext { // ==================== Compile and reload ==================== - public void compile() throws ServletException, FileNotFoundException { + public boolean compile() throws Exception { if (this.isOutDated()) { try { final List<CompilerError> errors = this.compiler.compile(); if ( errors != null ) { - //this.ioProvider.delete(getCompleteClassPath()); throw CompilerException.create(errors); } - this.wrapper.setReload(true); this.wrapper.setCompilationException(null); - } catch (ServletException se) { - this.wrapper.setCompilationException(se); - throw se; + return true; } catch (Exception ex) { - final ServletException se = new ServletException("Unable to compile servlet.", ex); // Cache compilation exception - this.wrapper.setCompilationException(se); - throw se; + this.wrapper.setCompilationException(ex); + throw ex; + } finally { + this.lastModificationTest = System.currentTimeMillis(); } } + this.lastModificationTest = System.currentTimeMillis(); + return false; } /** * Load the class. */ public Class<?> load() - throws ServletException, FileNotFoundException { + throws ServletException { final String name = this.getClassFilePath().substring(1).replace('/', '.'); try { servletClass = this.options.getClassLoader().loadClass(name); diff --git a/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java b/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java index 2e1e4a6..d6f1f1a 100644 --- a/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java +++ b/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java @@ -168,10 +168,13 @@ public class JavaScriptEngineFactory this.eventHandlerRegistration.unregister(); this.eventHandlerRegistration = null; } + if ( this.servletCache != null ) { + this.servletCache.destroy(); + this.servletCache = null; + } ioProvider = null; javaServletContext = null; servletConfig = null; - servletCache = null; compilerOptions = null; } @@ -279,7 +282,7 @@ public class JavaScriptEngineFactory private void handleModification(final String scriptName) { final ServletWrapper wrapper = this.servletCache.getWrapper(scriptName); if ( wrapper != null ) { - wrapper.getCompilationContext().setLastModificationTest(0); + wrapper.getCompilationContext().clearLastModificationTest(); } } diff --git a/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java b/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java index 481d832..a315648 100644 --- a/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java +++ b/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java @@ -50,13 +50,9 @@ public class ServletWrapper { /** The servlet config. */ private ServletConfig config; - /** Reload flag. */ - private boolean reload = true; - private Servlet theServlet; private long available = 0L; - private boolean firstTime = true; - private ServletException compileException; + private Exception compileException; private CompilationContext ctxt; /** @@ -73,53 +69,38 @@ public class ServletWrapper { ioProvider, servletCache, this); } - /** - * Set the reload flag. - * @param reload - */ - public void setReload(boolean reload) { - this.reload = reload; - } - public CompilationContext getCompilationContext() { return this.ctxt; } /** * Get the servlet. - * @return The servlet. * @throws ServletException * @throws IOException * @throws FileNotFoundException */ - private Servlet getServlet() - throws ServletException, IOException, FileNotFoundException { - if (reload) { - synchronized (this) { - if (reload) { - destroy(); + private void getServlet() + throws ServletException { + destroy(); - Servlet servlet = null; + Servlet servlet = null; - try { - final Class<?> servletClass = ctxt.load(); - servlet = (Servlet) servletClass.newInstance(); - } catch (IllegalAccessException e) { - throw new ServletException(e); - } catch (InstantiationException e) { - throw new ServletException(e); - } catch (Exception e) { - throw new ServletException(e); - } + try { + final Class<?> servletClass = ctxt.load(); + servlet = (Servlet) servletClass.newInstance(); + } catch (IllegalAccessException e) { + throw new ServletException(e); + } catch (InstantiationException e) { + throw new ServletException(e); + } catch (ServletException se) { + throw se; + } catch (Exception e) { + throw new ServletException(e); + } - servlet.init(config); + servlet.init(config); - theServlet = servlet; - reload = false; - } - } - } - return theServlet; + theServlet = servlet; } /** @@ -127,7 +108,7 @@ public class ServletWrapper { * * @param je The compilation exception */ - public void setCompilationException(ServletException je) { + public void setCompilationException(final Exception je) { this.compileException = je; } @@ -164,10 +145,9 @@ public class ServletWrapper { * @throws IOException * @throws FileNotFoundException */ - public void service(HttpServletRequest request, + private void service(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException, FileNotFoundException { - + throws ServletException, IOException { try { if (ctxt.isRemoved()) { @@ -180,43 +160,43 @@ public class ServletWrapper { response.sendError (HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Servlet unavailable."); + logger.error("Java servlet {} is unavailable.", this.servletUri); return; } // Wait period has expired. Reset. available = 0; } - /* - * (1) Compile - */ - if (firstTime || ctxt.getLastModificationTest() == 0 ) { + // check for compilation + if (ctxt.getLastModificationTest() == 0 ) { synchronized (this) { - firstTime = false; - - // The following sets reload to true, if necessary - ctxt.compile(); - } - } else { - if (compileException != null) { - // Throw cached compilation exception - throw compileException; + if (ctxt.getLastModificationTest() == 0 ) { + if ( ctxt.compile() ) { + // (re)load the servlet class + getServlet(); + } + + } else if (compileException != null) { + // Throw cached compilation exception + throw compileException; + } } + } else if (compileException != null) { + // Throw cached compilation exception + throw compileException; } - /* - * (2) (Re)load servlet class file - */ - getServlet(); - } catch (FileNotFoundException ex) { ctxt.incrementRemoved(); try { response.sendError(HttpServletResponse.SC_NOT_FOUND, ex.getMessage()); + logger.error("Java servlet {} not found.", this.servletUri); } catch (IllegalStateException ise) { logger.error("Java servlet source not found." + ex.getMessage(), ex); } + return; } catch (SlingException ex) { throw ex; } catch (ServletException ex) { @@ -231,9 +211,7 @@ public class ServletWrapper { try { - /* - * (3) Service request - */ + // invoke the servlet if (theServlet instanceof SingleThreadModel) { // sync on the wrapper so that the freshness // of the page is determined right before servicing @@ -254,6 +232,8 @@ public class ServletWrapper { response.sendError (HttpServletResponse.SC_SERVICE_UNAVAILABLE, ex.getMessage()); + logger.error("Java servlet {} is unavailable.", this.servletUri); + return; } catch (SlingException ex) { throw ex; } catch (ServletException ex) { -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
