Search in sources :

Example 1 with NestingIterable

use of org.neo4j.helpers.collection.NestingIterable in project neo4j-mobile-android by neo4j-contrib.

the class Service method ourOwnLoader.

private static <T> Iterable<T> ourOwnLoader(final Class<T> type) {
    Collection<URL> urls = new HashSet<URL>();
    try {
        Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + type.getName());
        while (resources.hasMoreElements()) {
            urls.add(resources.nextElement());
        }
    } catch (IOException e) {
        return null;
    }
    return new NestingIterable<T, BufferedReader>(FilteringIterable.notNull(new IterableWrapper<BufferedReader, URL>(urls) {

        @Override
        protected BufferedReader underlyingObjectToObject(URL url) {
            try {
                return new BufferedReader(new InputStreamReader(url.openStream()));
            } catch (IOException e) {
                return null;
            }
        }
    })) {

        @Override
        protected Iterator<T> createNestedIterator(final BufferedReader input) {
            return new PrefetchingIterator<T>() {

                @Override
                protected T fetchNextOrNull() {
                    try {
                        String line;
                        while (null != (line = input.readLine())) {
                            try {
                                return type.cast(Class.forName(line).newInstance());
                            } catch (Exception e) {
                            } catch (LinkageError e) {
                            }
                        }
                        input.close();
                        return null;
                    } catch (IOException e) {
                        return null;
                    }
                }

                /* Finalizer - close the input stream.
                     * Prevent leakage of open files. Finalizers impact GC performance,
                     * but there are expected to be few of these objects.
                     */
                @Override
                protected void finalize() throws Throwable {
                    input.close();
                }
            };
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) NestingIterable(org.neo4j.helpers.collection.NestingIterable) URL(java.net.URL) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Example 2 with NestingIterable

use of org.neo4j.helpers.collection.NestingIterable in project graphdb by neo4j-attic.

the class Service method ourOwnLoader.

private static <T> Iterable<T> ourOwnLoader(final Class<T> type) {
    Collection<URL> urls = new HashSet<URL>();
    try {
        Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + type.getName());
        while (resources.hasMoreElements()) {
            urls.add(resources.nextElement());
        }
    } catch (IOException e) {
        return null;
    }
    return new NestingIterable<T, BufferedReader>(FilteringIterable.notNull(new IterableWrapper<BufferedReader, URL>(urls) {

        @Override
        protected BufferedReader underlyingObjectToObject(URL url) {
            try {
                return new BufferedReader(new InputStreamReader(url.openStream()));
            } catch (IOException e) {
                return null;
            }
        }
    })) {

        @Override
        protected Iterator<T> createNestedIterator(final BufferedReader input) {
            return new PrefetchingIterator<T>() {

                @Override
                protected T fetchNextOrNull() {
                    try {
                        String line;
                        while (null != (line = input.readLine())) {
                            try {
                                return type.cast(Class.forName(line).newInstance());
                            } catch (Exception e) {
                            } catch (LinkageError e) {
                            }
                        }
                        input.close();
                        return null;
                    } catch (IOException e) {
                        return null;
                    }
                }

                /* Finalizer - close the input stream.
                     * Prevent leakage of open files. Finalizers impact GC performance,
                     * but there are expected to be few of these objects.
                     */
                @Override
                protected void finalize() throws Throwable {
                    input.close();
                }
            };
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) NestingIterable(org.neo4j.helpers.collection.NestingIterable) URL(java.net.URL) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Aggregations

BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 IterableWrapper (org.neo4j.helpers.collection.IterableWrapper)2 NestingIterable (org.neo4j.helpers.collection.NestingIterable)2 PrefetchingIterator (org.neo4j.helpers.collection.PrefetchingIterator)2