aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon/resources/ResourcesStorage.scala
blob: 2be7ed7568a1eef3b2c983a5a248156f6519ea0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package xyz.driver.pdsuicommon.resources

import scala.io.{Codec, Source}

trait ResourcesStorage {

  /**
    * @param resourcePath Don't forget / at start
    */
  def getFirstLine(resourcePath: String): String
}

object RealResourcesStorage extends ResourcesStorage {

  def getFirstLine(resourcePath: String): String = {
    val resourceUrl = getClass.getResource(resourcePath)
    Option(resourceUrl) match {
      case Some(url) =>
        val source = Source.fromURL(resourceUrl)(Codec.UTF8)
        try {
          val lines = source.getLines()
          if (lines.isEmpty) throw new RuntimeException(s"'$resourcePath' is empty")
          else lines.next()
        } finally {
          source.close()
        }
      case None =>
        throw new RuntimeException(s"Can not find the '$resourcePath'!")
    }
  }
}

object FakeResourcesStorage extends ResourcesStorage {

  def getFirstLine(resourcePath: String): String = ""
}