From 9a8f3f8076043841574706def7d559e40630ac61 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 10 Dec 2024 15:47:32 +0800 Subject: [PATCH] refactor module loader part, add empty project morny-core --- build.sbt | 12 +++ .../sukazyo/cono/morny/core/MornyCoeur.scala | 1 - .../sukazyo/cono/morny/core/ServerMain.scala | 10 ++- .../cono/morny/core/module/ModuleHelper.scala | 8 +- .../cono/morny/core/module/ModuleLoader.scala | 84 +++++++++++-------- .../morny/core/module/ModulesJarLoader.scala | 15 +++- morny-core/.gitignore | 3 + project/MornyConfiguration.scala | 16 +++- project/MornyProject.scala | 1 + 9 files changed, 109 insertions(+), 41 deletions(-) create mode 100644 morny-core/.gitignore diff --git a/build.sbt b/build.sbt index d1d35eb..e7c3f54 100644 --- a/build.sbt +++ b/build.sbt @@ -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)) .enablePlugins(BuildInfoPlugin) + .dependsOn(morny_core) .dependsOn(morny_system_lib) .settings( diff --git a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/MornyCoeur.scala b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/MornyCoeur.scala index a27316f..768bc83 100644 --- a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/MornyCoeur.scala +++ b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/MornyCoeur.scala @@ -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.UseString.MString 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.request.GetMe diff --git a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/ServerMain.scala b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/ServerMain.scala index ffd4ea8..5d3eb62 100644 --- a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/ServerMain.scala +++ b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/ServerMain.scala @@ -2,8 +2,10 @@ package cc.sukazyo.cono.morny.core 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.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.UseThrowable.toLogString import java.time.ZoneOffset import java.util.TimeZone @@ -170,7 +172,11 @@ object ServerMain { s"""The Skip Login feature is not implemented yet! |""".stripMargin - val loadedModules = ModuleLoader.loadCoreModules() + val loadedModules = ModuleHelper.loadCoeurModules( + (e: MornyModuleInitializingException) => { + logger.error(e.toLogString) + } + ) if (mode_echoVersion) { diff --git a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleHelper.scala b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleHelper.scala index 5569423..3872523 100644 --- a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleHelper.scala +++ b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleHelper.scala @@ -1,6 +1,8 @@ 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 object ModuleHelper { @@ -12,4 +14,8 @@ object ModuleHelper { ) } + def loadCoeurModules (onLoadingErrors: MornyModuleInitializingException =>Any): List[MornyModule] = { + loadFromJar(classOf[MornyCoeur], onLoadingErrors) + } + } diff --git a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleLoader.scala b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleLoader.scala index 1464f84..a97f6c3 100644 --- a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleLoader.scala +++ b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModuleLoader.scala @@ -1,51 +1,69 @@ package cc.sukazyo.cono.morny.core.module -import cc.sukazyo.cono.morny.core.{MornyCoeur, MornyModule} -import cc.sukazyo.cono.morny.core.Log.logger -import cc.sukazyo.cono.morny.util.UseThrowable.toLogString +import cc.sukazyo.cono.morny.core.MornyModule -import java.nio.charset.StandardCharsets import scala.collection.mutable.ListBuffer object ModuleLoader { - def loadCoreModules (): List[MornyModule] = { - loadFromJar(classOf[MornyCoeur]) + class MornyModuleInitializingException (val className: String, message: String) extends Exception ( + s"Failed to initialize module $className : $message" + ) + + class NotMornyModuleException (className: String) extends MornyModuleInitializingException ( + className, + s"Class is not a MornyModule, due to it does not implements the cc.sukazyo.cono.morny.core.MornyModule trait." + ) + + class MornyModuleNotFoundException (className: String) extends MornyModuleInitializingException ( + className, + s"Cannot found class in this name." + ) + + @throws[MornyModuleInitializingException] + def loadModuleByClass (clazz: Class[?]): MornyModule = { + try { + val instance = clazz.getConstructor().newInstance() + instance match + case module: MornyModule => + module + case _ => + throw NotMornyModuleException(clazz.getName) + } catch { + case e_module: MornyModuleInitializingException => + throw e_module + case e_any: Exception => + throw MornyModuleInitializingException(clazz.getName, e_any.getMessage).initCause(e_any) + } } - def loadFromJar (packageClazz: Class[?]): List[MornyModule] = { + @throws[MornyModuleInitializingException] + def loadModuleByClassName (className: String): MornyModule = { + 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]() - val moduleListFile = packageClazz.getResourceAsStream("/morny-modules.list") - .readAllBytes() - val modules = String(moduleListFile, StandardCharsets.UTF_8) - .split("\n") - .map(_.strip) - .filter(_.nonEmpty) - - modules.foreach { (clazzName: String) => + moduleClassNames.foreach { (clazzName: String) => try { - val clazz = Class.forName(clazzName) - val instance = clazz.getConstructor().newInstance() - instance match - case module: MornyModule => - list += module - case _ => - logger `error` - s"""Module is not a Morny Module : - | - in package : ${packageClazz.getName} - | - declared class name : $clazzName - |You need to implement a MornyModule trait to make it a REAL morny module!""".stripMargin - } catch case e: Exception => - logger `error` - s"""Failed to create a module instance : - | - in package : ${packageClazz.getName} - | - declared class name : $clazzName - |${e.toLogString} - |Is this a typo or packaging error? You need to add morny-modules.list and your code to the same jar.""".stripMargin + val module = loadModuleByClassName(clazzName) + list += module + } catch case e: MornyModuleInitializingException => + onLoadingErrors(e) } list.toList + } } diff --git a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModulesJarLoader.scala b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModulesJarLoader.scala index 236492e..7fcaff9 100644 --- a/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModulesJarLoader.scala +++ b/morny-coeur/src/main/scala/cc/sukazyo/cono/morny/core/module/ModulesJarLoader.scala @@ -1,9 +1,20 @@ 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 { - def load (): Unit = { - // TODO + def loadFromJar (packageClazz: Class[?], onLoadingErrors: MornyModuleInitializingException=>Any): List[MornyModule] = { + 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) } } diff --git a/morny-core/.gitignore b/morny-core/.gitignore new file mode 100644 index 0000000..23bf3bd --- /dev/null +++ b/morny-core/.gitignore @@ -0,0 +1,3 @@ + +# builds +/target diff --git a/project/MornyConfiguration.scala b/project/MornyConfiguration.scala index 3de5b4b..3e62a7d 100644 --- a/project/MornyConfiguration.scala +++ b/project/MornyConfiguration.scala @@ -60,11 +60,23 @@ object MornyConfiguration { // for test report "com.vladsch.flexmark" % "flexmark" % "0.64.8" % Test, "com.vladsch.flexmark" % "flexmark-profile-pegdown" % "0.64.8" % Test - + ) } + 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 { override val name = "Morny Coeur" @@ -108,7 +120,7 @@ object MornyConfiguration { // for test report "com.vladsch.flexmark" % "flexmark" % "0.64.8" % Test, "com.vladsch.flexmark" % "flexmark-profile-pegdown" % "0.64.8" % Test - + ) } diff --git a/project/MornyProject.scala b/project/MornyProject.scala index c0751cf..60e9727 100644 --- a/project/MornyProject.scala +++ b/project/MornyProject.scala @@ -54,6 +54,7 @@ object MornyProject { val source_encoding = "utf-8" val morny_system_lib = MornyConfiguration.Morny_System_Library + val morny_core = MornyConfiguration.Morny_Core val morny_coeur = MornyConfiguration.Morny_Coeur }