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.softdays.mandy.service.support.SchoolHolidayServiceImpl.java

private Document createDocument()
        throws FactoryConfigurationError, ParserConfigurationException, SAXException, IOException {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final InputStream input = classLoader.getResourceAsStream(XML_DATASOURCE);
    final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

    return dBuilder.parse(input);
}

From source file:cl.vmetrix.operation.persistence.XmlValidator.java

private String getFileSchema(String fileName) throws IOException {

    String result = "";

    ClassLoader classLoader = getClass().getClassLoader();
    try {/*from w  ww .  j a  va 2  s .  c o  m*/
        result = IOUtils.toString(classLoader.getResourceAsStream(fileName));
    } catch (IOException e) {
        throw new IOException(e);
    }

    return result;

}

From source file:com.mindmutex.draugiem.DraugiemHttpFilter.java

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    String configPath = filterConfig.getInitParameter("configPath");
    if (configPath == null) {
        configPath = DEFAULT_CONFIG_PATH;
    }//  w  w  w.  jav a  2s. c o  m

    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(configPath);
    if (inputStream == null) {
        throw new ServletException("unable to load configuration file: " + configPath);
    }
    configuration = new Properties();
    try {
        configuration.load(inputStream);
    } catch (IOException ex) {
        throw new ServletException(ex);
    }
    configureFilter();

    logger.info("Initialised");
}

From source file:jfs.sync.JFSFileProducerManager.java

/**
 * Registers all factories and sets the default factory.
 *///from www. j  a  v a2 s .  c o  m
@SuppressWarnings("unchecked")
private JFSFileProducerManager() {
    factories = new HashMap<String, JFSFileProducerFactory>();
    Properties p = new Properties();
    ClassLoader classLoader = this.getClass().getClassLoader();
    try {
        p.load(classLoader.getResourceAsStream("producers.properties"));
    } catch (Exception e) {
        log.error("JFSFileProducerManager()", e);
    } // try/catch
    for (Object property : p.keySet()) {
        String className = "" + property;
        if (className.startsWith("jfs.sync.")) {
            if ("on".equals(p.getProperty(className))) {
                try {
                    Class<JFSFileProducerFactory> c = (Class<JFSFileProducerFactory>) classLoader
                            .loadClass(className);
                    Constructor<JFSFileProducerFactory> constructor = c.getConstructor(new Class<?>[0]);
                    JFSFileProducerFactory factory = constructor.newInstance(new Object[0]);
                    String name = factory.getName();
                    factories.put(name, factory);
                } catch (Exception e) {
                    log.error("JFSFileProducerManager()", e);
                } // try/catch
            } // if
        } // if
    } // for
    defaultFactory = factories.get(JFSLocalFileProducerFactory.SCHEME_NAME);
}

From source file:com.glaf.core.util.ReflectUtils.java

public static InputStream getResourceAsStream(String name) {
    InputStream resourceStream = null;
    ClassLoader classLoader = getCustomClassLoader();
    if (classLoader != null) {
        resourceStream = classLoader.getResourceAsStream(name);
    }/*ww  w  .  j  a  v  a2 s  . co m*/

    if (resourceStream == null) {
        // Try the current Thread context classloader
        classLoader = Thread.currentThread().getContextClassLoader();
        resourceStream = classLoader.getResourceAsStream(name);
        if (resourceStream == null) {
            // Finally, try the classloader for this class
            classLoader = ReflectUtils.class.getClassLoader();
            resourceStream = classLoader.getResourceAsStream(name);
        }
    }
    return resourceStream;
}

From source file:org.camunda.bpm.ext.sdk.DeploymentBuilder.java

public DeploymentBuilder classPathResource(String classpathResource) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null) {
        cl = CamundaClient.class.getClassLoader();
    }//from  ww  w.ja  v a 2  s.c  o m
    InputStream inputStream = cl.getResourceAsStream(classpathResource);

    String filename = classpathResource;
    requestBuilder.addBinaryBody("resource-" + (++dataPartConter), inputStream,
            ContentType.APPLICATION_OCTET_STREAM, filename);
    return this;
}

