package org.kie.scanner;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.drools.compiler.kie.builder.impl.InternalKieModule;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.ReleaseId;
import org.kie.internal.utils.ClassLoaderResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.aether.artifact.Artifact;


public class MavenClassLoaderResolver implements ClassLoaderResolver {
    private static final ProtectionDomain  PROTECTION_DOMAIN;
    
    private static final Logger logger = LoggerFactory.getLogger(MavenClassLoaderResolver.class);

    static {
        PROTECTION_DOMAIN = (ProtectionDomain) AccessController.doPrivileged( new PrivilegedAction() {

            public Object run() {
                return MavenClassLoaderResolver.class.getProtectionDomain();
            }
        } );
    }
    

    @Override
    public ClassLoader getClassLoader(KieModule kmodule) {
        ClassLoader parent = Thread.currentThread().getContextClassLoader();
        if (parent == null) {
            parent = ClassLoader.getSystemClassLoader();
        }
        if (parent == null) {
            parent = MavenClassLoaderResolver.class.getClassLoader();
        }

        InternalKieModule internalKModule = (InternalKieModule)kmodule;
        Collection<ReleaseId> jarDependencies = internalKModule.getJarDependencies();

        if (jarDependencies.isEmpty()) {
            return parent;
        }

        ArtifactResolver resolver = ArtifactResolver.getResolverFor(kmodule.getReleaseId(),true);
        List<URL> urls = new ArrayList<URL>();
        List<ReleaseId> unresolvedDeps = new ArrayList<ReleaseId>();

        for (ReleaseId rid : jarDependencies) {
            try {
                Artifact artifact = resolver.resolveArtifact(rid);
                if( artifact != null ) {
                    File jar = artifact.getFile(); 
                    urls.add( jar.toURI().toURL() );
                } else {
                    logger.error( "Dependency artifact not found for: " + rid );
                    unresolvedDeps.add(rid);
                }
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }

        internalKModule.setUnresolvedDependencies(unresolvedDeps);
        return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
    }

}

@@ -328,16 +328,137 @@
 il.List;
+%0Aimport java.util.Queue;%0Aimport java.util.concurrent.ArrayBlockingQueue;%0Aimport java.util.concurrent.LinkedBlockingQueue;
 %0A%0Aimport
@@ -1692,164 +1692,8 @@
     
-Collection%3CReleaseId%3E jarDependencies = internalKModule.getJarDependencies();%0A%0A        if (jarDependencies.isEmpty()) %7B%0A            return parent;%0A        %7D
 %0A%0A  
@@ -1904,16 +1904,24 @@
 eId%3E();%0A
+        
 %0A       
@@ -1925,30 +1925,426 @@
     
-for (ReleaseId rid : j
+Queue%3CInternalKieModule%3E queue = new LinkedBlockingQueue%3CInternalKieModule%3E();%0A        queue.add(internalKModule);%0A        while (!queue.isEmpty()) %7B%0A        try %7B%0A        %09InternalKieModule module = queue.poll();%0A        %09%0A        %09queue.addAll(module.getKieDependencies().values());%0A        %09%0A%09        urls.add(module.getFile().toURI().toURL());%0A%09        %0A%09        Collection%3CReleaseId%3E jarDependencies = module.getJ
 arDe
@@ -2353,28 +2353,29 @@
 ndencies
-) %7B%0A
+();%0A%09
         
     try 
@@ -2362,31 +2362,61 @@
 );%0A%09        
-    try
+for (ReleaseId rid : jarDependencies)
  %7B%0A         
@@ -2784,28 +2784,35 @@
           %7D%0A
+%09
     
+    %7D%0A
         %7D ca
@@ -2855,20 +2855,16 @@
         
-    
 throw ne
@@ -2882,28 +2882,24 @@
 ception(e);%0A
-    
         %7D%0A  
@@ -2898,32 +2898,40 @@
     %7D%0A        %7D%0A
+        
 %0A        interna

Extend MavenClassLoaderResolver