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.apache.giraph.graph.GraphTaskManager.java

/**
 * Copied from JobConf to get the location of this jar.  Workaround for
 * things like Oozie map-reduce jobs. NOTE: Pure YARN profile cannot
 * make use of this, as the jars are unpacked at each container site.
 *
 * @param myClass Class to search the class loader path for to locate
 *        the relevant jar file/*ww  w . ja  v a  2s  . c  om*/
 * @return Location of the jar file containing myClass
 */
private static String findContainingJar(Class<?> myClass) {
    ClassLoader loader = myClass.getClassLoader();
    String classFile = myClass.getName().replaceAll("\\.", "/") + ".class";
    try {
        for (Enumeration<?> itr = loader.getResources(classFile); itr.hasMoreElements();) {
            URL url = (URL) itr.nextElement();
            if ("jar".equals(url.getProtocol())) {
                String toReturn = url.getPath();
                if (toReturn.startsWith("file:")) {
                    toReturn = toReturn.substring("file:".length());
                }
                toReturn = URLDecoder.decode(toReturn, "UTF-8");
                return toReturn.replaceAll("!.*$", "");
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return null;
}

From source file:com.liferay.portal.spring.hibernate.PortalExtletHibernateConfiguration.java

@Override
protected Configuration newConfiguration() {
    Configuration configuration = super.newConfiguration();

    ClassLoader classLoader = getConfigurationClassLoader();

    String resourceName = EXTLET_HIBERNATE_RESOURCE_NAME;

    try {/*  ww w  .  ja v a2 s .  c o m*/
        Enumeration<URL> resources = classLoader.getResources(resourceName);
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            try {
                if (_log.isDebugEnabled()) {
                    _log.debug("Loading extlet-hbm.xml from: " + resource);
                }
                InputStream is = new UrlResource(resource).getInputStream();

                if (is != null) {
                    try {
                        configuration.addInputStream(is);
                    } finally {
                        is.close();
                    }
                }
                if (_log.isDebugEnabled()) {
                    _log.debug("Loading OK: " + resource);
                }
            } catch (Exception e2) {
                if (_log.isWarnEnabled()) {
                    _log.warn("Problem while loading " + resource, e2);
                }
            }
        }
    } catch (Exception e2) {
        if (_log.isWarnEnabled()) {
            _log.warn("Problem while loading classLoader resources: " + resourceName, e2);
        }
    }

    return configuration;
}

From source file:org.pentaho.platform.pdi.AgileBiPluginResourceLoader.java

@Override
public List<URL> findResources(ClassLoader classLoader, String s) {
    String symbolicName = getSymbolicName(classLoader);
    try {/*from w  w w. j a v a  2  s. c  o m*/
        Enumeration<URL> enumeration = classLoader.getResources(symbolicName + "/" + s);
        List<URL> urls = new ArrayList<URL>();
        while (enumeration.hasMoreElements()) {
            URL nextElement = enumeration.nextElement();
            urls.add(nextElement);
        }
        return urls;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Collections.emptyList();
}

From source file:be.fedict.eid.idp.model.bean.AttributeServiceManagerBean.java

@SuppressWarnings("unchecked")
public List<IdentityProviderAttributeType> getAttributeServiceTypes() {

    List<IdentityProviderAttributeType> attributeServices = new LinkedList<IdentityProviderAttributeType>();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> resources;
    try {//from   ww  w. j a v  a 2  s .  co m
        resources = classLoader.getResources("META-INF/eid-idp-attribute.xml");
    } catch (IOException e) {
        LOG.error("I/O error: " + e.getMessage(), e);
        return attributeServices;
    }
    Unmarshaller unmarshaller;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        unmarshaller = jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        LOG.error("JAXB error: " + e.getMessage(), e);
        return attributeServices;
    }
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        LOG.debug("resource URL: " + resource.toString());
        JAXBElement<IdentityProviderAttributesType> jaxbElement;
        try {
            jaxbElement = (JAXBElement<IdentityProviderAttributesType>) unmarshaller.unmarshal(resource);
        } catch (JAXBException e) {
            LOG.error("JAXB error: " + e.getMessage(), e);
            continue;
        }
        IdentityProviderAttributesType identityProviderAttributes = jaxbElement.getValue();
        for (IdentityProviderAttributeType identityProviderAttribute : identityProviderAttributes
                .getIdentityProviderAttribute()) {
            attributeServices.add(identityProviderAttribute);
        }
    }
    return attributeServices;
}

From source file:com.openteach.diamond.container.DefaultClassLoaderDelegateHook.java

public Enumeration preFindResources(String name, BundleClassLoader classloader, BundleData data)
        throws FileNotFoundException {
    for (ClassLoader thirdClassLoader : thirdClassLoaders) {
        if (thirdClassLoader != null)
            try {
                return thirdClassLoader.getResources(name);
            } catch (IOException e) {
                // IGNORE
            }// ww  w . j a  v  a 2s . c  o m
    }
    return null;
}

From source file:be.fedict.eid.idp.model.bean.ProtocolServiceManagerBean.java

@SuppressWarnings("unchecked")
public List<IdentityProviderProtocolType> getProtocolServices() {
    List<IdentityProviderProtocolType> protocolServices = new LinkedList<IdentityProviderProtocolType>();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> resources;
    try {//  w  w w.  j ava  2  s . c o  m
        resources = classLoader.getResources("META-INF/eid-idp-protocol.xml");
    } catch (IOException e) {
        LOG.error("I/O error: " + e.getMessage(), e);
        return protocolServices;
    }
    Unmarshaller unmarshaller;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        unmarshaller = jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        LOG.error("JAXB error: " + e.getMessage(), e);
        return protocolServices;
    }
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        LOG.debug("resource URL: " + resource.toString());
        JAXBElement<IdentityProviderProtocolsType> jaxbElement;
        try {
            jaxbElement = (JAXBElement<IdentityProviderProtocolsType>) unmarshaller.unmarshal(resource);
        } catch (JAXBException e) {
            LOG.error("JAXB error: " + e.getMessage(), e);
            continue;
        }
        IdentityProviderProtocolsType identityProviderProtocols = jaxbElement.getValue();
        for (IdentityProviderProtocolType identityProviderProtocol : identityProviderProtocols
                .getIdentityProviderProtocol()) {
            protocolServices.add(identityProviderProtocol);
        }
    }
    return protocolServices;
}

