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:org.apache.hadoop.realtime.client.DragonClient.java

private static String getTestRuntimeClasspath() {

    InputStream classpathFileStream = null;
    BufferedReader reader = null;
    String envClassPath = "";

    LOG.info("Trying to generate classpath for app master from current thread's classpath");
    try {//from  w w w. j av  a2  s  .c om

        // Create classpath from generated classpath
        // Check maven ppom.xml for generated classpath info
        // Works if compile time env is same as runtime. Mainly tests.
        ClassLoader thisClassLoader = Thread.currentThread().getContextClassLoader();
        String generatedClasspathFile = "yarn-apps-ds-generated-classpath";
        classpathFileStream = thisClassLoader.getResourceAsStream(generatedClasspathFile);
        if (classpathFileStream == null) {
            LOG.info("Could not classpath resource from class loader");
            return envClassPath;
        }
        LOG.info("Readable bytes from stream=" + classpathFileStream.available());
        reader = new BufferedReader(new InputStreamReader(classpathFileStream));
        String cp = reader.readLine();
        if (cp != null) {
            envClassPath += cp.trim() + ":";
        }
        // Put the file itself on classpath for tasks.
        envClassPath += thisClassLoader.getResource(generatedClasspathFile).getFile();
    } catch (IOException e) {
        LOG.info("Could not find the necessary resource to generate class path for tests. Error="
                + e.getMessage());
    }

    try {
        if (classpathFileStream != null) {
            classpathFileStream.close();
        }
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        LOG.info("Failed to close class path file stream or reader. Error=" + e.getMessage());
    }
    return envClassPath;
}

From source file:de.alpharogroup.lang.ClassUtils.java

/**
 * Gives the Inputstream from the resource. Wrapes the Class.getResourceAsStream(String)-method.
 *
 * @param name/*from   www .  ja va 2 s .c  o m*/
 *            The name from the resource.
 * @param obj
 *            The Object.
 * @return The resource or null if the resource does not exists.
 */
public static InputStream getResourceAsStream(final String name, final Object obj) {
    InputStream inputStream = obj.getClass().getResourceAsStream(name);
    if (null == inputStream) {
        final ClassLoader loader = ClassUtils.getClassLoader(obj);
        inputStream = loader.getResourceAsStream(name);
    }
    return inputStream;
}

From source file:de.alpharogroup.lang.ClassUtils.java

/**
 * Gives the Inputstream from the resource. Wrapes the Class.getResourceAsStream(String)-method.
 *
 * @param name//ww w  .ja  va  2  s .c o m
 *            The name from the resource.
 * @return The resource or null if the resource does not exists.
 */
public static InputStream getResourceAsStream(final String name) {
    final ClassLoader loader = ClassUtils.getClassLoader();
    final InputStream inputStream = loader.getResourceAsStream(name);
    return inputStream;
}

From source file:com.ephesoft.dcma.util.XMLUtil.java

/**
 * To create Document from Resource./*from ww w .  ja  va2 s.  c  o  m*/
 * 
 * @param resourceName String
 * @return Document
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document createDocumentFromResource(String resourceName)
        throws ParserConfigurationException, SAXException, IOException {
    ClassLoader loader = XMLUtil.class.getClassLoader();
    InputStream inputStream = loader.getResourceAsStream(resourceName);
    return createDocumentFrom(inputStream);
}

From source file:corina.util.SimpleLog.java

private static InputStream getResourceAsStream(final String name) {
    return (InputStream) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            ClassLoader threadCL = getContextClassLoader();

            if (threadCL != null) {
                return threadCL.getResourceAsStream(name);
            } else {
                return ClassLoader.getSystemResourceAsStream(name);
            }//from   w  w  w. j  a va 2 s.  c  om
        }
    });
}

From source file:com.datatorrent.stram.StramMiniClusterTest.java

