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.springsource.insight.plugin.ldap.LdapOperationCollectionAspectTestSupport.java

private static Collection<Map<String, Set<String>>> readLdifEntries(String location) throws IOException {
    ClassLoader cl = ClassUtil.getDefaultClassLoader();
    InputStream in = cl.getResourceAsStream(location);
    assertNotNull("No LDIF input at " + location, in);

    BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
    try {//from w  ww .ja  v  a  2  s.  com
        return readLdifEntries(rdr);
    } finally {
        rdr.close();
    }
}

From source file:net.gazeplay.commons.utils.games.Utils.java

private static String loadLicenseFileAsString(ClassLoader classLoader) {
    try {//from w ww . j av  a 2 s  .  com
        try (InputStream is = classLoader.getResourceAsStream("data/common/licence.txt")) {
            return IOUtils.toString(is, Charset.forName("UTF-8"));
        }
    } catch (IOException e) {
        return "Failed to load the license file";
    }
}

From source file:ResourcesUtils.java

/**
 * Returns a resource on the classpath as a Stream object
 * //from   w  w w.  j av a2  s . c o m
 * @param resource
 *            The resource to find
 * @throws IOException
 *             If the resource cannot be found or read
 * @return The resource
 */
public static InputStream getResourceAsStream(String resource) throws IOException {
    InputStream in = null;
    ClassLoader loader = ResourcesUtils.class.getClassLoader();
    if (loader != null) {
        in = loader.getResourceAsStream(resource);
    }
    if (in == null) {
        in = ClassLoader.getSystemResourceAsStream(resource);
    }
    if (in == null) {
        throw new IOException("Could not find resource " + resource);
    }
    return in;
}

From source file:com.linecorp.armeria.server.docs.ThriftDocString.java

