Skip to content

Commit 2aea7ca

Browse files
committed
Remove use of reflection in Artemis connection factory creation
Fixes gh-42414
1 parent 0781e71 commit 2aea7ca

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ ActiveMQConnectionFactory jmsConnectionFactory(ArtemisProperties properties, Lis
5656
private static ActiveMQConnectionFactory createJmsConnectionFactory(ArtemisProperties properties,
5757
ListableBeanFactory beanFactory) {
5858
return new ArtemisConnectionFactoryFactory(beanFactory, properties)
59-
.createConnectionFactory(ActiveMQConnectionFactory.class);
59+
.createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new);
6060
}
6161

6262
@Configuration(proxyBeanMethods = false)
@@ -89,7 +89,7 @@ static class PooledConnectionFactoryConfiguration {
8989
@Bean(destroyMethod = "stop")
9090
JmsPoolConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties) {
9191
ActiveMQConnectionFactory connectionFactory = new ArtemisConnectionFactoryFactory(beanFactory, properties)
92-
.createConnectionFactory(ActiveMQConnectionFactory.class);
92+
.createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new);
9393
return new JmsPoolConnectionFactoryFactory(properties.getPool())
9494
.createPooledConnectionFactory(connectionFactory);
9595
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.jms.artemis;
1818

19-
import java.lang.reflect.Constructor;
19+
import java.util.function.Function;
2020

2121
import org.apache.activemq.artemis.api.core.TransportConfiguration;
2222
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
@@ -56,10 +56,11 @@ class ArtemisConnectionFactoryFactory {
5656
this.properties = properties;
5757
}
5858

59-
<T extends ActiveMQConnectionFactory> T createConnectionFactory(Class<T> factoryClass) {
59+
<T extends ActiveMQConnectionFactory> T createConnectionFactory(Function<String, T> nativeFactoryCreator,
60+
Function<ServerLocator, T> embeddedFactoryCreator) {
6061
try {
6162
startEmbeddedJms();
62-
return doCreateConnectionFactory(factoryClass);
63+
return doCreateConnectionFactory(nativeFactoryCreator, embeddedFactoryCreator);
6364
}
6465
catch (Exception ex) {
6566
throw new IllegalStateException("Unable to create ActiveMQConnectionFactory", ex);
@@ -79,15 +80,16 @@ private void startEmbeddedJms() {
7980
}
8081
}
8182

82-
private <T extends ActiveMQConnectionFactory> T doCreateConnectionFactory(Class<T> factoryClass) throws Exception {
83+
private <T extends ActiveMQConnectionFactory> T doCreateConnectionFactory(Function<String, T> nativeFactoryCreator,
84+
Function<ServerLocator, T> embeddedFactoryCreator) throws Exception {
8385
ArtemisMode mode = this.properties.getMode();
8486
if (mode == null) {
8587
mode = deduceMode();
8688
}
8789
if (mode == ArtemisMode.EMBEDDED) {
88-
return createEmbeddedConnectionFactory(factoryClass);
90+
return createEmbeddedConnectionFactory(embeddedFactoryCreator);
8991
}
90-
return createNativeConnectionFactory(factoryClass);
92+
return createNativeConnectionFactory(nativeFactoryCreator);
9193
}
9294

9395
/**
@@ -110,23 +112,22 @@ private boolean isEmbeddedJmsClassPresent() {
110112
return false;
111113
}
112114

113-
private <T extends ActiveMQConnectionFactory> T createEmbeddedConnectionFactory(Class<T> factoryClass)
114-
throws Exception {
115+
private <T extends ActiveMQConnectionFactory> T createEmbeddedConnectionFactory(
116+
Function<ServerLocator, T> factoryCreator) throws Exception {
115117
try {
116118
TransportConfiguration transportConfiguration = new TransportConfiguration(
117119
InVMConnectorFactory.class.getName(), this.properties.getEmbedded().generateTransportParameters());
118-
ServerLocator serviceLocator = ActiveMQClient.createServerLocatorWithoutHA(transportConfiguration);
119-
return factoryClass.getConstructor(ServerLocator.class).newInstance(serviceLocator);
120+
ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(transportConfiguration);
121+
return factoryCreator.apply(serverLocator);
120122
}
121123
catch (NoClassDefFoundError ex) {
122124
throw new IllegalStateException("Unable to create InVM "
123125
+ "Artemis connection, ensure that artemis-jms-server.jar is in the classpath", ex);
124126
}
125127
}
126128

127-
private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Class<T> factoryClass)
128-
throws Exception {
129-
T connectionFactory = newNativeConnectionFactory(factoryClass);
129+
private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Function<String, T> factoryCreator) {
130+
T connectionFactory = newNativeConnectionFactory(factoryCreator);
130131
String user = this.properties.getUser();
131132
if (StringUtils.hasText(user)) {
132133
connectionFactory.setUser(user);
@@ -135,12 +136,10 @@ private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Cl
135136
return connectionFactory;
136137
}
137138

138-
private <T extends ActiveMQConnectionFactory> T newNativeConnectionFactory(Class<T> factoryClass) throws Exception {
139+
private <T extends ActiveMQConnectionFactory> T newNativeConnectionFactory(Function<String, T> factoryCreator) {
139140
String brokerUrl = StringUtils.hasText(this.properties.getBrokerUrl()) ? this.properties.getBrokerUrl()
140141
: DEFAULT_BROKER_URL;
141-
Constructor<T> constructor = factoryClass.getConstructor(String.class);
142-
return constructor.newInstance(brokerUrl);
143-
142+
return factoryCreator.apply(brokerUrl);
144143
}
145144

146145
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,14 +46,14 @@ class ArtemisXAConnectionFactoryConfiguration {
4646
ConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties,
4747
XAConnectionFactoryWrapper wrapper) throws Exception {
4848
return wrapper.wrapConnectionFactory(new ArtemisConnectionFactoryFactory(beanFactory, properties)
49-
.createConnectionFactory(ActiveMQXAConnectionFactory.class));
49+
.createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new));
5050
}
5151

5252
@Bean
5353
ActiveMQXAConnectionFactory nonXaJmsConnectionFactory(ListableBeanFactory beanFactory,
5454
ArtemisProperties properties) {
5555
return new ArtemisConnectionFactoryFactory(beanFactory, properties)
56-
.createConnectionFactory(ActiveMQXAConnectionFactory.class);
56+
.createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new);
5757
}
5858

5959
}

0 commit comments

Comments
 (0)