Example usage for org.apache.commons.discovery.tools ResourceUtils getResource

List of usage examples for org.apache.commons.discovery.tools ResourceUtils getResource

Introduction

In this page you can find the example usage for org.apache.commons.discovery.tools ResourceUtils getResource.

Prototype

public static Resource getResource(Class spi, String resourceName, ClassLoaders loaders)
        throws DiscoveryException 

Source Link

Document

Load the resource resourceName.

Usage

From source file:mitm.common.util.ResourceLocator.java

/**
 * tries to load the resource by name. Returns null if the resource cannot be found.
 *//*from w ww .  jav a 2  s . c om*/
@SuppressWarnings("resource")
public InputStream getResourceAsStream(String resourceName) throws FileNotFoundException {
    InputStream input = null;

    File file = null;

    if (systemProperty != null) {
        String base = System.getProperty(systemProperty);

        if (base != null) {
            file = new File(base, resourceName);
        }
    }

    if (file == null || !file.exists()) {
        file = new File(baseDir, resourceName);
    }

    if (file.exists()) {
        logger.debug("Resource found at: " + file);

        input = new FileInputStream(file);
    } else {
        ClassLoaders classLoaders = ClassLoaders.getAppLoaders(null, null, true);

        Resource resource = ResourceUtils.getResource(null, resourceName, classLoaders);

        if (resource != null) {
            logger.debug("Resource found at: " + resource.getResource());

            input = resource.getResourceAsStream();
        }
    }

    return input;
}

From source file:org.mule.config.dsl.util.ClasspathBuilder.java

/**
 * Getter of the input stream.//from   www  . j  ava  2 s  .  co  m
 *
 * @return the input stream ready to use
 * @throws org.mule.config.dsl.IOException if resource not found
 * @see java.io.InputStream
 */
public InputStream get() throws IOException {
    if (content == null) {
        final Resource res = ResourceUtils.getResource(this.getClass(), path, null);
        if (res == null) {
            throw new IOException("Resource not found.");
        }
        this.content = res.getResourceAsStream();
    }
    return content;
}