public static URL getResourceURL ( String resourcePath , Object source ) throws ResourceNotFoundException { // Try the current classloader URL url = source . getClass ( ) . getResource ( resourcePath ) ; // Weblogic 10 likes this one better.. if ( null == url ) { ClassLoader cl = source . getClass ( ) . getClassLoader ( ) ; if ( null != cl ) url = cl . getResource ( 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 == url ) { url = Thread . currentThread ( ) . getContextClassLoader ( ) . getResource ( resourcePath ) ; // Last chance, hack in the classloader if ( null == url ) { ClassLoader threadClassLoader = Thread . currentThread ( ) . getContextClassLoader ( ) ; try { Thread . currentThread ( ) . setContextClassLoader ( source . getClass ( ) . getClassLoader ( ) ) ; if ( Thread . currentThread ( ) . getContextClassLoader ( ) != null ) { url = Thread . currentThread ( ) . getContextClassLoader ( ) . getResource ( resourcePath ) ; } } finally { Thread . currentThread ( ) . setContextClassLoader ( threadClassLoader ) ; } } if ( null == url ) { throw new ResourceNotFoundException ( resourcePath + " could not be found. " ) ; } } return url ; }
Attempts to find the URL of a resource from the classpath either usinf the caller s class loader or the current thread s context classloader .