All Downloads are FREE. Search and download functionalities are using the official Maven repository.

skinny.ClassPathResourceLoader.scala Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package skinny

/**
 * Class path resource loader.
 */
object ClassPathResourceLoader {

  /**
   * Returns a resource as an InputStream if exists.
   *
   * @param path path
   * @return resource if exists
   */
  def getClassPathResource(path: String): Option[ClassPathResource] = {
    val relativePath = path.stripPrefix("/")
    getResourceFromClassLoader(Thread.currentThread.getContextClassLoader, relativePath)
      .orElse(getResourceFromClassLoader(getClass.getClassLoader, relativePath))
  }

  private[this] def getResourceFromClassLoader(classLoader: ClassLoader, path: String): Option[ClassPathResource] = {
    try {
      val resource = classLoader.getResource(path)
      val conn = resource.openConnection
      val lastModified = conn.getLastModified
      Some(ClassPathResource(conn.getInputStream, lastModified))
    } catch {
      case scala.util.control.NonFatal(_) => None
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy