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:com.scottjjohnson.finance.analysis.HttpClient.java

/**
 * Extracts http connection properties from the configuration file and returns them as a Properties object.
 * //from w ww . j a  v  a2s  . co  m
 * @returns map of cookie key-value pairs (never null)
 */
public static Properties getDefaultConnectionProperties() {
    Properties connectionProps = new Properties();
    try {
        ClassLoader loader = HttpClient.class.getClassLoader();
        connectionProps.load(loader.getResourceAsStream(CONNECTION_CONFIG_PROPERTIES_FILE));

        LOGGER.debug("Connection configuration properties: {}.", connectionProps);
    } catch (IOException e) {
        LOGGER.warn("Unable to load connection properties from {}.", CONNECTION_CONFIG_PROPERTIES_FILE);
    }

    return connectionProps;
}

From source file:eu.databata.engine.util.PropagatorRecreateDatabaseTool.java

private static String getFileContent(ClassLoader contextClassLoader, String fileName) throws IOException {
    LOG.info("Loading file '" + fileName + "'");
    InputStream submitFileStream = contextClassLoader.getResourceAsStream(fileName);
    if (submitFileStream == null) {
        LOG.info("File with name '" + fileName
                + "' cannot be read from classpath. Trying to load default submit file.");
        return null;
    }//from  w  ww.j  av  a 2  s  .c om
    InputStreamReader streamReader = new InputStreamReader(submitFileStream);
    BufferedReader bufferedReader = new BufferedReader(streamReader);

    String result = "";
    while (bufferedReader.ready()) {
        result += bufferedReader.readLine();
        result += "\n";
    }

    return result;
}

From source file:Main.java

/**
 * Finds a resource with the given name.  Checks the Thread Context
 * classloader, then uses the System classloader.  Should replace all
 * calls to <code>Class.getResourceAsString</code> when the resource
 * might come from a different classloader.  (e.g. a webapp).
 * @param claz Class to use when getting the System classloader (used if no Thread
 * Context classloader available or fails to get resource).
 * @param name name of the resource// ww  w . j  a v  a 2s . c o  m
 * @return InputStream for the resource.
 */
public static InputStream getResourceAsStream(Class claz, String name) {
    InputStream result = null;

    /**
     * remove leading slash so path will work with classes in a JAR file
     */
    while (name.startsWith("/")) {
        name = name.substring(1);
    }

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    if (classLoader == null) {
        classLoader = claz.getClassLoader();
        result = classLoader.getResourceAsStream(name);
    } else {
        result = classLoader.getResourceAsStream(name);

        /**
        * for compatibility with texen / ant tasks, fall back to
        * old method when resource is not found.
        */

        if (result == null) {
            classLoader = claz.getClassLoader();
            if (classLoader != null)
                result = classLoader.getResourceAsStream(name);
        }
    }

    return result;

}

From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java

public synchronized static X509Certificate getRootCaCert() throws IOException {
    if (rootCaCert == null) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(CA_Cert_FILE);
        rootCaCert = PKCSReader.readCert(is);
        is.close();/*from   w ww . j av  a 2  s.co  m*/
    }
    return rootCaCert;
}

From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java

public synchronized static KeyPair getRootCaKeyPair() throws IOException {
    if (rootCaKeyPair == null) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(CA_KEY_FILE);
        rootCaKeyPair = PKCSReader.readKeyPair(is, null);
        is.close();/*from   w ww  .  ja v a 2 s  .c o  m*/
    }
    return rootCaKeyPair;
}

From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java

public synchronized static KeyPair getRootCaKeyPair(char[] pwd) throws IOException {
    if (rootCaKeyPair == null) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(CA_KEY_FILE);
        rootCaKeyPair = PKCSReader.readKeyPair(is, pwd);
        is.close();//from   w w  w  . j  ava2s . co  m
    }
    return rootCaKeyPair;
}

From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java

public synchronized static X509Certificate getClass1CaCert() throws IOException {
    if (class1CaCert == null) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(CLASS1_CA_Cert_FILE);
        class1CaCert = PKCSReader.readCert(is);
        is.close();// www.  j a  v  a 2 s . co  m
    }
    return class1CaCert;
}

From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java

public synchronized static KeyPair getClass1CaKeyPair() throws IOException {
    if (class1CaKeyPair == null) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(CLASS1_CA_KEY_FILE);
        class1CaKeyPair = PKCSReader.readKeyPair(is, null);
        is.close();// ww  w  .j  a v a 2s . c  o  m
    }
    return class1CaKeyPair;
}

From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java

public synchronized static KeyPair getClass1CaKeyPair(char[] pwd) throws IOException {
    if (class1CaKeyPair == null) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(CLASS1_CA_KEY_FILE);
        class1CaKeyPair = PKCSReader.readKeyPair(is, pwd);
        is.close();//from  ww w.j  av a 2 s . c om
    }
    return class1CaKeyPair;
}

From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java

public synchronized static X509Certificate getClass2CaCert() throws IOException {
    if (class2CaCert == null) {
        ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(CLASS2_CA_Cert_FILE);
        class2CaCert = PKCSReader.readCert(is);
        is.close();//from   ww  w . j  a  v a 2  s .c o  m
    }
    return class2CaCert;
}