001package org.andromda.core.common;
002
003import java.net.URL;
004import java.util.ArrayList;
005import java.util.Collection;
006import java.util.Enumeration;
007
008/**
009 * Finds and loads file resources from the current classpath.
010 *
011 * @author Chad Brandon
012 */
013public class ResourceFinder
014{
015    /**
016     * Returns a URL[] containing the URL of each resource and the File which represents the library the resource was
017     * found in.
018     *
019     * @param resource the resource to find
020     * @return a <code>array of resource URLs</code>
021     */
022    public static URL[] findResources(final String resource)
023    {
024        ExceptionUtils.checkEmpty("resource", resource);
025        try
026        {
027            final Collection<URL> resources = new ArrayList<URL>();
028            for (final Enumeration<URL> enumeration = ClassUtils.getClassLoader().getResources(resource);
029                 enumeration.hasMoreElements();)
030            {
031                resources.add(enumeration.nextElement());
032            }
033
034            return resources.toArray(new URL[resources.size()]);
035        }
036        catch (final Exception exception)
037        {
038            throw new ResourceFinderException(exception);
039        }
040    }
041}