/
PluginDef.scala
79 lines (63 loc) · 2.34 KB
/
PluginDef.scala
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
72
73
74
75
76
77
78
79
package ch.epfl.bluebrain.nexus.delta.sdk.plugin
import cats.effect.IO
import ch.epfl.bluebrain.nexus.delta.sdk.model.ComponentDescription.PluginDescription
import ch.epfl.bluebrain.nexus.delta.sdk.model.Name
import com.typesafe.config.{Config, ConfigFactory, ConfigParseOptions, ConfigResolveOptions}
import izumi.distage.model.Locator
import izumi.distage.model.definition.ModuleDef
import java.io.File
trait PluginDef {
/**
* Distage module definition for this plugin.
*/
def module: ModuleDef
/**
* Plugin description
*/
def info: PluginDescription
/**
* @return
* the plugin configuration filename
*/
def configFileName: String = s"${info.name}.conf"
/**
* @return
* true if the plugin is enabled, false otherwise
*/
def enabled: Boolean = pluginConfigObject.getBoolean("enabled")
/**
* The priority of this plugin. This value will decide the order in which this plugin is executed compared to the
* rest of the plugins. It affects Routes ordering and classpath ordering.
*
* The value is retrieved from the plugin configuration inside the priority field
*/
def priority: Int = pluginConfigObject.getInt("priority")
/**
* Initialize the plugin.
*
* @param locator
* distage dependencies [[Locator]]
*
* @return
* [[Plugin]] instance.
*/
def initialize(locator: Locator): IO[Plugin]
protected lazy val pluginConfigObject: Config = PluginDef.load(getClass.getClassLoader, info.name, configFileName)
}
object PluginDef {
private val parseOptions = ConfigParseOptions.defaults().setAllowMissing(false)
private val resolveOptions = ConfigResolveOptions.defaults().setAllowUnresolved(true)
private val externalConfigEnvVariable = "DELTA_EXTERNAL_CONF"
private val externalConfig = sys.env.get(externalConfigEnvVariable).fold(ConfigFactory.empty()) { p =>
ConfigFactory.parseFile(new File(p), parseOptions)
}
private[plugin] def load(classLoader: ClassLoader, name: Name, configFileName: String) = ConfigFactory
.defaultOverrides()
.withFallback(externalConfig)
.withFallback(
ConfigFactory.parseResources(classLoader, configFileName, parseOptions)
)
.resolve(resolveOptions)
.getConfig(s"plugins.${name.value}")
implicit val pluginDefOrdering: Ordering[PluginDef] = Ordering.by(_.priority)
}