public static InputStream getResourceAsStream ( String resourcePath , Object source ) throws FileNotFoundException { // Try the current classloader InputStream is = source . getClass ( ) . getResourceAsStream ( resourcePath ) ; // Weblogic 10 likes this one better.. if ( null == is ) { ClassLoader cl = source . getClass ( ) . getClassLoader ( ) ; if ( null != cl ) { is = cl . getResourceAsStream ( 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 == is ) { is = Thread . currentThread ( ) . getContextClassLoader ( ) . getResourceAsStream ( resourcePath ) ; } if ( null == is ) { // Try to use the classloader of the current Jawr Config Manager // MBean // This will be used when a refresh is done in the configuration // using the JMX MBean MBeanServer mbs = JmxUtils . getMBeanServer ( ) ; if ( mbs != null ) { ObjectName name = ThreadLocalJawrContext . getJawrConfigMgrObjectName ( ) ; if ( name != null ) { try { ClassLoader cl = mbs . getClassLoaderFor ( name ) ; is = cl . getResourceAsStream ( resourcePath ) ; } catch ( Exception e ) { LOGGER . error ( "Unable to instanciate the Jawr MBean '" + name . getCanonicalName ( ) + "'" , e ) ; } } } } // Try to retrieve by URL if ( null == is ) { try { URL url = getResourceURL ( resourcePath , source ) ; is = new FileInputStream ( new File ( url . getFile ( ) ) ) ; } catch ( ResourceNotFoundException | IOException e ) { throw new FileNotFoundException ( resourcePath + " could not be found. " ) ; } } return is ; }
Attempots to load a resource from the classpath either usinf the caller s class loader or the current thread s context classloader .