All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hotels.styx.api.io.ClasspathResourceIndex Maven / Gradle / Ivy

There is a newer version: 1.0.0.beta9
Show newest version
/*
  Copyright (C) 2013-2018 Expedia Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
package com.hotels.styx.api.io;

import com.google.common.collect.Iterators;
import com.hotels.styx.api.Resource;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.stream.Stream;

import static com.google.common.base.Throwables.propagate;
import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;

/**
 * A resource index that scans the class path for the resources.
 */
public class ClasspathResourceIndex implements ResourceIndex {
    private final ClassLoader classLoader;

    public ClasspathResourceIndex(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    public Iterable list(String path, String suffix) {
        String classpath = path.replace("classpath:", "");
        ResourceIteratorFactory resourceIteratorFactory = new DelegatingResourceIteratorFactory();

        try {
            Enumeration resources = classLoader.getResources(classpath);

            return enumerationStream(resources)
                    .map(url -> resourceIteratorFactory.createIterator(url, classpath, suffix))
                    .flatMap(ClasspathResourceIndex::iteratorStream)
                    .collect(toList());

        } catch (IOException e) {
            throw propagate(e);
        }
    }

    private static  Stream enumerationStream(Enumeration enumeration) {
        return iteratorStream(Iterators.forEnumeration(enumeration));
    }

    private static  Stream iteratorStream(Iterator iterator) {
        return stream(spliteratorUnknownSize(iterator, ORDERED), false);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy