diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java index a4d242600e5..897c69576c0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.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. @@ -61,9 +61,11 @@ class ActiveMQConnectionFactoryConfiguration { private static ActiveMQConnectionFactory createJmsConnectionFactory(ActiveMQProperties properties, ObjectProvider factoryCustomizers, ActiveMQConnectionDetails connectionDetails) { - return new ActiveMQConnectionFactoryFactory(properties, factoryCustomizers.orderedStream().toList(), - connectionDetails) - .createConnectionFactory(ActiveMQConnectionFactory.class); + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionDetails.getUser(), + connectionDetails.getPassword(), connectionDetails.getBrokerUrl()); + new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList()) + .configure(connectionFactory); + return connectionFactory; } @Configuration(proxyBeanMethods = false) @@ -98,9 +100,10 @@ class ActiveMQConnectionFactoryConfiguration { JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties, ObjectProvider factoryCustomizers, ActiveMQConnectionDetails connectionDetails) { - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties, - factoryCustomizers.orderedStream().toList(), connectionDetails) - .createConnectionFactory(ActiveMQConnectionFactory.class); + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionDetails.getUser(), + connectionDetails.getPassword(), connectionDetails.getBrokerUrl()); + new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList()) + .configure(connectionFactory); return new JmsPoolConnectionFactoryFactory(properties.getPool()) .createPooledConnectionFactory(connectionFactory); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfigurer.java similarity index 55% rename from spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryFactory.java rename to spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfigurer.java index 67768c0363a..aa9c027540f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfigurer.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. @@ -16,7 +16,6 @@ package org.springframework.boot.autoconfigure.jms.activemq; -import java.lang.reflect.InvocationTargetException; import java.util.Collections; import java.util.List; @@ -24,43 +23,30 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties.Packages; import org.springframework.util.Assert; -import org.springframework.util.StringUtils; /** - * Factory to create a {@link ActiveMQConnectionFactory} instance from properties defined - * in {@link ActiveMQProperties}. + * Class to configure an {@link ActiveMQConnectionFactory} instance from properties + * defined in {@link ActiveMQProperties} and any + * {@link ActiveMQConnectionFactoryCustomizer customizers}. * * @author Phillip Webb * @author Venil Noronha * @author Eddú Meléndez */ -class ActiveMQConnectionFactoryFactory { +class ActiveMQConnectionFactoryConfigurer { private final ActiveMQProperties properties; private final List factoryCustomizers; - private final ActiveMQConnectionDetails connectionDetails; - - ActiveMQConnectionFactoryFactory(ActiveMQProperties properties, - List factoryCustomizers, ActiveMQConnectionDetails connectionDetails) { + ActiveMQConnectionFactoryConfigurer(ActiveMQProperties properties, + List factoryCustomizers) { Assert.notNull(properties, "Properties must not be null"); this.properties = properties; this.factoryCustomizers = (factoryCustomizers != null) ? factoryCustomizers : Collections.emptyList(); - this.connectionDetails = connectionDetails; } - T createConnectionFactory(Class factoryClass) { - try { - return doCreateConnectionFactory(factoryClass); - } - catch (Exception ex) { - throw new IllegalStateException("Unable to create ActiveMQConnectionFactory", ex); - } - } - - private T doCreateConnectionFactory(Class factoryClass) throws Exception { - T factory = createConnectionFactoryInstance(factoryClass); + void configure(ActiveMQConnectionFactory factory) { if (this.properties.getCloseTimeout() != null) { factory.setCloseTimeout((int) this.properties.getCloseTimeout().toMillis()); } @@ -76,19 +62,6 @@ class ActiveMQConnectionFactoryFactory { factory.setTrustedPackages(packages.getTrusted()); } customize(factory); - return factory; - } - - private T createConnectionFactoryInstance(Class factoryClass) - throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { - String brokerUrl = this.connectionDetails.getBrokerUrl(); - String user = this.connectionDetails.getUser(); - String password = this.connectionDetails.getPassword(); - if (StringUtils.hasLength(user) && StringUtils.hasLength(password)) { - return factoryClass.getConstructor(String.class, String.class, String.class) - .newInstance(user, password, brokerUrl); - } - return factoryClass.getConstructor(String.class).newInstance(brokerUrl); } private void customize(ActiveMQConnectionFactory connectionFactory) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQXAConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQXAConnectionFactoryConfiguration.java index 6458c5824ae..d03fabc1192 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQXAConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQXAConnectionFactoryConfiguration.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. @@ -49,9 +49,10 @@ class ActiveMQXAConnectionFactoryConfiguration { ConnectionFactory jmsConnectionFactory(ActiveMQProperties properties, ObjectProvider factoryCustomizers, XAConnectionFactoryWrapper wrapper, ActiveMQConnectionDetails connectionDetails) throws Exception { - ActiveMQXAConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties, - factoryCustomizers.orderedStream().toList(), connectionDetails) - .createConnectionFactory(ActiveMQXAConnectionFactory.class); + ActiveMQXAConnectionFactory connectionFactory = new ActiveMQXAConnectionFactory(connectionDetails.getUser(), + connectionDetails.getPassword(), connectionDetails.getBrokerUrl()); + new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList()) + .configure(connectionFactory); return wrapper.wrapConnectionFactory(connectionFactory); } @@ -61,9 +62,11 @@ class ActiveMQXAConnectionFactoryConfiguration { ActiveMQConnectionFactory nonXaJmsConnectionFactory(ActiveMQProperties properties, ObjectProvider factoryCustomizers, ActiveMQConnectionDetails connectionDetails) { - return new ActiveMQConnectionFactoryFactory(properties, factoryCustomizers.orderedStream().toList(), - connectionDetails) - .createConnectionFactory(ActiveMQConnectionFactory.class); + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionDetails.getUser(), + connectionDetails.getPassword(), connectionDetails.getBrokerUrl()); + new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList()) + .configure(connectionFactory); + return connectionFactory; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQPropertiesTests.java index a839b4d03d8..3c4ee27987c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQPropertiesTests.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. @@ -16,15 +16,13 @@ package org.springframework.boot.autoconfigure.jms.activemq; -import java.util.Collections; - import org.apache.activemq.ActiveMQConnectionFactory; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link ActiveMQProperties} and {@link ActiveMQConnectionFactoryFactory}. + * Tests for {@link ActiveMQProperties} and {@link ActiveMQConnectionFactoryConfigurer}. * * @author Stephane Nicoll * @author Aurélien Leboulanger @@ -50,25 +48,21 @@ class ActiveMQPropertiesTests { @Test void setTrustAllPackages() { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); this.properties.getPackages().setTrustAll(true); - assertThat(createFactory(this.properties).createConnectionFactory(ActiveMQConnectionFactory.class) - .isTrustAllPackages()).isTrue(); + new ActiveMQConnectionFactoryConfigurer(this.properties, null).configure(factory); + assertThat(factory.isTrustAllPackages()).isTrue(); } @Test void setTrustedPackages() { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); this.properties.getPackages().setTrustAll(false); this.properties.getPackages().getTrusted().add("trusted.package"); - ActiveMQConnectionFactory factory = createFactory(this.properties) - .createConnectionFactory(ActiveMQConnectionFactory.class); + new ActiveMQConnectionFactoryConfigurer(this.properties, null).configure(factory); assertThat(factory.isTrustAllPackages()).isFalse(); assertThat(factory.getTrustedPackages()).hasSize(1); assertThat(factory.getTrustedPackages().get(0)).isEqualTo("trusted.package"); } - private ActiveMQConnectionFactoryFactory createFactory(ActiveMQProperties properties) { - return new ActiveMQConnectionFactoryFactory(properties, Collections.emptyList(), - new ActiveMQAutoConfiguration.PropertiesActiveMQConnectionDetails(properties)); - } - }