private static String getTestRuntimeClasspath() {

    InputStream classpathFileStream = null;
    BufferedReader reader = null;
    String envClassPath = "";

    LOG.info("Trying to generate classpath for app master from current thread's classpath");
    try {/*  w  w w  .  j a v  a 2  s . c  o  m*/

        // Create classpath from generated classpath
        // Check maven pom.xml for generated classpath info
        // Works in tests where compile time env is same as runtime.
        ClassLoader thisClassLoader = Thread.currentThread().getContextClassLoader();
        String generatedClasspathFile = "mvn-generated-classpath";
        classpathFileStream = thisClassLoader.getResourceAsStream(generatedClasspathFile);
        if (classpathFileStream == null) {
            LOG.info("Could not load classpath resource " + generatedClasspathFile);
            return envClassPath;
        }
        LOG.info("Readable bytes from stream=" + classpathFileStream.available());
        reader = new BufferedReader(new InputStreamReader(classpathFileStream));
        String cp = reader.readLine();
        if (cp != null) {
            envClassPath += cp.trim() + ":";
        }
        // Put the file itself on classpath for tasks.
        envClassPath += thisClassLoader.getResource(generatedClasspathFile).getFile();
    } catch (IOException e) {
        LOG.info("Could not find the necessary resource to generate class path for tests. Error="
                + e.getMessage());
    }

    try {
        if (classpathFileStream != null) {
            classpathFileStream.close();
        }
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        LOG.info("Failed to close class path file stream or reader. Error=" + e.getMessage());
    }
    return envClassPath;
}

From source file:net.ontopia.topicmaps.db2tm.RelationMapping.java

protected static InputSource getRelaxNGSchema() throws IOException {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream i = cl.getResourceAsStream("net/ontopia/topicmaps/db2tm/db2tm.rnc");
    return new InputSource(i);
}

From source file:net.ontopia.topicmaps.db2tm.RelationMapping.java

public static RelationMapping readFromClasspath(String resource) throws IOException {
    if (resource == null)
        throw new DB2TMConfigException("Parameter 'resource' must be specified.");
    ClassLoader cloader = RelationMapping.class.getClassLoader();
    InputStream istream = cloader.getResourceAsStream(resource);
    if (istream != null) {
        log.debug("{}: loading from classpath", resource);
        return read(istream, null);
    } else {/*from www.  j a v a  2  s. c om*/
        throw new DB2TMConfigException("Resource '" + resource + "' not found on classpath.");
    }
}

From source file:it.jnrpe.utils.PluginRepositoryUtil.java

/**
 * Parse an XML plugin definition.//w  ww . j  a  v  a2s.c  o m
 * 
 * @param cl
 *            The classloader to be used to load classes
 * @param plugin
 *            The plugin XML element
        
        
 * @return the parsed plugin definition * @throws PluginConfigurationException
 *             - */
@SuppressWarnings("rawtypes")
private static PluginDefinition parsePluginDefinition(final ClassLoader cl, final Element plugin)
        throws PluginConfigurationException {

    // Check if the plugin definition is inside its own file
    if (getAttributeValue(plugin, "definedIn", false) != null) {
        StreamManager sm = new StreamManager();

        String sFileName = getAttributeValue(plugin, "definedIn", false);

        try {
            InputStream in = sm.handle(cl.getResourceAsStream(sFileName));

            return parseXmlPluginDefinition(cl, in);
        } finally {
            sm.closeAll();
        }
    }

    String pluginClass = getAttributeValue(plugin, "class", true);

    Class clazz;
    try {
        clazz = LoadedClassCache.getClass(cl, pluginClass);

        if (!IPluginInterface.class.isAssignableFrom(clazz)) {
            throw new PluginConfigurationException("Specified class '" + clazz.getName()
                    + "' in the plugin.xml file does not implement " + "the IPluginInterface interface");
        }

        if (isAnnotated(clazz)) {
            return loadFromPluginAnnotation(clazz);
        }

    } catch (ClassNotFoundException e) {
        throw new PluginConfigurationException(e.getMessage(), e);
    }

    // The class is not annotated not has an external definition file...
    // Loading from current xml file...

    String sDescription = getAttributeValue(plugin, "description", false);

    @SuppressWarnings("unchecked")
    PluginDefinition pluginDef = new PluginDefinition(getAttributeValue(plugin, "name", true), sDescription,
            clazz);

    parseCommandLine(pluginDef, plugin);
    return pluginDef;
}

From source file:be.fedict.eid.tsl.BelgianTrustServiceListFactory.java

private static Document loadDocumentFromResource(String resourceName) {
    Thread currentThread = Thread.currentThread();
    ClassLoader classLoader = currentThread.getContextClassLoader();
    InputStream documentInputStream = classLoader.getResourceAsStream(resourceName);
    if (null == documentInputStream) {
        throw new IllegalArgumentException("resource not found: " + resourceName);
    }/*from  w  w w.  ja  va2  s.co  m*/
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document tslDocument = documentBuilder.parse(documentInputStream);
        return tslDocument;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}