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

cz.jalasoft.util.configuration.PropertiesLoader Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package cz.jalasoft.util.configuration;

import cz.jalasoft.util.configuration.annotation.SourceType;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * A loader of {@link java.util.Properties} based on
 * location properties.
 *
 * @author Honza Lastovicka
 */
abstract class PropertiesLoader {

    static PropertiesLoader of(SourceType type) {
        switch(type) {
            case FILESYSTEM:
                return new PropertiesLoader() {
                    @Override
                    InputStream from(String name) throws IOException {
                        Path path = Paths.get(name);
                        return Files.newInputStream(path);
                    }
                };

            case CLASSPATH:
                return new PropertiesLoader() {
                    @Override
                    InputStream from(String name) throws IOException {
                        return ClassLoader.getSystemResourceAsStream(name);
                    }
                };

            default:
                throw new IllegalArgumentException("Unexpected type of source: " + type);
        }
    }

    final Properties load(String name) throws IOException {
        Properties properties = new Properties();
        properties.load(from(name));
        return properties;
    }

    abstract InputStream from(String name) throws IOException;

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy