Small refactoring

This commit is contained in:
grimsi
2024-10-31 09:07:46 +01:00
parent f89086a06d
commit 5ebd074d3b
8 changed files with 34 additions and 40 deletions
@@ -9,7 +9,7 @@ import java.nio.file.Path
/** /**
* @see https://stackoverflow.com/questions/73654174/my-application-cant-find-the-extension-with-pf4j * @see https://stackoverflow.com/questions/73654174/my-application-cant-find-the-extension-with-pf4j
*/ */
class SpringDevtoolsDevelopmentPluginLoader( class GameyfinPluginLoader(
pluginManager: PluginManager, pluginManager: PluginManager,
private val parentClassLoader: ClassLoader private val parentClassLoader: ClassLoader
) : DevelopmentPluginLoader(pluginManager) { ) : DevelopmentPluginLoader(pluginManager) {
@@ -18,7 +18,7 @@ import java.nio.file.Path
/** /**
* @see https://stackoverflow.com/questions/73654174/my-application-cant-find-the-extension-with-pf4j * @see https://stackoverflow.com/questions/73654174/my-application-cant-find-the-extension-with-pf4j
*/ */
class SpringDevtoolsPluginManager( class GameyfinPluginManager(
path: Path, path: Path,
private val pluginConfigRepository: PluginConfigRepository private val pluginConfigRepository: PluginConfigRepository
) : DefaultPluginManager(path) { ) : DefaultPluginManager(path) {
@@ -34,7 +34,7 @@ class SpringDevtoolsPluginManager(
override fun createPluginLoader(): PluginLoader { override fun createPluginLoader(): PluginLoader {
val compoundPluginLoader = CompoundPluginLoader() val compoundPluginLoader = CompoundPluginLoader()
val developmentPluginLoader = SpringDevtoolsDevelopmentPluginLoader(this, javaClass.classLoader) val developmentPluginLoader = GameyfinPluginLoader(this, javaClass.classLoader)
val jarPluginLoader = JarPluginLoader(this) val jarPluginLoader = JarPluginLoader(this)
val defaultPluginLoader = DefaultPluginLoader(this) val defaultPluginLoader = DefaultPluginLoader(this)
@@ -7,12 +7,12 @@ import org.springframework.stereotype.Service
@Service @Service
class PluginConfigService( class PluginConfigService(
private val pluginConfigRepository: PluginConfigRepository, private val pluginConfigRepository: PluginConfigRepository,
private val pluginManager: SpringDevtoolsPluginManager private val pluginManager: GameyfinPluginManager
) { ) {
fun getConfigMetadata(pluginId: String): List<PluginConfigElement> { fun getConfigMetadata(pluginId: String): List<PluginConfigElement> {
val plugin = pluginManager.getPlugin(pluginId).plugin as GameyfinPlugin val plugin = pluginManager.getPlugin(pluginId).plugin as GameyfinPlugin
return plugin.getConfigMetadata() return plugin.configMetadata
} }
fun getConfig(pluginId: String): Map<String, String?> { fun getConfig(pluginId: String): Map<String, String?> {
@@ -15,7 +15,7 @@ class PluginManagerConfig(
private val pluginPath = System.getProperty("pf4j.pluginsDir", "plugins") private val pluginPath = System.getProperty("pf4j.pluginsDir", "plugins")
@Bean @Bean
fun pluginManager() = SpringDevtoolsPluginManager(Path.of(pluginPath), pluginConfigRepository) fun pluginManager() = GameyfinPluginManager(Path.of(pluginPath), pluginConfigRepository)
@EventListener(ApplicationReadyEvent::class) @EventListener(ApplicationReadyEvent::class)
fun loadPlugins() { fun loadPlugins() {
+2
View File
@@ -14,6 +14,8 @@ dependencies {
exclude(group = "org.slf4j") exclude(group = "org.slf4j")
} }
api("org.slf4j:slf4j-api:2.0.16")
implementation(kotlin("stdlib")) implementation(kotlin("stdlib"))
// Test dependencies // Test dependencies
@@ -1,7 +1,18 @@
package de.grimsi.gameyfin.pluginapi.core package de.grimsi.gameyfin.pluginapi.core
interface GameyfinPlugin { import org.pf4j.Plugin
fun getConfigMetadata(): List<PluginConfigElement> import org.pf4j.PluginWrapper
fun getCurrentConfig(): Map<String, String?>
fun loadConfig(config: Map<String, String?>) abstract class GameyfinPlugin(wrapper: PluginWrapper) : Plugin(wrapper) {
abstract val configMetadata: List<PluginConfigElement>
protected open var config: Map<String, String?> = emptyMap()
open fun getCurrentConfig(): Map<String, String?> {
return config
}
open fun loadConfig(config: Map<String, String?>) {
this.config = config
}
} }
-1
View File
@@ -7,7 +7,6 @@ subprojects {
dependencies { dependencies {
compileOnly(project(":plugin-api")) compileOnly(project(":plugin-api"))
implementation("io.github.oshai:kotlin-logging-jvm:7.0.0")
} }
tasks.jar { tasks.jar {
@@ -11,66 +11,48 @@ import de.grimsi.gameyfin.pluginapi.gamemetadata.GameMetadata
import de.grimsi.gameyfin.pluginapi.gamemetadata.GameMetadataFetcher import de.grimsi.gameyfin.pluginapi.gamemetadata.GameMetadataFetcher
import de.grimsi.gameyfin.pluginapi.gamemetadata.Genre import de.grimsi.gameyfin.pluginapi.gamemetadata.Genre
import de.grimsi.gameyfin.pluginapi.gamemetadata.Theme import de.grimsi.gameyfin.pluginapi.gamemetadata.Theme
import io.github.oshai.kotlinlogging.KotlinLogging
import org.pf4j.Extension import org.pf4j.Extension
import org.pf4j.Plugin
import org.pf4j.PluginWrapper import org.pf4j.PluginWrapper
import org.slf4j.LoggerFactory
import java.time.Instant import java.time.Instant
import kotlin.collections.filter import kotlin.collections.filter
class IgdbPlugin(wrapper: PluginWrapper) : Plugin(wrapper), GameyfinPlugin { class IgdbPlugin(wrapper: PluginWrapper) : GameyfinPlugin(wrapper) {
private val log = KotlinLogging.logger {} override val configMetadata: List<PluginConfigElement> = listOf(
private val configMetadata: List<PluginConfigElement> = listOf(
PluginConfigElement("clientId", "Twitch client ID", "Your Twitch Client ID"), PluginConfigElement("clientId", "Twitch client ID", "Your Twitch Client ID"),
PluginConfigElement("clientSecret", "Twitch client secret", "Your Twitch Client Secret") PluginConfigElement("clientSecret", "Twitch client secret", "Your Twitch Client Secret")
) )
private var config: Map<String, String?> = configMetadata.associate { it.key to null }
override fun getConfigMetadata(): List<PluginConfigElement> {
return configMetadata
}
override fun getCurrentConfig(): Map<String, String?> {
return config
}
override fun loadConfig(config: Map<String, String?>) {
this.config = config
}
override fun start() { override fun start() {
try { try {
authenticate() authenticate()
} catch (e: PluginConfigError) { } catch (e: PluginConfigError) {
log.error { e.message } log.error(e.message)
} }
} }
override fun stop() { override fun stop() {
log.debug { "IgdbPlugin.stop()" } log.debug("IgdbPlugin.stop()")
} }
private fun authenticate() { private fun authenticate() {
log.debug { "Authenticating on Twitch API..." } log.debug("Authenticating on Twitch API...")
val clientId: String = config["clientId"] ?: throw PluginConfigError("Twitch Client ID not set") val clientId: String = config["clientId"] ?: throw PluginConfigError("Twitch Client ID not set")
val clientSecret: String = config["clientSecret"] ?: throw PluginConfigError("Twitch Client Secret not set") val clientSecret: String = config["clientSecret"] ?: throw PluginConfigError("Twitch Client Secret not set")
val token = TwitchAuthenticator.requestTwitchToken(clientId, clientSecret) val token = TwitchAuthenticator.requestTwitchToken(clientId, clientSecret)
?: throw PluginConfigError("Failed to authenticate on Twitch API") ?: throw PluginConfigError("Failed to authenticate on Twitch API with provided credentials")
IGDBWrapper.setCredentials(clientId, token.access_token) IGDBWrapper.setCredentials(clientId, token.access_token)
log.debug { "Authentication successful" } log.debug("Authentication successful")
} }
@Extension @Extension
class IgdbMetadataFetcher : GameMetadataFetcher { class IgdbMetadataFetcher : GameMetadataFetcher {
private val log = KotlinLogging.logger {} private val log = LoggerFactory.getLogger(javaClass)
override fun fetchMetadata(gameId: String): GameMetadata { override fun fetchMetadata(gameId: String): GameMetadata {
val findGameByName = APICalypse() val findGameByName = APICalypse()
@@ -124,7 +106,7 @@ class IgdbPlugin(wrapper: PluginWrapper) : Plugin(wrapper), GameyfinPlugin {
"hack-and-slash-beat-em-up" -> Genre.HACK_AND_SLASH_BEAT_EM_UP "hack-and-slash-beat-em-up" -> Genre.HACK_AND_SLASH_BEAT_EM_UP
"quiz-trivia" -> Genre.QUIZ_TRIVIA "quiz-trivia" -> Genre.QUIZ_TRIVIA
else -> { else -> {
log.warn { "Unknown genre: ${genre.slug}" } log.warn("Unknown genre: {}", genre.slug)
Genre.UNKNOWN Genre.UNKNOWN
} }
} }
@@ -155,7 +137,7 @@ class IgdbPlugin(wrapper: PluginWrapper) : Plugin(wrapper), GameyfinPlugin {
"erotic" -> Theme.EROTIC "erotic" -> Theme.EROTIC
"romance" -> Theme.ROMANCE "romance" -> Theme.ROMANCE
else -> { else -> {
log.warn { "Unknown theme: ${theme.slug}" } log.warn("Unknown theme: {}", theme.slug)
Theme.UNKNOWN Theme.UNKNOWN
} }
} }