From source file:com.sonicle.webtop.core.app.shiro.filter.Anonymous.java

protected void doWriteRobots(ServletRequest request, ServletResponse response) throws IOException {
    ClassLoader cl = LangUtils.findClassLoader(this.getClass());
    HttpServletResponse httpResponse = WebUtils.toHttp(response);

    InputStream is = null;//from  ww w . j  a  v  a2 s . c om
    try {
        is = cl.getResourceAsStream(ROBOTS_FILE);
        ServletUtils.setCharacterEncoding(httpResponse, "UTF-8");
        ServletUtils.setCacheControl(httpResponse, 504);
        ServletUtils.writeContent(httpResponse, is, "text/plain");
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:fr.cyann.jinyparser.benchmarkTest.GaselTest.java

public void testGarFile() throws IOException {

    GrammarElement.ProcessedGrammar grammar = getGrammar();

    // to BNF// w w w.  ja  v a2 s. co m
    System.out.println("Grammar tree:\n" + grammar.toBnf());
    System.out.println();

    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream is = cl.getResourceAsStream("test.gar");

    String source = IOUtils.toString(is);
    /*String source = "#;AIRBLOCK;1;413;20160526;20160622;6763;EAR_P\n"
     + "A;000EG;74\n"
     + "P;57;-15";*/

    long time = System.currentTimeMillis();

    // parse
    GrammarContext c = grammar.parse(source);

    System.out.println(String.format("Time spent %d", System.currentTimeMillis() - time));

    System.out.println("Parse tree: " + c.getParseTree());
}

From source file:com.espertech.esper.client.Configuration.java

/**
 * Returns an input stream from an application resource in the classpath.
 * <p>// w  ww.  jav  a 2s.  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 = Configuration.class.getResourceAsStream(resource);
    }
    if (stream == null) {
        stream = Configuration.class.getClassLoader().getResourceAsStream(stripped);
    }
    if (stream == null) {
        throw new EPException(resource + " not found");
    }
    return stream;
}

From source file:org.apache.axis2.deployment.TransportDeployer.java

public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException {
    boolean isDirectory = deploymentFileData.getFile().isDirectory();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {/*  w w w.  j a v a 2  s  . co m*/
        deploymentFileData.setClassLoader(isDirectory, axisConfig.getModuleClassLoader(),
                (File) axisConfig.getParameterValue(Constants.Configuration.ARTIFACTS_TEMP_DIR),
                axisConfig.isChildFirstClassLoading());

        ClassLoader loader = deploymentFileData.getClassLoader();
        Thread.currentThread().setContextClassLoader(loader);
        InputStream xmlStream = loader.getResourceAsStream("META-INF/transport.xml");
        OMElement element = (OMElement) XMLUtils.toOM(xmlStream);
        element.build();
        AxisConfigBuilder builder = new AxisConfigBuilder(axisConfig);
        // Processing Transport Receivers
        Iterator trs_Reivers = element
                .getChildrenWithName(new QName(DeploymentConstants.TAG_TRANSPORT_RECEIVER));
        ArrayList transportReceivers = builder.processTransportReceivers(trs_Reivers);
        for (int i = 0; i < transportReceivers.size(); i++) {
            TransportInDescription transportInDescription = (TransportInDescription) transportReceivers.get(i);
            Parameter paramter = transportInDescription.getParameter("AutoStart");
            if (paramter != null) {
                configCtx.getListenerManager().addListener(transportInDescription, false);
                log.info("starting the transport : " + transportInDescription.getName());
            }
        }

        // Processing Transport Senders
        Iterator trs_senders = element.getChildrenWithName(new QName(DeploymentConstants.TAG_TRANSPORT_SENDER));

        builder.processTransportSenders(trs_senders);
        super.deploy(deploymentFileData);
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
}