Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix property reflective access #910

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bom/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
<url>https://github.com/holunda-io/camunda-bpm-taskpool/</url>

<properties>
<springboot.version>3.1.4</springboot.version>
<springboot.version>3.1.5</springboot.version>
<camunda-commons-typed-values.version>7.20.0</camunda-commons-typed-values.version>

<axon-bom.version>4.8.3</axon-bom.version>
<axon-bom.version>4.9.0</axon-bom.version>
<axon-kotlin.version>4.9.0</axon-kotlin.version>
<axon-gateway-extension.version>2.0.0</axon-gateway-extension.version>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ internal class DataEntryQueriesDeserializationTest {
DataEntriesForUserQuery(
user = User(
username = "kermit", groups = setOf("muppets")
), page = 1, size = 50, sort = "+name", filters = listOf("data.name=test")
), page = 1, size = 50, sort = listOf("+name"), filters = listOf("data.name=test")
)
), Arguments.of(
DataEntriesForDataEntryTypeQuery::class.java,
DataEntriesForDataEntryTypeQuery(
entryType = "domain.type", page = 1, size = 50, sort = "+name"
entryType = "domain.type", page = 1, size = 50, sort = listOf("+name")
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class TaskQueriesDeserializationTest {
AllTasksQuery(
page = 1,
size = 50,
sort = "+name",
sort = listOf("+name"),
filters = listOf("task.name=test")
)
),
Expand All @@ -34,7 +34,7 @@ internal class TaskQueriesDeserializationTest {
AllTasksWithDataEntriesQuery(
page = 1,
size = 50,
sort = "+name",
sort = listOf("+name"),
filters = listOf("task.name=test")
)
),
Expand All @@ -56,7 +56,7 @@ internal class TaskQueriesDeserializationTest {
user = User(username = "kermit", groups = setOf("muppets")),
page = 1,
size = 50,
sort = "+name",
sort = listOf("+name"),
filters = listOf("task.name=test")
)
),
Expand All @@ -66,7 +66,7 @@ internal class TaskQueriesDeserializationTest {
user = User(username = "kermit", groups = setOf("muppets")),
page = 1,
size = 50,
sort = "+name",
sort = listOf("+name"),
filters = listOf("task.name=test")
)
),
Expand All @@ -76,7 +76,7 @@ internal class TaskQueriesDeserializationTest {
user = User(username = "kermit", groups = setOf("muppets")),
page = 1,
size = 50,
sort = "+name",
sort = listOf("+name"),
filters = listOf("task.name=test")
)
),
Expand All @@ -86,7 +86,7 @@ internal class TaskQueriesDeserializationTest {
user = User(username = "kermit", groups = setOf("muppets")),
page = 1,
size = 50,
sort = "+name",
sort = listOf("+name"),
filters = listOf("task.name=test")
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.holunda.polyflow.datapool.core.itest
import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.security.AnyTypePermission
import io.holunda.polyflow.datapool.core.EnablePolyflowDataPool
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine
import org.axonframework.serialization.Serializer
import org.axonframework.serialization.xml.XStreamSerializer
import org.springframework.beans.factory.annotation.Qualifier
Expand All @@ -16,4 +17,6 @@ class TestApplication {
@Qualifier("eventSerializer")
fun myEventSerializerForProcess(): Serializer = XStreamSerializer.builder().xStream(XStream().apply { addPermission(AnyTypePermission.ANY) }).build()

@Bean
fun inMemoryStorageEngine() = InMemoryEventStorageEngine()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import org.springframework.core.annotation.AnnotationUtils
import org.springframework.core.env.get
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.javaGetter
import kotlin.reflect.jvm.javaSetter

/**
* A bean post processor that inspects all [ConfigurationProperties] beans, looks for a mutable property of type `String` called `applicationName` and replaces it with the value
Expand All @@ -17,15 +21,21 @@ class ApplicationNameBeanPostProcessor(private val applicationContext: Applicati

companion object {
const val UNSET_APPLICATION_NAME = "unset-application-name"
val CANDIDATES = arrayOf(
"io.holunda.polyflow.taskpool.collector.CamundaTaskpoolCollectorProperties",
"io.holunda.polyflow.client.camunda.CamundaEngineClientProperties",
"io.holunda.polyflow.datapool.DataEntrySenderProperties",
)
}

private val applicationName: String by lazy { applicationContext.environment["spring.application.name"] ?: UNSET_APPLICATION_NAME }
override fun postProcessAfterInitialization(bean: Any, beanName: String): Any {
if (AnnotationUtils.findAnnotation(bean::class.java, ConfigurationProperties::class.java) != null) {
if (CANDIDATES.contains(bean::class.java.name)
&& AnnotationUtils.findAnnotation(bean::class.java, ConfigurationProperties::class.java) != null) {
@Suppress("UNCHECKED_CAST")
val applicationNameProperty =
bean::class.memberProperties.find { it.name == "applicationName" && it is KMutableProperty1<out Any, *> && it.returnType.classifier == String::class } as KMutableProperty1<Any, String>?
if (applicationNameProperty != null) {
if ( applicationNameProperty != null) {
if (applicationNameProperty.get(bean) == UNSET_APPLICATION_NAME) {
applicationNameProperty.set(bean, applicationName)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.holunda.polyflow.datapool

import io.holunda.polyflow.spring.ApplicationNameBeanPostProcessor
import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "polyflow.test")
data class DataEntrySenderProperties(var applicationName: String = ApplicationNameBeanPostProcessor.UNSET_APPLICATION_NAME)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.holunda.polyflow.spring

import io.holunda.polyflow.datapool.DataEntrySenderProperties
import io.holunda.polyflow.spring.ApplicationNameBeanPostProcessor.Companion.UNSET_APPLICATION_NAME
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand All @@ -20,20 +21,39 @@ internal class ApplicationNameBeanPostProcessorTest {
.withPropertyValues(
"spring.application.name=my-test-application"
).run {
val testProperties = it.getBean(TestProperties::class.java)
val privateTestProperties = it.getBean(PrivateTestProperties::class.java)
assertThat(privateTestProperties).isNotNull
assertThat(privateTestProperties.getApplicationNamePropertyValue()).isEqualTo("unset-application-name")
}
contextRunner
.withPropertyValues(
"spring.application.name=my-test-application",
).run {
val testProperties = it.getBean(DataEntrySenderProperties::class.java)
assertThat(testProperties).isNotNull
assertThat(testProperties.applicationName).isEqualTo("my-test-application")
}

}

@Test
fun `leaves application name if specifically set`() {
contextRunner
.withPropertyValues(
"spring.application.name=my-test-application",
"polyflow.test.private.application-name=my-polyflow-test-application"
).run {
val privateTestProperties = it.getBean(PrivateTestProperties::class.java)
assertThat(privateTestProperties).isNotNull
assertThat(privateTestProperties.getApplicationNamePropertyValue()).isEqualTo("my-polyflow-test-application")
}

contextRunner
.withPropertyValues(
"spring.application.name=my-test-application",
"polyflow.test.application-name=my-polyflow-test-application"
).run {
val testProperties = it.getBean(TestProperties::class.java)
val testProperties = it.getBean(DataEntrySenderProperties::class.java)
assertThat(testProperties).isNotNull
assertThat(testProperties.applicationName).isEqualTo("my-polyflow-test-application")
}
Expand All @@ -43,17 +63,25 @@ internal class ApplicationNameBeanPostProcessorTest {
fun `leaves application name at default if spring-application-name is not set`() {
contextRunner
.run {
val testProperties = it.getBean(TestProperties::class.java)
val privateTestProperties = it.getBean(PrivateTestProperties::class.java)
assertThat(privateTestProperties).isNotNull
assertThat(privateTestProperties.getApplicationNamePropertyValue()).isEqualTo(UNSET_APPLICATION_NAME)
}
contextRunner
.run {
val testProperties = it.getBean(DataEntrySenderProperties::class.java)
assertThat(testProperties).isNotNull
assertThat(testProperties.applicationName).isEqualTo(UNSET_APPLICATION_NAME)
}
}
}

@Configuration
@EnableConfigurationProperties(TestProperties::class)
@EnableConfigurationProperties(value = [PrivateTestProperties::class, DataEntrySenderProperties::class])
@Import(ApplicationNameBeanPostProcessor::class)
class TestConfig

@ConfigurationProperties(prefix = "polyflow.test")
data class TestProperties(var applicationName: String = UNSET_APPLICATION_NAME)
@ConfigurationProperties(prefix = "polyflow.test.private")
data class PrivateTestProperties(private var applicationName: String = UNSET_APPLICATION_NAME) {
fun getApplicationNamePropertyValue() = this.applicationName
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.holunda.polyflow.taskpool.core

import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.security.AnyTypePermission
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine
import org.axonframework.serialization.Serializer
import org.axonframework.serialization.xml.XStreamSerializer
import org.springframework.beans.factory.annotation.Qualifier
Expand All @@ -15,4 +16,6 @@ class TestApplication {
@Qualifier("eventSerializer")
fun myEventSerializerForProcess(): Serializer = XStreamSerializer.builder().xStream(XStream().apply { addPermission(AnyTypePermission.ANY) }).build()

@Bean
fun inMemoryEventStoreEngine() = InMemoryEventStorageEngine()
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<configuration>
<rules>
<requireMavenVersion>
<version>3.6.0</version>
<version>3.9.0</version>
</requireMavenVersion>
<requireJavaVersion>
<version>17</version>
Expand Down