From 2aea7caf36b34f4ba3b5ed5c783ac0b912775f84 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 23 Sep 2024 14:08:35 +0100 Subject: [PATCH] Remove use of reflection in Artemis connection factory creation Fixes gh-42414 --- ...ArtemisConnectionFactoryConfiguration.java | 6 ++-- .../ArtemisConnectionFactoryFactory.java | 35 +++++++++---------- ...temisXAConnectionFactoryConfiguration.java | 6 ++-- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java index 98a305be8f3..a6391df2bb9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,7 +56,7 @@ class ArtemisConnectionFactoryConfiguration { private static ActiveMQConnectionFactory createJmsConnectionFactory(ArtemisProperties properties, ListableBeanFactory beanFactory) { return new ArtemisConnectionFactoryFactory(beanFactory, properties) - .createConnectionFactory(ActiveMQConnectionFactory.class); + .createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new); } @Configuration(proxyBeanMethods = false) @@ -89,7 +89,7 @@ class ArtemisConnectionFactoryConfiguration { @Bean(destroyMethod = "stop") JmsPoolConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties) { ActiveMQConnectionFactory connectionFactory = new ArtemisConnectionFactoryFactory(beanFactory, properties) - .createConnectionFactory(ActiveMQConnectionFactory.class); + .createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new); return new JmsPoolConnectionFactoryFactory(properties.getPool()) .createPooledConnectionFactory(connectionFactory); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java index 344f8ace981..5221b37be53 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package org.springframework.boot.autoconfigure.jms.artemis; -import java.lang.reflect.Constructor; +import java.util.function.Function; import org.apache.activemq.artemis.api.core.TransportConfiguration; import org.apache.activemq.artemis.api.core.client.ActiveMQClient; @@ -56,10 +56,11 @@ class ArtemisConnectionFactoryFactory { this.properties = properties; } - T createConnectionFactory(Class factoryClass) { + T createConnectionFactory(Function nativeFactoryCreator, + Function embeddedFactoryCreator) { try { startEmbeddedJms(); - return doCreateConnectionFactory(factoryClass); + return doCreateConnectionFactory(nativeFactoryCreator, embeddedFactoryCreator); } catch (Exception ex) { throw new IllegalStateException("Unable to create ActiveMQConnectionFactory", ex); @@ -79,15 +80,16 @@ class ArtemisConnectionFactoryFactory { } } - private T doCreateConnectionFactory(Class factoryClass) throws Exception { + private T doCreateConnectionFactory(Function nativeFactoryCreator, + Function embeddedFactoryCreator) throws Exception { ArtemisMode mode = this.properties.getMode(); if (mode == null) { mode = deduceMode(); } if (mode == ArtemisMode.EMBEDDED) { - return createEmbeddedConnectionFactory(factoryClass); + return createEmbeddedConnectionFactory(embeddedFactoryCreator); } - return createNativeConnectionFactory(factoryClass); + return createNativeConnectionFactory(nativeFactoryCreator); } /** @@ -110,13 +112,13 @@ class ArtemisConnectionFactoryFactory { return false; } - private T createEmbeddedConnectionFactory(Class factoryClass) - throws Exception { + private T createEmbeddedConnectionFactory( + Function factoryCreator) throws Exception { try { TransportConfiguration transportConfiguration = new TransportConfiguration( InVMConnectorFactory.class.getName(), this.properties.getEmbedded().generateTransportParameters()); - ServerLocator serviceLocator = ActiveMQClient.createServerLocatorWithoutHA(transportConfiguration); - return factoryClass.getConstructor(ServerLocator.class).newInstance(serviceLocator); + ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(transportConfiguration); + return factoryCreator.apply(serverLocator); } catch (NoClassDefFoundError ex) { throw new IllegalStateException("Unable to create InVM " @@ -124,9 +126,8 @@ class ArtemisConnectionFactoryFactory { } } - private T createNativeConnectionFactory(Class factoryClass) - throws Exception { - T connectionFactory = newNativeConnectionFactory(factoryClass); + private T createNativeConnectionFactory(Function factoryCreator) { + T connectionFactory = newNativeConnectionFactory(factoryCreator); String user = this.properties.getUser(); if (StringUtils.hasText(user)) { connectionFactory.setUser(user); @@ -135,12 +136,10 @@ class ArtemisConnectionFactoryFactory { return connectionFactory; } - private T newNativeConnectionFactory(Class factoryClass) throws Exception { + private T newNativeConnectionFactory(Function factoryCreator) { String brokerUrl = StringUtils.hasText(this.properties.getBrokerUrl()) ? this.properties.getBrokerUrl() : DEFAULT_BROKER_URL; - Constructor constructor = factoryClass.getConstructor(String.class); - return constructor.newInstance(brokerUrl); - + return factoryCreator.apply(brokerUrl); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java index b296ae14b89..28d8792d136 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,14 +46,14 @@ class ArtemisXAConnectionFactoryConfiguration { ConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties, XAConnectionFactoryWrapper wrapper) throws Exception { return wrapper.wrapConnectionFactory(new ArtemisConnectionFactoryFactory(beanFactory, properties) - .createConnectionFactory(ActiveMQXAConnectionFactory.class)); + .createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new)); } @Bean ActiveMQXAConnectionFactory nonXaJmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties) { return new ArtemisConnectionFactoryFactory(beanFactory, properties) - .createConnectionFactory(ActiveMQXAConnectionFactory.class); + .createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new); } }