Search in sources :

Example 1 with InputStreamAccess

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

use of org.hibernate.boot.archive.spi.InputStreamAccess in project wildfly by wildfly.

the class VirtualFileSystemArchiveDescriptor method processVirtualFile.

private void processVirtualFile(VirtualFile virtualFile, String path, ArchiveContext archiveContext) {
    if (path == null) {
        path = "";
    } else {
        if (!path.endsWith("/'")) {
            path = path + "/";
        }
    }
    for (VirtualFile child : virtualFile.getChildren()) {
        if (!child.exists()) {
            // should never happen conceptually, but...
            continue;
        }
        if (child.isDirectory()) {
            processVirtualFile(child, path + child.getName(), archiveContext);
            continue;
        }
        final String name = child.getPathName();
        final String relativeName = path + child.getName();
        final InputStreamAccess inputStreamAccess = new VirtualFileInputStreamAccess(name, child);
        final ArchiveEntry entry = new ArchiveEntry() {

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

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

            @Override
            public InputStreamAccess getStreamAccess() {
                return inputStreamAccess;
            }
        };
        archiveContext.obtainArchiveEntryHandler(entry).handleEntry(entry, archiveContext);
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) InputStreamAccess(org.hibernate.boot.archive.spi.InputStreamAccess) ArchiveEntry(org.hibernate.boot.archive.spi.ArchiveEntry)

Example 3 with InputStreamAccess

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

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

the class ExplodedArchiveDescriptor method processDirectory.

private void processDirectory(File directory, String path, ArchiveContext context) {
    if (directory == null) {
        return;
    }
    final File[] files = directory.listFiles();
    if (files == null) {
        return;
    }
    path = path == null ? "" : path + "/";
    for (final File localFile : files) {
        if (!localFile.exists()) {
            // should never happen conceptually, but...
            continue;
        }
        if (localFile.isDirectory()) {
            processDirectory(localFile, path + localFile.getName(), context);
            continue;
        }
        final String name = localFile.getAbsolutePath();
        final String relativeName = path + localFile.getName();
        final InputStreamAccess inputStreamAccess = new FileInputStreamAccess(name, localFile);
        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);
    }
}
Also used : InputStreamAccess(org.hibernate.boot.archive.spi.InputStreamAccess) ArchiveEntry(org.hibernate.boot.archive.spi.ArchiveEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 5 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