public static Enumeration < URL > getResources ( String resourcePath , Object source ) { // Try the current classloader Enumeration < URL > urls = null ; ClassLoader cl = source . getClass ( ) . getClassLoader ( ) ; try { if ( null != cl ) { urls = cl . getResources ( resourcePath ) ; } // If current classloader failed, try with the Threads context // classloader. If that fails ott, the resource is either not on the // classpath or inaccessible from the current context. if ( null == urls ) { urls = Thread . currentThread ( ) . getContextClassLoader ( ) . getResources ( resourcePath ) ; } // Last chance, hack in the classloader if ( null == urls ) { ClassLoader threadClassLoader = Thread . currentThread ( ) . getContextClassLoader ( ) ; try { Thread . currentThread ( ) . setContextClassLoader ( source . getClass ( ) . getClassLoader ( ) ) ; if ( Thread . currentThread ( ) . getContextClassLoader ( ) != null ) { urls = Thread . currentThread ( ) . getContextClassLoader ( ) . getResources ( resourcePath ) ; } } finally { Thread . currentThread ( ) . setContextClassLoader ( threadClassLoader ) ; } } } catch ( IOException e ) { LOGGER . warn ( "Unable to load " + resourcePath , e ) ; } return urls ; }
Attempts to find the URLs of a resource from the classpath either using the caller s class loader or the current thread s context classloader .