Skip to content

Commit

Permalink
fix reflective property access, upgrade to spb 3.1.5 and axon 4.9, fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zambrovski authored Nov 20, 2023
1 parent 4c82578 commit 9e828e7
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
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 @@ -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

0 comments on commit 9e828e7

Please sign in to comment.