Search in sources :

Example 1 with ArchiveException

use of org.hibernate.boot.archive.spi.ArchiveException in project hibernate-orm by hibernate.

the class ClassFileArchiveEntryHandler method toClassFile.

private ClassFile toClassFile(ArchiveEntry entry) {
    final InputStream inputStream = entry.getStreamAccess().accessInputStream();
    final DataInputStream dataInputStream = new DataInputStream(inputStream);
    try {
        return new ClassFile(dataInputStream);
    } catch (IOException e) {
        throw new ArchiveException("Could not build ClassFile", e);
    } finally {
        try {
            dataInputStream.close();
        } catch (Exception ignore) {
        }
        try {
            inputStream.close();
        } catch (IOException ignore) {
        }
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) IOException(java.io.IOException)

Example 2 with ArchiveException

use of org.hibernate.boot.archive.spi.ArchiveException in project hibernate-orm by hibernate.

the class JarFileBasedArchiveDescriptor method visitArchive.

@Override
public void visitArchive(ArchiveContext context) {
    final JarFile jarFile = resolveJarFileReference();
    if (jarFile == null) {
        return;
    }
    final Enumeration<? extends ZipEntry> zipEntries = jarFile.entries();
    while (zipEntries.hasMoreElements()) {
        final ZipEntry zipEntry = zipEntries.nextElement();
        final String entryName = extractName(zipEntry);
        if (getEntryBasePrefix() != null && !entryName.startsWith(getEntryBasePrefix())) {
            continue;
        }
        if (zipEntry.isDirectory()) {
            continue;
        }
        if (entryName.equals(getEntryBasePrefix())) {
            // just any random entry
            try (InputStream is = new BufferedInputStream(jarFile.getInputStream(zipEntry))) {
                final JarInputStream jarInputStream = new JarInputStream(is);
                ZipEntry subZipEntry = jarInputStream.getNextEntry();
                while (subZipEntry != null) {
                    if (!subZipEntry.isDirectory()) {
                        final String name = extractName(subZipEntry);
                        final String relativeName = extractRelativeName(subZipEntry);
                        final InputStreamAccess inputStreamAccess = buildByteBasedInputStreamAccess(name, jarInputStream);
                        final ArchiveEntry entry = new ArchiveEntry() {

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public String getNameWithinArchive() {
                                return relativeName;
                            }

                            @Override
                            public InputStreamAccess getStreamAccess() {
                                return inputStreamAccess;
                            }
                        };
                        final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler(entry);
                        entryHandler.handleEntry(entry, context);
                    }
                    subZipEntry = jarInputStream.getNextEntry();
                }
            } catch (Exception e) {
                throw new ArchiveException("Error accessing JarFile entry [" + zipEntry.getName() + "]", e);
            }
        } else {
            final String name = extractName(zipEntry);
            final String relativeName = extractRelativeName(zipEntry);
            final InputStreamAccess inputStreamAccess;
            try (InputStream is = jarFile.getInputStream(zipEntry)) {
                inputStreamAccess = buildByteBasedInputStreamAccess(name, is);
            } catch (IOException e) {
                throw new ArchiveException(String.format("Unable to access stream from jar file [%s] for entry [%s]", jarFile.getName(), zipEntry.getName()));
            }
            final ArchiveEntry entry = new ArchiveEntry() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getNameWithinArchive() {
                    return relativeName;
                }

                @Override
                public InputStreamAccess getStreamAccess() {
                    return inputStreamAccess;
                }
            };
            final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler(entry);
            entryHandler.handleEntry(entry, context);
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) InputStreamAccess(org.hibernate.boot.archive.spi.InputStreamAccess) BufferedInputStream(java.io.BufferedInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArchiveEntry(org.hibernate.boot.archive.spi.ArchiveEntry) ArchiveEntryHandler(org.hibernate.boot.archive.spi.ArchiveEntryHandler) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream)

Example 3 with ArchiveException

use of org.hibernate.boot.archive.spi.ArchiveException in project hibernate-orm by hibernate.

the class ExplodedArchiveDescriptor method processZippedRoot.

private void processZippedRoot(File rootFile, ArchiveContext context) {
    try {
        final JarFile jarFile = new JarFile(rootFile);
        final Enumeration<? extends ZipEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry zipEntry = entries.nextElement();
            if (zipEntry.isDirectory()) {
                continue;
            }
            final String name = extractName(zipEntry);
            final String relativeName = extractRelativeName(zipEntry);
            final InputStreamAccess inputStreamAccess;
            try {
                inputStreamAccess = buildByteBasedInputStreamAccess(name, jarFile.getInputStream(zipEntry));
            } catch (IOException e) {
                throw new ArchiveException(String.format("Unable to access stream from jar file [%s] for entry [%s]", jarFile.getName(), zipEntry.getName()));
            }
            final ArchiveEntry entry = new ArchiveEntry() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getNameWithinArchive() {
                    return relativeName;
                }

                @Override
                public InputStreamAccess getStreamAccess() {
                    return inputStreamAccess;
                }
            };
            context.obtainArchiveEntryHandler(entry).handleEntry(entry, context);
        }
    } catch (IOException e) {
        throw new ArchiveException("Error accessing jar file [" + rootFile.getAbsolutePath() + "]", e);
    }
}
Also used : InputStreamAccess(org.hibernate.boot.archive.spi.InputStreamAccess) ZipEntry(java.util.zip.ZipEntry) ArchiveEntry(org.hibernate.boot.archive.spi.ArchiveEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException)

