refactor module loader part, add empty project morny-core
Some checks failed
Tests / check-build (push) Has been cancelled
Tests / check-unit-tests (push) Has been cancelled
Tests / check-assembly-run (push) Has been cancelled

This commit is contained in:
A.C.Sukazyo Eyre 2024-12-10 15:47:32 +08:00
parent 7c1549e35f
commit 9a8f3f8076
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
9 changed files with 109 additions and 41 deletions

View File

@ -64,8 +64,20 @@ lazy val morny_system_lib = (project in file (MornyProject.morny_system_lib.id))
) )
lazy val morny_core = (project in file(MornyProject.morny_core.id))
.dependsOn(morny_system_lib)
.settings(
name := MornyProject.morny_core.name,
moduleName := MornyProject.morny_core.id,
libraryDependencies ++= MornyProject.morny_core.dependencies,
)
lazy val morny_coeur = (project in file(MornyProject.morny_coeur.id)) lazy val morny_coeur = (project in file(MornyProject.morny_coeur.id))
.enablePlugins(BuildInfoPlugin) .enablePlugins(BuildInfoPlugin)
.dependsOn(morny_core)
.dependsOn(morny_system_lib) .dependsOn(morny_system_lib)
.settings( .settings(

View File

@ -16,7 +16,6 @@ import cc.sukazyo.cono.morny.util.schedule.Scheduler
import cc.sukazyo.cono.morny.util.time.WatchDog import cc.sukazyo.cono.morny.util.time.WatchDog
import cc.sukazyo.cono.morny.util.UseString.MString import cc.sukazyo.cono.morny.util.UseString.MString
import cc.sukazyo.cono.morny.util.UseThrowable.toLogString import cc.sukazyo.cono.morny.util.UseThrowable.toLogString
import cc.sukazyo.cono.morny.util.dataview.Table
import com.pengrad.telegrambot.TelegramBot import com.pengrad.telegrambot.TelegramBot
import com.pengrad.telegrambot.request.GetMe import com.pengrad.telegrambot.request.GetMe

View File

@ -2,8 +2,10 @@ package cc.sukazyo.cono.morny.core
import cc.sukazyo.cono.morny.core.Log.logger import cc.sukazyo.cono.morny.core.Log.logger
import cc.sukazyo.cono.morny.core.MornyConfig.{CheckFailure, PROP_TOKEN_KEY} import cc.sukazyo.cono.morny.core.MornyConfig.{CheckFailure, PROP_TOKEN_KEY}
import cc.sukazyo.cono.morny.core.module.{ModuleHelper, ModuleLoader} import cc.sukazyo.cono.morny.core.module.ModuleHelper
import cc.sukazyo.cono.morny.core.module.ModuleLoader.MornyModuleInitializingException
import cc.sukazyo.cono.morny.util.CommonFormat import cc.sukazyo.cono.morny.util.CommonFormat
import cc.sukazyo.cono.morny.util.UseThrowable.toLogString
import java.time.ZoneOffset import java.time.ZoneOffset
import java.util.TimeZone import java.util.TimeZone
@ -170,7 +172,11 @@ object ServerMain {
s"""The Skip Login feature is not implemented yet! s"""The Skip Login feature is not implemented yet!
|""".stripMargin |""".stripMargin
val loadedModules = ModuleLoader.loadCoreModules() val loadedModules = ModuleHelper.loadCoeurModules(
(e: MornyModuleInitializingException) => {
logger.error(e.toLogString)
}
)
if (mode_echoVersion) { if (mode_echoVersion) {

View File

@ -1,6 +1,8 @@
package cc.sukazyo.cono.morny.core.module package cc.sukazyo.cono.morny.core.module
import cc.sukazyo.cono.morny.core.MornyModule import cc.sukazyo.cono.morny.core.{MornyCoeur, MornyModule}
import cc.sukazyo.cono.morny.core.module.ModuleLoader.MornyModuleInitializingException
import cc.sukazyo.cono.morny.core.module.ModulesJarLoader.loadFromJar
import cc.sukazyo.cono.morny.util.dataview.Table import cc.sukazyo.cono.morny.util.dataview.Table
object ModuleHelper { object ModuleHelper {
@ -12,4 +14,8 @@ object ModuleHelper {
) )
} }
def loadCoeurModules (onLoadingErrors: MornyModuleInitializingException =>Any): List[MornyModule] = {
loadFromJar(classOf[MornyCoeur], onLoadingErrors)
}
} }

View File

@ -1,51 +1,69 @@
package cc.sukazyo.cono.morny.core.module package cc.sukazyo.cono.morny.core.module
import cc.sukazyo.cono.morny.core.{MornyCoeur, MornyModule} import cc.sukazyo.cono.morny.core.MornyModule
import cc.sukazyo.cono.morny.core.Log.logger
import cc.sukazyo.cono.morny.util.UseThrowable.toLogString
import java.nio.charset.StandardCharsets
import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer
object ModuleLoader { object ModuleLoader {
def loadCoreModules (): List[MornyModule] = { class MornyModuleInitializingException (val className: String, message: String) extends Exception (
loadFromJar(classOf[MornyCoeur]) s"Failed to initialize module $className : $message"
} )
def loadFromJar (packageClazz: Class[?]): List[MornyModule] = { class NotMornyModuleException (className: String) extends MornyModuleInitializingException (
val list = ListBuffer[MornyModule]() className,
s"Class is not a MornyModule, due to it does not implements the cc.sukazyo.cono.morny.core.MornyModule trait."
)
val moduleListFile = packageClazz.getResourceAsStream("/morny-modules.list") class MornyModuleNotFoundException (className: String) extends MornyModuleInitializingException (
.readAllBytes() className,
val modules = String(moduleListFile, StandardCharsets.UTF_8) s"Cannot found class in this name."
.split("\n") )
.map(_.strip)
.filter(_.nonEmpty)
modules.foreach { (clazzName: String) => @throws[MornyModuleInitializingException]
def loadModuleByClass (clazz: Class[?]): MornyModule = {
try { try {
val clazz = Class.forName(clazzName)
val instance = clazz.getConstructor().newInstance() val instance = clazz.getConstructor().newInstance()
instance match instance match
case module: MornyModule => case module: MornyModule =>
list += module module
case _ => case _ =>
logger `error` throw NotMornyModuleException(clazz.getName)
s"""Module is not a Morny Module : } catch {
| - in package : ${packageClazz.getName} case e_module: MornyModuleInitializingException =>
| - declared class name : $clazzName throw e_module
|You need to implement a MornyModule trait to make it a REAL morny module!""".stripMargin case e_any: Exception =>
} catch case e: Exception => throw MornyModuleInitializingException(clazz.getName, e_any.getMessage).initCause(e_any)
logger `error` }
s"""Failed to create a module instance : }
| - in package : ${packageClazz.getName}
| - declared class name : $clazzName @throws[MornyModuleInitializingException]
|${e.toLogString} def loadModuleByClassName (className: String): MornyModule = {
|Is this a typo or packaging error? You need to add morny-modules.list and your code to the same jar.""".stripMargin try {
val clazz = Class.forName(className)
loadModuleByClass(clazz)
} catch {
case e_module: MornyModuleInitializingException =>
throw e_module
case e_notFound: ClassNotFoundException =>
throw MornyModuleNotFoundException(className).initCause(e_notFound)
}
}
def loadModuleByNameList (moduleClassNames: List[String], onLoadingErrors: MornyModuleInitializingException =>Any): List[MornyModule] = {
val list = ListBuffer[MornyModule]()
moduleClassNames.foreach { (clazzName: String) =>
try {
val module = loadModuleByClassName(clazzName)
list += module
} catch case e: MornyModuleInitializingException =>
onLoadingErrors(e)
} }
list.toList list.toList
} }
} }

View File

@ -1,9 +1,20 @@
package cc.sukazyo.cono.morny.core.module package cc.sukazyo.cono.morny.core.module
import cc.sukazyo.cono.morny.core.MornyModule
import cc.sukazyo.cono.morny.core.module.ModuleLoader.{loadModuleByNameList, MornyModuleInitializingException}
import java.nio.charset.StandardCharsets
object ModulesJarLoader { object ModulesJarLoader {
def load (): Unit = { def loadFromJar (packageClazz: Class[?], onLoadingErrors: MornyModuleInitializingException=>Any): List[MornyModule] = {
// TODO val moduleListFile = packageClazz.getResourceAsStream("/morny-modules.list")
.readAllBytes()
val modules = String(moduleListFile, StandardCharsets.UTF_8)
.split("\n")
.map(_.strip)
.filter(_.nonEmpty)
loadModuleByNameList(modules.toList, onLoadingErrors)
} }
} }

3
morny-core/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# builds
/target

View File

@ -65,6 +65,18 @@ object MornyConfiguration {
} }
object Morny_Core extends ProjectMetadata {
override val name = "Morny Core"
override val id = "morny-core"
override val group = GROUP
override val root_package = s"$GROUP.cono.morny.core"
override val dependencies = Seq()
}
object Morny_Coeur extends ProjectMetadata with Runnable { object Morny_Coeur extends ProjectMetadata with Runnable {
override val name = "Morny Coeur" override val name = "Morny Coeur"

View File

@ -54,6 +54,7 @@ object MornyProject {
val source_encoding = "utf-8" val source_encoding = "utf-8"
val morny_system_lib = MornyConfiguration.Morny_System_Library val morny_system_lib = MornyConfiguration.Morny_System_Library
val morny_core = MornyConfiguration.Morny_Core
val morny_coeur = MornyConfiguration.Morny_Coeur val morny_coeur = MornyConfiguration.Morny_Coeur
} }