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:com.athomas.androidkickstartr.util.ResourcesUtils.java

public static void copyResourcesTo(File resourcesDir, String filepathMarker) throws IOException {

    if (resourcesDir == null) {
        throw new IllegalArgumentException("The resources dir must not be null");
    }/*  w  ww . j a  v a2  s. co  m*/

    ClassLoader classLoader = ResourcesUtils.class.getClassLoader();
    URL url = classLoader.getResource(filepathMarker);
    String protocol = url.getProtocol();

    if (protocol.equals("file")) {
        File src = new File("src/main/resources");
        FileUtils.copyDirectory(src, resourcesDir);
    } else if (protocol.equals("jar")) {
        copyResourcesToFromJar(resourcesDir, url);
    }
}

From source file:com.metadave.eql.parser.EQLParserTest.java

public static String loadResource(String name) throws Exception {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource(name);
    File f = new File(url.getPath());
    return org.apache.commons.io.FileUtils.readFileToString(f, "UTF-8");
}

From source file:com.knewton.mapreduce.example.SSTableMRExample.java

private static Job getJobConf(CommandLine cli) throws URISyntaxException, IOException {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf);/*from ww  w . ja va 2  s  . c o  m*/
    ClassLoader loader = SSTableMRExample.class.getClassLoader();
    URL url = loader.getResource("knewton-site.xml");
    conf.addResource(url);

    SSTableInputFormat.setPartitionerClass(RandomPartitioner.class.getName(), job);
    SSTableInputFormat.setComparatorClass(LongType.class.getName(), job);
    SSTableInputFormat.setColumnFamilyName("StudentEvents", job);
    SSTableInputFormat.setKeyspaceName("demoKeyspace", job);

    if (cli.hasOption('s')) {
        conf.set(PropertyConstants.START_DATE.txt, cli.getOptionValue('s'));
    }
    if (cli.hasOption('e')) {
        conf.set(PropertyConstants.END_DATE.txt, cli.getOptionValue('e'));
    }
    return job;
}

From source file:com.boundary.sdk.snmp.metric.OidMapList.java

public static OidMapList load(String resource) throws URISyntaxException {
    OidMapList instance = new OidMapList();

    ClassLoader classLoader = instance.getClass().getClassLoader();
    URL url = classLoader.getResource(resource);
    File file = new File(url.toURI());

    ObjectMapper mapper = new ObjectMapper();

    try {/*  w  w w.j  a  v  a 2  s . c o  m*/
        instance = mapper.readValue(file, OidMapList.class);
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return instance;
}

From source file:de.uni.bremen.monty.moco.CompileFilesBaseTest.java

protected static Collection<File> getFiles4Filter(String folderPath, String[] filter)
        throws URISyntaxException {
    Class aClass = CompileTestProgramsTest.class;
    ClassLoader classLoader = aClass.getClassLoader();
    File testprogramFolder = new File(classLoader.getResource(folderPath).toURI());
    return FileUtils.listFiles(testprogramFolder, filter, true);
}

From source file:com.boundary.sdk.snmp.metric.HostLists.java

public static HostLists load(String resource) throws URISyntaxException {
    HostLists instance = new HostLists();

    ClassLoader classLoader = instance.getClass().getClassLoader();
    URL url = classLoader.getResource(resource);
    File file = new File(url.toURI());

    ObjectMapper mapper = new ObjectMapper();

    try {/*from   ww w .j av  a2 s.co  m*/
        instance = mapper.readValue(file, HostLists.class);
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return instance;
}

From source file:com.boundary.sdk.util.UnixTimeSerializerTest.java

public static MyDate read(String resource) throws URISyntaxException {
    MyDate instance = new MyDate();

    ClassLoader classLoader = instance.getClass().getClassLoader();
    URL url = classLoader.getResource(resource);
    File file = new File(url.toURI());

    ObjectMapper mapper = new ObjectMapper();

    try {/*  w ww  .  ja v a 2s  . com*/
        instance = mapper.readValue(file, MyDate.class);
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return instance;
}

From source file:com.google.refine.model.medadata.PackageExtension.java

/**
 * To build a Package object from a template file contains empty metadata
 *  /*ww w . j  av  a2 s. c om*/
 * @param templateFile
 */
public static Package buildPackageFromTemplate() {
    try {
        ClassLoader classLoader = PackageExtension.class.getClassLoader();
        File file = new File(classLoader.getResource(DATAPACKAGE_TEMPLATE_FILE).getFile());
        return new Package(FileUtils.readFileToString(file), false);
    } catch (ValidationException e) {
        logger.error("validation failed", ExceptionUtils.getStackTrace(e));
    } catch (DataPackageException e) {
        logger.error("DataPackage Exception", ExceptionUtils.getStackTrace(e));
    } catch (IOException e) {
        logger.error("IOException when build package from template", ExceptionUtils.getStackTrace(e));
    }

    return null;
}

From source file:com.googlecode.xmlzen.utils.FileUtils.java

/**
 * Gets a {@link File} from a String that represents file path which is 
 * relative to current Class Path/*from w ww .j av  a  2s. c om*/
 * 
 * @param path Path to File
 * @param classLoader {@link ClassLoader} that should be able to see the 
 *         file
 * @return {@link File} object or null if nothing is found
 */
public static File getClassPathFile(final String path, final ClassLoader classLoader) {
    final URL url = classLoader.getResource(path);
    if (url == null) {
        return null;
    }
    return new File(url.getFile());
}

From source file:Main.java

private static void recurse(List<Class<?>> classes, String packageName, Class<? extends Annotation> a)
        throws ClassNotFoundException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    String path = packageName.replace('.', '/');
    URL resource = loader.getResource(path);
    if (resource != null) {
        String filePath = resource.getFile();
        if (filePath != null && new File(filePath).isDirectory()) {
            for (String file : new File(filePath).list()) {
                if (file.endsWith(".class")) {
                    String name = packageName + '.' + file.substring(0, file.indexOf(".class"));
                    Class<?> clazz = Class.forName(name);
                    if (clazz.isAnnotationPresent(a))
                        classes.add(clazz);
                } else if (new File(filePath, file).isDirectory()) {
                    recurse(classes, packageName + "." + file, a);
                }//  ww w . ja va 2 s . co m
            }
        }
    }
}