mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Create ActiveMQConnectionFactory without using reflection
Fixes gh-41212
This commit is contained in:
parent
55af8a38f5
commit
b8927ebd90
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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,
|
private static ActiveMQConnectionFactory createJmsConnectionFactory(ActiveMQProperties properties,
|
||||||
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers,
|
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers,
|
||||||
ActiveMQConnectionDetails connectionDetails) {
|
ActiveMQConnectionDetails connectionDetails) {
|
||||||
return new ActiveMQConnectionFactoryFactory(properties, factoryCustomizers.orderedStream().toList(),
|
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionDetails.getUser(),
|
||||||
connectionDetails)
|
connectionDetails.getPassword(), connectionDetails.getBrokerUrl());
|
||||||
.createConnectionFactory(ActiveMQConnectionFactory.class);
|
new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList())
|
||||||
|
.configure(connectionFactory);
|
||||||
|
return connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ -98,9 +100,10 @@ class ActiveMQConnectionFactoryConfiguration {
|
|||||||
JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
|
JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
|
||||||
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers,
|
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers,
|
||||||
ActiveMQConnectionDetails connectionDetails) {
|
ActiveMQConnectionDetails connectionDetails) {
|
||||||
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties,
|
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionDetails.getUser(),
|
||||||
factoryCustomizers.orderedStream().toList(), connectionDetails)
|
connectionDetails.getPassword(), connectionDetails.getBrokerUrl());
|
||||||
.createConnectionFactory(ActiveMQConnectionFactory.class);
|
new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList())
|
||||||
|
.configure(connectionFactory);
|
||||||
return new JmsPoolConnectionFactoryFactory(properties.getPool())
|
return new JmsPoolConnectionFactoryFactory(properties.getPool())
|
||||||
.createPooledConnectionFactory(connectionFactory);
|
.createPooledConnectionFactory(connectionFactory);
|
||||||
}
|
}
|
||||||
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
package org.springframework.boot.autoconfigure.jms.activemq;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
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.boot.autoconfigure.jms.activemq.ActiveMQProperties.Packages;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory to create a {@link ActiveMQConnectionFactory} instance from properties defined
|
* Class to configure an {@link ActiveMQConnectionFactory} instance from properties
|
||||||
* in {@link ActiveMQProperties}.
|
* defined in {@link ActiveMQProperties} and any
|
||||||
|
* {@link ActiveMQConnectionFactoryCustomizer customizers}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Venil Noronha
|
* @author Venil Noronha
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
*/
|
*/
|
||||||
class ActiveMQConnectionFactoryFactory {
|
class ActiveMQConnectionFactoryConfigurer {
|
||||||
|
|
||||||
private final ActiveMQProperties properties;
|
private final ActiveMQProperties properties;
|
||||||
|
|
||||||
private final List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers;
|
private final List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers;
|
||||||
|
|
||||||
private final ActiveMQConnectionDetails connectionDetails;
|
ActiveMQConnectionFactoryConfigurer(ActiveMQProperties properties,
|
||||||
|
List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
|
||||||
ActiveMQConnectionFactoryFactory(ActiveMQProperties properties,
|
|
||||||
List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers, ActiveMQConnectionDetails connectionDetails) {
|
|
||||||
Assert.notNull(properties, "Properties must not be null");
|
Assert.notNull(properties, "Properties must not be null");
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
this.factoryCustomizers = (factoryCustomizers != null) ? factoryCustomizers : Collections.emptyList();
|
this.factoryCustomizers = (factoryCustomizers != null) ? factoryCustomizers : Collections.emptyList();
|
||||||
this.connectionDetails = connectionDetails;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
<T extends ActiveMQConnectionFactory> T createConnectionFactory(Class<T> factoryClass) {
|
void configure(ActiveMQConnectionFactory factory) {
|
||||||
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);
|
|
||||||
if (this.properties.getCloseTimeout() != null) {
|
if (this.properties.getCloseTimeout() != null) {
|
||||||
factory.setCloseTimeout((int) this.properties.getCloseTimeout().toMillis());
|
factory.setCloseTimeout((int) this.properties.getCloseTimeout().toMillis());
|
||||||
}
|
}
|
||||||
@ -76,19 +62,6 @@ class ActiveMQConnectionFactoryFactory {
|
|||||||
factory.setTrustedPackages(packages.getTrusted());
|
factory.setTrustedPackages(packages.getTrusted());
|
||||||
}
|
}
|
||||||
customize(factory);
|
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) {
|
private void customize(ActiveMQConnectionFactory connectionFactory) {
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -49,9 +49,10 @@ class ActiveMQXAConnectionFactoryConfiguration {
|
|||||||
ConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
|
ConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
|
||||||
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers, XAConnectionFactoryWrapper wrapper,
|
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers, XAConnectionFactoryWrapper wrapper,
|
||||||
ActiveMQConnectionDetails connectionDetails) throws Exception {
|
ActiveMQConnectionDetails connectionDetails) throws Exception {
|
||||||
ActiveMQXAConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties,
|
ActiveMQXAConnectionFactory connectionFactory = new ActiveMQXAConnectionFactory(connectionDetails.getUser(),
|
||||||
factoryCustomizers.orderedStream().toList(), connectionDetails)
|
connectionDetails.getPassword(), connectionDetails.getBrokerUrl());
|
||||||
.createConnectionFactory(ActiveMQXAConnectionFactory.class);
|
new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList())
|
||||||
|
.configure(connectionFactory);
|
||||||
return wrapper.wrapConnectionFactory(connectionFactory);
|
return wrapper.wrapConnectionFactory(connectionFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,9 +62,11 @@ class ActiveMQXAConnectionFactoryConfiguration {
|
|||||||
ActiveMQConnectionFactory nonXaJmsConnectionFactory(ActiveMQProperties properties,
|
ActiveMQConnectionFactory nonXaJmsConnectionFactory(ActiveMQProperties properties,
|
||||||
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers,
|
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers,
|
||||||
ActiveMQConnectionDetails connectionDetails) {
|
ActiveMQConnectionDetails connectionDetails) {
|
||||||
return new ActiveMQConnectionFactoryFactory(properties, factoryCustomizers.orderedStream().toList(),
|
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionDetails.getUser(),
|
||||||
connectionDetails)
|
connectionDetails.getPassword(), connectionDetails.getBrokerUrl());
|
||||||
.createConnectionFactory(ActiveMQConnectionFactory.class);
|
new ActiveMQConnectionFactoryConfigurer(properties, factoryCustomizers.orderedStream().toList())
|
||||||
|
.configure(connectionFactory);
|
||||||
|
return connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
package org.springframework.boot.autoconfigure.jms.activemq;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
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 Stephane Nicoll
|
||||||
* @author Aurélien Leboulanger
|
* @author Aurélien Leboulanger
|
||||||
@ -50,25 +48,21 @@ class ActiveMQPropertiesTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void setTrustAllPackages() {
|
void setTrustAllPackages() {
|
||||||
|
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
|
||||||
this.properties.getPackages().setTrustAll(true);
|
this.properties.getPackages().setTrustAll(true);
|
||||||
assertThat(createFactory(this.properties).createConnectionFactory(ActiveMQConnectionFactory.class)
|
new ActiveMQConnectionFactoryConfigurer(this.properties, null).configure(factory);
|
||||||
.isTrustAllPackages()).isTrue();
|
assertThat(factory.isTrustAllPackages()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void setTrustedPackages() {
|
void setTrustedPackages() {
|
||||||
|
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
|
||||||
this.properties.getPackages().setTrustAll(false);
|
this.properties.getPackages().setTrustAll(false);
|
||||||
this.properties.getPackages().getTrusted().add("trusted.package");
|
this.properties.getPackages().getTrusted().add("trusted.package");
|
||||||
ActiveMQConnectionFactory factory = createFactory(this.properties)
|
new ActiveMQConnectionFactoryConfigurer(this.properties, null).configure(factory);
|
||||||
.createConnectionFactory(ActiveMQConnectionFactory.class);
|
|
||||||
assertThat(factory.isTrustAllPackages()).isFalse();
|
assertThat(factory.isTrustAllPackages()).isFalse();
|
||||||
assertThat(factory.getTrustedPackages()).hasSize(1);
|
assertThat(factory.getTrustedPackages()).hasSize(1);
|
||||||
assertThat(factory.getTrustedPackages().get(0)).isEqualTo("trusted.package");
|
assertThat(factory.getTrustedPackages().get(0)).isEqualTo("trusted.package");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ActiveMQConnectionFactoryFactory createFactory(ActiveMQProperties properties) {
|
|
||||||
return new ActiveMQConnectionFactoryFactory(properties, Collections.emptyList(),
|
|
||||||
new ActiveMQAutoConfiguration.PropertiesActiveMQConnectionDetails(properties));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user