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.swordlord.gozer.builder.Parser.java

/**
 * Helper class for getClasses and getPackageNames (using Delegation)
 * /*from  ww w  . j  a  v  a  2  s.  com*/
 * @param pckgname
 * @return
 * @throws ClassNotFoundException
 */
private static File getClassesHelper(String pckgname) throws ClassNotFoundException {
    // Get a File object for the package
    File directory = null;
    try {
        ClassLoader cld = Thread.currentThread().getContextClassLoader();
        if (cld == null) {
            throw new ClassNotFoundException("Can't get class loader.");
        }
        String path = pckgname.replace('.', '/');
        URL resource = cld.getResource(path);
        if (resource == null) {
            throw new ClassNotFoundException("No resource for " + path);
        }
        directory = new File(resource.getFile());
        return directory;
    } catch (NullPointerException x) {
        throw new ClassNotFoundException(
                pckgname + " (" + directory + ") does not appear to be a valid package");
    }
}

From source file:net.sf.jasperreports.engine.util.JRResourcesUtil.java

/**
 * Attempts to find a resource using a class loader.
 * <p/>/*from  w  w  w  .j  a  v  a2  s  . c  o  m*/
 * The following sources are tried:
 * <ul>
 *    <li>the class loader returned by {@link #getClassLoader(ClassLoader) getClassLoader(ClassLoader)}</li>
 *    <li>the context class loader</li>
 *    <li><code>clazz.getClassLoader()</code></li>
 *    <li><code>clazz.getResource()</code></li>
 * </ul>
 * 
 * @param location the resource name
 * @param clsLoader a class loader
 * @param clazz a class
 * @return the resource URL if found
 * @deprecated Replaced by {@link #findClassLoaderResource(String, ClassLoader)}.
 */
public static URL findClassLoaderResource(String location, ClassLoader clsLoader, Class<?> clazz) {
    ClassLoader classLoader = getClassLoader(clsLoader);

    URL url = null;

    if (classLoader != null) {
        url = classLoader.getResource(location);
    }

    if (url == null) {
        classLoader = Thread.currentThread().getContextClassLoader();

        if (classLoader != null) {
            url = classLoader.getResource(location);
        }

        if (url == null) {
            classLoader = clazz.getClassLoader();
            if (classLoader == null) {
                url = clazz.getResource("/" + location);
            } else {
                url = classLoader.getResource(location);
            }
        }
    }

    return url;
}

From source file:net.sf.jasperreports.engine.util.JRResourcesUtil.java

/**
 * Attempts to find a resource using a class loader.
 * <p/>/*  ww w . j  a va2s.  c o  m*/
 * The following sources are tried:
 * <ul>
 *    <li>the class loader returned by {@link #getClassLoader(ClassLoader) getClassLoader(ClassLoader)}</li>
 *    <li>the context class loader</li>
 *    <li><code>JRLoader.class.getClassLoader()</code></li>
 *    <li><code>JRLoader.class.getResource()</code></li>
 * </ul>
 * 
 * @param location the resource name
 * @param clsLoader a class loader
 * @return the resource URL if found
 */
public static URL findClassLoaderResource(String location, ClassLoader clsLoader) {
    ClassLoader classLoader = getClassLoader(clsLoader);

    URL url = null;

    if (classLoader != null) {
        url = classLoader.getResource(location);
    }

    if (url == null) {
        classLoader = Thread.currentThread().getContextClassLoader();

        if (classLoader != null) {
            url = classLoader.getResource(location);
        }

        if (url == null) {
            classLoader = JRLoader.class.getClassLoader();
            if (classLoader == null) {
                url = JRLoader.class.getResource("/" + location);
            } else {
                url = classLoader.getResource(location);
            }
        }
    }

    return url;
}

From source file:de.alpharogroup.resourcebundle.properties.PropertiesExtensions.java

/**
 * Resolves all the available languages for the given resource bundle name in the given bundle
 * package./*from w  w w  .j a  va  2 s .  c o m*/
 * Note the default resource bundle is excluded.
 *
 * @param bundlepackage
 *            The package that contains the properties files.
 * @param bundlename
 *            The name of the resource bundle.
 * @return a Set of String objects with the available languages excluding the default.
 */
public static Set<String> resolveAvailableLanguages(final String bundlepackage, final String bundlename) {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final File root = new File(loader.getResource(bundlepackage.replace('.', '/')).getFile());
    final File[] files = root.listFiles(new PropertiesResourceBundleFilenameFilter(bundlename));

    final Set<String> languages = new TreeSet<>();
    for (final File file : files) {
        final String language = file.getName().replaceAll("^" + bundlename + "(_)?|\\.properties$", "");
        if ((language != null) && !language.isEmpty()) {
            languages.add(language);
        }
    }
    return languages;
}

From source file:org.apache.hadoop.streaming.StreamUtil.java

/** @return a jar file path or a base directory or null if not found.
 *///from   w w  w  . ja va  2  s .  co  m
public static String findInClasspath(String className, ClassLoader loader) {

    String relPath = className;
    relPath = relPath.replace('.', '/');
    relPath += ".class";
    java.net.URL classUrl = loader.getResource(relPath);

    String codePath;
    if (classUrl != null) {
        boolean inJar = classUrl.getProtocol().equals("jar");
        codePath = classUrl.toString();
        if (codePath.startsWith("jar:")) {
            codePath = codePath.substring("jar:".length());
        }
        if (codePath.startsWith("file:")) { // can have both
            codePath = codePath.substring("file:".length());
        }
        if (inJar) {
            // A jar spec: remove class suffix in /path/my.jar!/package/Class
            int bang = codePath.lastIndexOf('!');
            codePath = codePath.substring(0, bang);
        } else {
            // A class spec: remove the /my/package/Class.class portion
            int pos = codePath.lastIndexOf(relPath);
            if (pos == -1) {
                throw new IllegalArgumentException(
                        "invalid codePath: className=" + className + " codePath=" + codePath);
            }
            codePath = codePath.substring(0, pos);
        }
    } else {
        codePath = null;
    }
    return codePath;
}

From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java

/**
 * Gets all the available Locales to the given resource bundle name in the given bundle package.
 * For now it is only for properties files.
 * /*from  w  ww .  jav  a2 s .  c  om*/
 * @param bundlepackage
 *            The package that contains the properties files.
 * @param bundlename
 *            The name of the resource bundle.
 * @return a Map of File objects with the corresponding Locales to it.
 */
public static Map<File, Locale> getLocales(String bundlepackage, final String bundlename) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    File root = new File(loader.getResource(bundlepackage.replace('.', '/')).getFile());
    File[] files = root.listFiles(new ResourceBundleFilenameFilter(bundlename));
    Map<File, Locale> locales = new HashMap<>();
    for (File file : files) {
        Locale current = getLocale(file);
        locales.put(file, current);
    }
    return locales;
}

From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java

/**
 * Gets all the available languages to the given resource bundle name in the given bundle
 * package.//from w  w w.  ja  v a 2  s.com
 * 
 * @param bundlepackage
 *            The package that contains the properties files.
 * @param bundlename
 *            The name of the resource bundle.
 * @return a Set of String objects with the available languages.
 */

public static Set<String> getAvailableLanguages(String bundlepackage, final String bundlename) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    File root = new File(loader.getResource(bundlepackage.replace('.', '/')).getFile());
    File[] files = root.listFiles(new ResourceBundleFilenameFilter(bundlename));

    Set<String> languages = new TreeSet<>();
    for (File file : files) {
        languages.add(file.getName().replaceAll("^" + bundlename + "(_)?|\\.properties$", ""));
    }
    return languages;
}

From source file:de.ingrid.portal.interfaces.impl.WMSInterfaceImpl.java

public static String getResourceAsStream(String resource) throws Exception {
    String stripped = resource.startsWith("/") ? resource.substring(1) : resource;

    String stream = null;//from   ww  w.  ja v a2  s. com
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        stream = classLoader.getResource(stripped).toString();
    }
    if (stream == null) {
        Environment.class.getResourceAsStream(resource);
    }
    if (stream == null) {
        stream = Environment.class.getClassLoader().getResource(stripped).toString();
    }
    if (stream == null) {
        throw new Exception(resource + " not found");
    }
    return stream;
}

