Create ActiveMQConnectionFactory without using reflection

Fixes gh-41212
This commit is contained in:
Andy Wilkinson 2024-06-24 11:50:31 +01:00
parent 55af8a38f5
commit b8927ebd90
4 changed files with 35 additions and 62 deletions

View File

@ -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<ActiveMQConnectionFactoryCustomizer> 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<ActiveMQConnectionFactoryCustomizer> 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);
}

View File

@ -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<ActiveMQConnectionFactoryCustomizer> factoryCustomizers;
private final ActiveMQConnectionDetails connectionDetails;
ActiveMQConnectionFactoryFactory(ActiveMQProperties properties,
List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers, ActiveMQConnectionDetails connectionDetails) {
ActiveMQConnectionFactoryConfigurer(ActiveMQProperties properties,
List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
Assert.notNull(properties, "Properties must not be null");
this.properties = properties;
this.factoryCustomizers = (factoryCustomizers != null) ? factoryCustomizers : Collections.emptyList();
this.connectionDetails = connectionDetails;
}
<T extends ActiveMQConnectionFactory> T createConnectionFactory(Class<T> factoryClass) {
try {
return doCreateConnectionFactory(factoryClass);
}
catch (Exception ex) {
throw new IllegalStateException("Unable to create ActiveMQConnectionFactory", ex);
}
}
private <T extends ActiveMQConnectionFactory> T doCreateConnectionFactory(Class<T> 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 extends ActiveMQConnectionFactory> T createConnectionFactoryInstance(Class<T> 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) {

View File

@ -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<ActiveMQConnectionFactoryCustomizer> 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<ActiveMQConnectionFactoryCustomizer> 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;
}
}

View File

@ -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));
}
}