@VisibleForTesting
static Map<String, String> getDocStringsFromJsonResource(ClassLoader classLoader, String jsonResourcePath) {
    ImmutableMap.Builder<String, String> docStrings = ImmutableMap.builder();
    try (InputStream in = classLoader.getResourceAsStream(jsonResourcePath)) {
        if (in == null) {
            throw new IllegalStateException("not found: " + jsonResourcePath);
        }/* w  ww  .  ja  v a2s.c om*/

        final Map<String, Object> json = new ObjectMapper().readValue(in, JSON_VALUE_TYPE);
        @SuppressWarnings("unchecked")
        final Map<String, Object> namespaces = (Map<String, Object>) json.getOrDefault("namespaces",
                ImmutableMap.of());
        final String packageName = (String) namespaces.get("java");
        json.forEach((key, children) -> {
            if (children instanceof Collection) {
                ((Collection) children).forEach(grandChild -> {
                    traverseChildren(docStrings, packageName, FQCN_DELIM, grandChild);
                });
            }
        });

        return docStrings.build();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.espertech.esperio.socket.config.ConfigurationSocketAdapter.java

/**
 * Returns an input stream from an application resource in the classpath.
 * <p>/*from ww w.  j  ava2 s.  c  om*/
 * The method first removes the '/' character from the resource name if
 * the first character is '/'.
 * <p>
 * The lookup order is as follows:
 * <p>
 * If a thread context class loader exists, use <tt>Thread.currentThread().getResourceAsStream</tt>
 * to obtain an InputStream.
 * <p>
 * If no input stream was returned, use the <tt>Configuration.class.getResourceAsStream</tt>.
 * to obtain an InputStream.
 * <p>
 * If no input stream was returned, use the <tt>Configuration.class.getClassLoader().getResourceAsStream</tt>.
 * to obtain an InputStream.
 * <p>
 * If no input stream was returned, throw an Exception.
 *
 * @param resource to get input stream for
 * @return input stream for resource
 */
protected static InputStream getResourceAsStream(String resource) {
    String stripped = resource.startsWith("/") ? resource.substring(1) : resource;

    InputStream stream = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        stream = classLoader.getResourceAsStream(stripped);
    }
    if (stream == null) {
        stream = ConfigurationSocketAdapter.class.getResourceAsStream(resource);
    }
    if (stream == null) {
        stream = ConfigurationSocketAdapter.class.getClassLoader().getResourceAsStream(stripped);
    }
    if (stream == null) {
        throw new RuntimeException(resource + " not found");
    }
    return stream;
}

From source file:gemlite.core.util.Util.java

public static Resource toResource(ClassLoader loader, String file) {
    InputStream in = loader.getResourceAsStream(file);
    byte[] bt;/*from  ww w.j av  a 2  s .c o  m*/
    try {
        bt = new byte[in.available()];
        in.read(bt);
        in.close();
        ByteArrayResource res = new ByteArrayResource(bt);
        return res;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.siphyc.utils.Utilities.java

public static Properties loadConfig() {
    try {/*from ww  w  .  jav a 2s. c  o m*/
        Properties prop = new Properties();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        prop.load(classLoader.getResourceAsStream("active.properties"));
        return prop;
    } catch (IOException ex) {
        logger.error("<>shit hit the fan !" + ex);
        return null;
    }
}

From source file:com.amazonaws.services.kinesis.multilang.MultiLangDaemonConfig.java

private static Properties loadProperties(ClassLoader classLoader, String propertiesFileName)
        throws IOException {
    Properties properties = new Properties();
    try (InputStream propertiesStream = classLoader.getResourceAsStream(propertiesFileName)) {
        properties.load(propertiesStream);
        return properties;
    }//from   w w  w.j av  a2 s.c  o  m
}

From source file:com.espertech.esperio.http.config.ConfigurationHTTPAdapter.java

/**
 * Returns an input stream from an application resource in the classpath.
 * <p>/* ww  w .j a  v a2s.co m*/
 * The method first removes the '/' character from the resource name if
 * the first character is '/'.
 * <p>
 * The lookup order is as follows:
 * <p>
 * If a thread context class loader exists, use <tt>Thread.currentThread().getResourceAsStream</tt>
 * to obtain an InputStream.
 * <p>
 * If no input stream was returned, use the <tt>Configuration.class.getResourceAsStream</tt>.
 * to obtain an InputStream.
 * <p>
 * If no input stream was returned, use the <tt>Configuration.class.getClassLoader().getResourceAsStream</tt>.
 * to obtain an InputStream.
 * <p>
 * If no input stream was returned, throw an Exception.
 *
 * @param resource to get input stream for
 * @return input stream for resource
 */
protected static InputStream getResourceAsStream(String resource) {
    String stripped = resource.startsWith("/") ? resource.substring(1) : resource;

    InputStream stream = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        stream = classLoader.getResourceAsStream(stripped);
    }
    if (stream == null) {
        stream = ConfigurationHTTPAdapter.class.getResourceAsStream(resource);
    }
    if (stream == null) {
        stream = ConfigurationHTTPAdapter.class.getClassLoader().getResourceAsStream(stripped);
    }
    if (stream == null) {
        throw new RuntimeException(resource + " not found");
    }
    return stream;
}

From source file:com.sun.socialsite.business.EmfProvider.java

/**
 * Loads properties from given resourceName using given class loader
 * @param resourceName The name of the resource containing properties
 * @param cl Classloeder to be used to locate the resouce
 * @return A properties object//w  w  w  . j  ava 2s  .  c o  m
 * @throws SocialSiteException
 */
protected static Properties loadPropertiesFromResourceName(String resourceName, ClassLoader cl)
        throws SocialSiteException {
    Properties props = new Properties();
    InputStream in = null;
    in = cl.getResourceAsStream(resourceName);
    if (in == null) {
        throw new SocialSiteException("Could not locate properties to load " + resourceName);
    }
    try {
        props.load(in);
    } catch (IOException ioe) {
        throw new SocialSiteException("Could not load properties from " + resourceName);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ioe) {
            }
        }
    }

    return props;
}