From source file:de.alpharogroup.lang.PropertiesUtils.java

/**
 * Gets all the available Locales to the given resource bundle name in the given bundle package.
 * For now it is only for properties files.
 * /*from w w w.  ja  v  a 2 s . co  m*/
 * @param bundlepackage
 *            The package that contains the properties files.
 * @param bundlename
 *            The name of the resource bundle.
 * @return a Map of File objects with the corresponding Locales to it.
 */
public static Map<File, Locale> getLocales(final String bundlepackage, final String bundlename) {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final File root = new File(loader.getResource(bundlepackage.replace('.', '/')).getFile());
    final File[] files = root.listFiles(new ResourceBundleFilenameFilter(bundlename));
    final Map<File, Locale> locales = new HashMap<>();
    for (final File file : files) {
        final Locale current = getLocale(file);
        locales.put(file, current);
    }
    return locales;
}

From source file:de.alpharogroup.lang.PropertiesUtils.java

/**
 * Gets all the available languages to the given resource bundle name in the given bundle
 * package.// w  ww.  j  a  v a 2  s  .co m
 * 
 * @param bundlepackage
 *            The package that contains the properties files.
 * @param bundlename
 *            The name of the resource bundle.
 * @return a Set of String objects with the available languages.
 */

public static Set<String> getAvailableLanguages(final String bundlepackage, final String bundlename) {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final File root = new File(loader.getResource(bundlepackage.replace('.', '/')).getFile());
    final File[] files = root.listFiles(new ResourceBundleFilenameFilter(bundlename));

    final Set<String> languages = new TreeSet<>();
    for (final File file : files) {
        languages.add(file.getName().replaceAll("^" + bundlename + "(_)?|\\.properties$", ""));
    }
    return languages;
}