Example usage for java.lang ClassLoader getResourceAsStream

List of usage examples for java.lang ClassLoader getResourceAsStream

Introduction

In this page you can find the example usage for java.lang ClassLoader getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String name) 

Source Link

Document

Returns an input stream for reading the specified resource.

Usage

From source file:nl.strohalm.cyclos.utils.PropertiesHelper.java

/**
 * Loads a property file from a base name, like "package.File", which would be translated into a classpath resource "package/File.properties".
 * Returns null if no such file exists// w  w w  . j  av a  2  s  .c om
 */
public static Properties loadFromResource(final String baseName) {
    final String path = baseName.replace('.', '/');
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final InputStream in = loader.getResourceAsStream(path + ".properties");
    if (in == null) {
        return null;
    }
    return loadFromStream(in);
}

From source file:io.dfox.junit.http.util.TestUtils.java

/**
 * Get the fixture with the specified path or null if not found. The fixture must be a valid
 * JSON file available on the classpath in a resource directory called "fixtures". It may be
 * contained in a further directory structure under that directory.
 *
 * @param path The path to the fixture//  ww  w . j a  v  a2  s  . c  om
 * @return The fixture as a parsed JsonNode
 * @throws IOException If the fixture cannot be loaded.
 */
public static JsonNode getTestData(final String path) throws IOException {
    String fullPath = DATA_DIR + "/" + path;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try (final InputStream stream = classLoader.getResourceAsStream(fullPath)) {
        if (stream == null) {
            try (final InputStream stream2 = TestUtils.class.getResourceAsStream(fullPath)) {
                if (stream2 == null) {
                    return null;
                } else {
                    return JSON_MAPPER.readTree(stream2);
                }
            }
        } else {
            return JSON_MAPPER.readTree(stream);
        }
    }
}

From source file:org.apache.axis2.dataretrieval.DataRetrievalUtil.java

private static InputStream getInputStream(ClassLoader classLoader, String file) throws XMLStreamException {

    InputStream servicexmlStream = classLoader.getResourceAsStream(file);

    if (servicexmlStream == null) {
        String message = "File does not exist in the Service Repository! File=" + file;
        if (log.isDebugEnabled()) {
            log.debug(message);//from w w  w.j  a v  a 2  s  .c o  m
        }
        throw new XMLStreamException(message);
    }
    return servicexmlStream;

}

From source file:com.wavemaker.commons.classloader.ClassLoaderUtils.java

public static InputStream getResourceAsStream(String path, ClassLoader cl) {
    return cl.getResourceAsStream(path);
}

From source file:com.moneydance.modules.features.importlist.util.Helper.java

public static InputStream getInputStreamFromResource(final String resource) {
    ClassLoader cloader = Helper.class.getClassLoader();
    InputStream inputStream = cloader.getResourceAsStream(resource);
    Validate.notNull(inputStream, "Resource %s was not found.", resource);
    return inputStream;
}

From source file:org.apache.openaz.pepapi.std.PepUtils.java

public static Properties loadProperties(String propertyFile) {
    Properties properties = new Properties();

    // Try the location as a file first.
    File file = new File(propertyFile);
    InputStream in = null;//from w ww  .  j  a  v a  2  s. c  om
    if (file.exists() && file.canRead()) {
        if (!file.isAbsolute()) {
            file = file.getAbsoluteFile();
        }
        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            logger.error("Error while accessing file: " + propertyFile);
            throw new IllegalArgumentException(e);
        }
    } else {
        Set<ClassLoader> classLoaders = new HashSet<>();
        classLoaders.add(PepUtils.class.getClassLoader());
        classLoaders.add(Thread.currentThread().getContextClassLoader());
        for (ClassLoader classLoader : classLoaders) {
            in = classLoader.getResourceAsStream(propertyFile);
            if (in != null) {
                break;
            }
        }
        if (in == null) {
            logger.error("Invalid classpath or file location: " + propertyFile);
            throw new IllegalArgumentException("Invalid classpath or file location: " + propertyFile);
        }
    }

    try {
        properties.load(in);
    } catch (IOException e) {
        logger.error(e);
        throw new IllegalArgumentException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.debug("Error closing stream", e);
            }
        }
    }
    return properties;
}

From source file:org.apache.kylin.jdbc.TestUtil.java

public static String getResourceContent(String path) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    StringWriter writer = new StringWriter();
    InputStream is = loader.getResourceAsStream(path);
    if (is == null) {
        throw new IllegalArgumentException(new FileNotFoundException(path + " not found in class path"));
    }/*from   w  w w  .  j  a v a 2s . c  om*/
    try {
        IOUtils.copy(is, writer, "utf-8");
        return writer.toString();
    } catch (IOException e) {
        IOUtils.closeQuietly(is);
        throw new RuntimeException(e);
    }
}

From source file:com.github.restdriver.clientdriver.unit.SecureClientDriverFactoryTest.java

static KeyStore getKeystore() throws Exception {
    ClassLoader loader = SecureClientDriverTest.class.getClassLoader();
    byte[] binaryContent = IOUtils.toByteArray(loader.getResourceAsStream("keystore.jks"));
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new ByteArrayInputStream(binaryContent), "password".toCharArray());
    return keyStore;
}

From source file:fr.sewatech.sewatoool.impress.Main.java

/**
 * Affiche l'aide en ligne//from w w  w .  ja  v  a2s  .com
 */
private static void doHelp() {
    try {
        ClassLoader loader = Main.class.getClassLoader();
        InputStream usageStream = loader.getResourceAsStream("usage.txt");
        BufferedReader usageReader = new BufferedReader(new InputStreamReader(usageStream));
        String line;
        do {
            line = usageReader.readLine();
            message(line == null ? "" : line);

        } while (line != null);
    } catch (IOException e) {
        logger.warn("Probleme pour l'aide", e);
    } finally {
        System.exit(0);
    }
}

From source file:com.michellemay.config.ConfigReader.java

/**
 * Load profiles configuration from the classpath in a specific directory.
 *
 * <p>This is usually used to load built-in profiles, shipped with the jar.</p>
 *
 * @param classLoader the ClassLoader to load the profiles from. Use {@code MyClass.class.getClassLoader()}
 * @param configName profile path inside the classpath.
 *
 * @return New {@link Config} object.//from   w  w  w  . ja  v  a2 s. c o m
 *
 * @throws IOException if an error occurs while reading the configuration.
 */
public static Config read(ClassLoader classLoader, String configName) throws IOException {
    try (InputStream in = classLoader.getResourceAsStream(configName)) {
        if (in == null) {
            throw new IOException("No config file available named at " + configName + "!");
        }
        return read(in);
    }
}