Example 4 with ArchiveException

use of org.hibernate.boot.archive.spi.ArchiveException in project hibernate-orm by hibernate.

the class JarInputStreamBasedArchiveDescriptor method visitArchive.

@Override
public void visitArchive(ArchiveContext context) {
    final JarInputStream jarInputStream;
    try {
        jarInputStream = new JarInputStream(getArchiveUrl().openStream());
    } catch (Exception e) {
        //really should catch IOException but Eclipse is buggy and raise NPE...
        URL_LOGGER.logUnableToFindFileByUrl(getArchiveUrl(), e);
        return;
    }
    try {
        JarEntry jarEntry;
        while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
            final String jarEntryName = jarEntry.getName();
            if (getEntryBasePrefix() != null && !jarEntryName.startsWith(getEntryBasePrefix())) {
                continue;
            }
            if (jarEntry.isDirectory()) {
                continue;
            }
            if (jarEntryName.equals(getEntryBasePrefix())) {
                // just any random entry
                try {
                    final JarInputStream subJarInputStream = new JarInputStream(jarInputStream);
                    try {
                        ZipEntry subZipEntry = jarInputStream.getNextEntry();
                        while (subZipEntry != null) {
                            if (!subZipEntry.isDirectory()) {
                                final String subName = extractName(subZipEntry);
                                final InputStreamAccess inputStreamAccess = buildByteBasedInputStreamAccess(subName, subJarInputStream);
                                final ArchiveEntry entry = new ArchiveEntry() {

                                    @Override
                                    public String getName() {
                                        return subName;
                                    }

                                    @Override
                                    public String getNameWithinArchive() {
                                        return subName;
                                    }

                                    @Override
                                    public InputStreamAccess getStreamAccess() {
                                        return inputStreamAccess;
                                    }
                                };
                                context.obtainArchiveEntryHandler(entry).handleEntry(entry, context);
                            }
                            subZipEntry = jarInputStream.getNextJarEntry();
                        }
                    } finally {
                        subJarInputStream.close();
                    }
                } catch (Exception e) {
                    throw new ArchiveException("Error accessing nested jar", e);
                }
            } else {
                final String entryName = extractName(jarEntry);
                final InputStreamAccess inputStreamAccess = buildByteBasedInputStreamAccess(entryName, jarInputStream);
                final String relativeName = extractRelativeName(jarEntry);
                final ArchiveEntry entry = new ArchiveEntry() {

                    @Override
                    public String getName() {
                        return entryName;
                    }

                    @Override
                    public String getNameWithinArchive() {
                        return relativeName;
                    }

                    @Override
                    public InputStreamAccess getStreamAccess() {
                        return inputStreamAccess;
                    }
                };
                context.obtainArchiveEntryHandler(entry).handleEntry(entry, context);
            }
        }
        jarInputStream.close();
    } catch (IOException ioe) {
        throw new ArchiveException(String.format("Error accessing JarInputStream [%s]", getArchiveUrl()), ioe);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) InputStreamAccess(org.hibernate.boot.archive.spi.InputStreamAccess) ZipEntry(java.util.zip.ZipEntry) ArchiveEntry(org.hibernate.boot.archive.spi.ArchiveEntry) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 ArchiveException (org.hibernate.boot.archive.spi.ArchiveException)4 ZipEntry (java.util.zip.ZipEntry)3 ArchiveEntry (org.hibernate.boot.archive.spi.ArchiveEntry)3 InputStreamAccess (org.hibernate.boot.archive.spi.InputStreamAccess)3 InputStream (java.io.InputStream)2 JarFile (java.util.jar.JarFile)2 JarInputStream (java.util.jar.JarInputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 URISyntaxException (java.net.URISyntaxException)1 JarEntry (java.util.jar.JarEntry)1 ClassFile (javassist.bytecode.ClassFile)1 ArchiveEntryHandler (org.hibernate.boot.archive.spi.ArchiveEntryHandler)1