Example usage for java.lang ClassLoader getResource

List of usage examples for java.lang ClassLoader getResource

Introduction

In this page you can find the example usage for java.lang ClassLoader getResource.

Prototype

public URL getResource(String name) 

Source Link

Document

Finds the resource with the given name.

Usage

From source file:org.apache.drill.exec.server.rest.WebResourceServer.java

@GET
@Path("/{path}")
@Produces(MediaType.TEXT_PLAIN)//ww  w.ja va 2  s .c  om
public Response getResource(@PathParam("path") String path) throws IOException {
    try {
        String s = "rest/www/" + path;
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream is = new BufferedInputStream(cl.getResource(s).openStream());

        String mime = "text/plain";
        if (s.endsWith(".js")) {
            mime = "text/javascript";
        } else if (s.endsWith(".css")) {
            mime = "text/css";
        } else {
            mime = URLConnection.guessContentTypeFromStream(is);
        }

        byte[] d = IOUtils.toByteArray(is);
        return Response.ok(d).type(mime).build();
    } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
    }

    return Response.noContent().status(Status.NOT_FOUND).build();
}

From source file:pt.webdetails.cdb.bean.factory.CoreBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    try {/*from   www .  j  ava  2  s  .com*/
        final ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource(config);
        if (url != null) {
            logger.debug("Found spring file @ " + url); //$NON-NLS-1$
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(config) {
                @Override
                protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {

                    beanDefinitionReader.setBeanClassLoader(cl);
                }

                @Override
                protected void prepareBeanFactory(ConfigurableListableBeanFactory clBeanFactory) {
                    super.prepareBeanFactory(clBeanFactory);
                    clBeanFactory.setBeanClassLoader(cl);
                }

                /**
                 * Critically important to override this and return the desired
                 * CL
                 **/
                @Override
                public ClassLoader getClassLoader() {
                    return cl;
                }
            };
            return context;
        }
    } catch (Exception e) {
        logger.fatal("Error loading " + CDB_SPRING_BEAN, e);
    }
    logger.fatal(
            "Spring definition file does not exist. There should be a <plugin_name>.spring.xml file on the classpath ");
    return null;

}

From source file:com.boundary.sdk.event.esper.QueryListTest.java

@Test
public void testReadURIClasspath() throws IOException, URISyntaxException {
    ClassLoader classLoader = this.getClass().getClassLoader();
    URL url = classLoader.getResource(QUERY_LIST_FILE);
    File file = new File(url.toURI());
    assertTrue("check if file", file.isFile());
    assertTrue("check file exists", file.exists());
}

From source file:com.baeldung.file.FileOperationsManualTest.java

@Test
public void givenFileName_whenUsingFileUtils_thenFileData() throws IOException {
    String expectedData = "Hello World from fileTest.txt!!!";

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("fileTest.txt").getFile());
    String data = FileUtils.readFileToString(file);

    assertEquals(expectedData, data.trim());
}

From source file:org.callistasoftware.netcare.mvk.authentication.service.impl.MvkAuthenticationServiceImpl.java

private ValidateToken getService(final String address) {

    try {// w w w  .j  a  v  a  2 s. co  m
        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
        final URL wsdl = loader
                .getResource("schemas/ValidateID/wsdl/interactions/MvkAsb_ValidateID_ValidateToken.wsdl");

        final ValidateTokenValidateTokenHttpService service = new ValidateTokenValidateTokenHttpService(wsdl);
        final ValidateToken serviceInterface = service.getExportsValidateTokenValidateTokenHttpPort();

        ((BindingProvider) serviceInterface).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                address);

        return serviceInterface;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:pt.webdetails.cdc.bean.factory.CoreBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    try {/*from ww  w  .  j av a  2s  .  com*/
        final ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource(config);
        if (url != null) {
            logger.debug("Found spring file @ " + url); //$NON-NLS-1$
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(config) {
                @Override
                protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {

                    beanDefinitionReader.setBeanClassLoader(cl);
                }

                @Override
                protected void prepareBeanFactory(ConfigurableListableBeanFactory clBeanFactory) {
                    super.prepareBeanFactory(clBeanFactory);
                    clBeanFactory.setBeanClassLoader(cl);
                }

                /**
                 * Critically important to override this and return the desired
                 * CL
                 **/
                @Override
                public ClassLoader getClassLoader() {
                    return cl;
                }
            };
            return context;
        }
    } catch (Exception e) {
        logger.fatal("Error loading " + CDC_SPRING_BEAN, e);
    }
    logger.fatal(
            "Spring definition file does not exist. There should be a <plugin_name>.spring.xml file on the classpath ");
    return null;

}

From source file:com.baeldung.file.FileOperationsManualTest.java

@Test
public void givenFileName_whenUsingClassloader_thenFileData() throws IOException {
    String expectedData = "Hello World from fileTest.txt!!!";

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("fileTest.txt").getFile());
    InputStream inputStream = new FileInputStream(file);
    String data = readFromInputStream(inputStream);

    assertEquals(expectedData, data.trim());
}

From source file:pt.webdetails.cpf.bean.AbstractBeanFactory.java

protected ConfigurableApplicationContext getSpringBeanFactory(String config) {
    getLogger().debug("bean factory init");

    try {/*from  w ww. ja  v a 2 s.co m*/
        // important: use the plugin's classloader
        final ClassLoader cl = getClass().getClassLoader();

        URL url = cl.getResource(config);
        if (url != null) {
            getLogger().debug("Found spring file @ " + url);

            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(config) {

                @Override
                protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
                    beanDefinitionReader.setBeanClassLoader(cl);
                }

                @Override
                protected void prepareBeanFactory(ConfigurableListableBeanFactory clBeanFactory) {
                    super.prepareBeanFactory(clBeanFactory);
                    clBeanFactory.setBeanClassLoader(cl);
                }

                /**
                 * Critically important to override this and return the desired classloader
                 **/
                @Override
                public ClassLoader getClassLoader() {
                    return cl;
                }
            };
            getLogger().debug("bean factory context");
            return context;
        }
    } catch (Exception e) {
        getLogger().fatal("Error loading " + config, e);
    }
    getLogger().fatal("Spring definition file does not exist. " + "There should be a " + config
            + " file on the classpath ");
    return null;

}

From source file:io.hops.tensorflow.TestYarnTF.java

@Test(timeout = 90000)
public void testCreateClusterSpec() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    String mainPath = classLoader.getResource("create_cluster_server.py").getPath();
    String[] args = { "--" + AM_JAR, APPMASTER_JAR, "--" + WORKERS, "4", "--" + PSES, "1", "--" + MEMORY, "256",
            "--" + VCORES, "1", "--" + MAIN, mainPath, "--" + ARGS,
            "--images mnist/tfr/train --format tfr --mode train --model mnist_model", "--" + TENSORBOARD };

    LOG.info("Initializing yarntf Client");
    final Client client = new Client(new Configuration(yarnCluster.getConfig()));
    boolean initSuccess = client.init(args);
    Assert.assertTrue(initSuccess);/* ww  w .j  a v  a  2s  .co  m*/
    LOG.info("Running yarntf Client");
    final ApplicationId appId = client.submitApplication();

    boolean result = client.monitorApplication(appId);
    LOG.info("Client run completed. Result=" + result);

    Assert.assertTrue(TestUtils.dumpAllRemoteContainersLogs(yarnCluster, appId));
    Assert.assertEquals(5, TestUtils.verifyContainerLog(yarnCluster, 5, null, true, "Number of arguments: 9"));
    Assert.assertEquals(4,
            TestUtils.verifyContainerLog(yarnCluster, 5, null, true, "YARNTF_TB_DIR=tensorboard_"));
}

From source file:com.boundary.sdk.event.esper.EsperRouteBuilder.java

/**
 * TODO: Proper handling of exception/*from w w w.j a  va 2s .co m*/
 * @throws URISyntaxException {@link URISyntaxException}
 */
protected void loadConfiguration() throws URISyntaxException {
    ClassLoader classLoader = this.getClass().getClassLoader();
    URL url = classLoader.getResource(this.configuration);
    this.queryList = load(url.toURI());
}