Example usage for java.lang ClassLoader getResources

List of usage examples for java.lang ClassLoader getResources

Introduction

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

Prototype

public Enumeration<URL> getResources(String name) throws IOException 

Source Link

Document

Finds all the resources with the given name.

Usage

From source file:org.seasar.teeda.core.util.IteratorUtil.java

public static Iterator getResourcesIterator(ClassLoader loader, String path) {
    try {/* w ww  .jav a 2  s.co m*/
        Enumeration e = loader.getResources(path);
        return new EnumerationIterator(e);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}

From source file:org.nuclos.common.startup.Startup.java

public static InputStream getClasspathResource(String path) throws IOException {
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    final Enumeration<URL> en = cl.getResources(path);
    if (!en.hasMoreElements()) {
        throw new IllegalArgumentException("classpath resource " + path + " not found");
    }//from  w  w w  . j a va  2 s .  c  o m
    final URL url = en.nextElement();
    if (en.hasMoreElements()) {
        throw new IllegalArgumentException(
                "duplicated classpath resource " + path + " at " + url + " and " + en.nextElement());
    }
    return url.openStream();
}

From source file:org.jcurl.core.helpers.Version.java

static final Manifest findManifest(final ClassLoader clz, final String marker) throws IOException {
    for (final Enumeration<URL> enu = clz.getResources("META-INF/MANIFEST.MF"); enu.hasMoreElements();) {
        final URL url = enu.nextElement();
        if (url.getPath().indexOf(marker) >= 0)
            return new Manifest(url.openStream());
    }/*from   ww  w . ja  v  a 2  s.  co  m*/
    log.info("Manifest not found in");
    for (final Enumeration<URL> enu = clz.getResources("META-INF/MANIFEST.MF"); enu.hasMoreElements();)
        log.info("url=" + enu.nextElement());
    return new Manifest(new File("config/jcurl.jar/" + "META-INF/MANIFEST.MF").toURL().openStream());
}

From source file:org.apache.asterix.external.provider.ParserFactoryProvider.java

protected static Map<String, Class> initFactories() throws AsterixException {
    Map<String, Class> factories = new HashMap<>();
    ClassLoader cl = ParserFactoryProvider.class.getClassLoader();
    try {/*from w w  w  . jav  a 2s . co  m*/
        Enumeration<URL> urls = cl.getResources(RESOURCE);
        for (URL url : Collections.list(urls)) {
            List<String> classNames;
            try (InputStream is = url.openStream()) {
                classNames = IOUtils.readLines(is, StandardCharsets.UTF_8);
            }
            for (String className : classNames) {
                if (className.startsWith("#")) {
                    continue;
                }
                final Class<?> clazz = Class.forName(className);
                String[] formats = ((IDataParserFactory) clazz.newInstance()).getFormats();
                for (String format : formats) {
                    if (factories.containsKey(format)) {
                        throw new AsterixException("Duplicate format " + format);
                    }
                    factories.put(format, clazz);
                }
            }
        }
    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new AsterixException(e);
    }
    return factories;
}

From source file:io.fabric8.maven.core.util.ClassUtil.java

public static Set<String> getResources(String resource, List<ClassLoader> additionalClassLoaders)
        throws IOException {

    ClassLoader[] classLoaders = mergeClassLoaders(additionalClassLoaders);

    Set<String> ret = new HashSet<>();
    for (ClassLoader cl : classLoaders) {
        Enumeration<URL> urlEnum = cl.getResources(resource);
        ret.addAll(extractUrlAsStringsFromEnumeration(urlEnum));
    }/*from  w ww. ja  v a 2 s.c om*/
    return ret;
}

From source file:org.wings.util.PropertyDiscovery.java

/**
 * Loads a file from the webapp's classpath into a properties file.
 *
 * @param propertyFileClasspath the file's classpath
 * @return <code>true</code> if something was found and loaded
 *///  www  .j  a va  2s .c o m
private static boolean loadPropertiesFromClasspath(final Properties properties,
        final String propertyFileClasspath) throws IOException {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final Enumeration<URL> filesFound = classLoader.getResources(propertyFileClasspath);
    boolean somethingLoaded = false;
    while (filesFound.hasMoreElements()) {
        final URL propertyFile = filesFound.nextElement();
        try {
            InputStream content = propertyFile.openStream();
            properties.load(content);
            content.close();
            somethingLoaded = true;
            if (log.isDebugEnabled()) {
                log.debug("Loaded properties from classpath file '" + propertyFileClasspath + "'");
            }
        } catch (Exception e) {
            final String error = "Unable to open " + propertyFile.toExternalForm() + " from classpath due " + e
                    + ". Please check deployment!";
            throw new IOException(error);
        }
    }
    return somethingLoaded;
}

From source file:org.op4j.devutils.selected.SelectedImplFileCreator.java

private static Class<?>[] getClasses(String packageName) throws ClassNotFoundException, IOException {

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile().replaceAll("%20", " ")));
    }/*w  w  w .j a  v a  2s.c  om*/
    ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
}

From source file:Main.java

private static List<Class> getClasses(String packageName) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = null;
    try {//  ww  w.  j a v  a2 s.co m
        resources = classLoader.getResources(path);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName));
    }
    return classes;
}

From source file:org.springframework.context.index.CandidateComponentsIndexLoader.java

@Nullable
private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) {
    if (shouldIgnoreIndex) {
        return null;
    }/*from  w  w  w  . j  ava 2s  .c  om*/

    try {
        Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION);
        if (!urls.hasMoreElements()) {
            return null;
        }
        List<Properties> result = new ArrayList<>();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
            result.add(properties);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + result.size() + "] index(es)");
        }
        int totalCount = result.stream().mapToInt(Properties::size).sum();
        return (totalCount > 0 ? new CandidateComponentsIndex(result) : null);
    } catch (IOException ex) {
        throw new IllegalStateException(
                "Unable to load indexes from location [" + COMPONENTS_RESOURCE_LOCATION + "]", ex);
    }
}

From source file:com.netflix.config.ClasspathPropertiesConfiguration.java

private static void loadResources(ClassLoader loader, String resourceName) throws Exception {
    Enumeration<URL> resources = loader.getResources(resourceName);
    boolean loadedResources = false;
    while (resources.hasMoreElements()) {
        URL from = resources.nextElement();
        /* Properties props = loadProperties(from);
        String configName = getConfigName(props, from);
        containerConfiguration.addConfiguration(
              new PropertiesConfiguration(from), configName); */
        ConfigurationManager.loadPropertiesFromResources(from.getPath());
        log.debug("Added properties from:" + from + from.getPath());
        loadedResources = true;// ww  w  . j  a  v  a  2s  .  c  om
    }
    if (!loadedResources) {
        log.debug("Did not find any properties resource in the classpath with name:"
                + propertiesResourceRelativePath);
    }
}