Search in sources :

Example 1 with PrefetchingRawIterator

use of org.neo4j.collection.PrefetchingRawIterator in project neo4j by neo4j.

the class ProcedureJarLoader method listClassesIn.

private RawIterator<Class<?>, IOException> listClassesIn(URL jar, ClassLoader loader) throws IOException {
    ZipInputStream zip = new ZipInputStream(jar.openStream());
    return new PrefetchingRawIterator<Class<?>, IOException>() {

        @Override
        protected Class<?> fetchNextOrNull() throws IOException {
            try {
                while (true) {
                    ZipEntry nextEntry = zip.getNextEntry();
                    if (nextEntry == null) {
                        zip.close();
                        return null;
                    }
                    String name = nextEntry.getName();
                    if (name.endsWith(".class")) {
                        String className = name.substring(0, name.length() - ".class".length()).replace("/", ".");
                        try {
                            Class<?> aClass = loader.loadClass(className);
                            // We do getDeclaredMethods to trigger NoClassDefErrors, which loadClass above does
                            // not do.
                            // This way, even if some of the classes in a jar cannot be loaded, we still check
                            // the others.
                            aClass.getDeclaredMethods();
                            return aClass;
                        } catch (UnsatisfiedLinkError | NoClassDefFoundError | Exception e) {
                            log.warn("Failed to load `%s` from plugin jar `%s`: %s", className, jar.getFile(), e.getMessage());
                        }
                    }
                }
            } catch (IOException | RuntimeException e) {
                zip.close();
                throw e;
            }
        }
    };
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) PrefetchingRawIterator(org.neo4j.collection.PrefetchingRawIterator) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) KernelException(org.neo4j.kernel.api.exceptions.KernelException)

Aggregations

IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 PrefetchingRawIterator (org.neo4j.collection.PrefetchingRawIterator)1 KernelException (org.neo4j.kernel.api.exceptions.KernelException)1