// Copyright 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.internal.plastic;

public class PlasticClassLoader extends ClassLoader
{
    private final ClassLoaderDelegate delegate;

    public PlasticClassLoader(ClassLoader parent, ClassLoaderDelegate delegate)
    {
        super(parent);

        this.delegate = delegate;
    }

    @Override
    protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
    {
        Class<?> loadedClass = findLoadedClass(name);

        if (loadedClass != null)
            return loadedClass;

        if (delegate.shouldInterceptClassLoading(name))
        {
            Class<?> c = delegate.loadAndTransformClass(name);

            if (resolve)
                resolveClass(c);

            return c;
        } else
        {
            return super.loadClass(name, resolve);
        }
    }

    public synchronized Class<?> defineClassWithBytecode(String className, byte[] bytecode)
    {
        return defineClass(className, bytecode, 0, bytecode.length);
    }
}

@@ -927,29 +927,16 @@
 otected 
-synchronized 
 Class%3C?%3E
@@ -1008,24 +1008,86 @@
 ption%0A    %7B%0A
+        synchronized(getClassLoadingLock(name))%0A        %7B%0A    
         Clas
@@ -1121,32 +1121,36 @@
 edClass(name);%0A%0A
+    
         if (load
@@ -1174,24 +1174,28 @@
             
+    
 return loade
@@ -1203,16 +1203,20 @@
 Class;%0A%0A
+    
         
@@ -1263,32 +1263,36 @@
 (name))%0A        
+    
 %7B%0A            Cl
@@ -1281,32 +1281,36 @@
   %7B%0A            
+ 
+   
 Class%3C?%3E c = del
@@ -1353,24 +1353,28 @@
             
+    
 if (resolve)
@@ -1374,16 +1374,20 @@
 esolve)%0A
+    
         
@@ -1420,24 +1420,28 @@
             
+  
+  
 return c;%0A  
@@ -1446,16 +1446,20 @@
         
+    
 %7D else%0A 
@@ -1457,34 +1457,42 @@
  %7D else%0A        
+ 
-%7B%0A
+   %7B%0A    
             retu
@@ -1526,16 +1526,30 @@
 solve);%0A
+            %7D%0A
         
@@ -1647,32 +1647,99 @@
 bytecode)%0A    %7B%0A
+        synchronized(getClassLoadingLock(className))%0A        %7B%0A    
         return d
@@ -1791,16 +1791,26 @@
 ength);%0A
+        %7D%0A
     %7D%0A%7D%0A

use class loading lock for synchronization This does not change the previous behavior since PlasticClassLoader is not (yet) registered as parallel capable