From source file:com.sworddance.util.CUtilities.java

/**
 * @param searchRoot//w  w  w  .  j  a  v a  2 s.  co m
 * @param fileName
 * @param optional
 * @param searchPaths
 * @return a de-duped Enumeration<URL> never returns null.
 */
private static Collection<URL> getResources(Object searchRoot, String fileName, boolean optional,
        List<String> searchPaths) {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader searchRootClassLoader = searchRoot != null ? searchRoot.getClass().getClassLoader() : null;
    HashMap<String, URL> results = new HashMap<String, URL>();
    for (String searchPath : searchPaths) {
        Enumeration<URL> resource = null;
        if (searchRootClassLoader != null) {
            try {
                resource = searchRootClassLoader.getResources(searchPath);
                for (URL url : NotNullIterator.<URL>newNotNullIterator(resource)) {
                    results.put(url.toString(), url);
                }
            } catch (IOException e) {
                // TODO what?
            }
        }
        if (contextClassLoader != null) {
            try {
                resource = contextClassLoader.getResources(searchPath);
                for (URL url : NotNullIterator.<URL>newNotNullIterator(resource)) {
                    results.put(url.toString(), url);
                }
            } catch (IOException e) {
                // TODO what?
            }
        }
        if (resource == null) {
            try {
                resource = ClassLoader.getSystemResources(searchPath);
                for (URL url : NotNullIterator.<URL>newNotNullIterator(resource)) {
                    results.put(url.toString(), url);
                }
            } catch (IOException e) {
                // TODO what?
            }
        }
    }
    if (isEmpty(results) && !optional) {
        if (fileName != null) {
            throw new ApplicationNullPointerException(fileName, " not found in ", join(searchPaths, ","),
                    " java.class.path=", System.getProperty("java.class.path"), " java.library.path=",
                    System.getProperty("java.library.path"), " searchRoot =", getClassSafely(searchRoot));
        } else {
            throw new ApplicationNullPointerException("No listed file found ", join(searchPaths, ","),
                    " java.class.path=", System.getProperty("java.class.path"), " java.library.path=",
                    System.getProperty("java.library.path"), " searchRoot =", getClassSafely(searchRoot));
        }
    } else {
        return results.values();
    }
}

From source file:org.apache.tiles.context.enhanced.EnhancedTilesApplicationContext.java

/**
 * Searches for resources in the classpath, given a path, using a class
 * loader./*ww  w.  ja v a  2 s . c om*/
 *
 * @param loader The class loader to use.
 * @param path The path to search into.
 * @return The set of found URLs.
 */
protected Set<URL> searchResources(ClassLoader loader, String path) {
    Set<URL> resources = new HashSet<URL>();
    try {
        Enumeration<URL> e = loader.getResources(path);
        while (e.hasMoreElements()) {
            resources.add(e.nextElement());
        }
    } catch (IOException e) {
        log.warn("Unable to retrieved resources from classloader: " + loader, e);
    }
    return resources;
}

From source file:be.fedict.eid.dss.model.bean.ServicesManagerSingletonBean.java

private Enumeration<URL> getResources(String resourceName) throws IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    return classLoader.getResources(resourceName);
}

From source file:ca.phon.query.script.QueryScript.java

/**
 * Setup library folders for 'require'//w  w  w  . j a  v  a2  s  . c o  m
 */
private void setupLibraryFolders() {
    final ClassLoader cl = PluginManager.getInstance();
    Enumeration<URL> libUrls;
    try {
        libUrls = cl.getResources("ca/phon/query/script/");
        while (libUrls.hasMoreElements()) {
            final URL url = libUrls.nextElement();
            try {
                final URI uri = url.toURI();
                super.addRequirePath(uri);
            } catch (URISyntaxException e) {
                LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
            }
        }
    } catch (IOException e1) {
        LOGGER.log(Level.SEVERE, e1.getLocalizedMessage(), e1);
    }

    super.addPackageImport("Packages.ca.phon.orthography");
    super.addPackageImport("Packages.ca.phon.ipa");
    super.addPackageImport("Packages.ca.phon.ipa.features");
    super.addPackageImport("Packages.ca.phon.phonex");
    super.addPackageImport("Packages.ca.phon.syllable");
    super.addPackageImport("Packages.ca.phon.util");
    super.addPackageImport("Packages.ca.phon.project");
    super.addPackageImport("Packages.ca.phon.session");

    super.addClassImport("Packages.org.apache.commons.lang3.StringUtils");
}