This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly.git


The following commit(s) were added to refs/heads/master by this push:
     new 757534c  SLING-7291 - HTL to Java compilation might fail under heavy 
load
757534c is described below

commit 757534c728f65f54682ca23e4fbada44c874b3f0
Author: Radu Cotescu <[email protected]>
AuthorDate: Fri Dec 8 15:44:23 2017 +0100

    SLING-7291 - HTL to Java compilation might fail under heavy load
    
    * added a lock based on the fully qualified class name so that if two
    concurrent threads try to compile the same class they cannot corrupt
    each other's *.class file
---
 .../impl/engine/SightlyJavaCompilerService.java      | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyJavaCompilerService.java
 
b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyJavaCompilerService.java
index e6e8e63..09757e3 100644
--- 
a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyJavaCompilerService.java
+++ 
b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/SightlyJavaCompilerService.java
@@ -23,7 +23,11 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.Reader;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -75,6 +79,8 @@ public class SightlyJavaCompilerService {
     @Reference
     private ScriptingResourceResolverProvider 
scriptingResourceResolverProvider = null;
 
+    private Map<String, Lock> compilationLocks = new HashMap<>();
+
     private Options options;
 
     /**
@@ -120,8 +126,17 @@ public class SightlyJavaCompilerService {
      * @return object instance of the class to compile
      */
     public Object compileSource(SourceIdentifier sourceIdentifier, String 
sourceCode) {
+        Lock lock;
+        final String fqcn = sourceIdentifier.getFullyQualifiedClassName();
+        synchronized (compilationLocks) {
+            lock = compilationLocks.get(fqcn);
+            if (lock == null) {
+                lock = new ReentrantLock();
+                compilationLocks.put(fqcn, lock);
+            }
+        }
+        lock.lock();
         try {
-            String fqcn = sourceIdentifier.getFullyQualifiedClassName();
             if (sightlyEngineConfiguration.keepGenerated()) {
                 String path = "/" + fqcn.replaceAll("\\.", "/") + ".java";
                 OutputStream os = classLoaderWriter.getOutputStream(path);
@@ -166,9 +181,12 @@ public class SightlyJavaCompilerService {
             return 
classLoaderWriter.getClassLoader().loadClass(fqcn).newInstance();
         } catch (Exception e) {
             throw new SightlyException(e);
+        } finally {
+            lock.unlock();
         }
     }
 
+
     private Object getUseObjectAndRecompileIfNeeded(Resource pojoResource)
             throws IOException, InstantiationException, 
IllegalAccessException, ClassNotFoundException {
         SourceIdentifier sourceIdentifier = new 
SourceIdentifier(sightlyEngineConfiguration, pojoResource.getPath());

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to