Search in sources :

Example 1 with AbstractResourceFinderAdapter

use of org.glassfish.jersey.server.internal.AbstractResourceFinderAdapter in project jersey by jersey.

the class FilesScanner method processFile.

private void processFile(final File f) {
    if (f.getName().endsWith(".jar") || f.getName().endsWith(".zip")) {
        try {
            compositeResourceFinder.push(new JarFileScanner(new FileInputStream(f), "", true));
        } catch (final IOException e) {
            // logging might be sufficient in this case
            throw new ResourceFinderException(e);
        }
    } else {
        compositeResourceFinder.push(new AbstractResourceFinderAdapter() {

            Stack<File> files = new Stack<File>() {

                {
                    if (f.isDirectory()) {
                        final File[] subDirFiles = f.listFiles();
                        if (subDirFiles != null) {
                            for (final File file : subDirFiles) {
                                push(file);
                            }
                        }
                    } else {
                        push(f);
                    }
                }
            };

            private File current;

            private File next;

            @Override
            public boolean hasNext() {
                while (next == null && !files.empty()) {
                    next = files.pop();
                    if (next.isDirectory()) {
                        if (recursive) {
                            processFile(next);
                        }
                        next = null;
                    } else if (next.getName().endsWith(".jar") || next.getName().endsWith(".zip")) {
                        processFile(next);
                        next = null;
                    }
                }
                return next != null;
            }

            @Override
            public String next() {
                if (next != null || hasNext()) {
                    current = next;
                    next = null;
                    return current.getName();
                }
                throw new NoSuchElementException();
            }

            @Override
            public InputStream open() {
                try {
                    return new FileInputStream(current);
                } catch (final FileNotFoundException e) {
                    throw new ResourceFinderException(e);
                }
            }

            @Override
            public void reset() {
                throw new UnsupportedOperationException();
            }
        });
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AbstractResourceFinderAdapter(org.glassfish.jersey.server.internal.AbstractResourceFinderAdapter) Stack(java.util.Stack) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with AbstractResourceFinderAdapter

use of org.glassfish.jersey.server.internal.AbstractResourceFinderAdapter in project jersey by jersey.

the class WebAppResourcesScanner method processPaths.

private void processPaths(final String... paths) {
    for (final String path : paths) {
        final Set<String> resourcePaths = sc.getResourcePaths(path);
        if (resourcePaths == null) {
            break;
        }
        compositeResourceFinder.push(new AbstractResourceFinderAdapter() {

            private final Deque<String> resourcePathsStack = new LinkedList<String>() {

                private static final long serialVersionUID = 3109256773218160485L;

                {
                    for (final String resourcePath : resourcePaths) {
                        push(resourcePath);
                    }
                }
            };

            private String current;

            private String next;

            @Override
            public boolean hasNext() {
                while (next == null && !resourcePathsStack.isEmpty()) {
                    next = resourcePathsStack.pop();
                    if (next.endsWith("/")) {
                        processPaths(next);
                        next = null;
                    } else if (next.endsWith(".jar")) {
                        try {
                            compositeResourceFinder.push(new JarFileScanner(sc.getResourceAsStream(next), "", true));
                        } catch (final IOException ioe) {
                            throw new ResourceFinderException(ioe);
                        }
                        next = null;
                    }
                }
                return next != null;
            }

            @Override
            public String next() {
                if (next != null || hasNext()) {
                    current = next;
                    next = null;
                    return current;
                }
                throw new NoSuchElementException();
            }

            @Override
            public InputStream open() {
                return sc.getResourceAsStream(current);
            }

            @Override
            public void reset() {
                throw new UnsupportedOperationException();
            }
        });
    }
}
Also used : InputStream(java.io.InputStream) ResourceFinderException(org.glassfish.jersey.server.internal.scanning.ResourceFinderException) IOException(java.io.IOException) JarFileScanner(org.glassfish.jersey.server.internal.scanning.JarFileScanner) AbstractResourceFinderAdapter(org.glassfish.jersey.server.internal.AbstractResourceFinderAdapter) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 NoSuchElementException (java.util.NoSuchElementException)2 AbstractResourceFinderAdapter (org.glassfish.jersey.server.internal.AbstractResourceFinderAdapter)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 LinkedList (java.util.LinkedList)1 Stack (java.util.Stack)1 JarFileScanner (org.glassfish.jersey.server.internal.scanning.JarFileScanner)1 ResourceFinderException (org.glassfish.jersey.server.internal.scanning.ResourceFinderException)1