Search in sources :

Example 6 with InputStreamAccess

use of org.hibernate.boot.archive.spi.InputStreamAccess 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

ArchiveEntry (org.hibernate.boot.archive.spi.ArchiveEntry)6 InputStreamAccess (org.hibernate.boot.archive.spi.InputStreamAccess)6 IOException (java.io.IOException)4 JarFile (java.util.jar.JarFile)3 ZipEntry (java.util.zip.ZipEntry)3 ArchiveException (org.hibernate.boot.archive.spi.ArchiveException)3 JarInputStream (java.util.jar.JarInputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 JarEntry (java.util.jar.JarEntry)1 PersistenceException (javax.persistence.PersistenceException)1 ArchiveEntryHandler (org.hibernate.boot.archive.spi.ArchiveEntryHandler)1 VirtualFile (org.jboss.vfs.VirtualFile)1