2021-05-08 18:49:01 +08:00
|
|
|
package io.neoterm.component
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2017-12-26 00:54:25 +08:00
|
|
|
import io.neolang.visitor.ConfigVisitor
|
2017-12-26 00:21:57 +08:00
|
|
|
import io.neoterm.component.config.ConfigureComponent
|
2021-05-08 18:49:01 +08:00
|
|
|
import io.neoterm.utils.NLog
|
2017-12-26 00:21:57 +08:00
|
|
|
import java.io.File
|
2017-12-26 23:57:20 +08:00
|
|
|
import java.io.FileFilter
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2021-05-08 18:49:01 +08:00
|
|
|
interface ConfigFileBasedObject {
|
|
|
|
@Throws(RuntimeException::class)
|
|
|
|
fun onConfigLoaded(configVisitor: ConfigVisitor)
|
|
|
|
}
|
|
|
|
|
2017-12-26 23:51:00 +08:00
|
|
|
abstract class ConfigFileBasedComponent<out T : ConfigFileBasedObject>(protected val baseDir: String) : NeoComponent {
|
2021-05-08 00:11:00 +08:00
|
|
|
companion object {
|
|
|
|
private val TAG = ConfigFileBasedComponent::class.java.simpleName
|
2017-12-26 23:57:20 +08:00
|
|
|
|
2021-05-08 00:11:00 +08:00
|
|
|
val NEOLANG_FILTER = FileFilter {
|
|
|
|
it.extension == "nl"
|
2017-12-26 00:21:57 +08:00
|
|
|
}
|
2021-05-08 00:11:00 +08:00
|
|
|
}
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2021-05-08 00:11:00 +08:00
|
|
|
open val checkComponentFileWhenObtained = false
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2021-05-08 00:11:00 +08:00
|
|
|
override fun onServiceInit() {
|
|
|
|
val baseDirFile = File(this.baseDir)
|
|
|
|
if (!baseDirFile.exists()) {
|
|
|
|
if (!baseDirFile.mkdirs()) {
|
|
|
|
throw RuntimeException("Cannot create component config directory: ${baseDirFile.absolutePath}")
|
|
|
|
}
|
2017-12-26 00:21:57 +08:00
|
|
|
}
|
2021-05-08 00:11:00 +08:00
|
|
|
onCheckComponentFiles()
|
|
|
|
}
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2021-05-08 00:11:00 +08:00
|
|
|
override fun onServiceDestroy() {
|
|
|
|
}
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2021-05-08 00:11:00 +08:00
|
|
|
override fun onServiceObtained() {
|
|
|
|
if (checkComponentFileWhenObtained) {
|
|
|
|
onCheckComponentFiles()
|
2017-12-26 00:21:57 +08:00
|
|
|
}
|
2021-05-08 00:11:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fun loadConfigure(file: File): T? {
|
|
|
|
return try {
|
|
|
|
val loaderService = ComponentManager.getComponent<ConfigureComponent>()
|
|
|
|
val configure = loaderService.newLoader(file).loadConfigure()
|
|
|
|
?: throw RuntimeException("Parse configuration failed.")
|
|
|
|
|
|
|
|
val configVisitor = configure.getVisitor()
|
|
|
|
val componentObject = onCreateComponentObject(configVisitor)
|
|
|
|
componentObject.onConfigLoaded(configVisitor)
|
|
|
|
componentObject
|
|
|
|
} catch (e: RuntimeException) {
|
|
|
|
NLog.e(TAG, "Failed to load config: ${file.absolutePath}: ${e.localizedMessage}")
|
|
|
|
null
|
2017-12-26 00:21:57 +08:00
|
|
|
}
|
2021-05-08 00:11:00 +08:00
|
|
|
}
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2021-05-08 00:11:00 +08:00
|
|
|
abstract fun onCheckComponentFiles()
|
2017-12-26 00:21:57 +08:00
|
|
|
|
2021-05-08 00:11:00 +08:00
|
|
|
abstract fun onCreateComponentObject(configVisitor: ConfigVisitor): T
|
2021-05-08 18:49:01 +08:00
|
|
|
}
|
|
|
|
|