Skip to content

Commit

Permalink
pref(spring): 简单优化spring中加载resources时资源不存在的错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jun 16, 2024
1 parent e6f0ded commit 3895cce
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/kdoc.yml.bk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'

# setup Gradle
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
#cache: 'gradle'

- name: Run All Tests
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'

# setup Gradle
Expand Down Expand Up @@ -113,7 +113,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'

# setup Gradle
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'

- name: Run All Tests
Expand Down Expand Up @@ -82,7 +82,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'

# setup Gradle
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'

- name: Run All Tests
Expand Down Expand Up @@ -50,7 +50,7 @@ jobs:
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'
- name: Run Detekt
uses: gradle/gradle-build-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-v4-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
java-version: 21
cache: 'gradle'
- name: Run Detekt
uses: gradle/gradle-build-action@v3
Expand Down
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ detekt {
tasks.withType<Detekt>().configureEach {
// internal 处理器不管
exclude("internal-processors/**")
// tests 不管
exclude("tests/**")

include("**/src/*Main/kotlin/**/*.kt")
include("**/src/*Main/kotlin/**/*.java")
Expand Down Expand Up @@ -142,6 +144,8 @@ apiValidation {
listOf(
"interface-uml-processor",
"simbot-test",
"tests",
"spring-boot-starter-test",
)
)

Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ include(":simbot-cores:simbot-core-spring-boot-starter-v2")
// extensions
include(":simbot-extensions:simbot-extension-continuous-session")

// local tests
include(":tests:spring-boot-starter-test")

// local
// if (!(System.getProperty("IS_CI") ?: System.getenv("IS_CI")).toBoolean()) {
// include(":tests:native-impl-test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ public final class love/forte/simbot/spring/common/application/SpringApplication
public final fun getAutoStartBots ()Z
public final fun getAutoStartMode ()Llove/forte/simbot/spring/common/application/BotAutoStartMode;
public final fun getConfigurationJsonResources ()Ljava/util/Set;
public final fun getIgnoreIOExceptionForResourcesLoad ()Z
public final fun setAutoRegistrationFailurePolicy (Llove/forte/simbot/spring/common/application/BotRegistrationFailurePolicy;)V
public final fun setAutoRegistrationMismatchConfigurableBotManagerPolicy (Llove/forte/simbot/spring/common/application/MismatchConfigurableBotManagerPolicy;)V
public final fun setAutoRegistrationResourceLoadFailurePolicy (Llove/forte/simbot/spring/common/application/BotConfigResourceLoadFailurePolicy;)V
public final fun setAutoStartBots (Z)V
public final fun setAutoStartMode (Llove/forte/simbot/spring/common/application/BotAutoStartMode;)V
public final fun setConfigurationJsonResources (Ljava/util/Set;)V
public final fun setIgnoreIOExceptionForResourcesLoad (Z)V
}

public final class love/forte/simbot/spring/common/application/SpringApplicationConfigurationProperties$BotProperties$Companion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public class SpringApplicationConfigurationProperties {
internal const val DEFAULT_JSON_RESOURCE_PATTERN: String = "classpath:simbot-bots/*.bot.json"
}

/**
* 是否忽略读取 [configurationJsonResources] 过程中产生的 `IOException`。
*
* @since 4.0.1
*/
public var ignoreIOExceptionForResourcesLoad: Boolean = true

/**
* 需要加载的所有组件下它们对应的所有 JSON 格式 的 bot 配置文件。
* 默认为 [`classpath:simbot-bots/\*.bot.json`][DEFAULT_JSON_RESOURCE_PATTERN]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import org.springframework.context.annotation.Configuration
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import java.io.FileNotFoundException
import java.io.IOException

/**
* [SimbotApplicationProcessor] 的默认实现
Expand Down Expand Up @@ -150,6 +151,7 @@ private class BotAutoLoader(

fun load() {
val resolver = PathMatchingResourcePatternResolver()
val ignoreIO = properties.ignoreIOExceptionForResourcesLoad

val botList =
properties.configurationJsonResources
Expand All @@ -172,6 +174,25 @@ private class BotAutoLoader(
fne
)

emptySequence()
} catch (ie: IOException) {
if (!ignoreIO) {
throw ie
}
logger.warn(
"Bot configuration resource path [{}] could not be resolved " +
"because of IOException(message={}), will be skipped.",
it,
ie.localizedMessage,
)
logger.debug(
"Bot configuration resource path [{}] could not be resolved " +
"because of IOException(message={}), will be skipped.",
it,
ie.localizedMessage,
ie
)

emptySequence()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import org.springframework.context.annotation.Configuration
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import java.io.FileNotFoundException
import java.io.IOException

/**
* [SimbotApplicationProcessor] 的默认实现
Expand Down Expand Up @@ -150,6 +151,7 @@ private class BotAutoLoader(

fun load() {
val resolver = PathMatchingResourcePatternResolver()
val ignoreIO = properties.ignoreIOExceptionForResourcesLoad

val botList =
properties.configurationJsonResources
Expand All @@ -160,18 +162,37 @@ private class BotAutoLoader(
} catch (fne: FileNotFoundException) {
logger.warn(
"Bot configuration resource path [{}] could not be resolved " +
"because of FileNotFoundException(message={}), will be skip.",
"because of FileNotFoundException(message={}), will be skipped.",
it,
fne.localizedMessage,
)
logger.debug(
"Bot configuration resource path [{}] could not be resolved " +
"because of FileNotFoundException(message={}), will be skip.",
"because of FileNotFoundException(message={}), will be skipped.",
it,
fne.localizedMessage,
fne
)

emptySequence()
} catch (ie: IOException) {
if (!ignoreIO) {
throw ie
}
logger.warn(
"Bot configuration resource path [{}] could not be resolved " +
"because of IOException(message={}), will be skipped.",
it,
ie.localizedMessage,
)
logger.debug(
"Bot configuration resource path [{}] could not be resolved " +
"because of IOException(message={}), will be skipped.",
it,
ie.localizedMessage,
ie
)

emptySequence()
}
}
Expand Down
58 changes: 58 additions & 0 deletions tests/spring-boot-starter-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024. ForteScarlet.
*
* Project https://github.com/simple-robot/simpler-robot
* Email ForteScarlet@163.com
*
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

plugins {
kotlin("jvm")
id("org.springframework.boot") version "3.3.0"
id("io.spring.dependency-management") version "1.1.5"
kotlin("plugin.spring") version "2.0.0"
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

repositories {
mavenCentral()
}

dependencies {
implementation(project(":simbot-cores:simbot-core-spring-boot-starter"))
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

kotlin {
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict")
}
}

tasks.withType<Test> {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024. ForteScarlet.
*
* Project https://github.com/simple-robot/simpler-robot
* Email ForteScarlet@163.com
*
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package love.forte.spring.test

import kotlinx.coroutines.runBlocking
import love.forte.simbot.application.Application
import love.forte.simbot.spring.EnableSimbot
import org.springframework.beans.factory.getBean
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication


/**
*
* @author ForteScarlet
*/
@SpringBootApplication
@EnableSimbot
class Main

fun main(args: Array<String>) {
val context = runApplication<Main>(*args)
val app = context.getBean<Application>()
runBlocking { app.join() }
}

0 comments on commit 3895cce

Please sign in to comment.