All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.verifier.jarloader.VerifierMapBackedClassLoader Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
package org.drools.verifier.jarloader;

import org.drools.rule.MapBackedClassLoader;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;


public class VerifierMapBackedClassLoader extends MapBackedClassLoader {
    public VerifierMapBackedClassLoader(Collection jarInputStreams) throws IOException {
        super(createClassLoader(null));

        initialize(jarInputStreams);
    }

    private static ClassLoader createClassLoader(ClassLoader classLoader) {
        if (classLoader == null) {
            classLoader = Thread.currentThread().getContextClassLoader();
            if (classLoader == null) {
                classLoader = VerifierMapBackedClassLoader.class.getClassLoader();
            }
        }
        return classLoader;
    }

    private void initialize(Collection jarInputStreams) throws IOException {
        for (JarInputStream jarInputStream : jarInputStreams) {
            readJarEntriesToClassLoader(jarInputStream);
        }
    }

    private void readJarEntriesToClassLoader(JarInputStream jarInputStream) throws IOException {
        byte[] buf = new byte[1024];
        int len;
        JarEntry entry;
        while ((entry = jarInputStream.getNextJarEntry()) != null) {
            if (isValidClassEntry(entry)) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                while ((len = jarInputStream.read(buf)) >= 0) {
                    out.write(buf,
                            0,
                            len);
                }

                addResource(entry.getName(),
                        out.toByteArray());

                out.close();
            }
        }
    }

    private boolean isValidClassEntry(JarEntry entry) {
        return !entry.isDirectory() && entry.getName().endsWith(".class");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy