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:pdsanchez.mywebtools.model.service.AuxSvcImpl.java

private File getDataFile() {
    ClassLoader classLoader = getClass().getClassLoader();
    return new File(classLoader.getResource("data.tsv").getFile());
}

From source file:myDarkDiary.service.model.Image.java

public Image(User userId, String fileName, String filePath) throws IOException {
    this.userId = userId;
    this.fileName = fileName;
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource(filePath).getFile());
    InputStream targetStream = new FileInputStream(file);
    this.data = IOUtils.toByteArray(targetStream);
}

From source file:com.flipkart.flux.MigrationUtil.MigrationsRunner.java

public void migrate(String dbName) {
    try {/*from w  w  w  . ja va  2 s  . c om*/
        Configuration configuration = yamlConfiguration.subset(dbName + ".Hibernate");
        Properties properties = new Properties();
        properties.put("user", configuration.getProperty("hibernate.connection.username"));
        properties.put("password", configuration.getProperty("hibernate.connection.password"));
        String url = (String) configuration.getProperty("hibernate.connection.url");
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        java.sql.Connection connection = DriverManager.getConnection(url, properties);
        Database database = DatabaseFactory.getInstance()
                .findCorrectDatabaseImplementation(new JdbcConnection(connection));
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(dbName + "/migrations.xml").getFile());
        Liquibase liquibase = new Liquibase(file.getCanonicalPath(), new FileSystemResourceAccessor(),
                database);
        liquibase.update(new Contexts());
    } catch (Exception e) {
        System.err.println("Unable to perform database migration.");
        e.printStackTrace();
    }
}

From source file:gemlite.core.internal.support.hotdeploy.scanner.ScannerIteratorItem.java

private byte[] readContent(ClassLoader loader, JarEntry entry) throws IOException {
    URL url = loader.getResource(entry.getName());
    URLConnection ulc = url.openConnection();
    InputStream in3 = ulc.getInputStream();
    InputStream in2 = url.openStream();

    InputStream in = loader.getResourceAsStream(entry.getName());
    if (in == null) {
        LogUtil.getCoreLog().trace("ReadContent inputStream is null entry.name={} , loader={}", entry.getName(),
                loader);/*from w w  w  . jav  a 2  s  .  c  o m*/
    }

    BufferedInputStream bi = new BufferedInputStream(in);
    byte[] bt = new byte[in.available()];
    bi.read(bt);
    bi.close();
    in.close();
    return bt;
}

From source file:org.wildfly.camel.test.spring.SpringJeeNamespaceTest.java

@Test
public void testLoadHandlersFromDeployment() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    URL resurl = classLoader.getResource("META-INF/spring.handlers");
    Assert.assertNotNull("URL not null", resurl);
}

From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java

/**
 * Find all resources from a given file or directory and add them to the provided
 * list./*from  w  w w.  ja va  2 s. co  m*/
 *
 * @author paouelle
 *
 * @param urls the non-<code>null</code> collection of resources where to add new
 *        found resources
 * @param file the non-<code>null</code> file or directory from which to find
 *        resources
 * @param resource the non-<code>null</code> resource being scanned that
 *        corresponds to given file or directory
 * @param cl the classloader to find the resources with
 */
private static void findResourcesFromFile(Collection<URL> urls, File file, String resource, ClassLoader cl) {
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            ReflectionUtils.findResourcesFromFile(urls, f, resource + File.separatorChar + f.getName(), cl);
        }
    } else {
        final URL u = cl.getResource(resource);

        if (u != null) {
            urls.add(u);
        }
    }
}

From source file:hudson.plugins.testlink.result.parser.tap.TestTapParser.java

public void parseInvalidTapFile() {
    ClassLoader cl = TestTapParser.class.getClassLoader();
    URL url = cl.getResource("hudson/plugins/testlink/result/parser/tap/invalid.tap");
    File file = new File(url.getFile());

    try {//from   www .jav a 2  s  .  c o m
        this.parser.parse(file);
    } catch (ParserException p) {
        assertNotNull(p);
    }

}

From source file:org.apache.axis2.classloader.JarFileClassLoaderTest.java

/**
 * Test that if one of the URLs is a directory, the class loader doesn't allow access to files
 * outside of that directory (by using ".." in the resource name). See AXIS2-4282.
 * <p>/*  ww  w . jav a 2  s.  c o  m*/
 * Note that while
 * {@linkplain http://java.sun.com/j2se/1.4.2/docs/guide/resources/resources.html} suggests
 * that ".." should be prohibited altogether, Sun's URLClassLoader implementation allows this,
 * as long as the resource name doesn't specify a file outside of the directory. E.g.
 * "dir/../a" is an allowed resource name (equivalent to "a").
 * 
 * @throws Exception
 */
public void testConfinement() throws Exception {
    ClassLoader cl = new JarFileClassLoader(new URL[] { new File(tmpDir, "root").toURL() });
    assertNull(cl.getResource("../outside"));
    assertNotNull(cl.getResource("a"));
    assertNotNull(cl.getResource("dir/b"));
}

From source file:org.createnet.search.raptor.search.query.impl.ObjectQueryTest.java

@Before
public void setUp() throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    Path path = Paths.get(classLoader.getResource("object-query.json").getPath());
    content = new String(Files.readAllBytes(path));
}