summaryrefslogtreecommitdiffstats
path: root/standalone-container/src/main/scala/com/yahoo/container/standalone/LocalFileDb.scala
blob: b1c7e9457ac93366633ab941e1cc61e28e61b909 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.standalone

import java.io.File
import java.lang.reflect.Constructor
import java.util
import java.util.concurrent.TimeUnit
import com.yahoo.config.FileReference
import com.yahoo.config.application.api.FileRegistry
import com.yahoo.config.application.api.FileRegistry.Entry
import com.yahoo.filedistribution.fileacquirer.FileAcquirer
import com.yahoo.net.HostName
import scala.collection.JavaConversions._


import LocalFileDb._
import scala.collection.mutable
import java.nio.file.Path


/**
 * FileAcquirer and FileRegistry working on a local directory.
 * @author tonytv
 */
class LocalFileDb(appPath: Path) extends FileAcquirer with FileRegistry {
  private val fileReferenceToFile = mutable.Map[FileReference, File]()

  /** *** FileAcquirer overrides *****/
  def waitFor(reference: FileReference, l: Long, timeUnit: TimeUnit): File = {
    synchronized {
      fileReferenceToFile.get(reference).getOrElse {
        throw new RuntimeException("Invalid file reference " + reference)
      }
    }
  }

  override def shutdown() {}

  /** *** FileRegistry overrides *****/
  def addFile(relativePath: String): FileReference = {
    val file = appPath.resolve(relativePath).toFile
    if (!file.exists) {
      throw new RuntimeException("The file does not exist: " + file.getPath)
    }

    val fileReference: FileReference = fileReferenceConstructor.newInstance("LocalFileDb:" + relativePath)
    fileReferenceToFile.put(fileReference, file)
    fileReference
  }

  def fileSourceHost: String =
    HostName.getHostName

  def allRelativePaths: java.util.Set[String] = {
    new java.util.HashSet(fileReferenceToFile.values.map(_.getPath))
  }

  override def export(): util.List[Entry] = {
    new java.util.ArrayList(fileReferenceToFile.keys.map{ (ref: FileReference) => new Entry(fileReferenceToFile.get(ref).get.getPath, ref)})
  }
}

object LocalFileDb {
  private def createFileReferenceConstructor: Constructor[FileReference] = {
    val method: Constructor[FileReference] = classOf[FileReference].getDeclaredConstructor(classOf[String])
    method.setAccessible(true)
    method
  }

  private val fileReferenceConstructor: Constructor[FileReference] = createFileReferenceConstructor
}