Skip to content

Commit 4b0fb8f

Browse files
committed
Polish 'Add @LocalRSocketServerPort support'
See gh-18287
1 parent 3c8fa3b commit 4b0fb8f

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.boot.autoconfigure.AutoConfigurations;
22+
import org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer;
2223
import org.springframework.boot.rsocket.server.RSocketServerBootstrap;
2324
import org.springframework.boot.rsocket.server.RSocketServerFactory;
2425
import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer;
2526
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2627
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
27-
import org.springframework.boot.web.context.RSocketPortInfoApplicationContextInitializer;
2828
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
2929
import org.springframework.context.annotation.Bean;
3030
import org.springframework.context.annotation.Configuration;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.web.server;
17+
package org.springframework.boot.rsocket.context;
1818

1919
import java.lang.annotation.Documented;
2020
import java.lang.annotation.ElementType;
Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.web.context;
17+
package org.springframework.boot.rsocket.context;
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
2121

2222
import org.springframework.beans.factory.annotation.Value;
23-
import org.springframework.boot.rsocket.context.RSocketServerInitializedEvent;
2423
import org.springframework.boot.rsocket.server.RSocketServer;
2524
import org.springframework.context.ApplicationContext;
2625
import org.springframework.context.ApplicationContextInitializer;
@@ -45,41 +44,52 @@
4544
* @since 2.2.0
4645
*/
4746
public class RSocketPortInfoApplicationContextInitializer
48-
implements ApplicationContextInitializer<ConfigurableApplicationContext>,
49-
ApplicationListener<RSocketServerInitializedEvent> {
50-
51-
private ConfigurableApplicationContext applicationContext;
47+
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
5248

5349
@Override
5450
public void initialize(ConfigurableApplicationContext applicationContext) {
55-
applicationContext.addApplicationListener(this);
56-
this.applicationContext = applicationContext;
51+
applicationContext.addApplicationListener(new Listener(applicationContext));
5752
}
5853

59-
@Override
60-
public void onApplicationEvent(RSocketServerInitializedEvent event) {
61-
String propertyName = "local.rsocket.server.port";
62-
setPortProperty(this.applicationContext, propertyName, event.getrSocketServer().address().getPort());
63-
}
54+
private static class Listener implements ApplicationListener<RSocketServerInitializedEvent> {
55+
56+
private static final String PROPERTY_NAME = "local.rsocket.server.port";
57+
58+
private ConfigurableApplicationContext applicationContext;
59+
60+
Listener(ConfigurableApplicationContext applicationContext) {
61+
this.applicationContext = applicationContext;
62+
}
6463

65-
private void setPortProperty(ApplicationContext context, String propertyName, int port) {
66-
if (context instanceof ConfigurableApplicationContext) {
67-
setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(), propertyName, port);
64+
@Override
65+
public void onApplicationEvent(RSocketServerInitializedEvent event) {
66+
setPortProperty(this.applicationContext, event.getrSocketServer().address().getPort());
6867
}
69-
if (context.getParent() != null) {
70-
setPortProperty(context.getParent(), propertyName, port);
68+
69+
private void setPortProperty(ApplicationContext context, int port) {
70+
if (context instanceof ConfigurableApplicationContext) {
71+
setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(), port);
72+
}
73+
if (context.getParent() != null) {
74+
setPortProperty(context.getParent(), port);
75+
}
7176
}
72-
}
7377

74-
@SuppressWarnings("unchecked")
75-
private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) {
76-
MutablePropertySources sources = environment.getPropertySources();
77-
PropertySource<?> source = sources.get("server.ports");
78-
if (source == null) {
79-
source = new MapPropertySource("server.ports", new HashMap<>());
80-
sources.addFirst(source);
78+
private void setPortProperty(ConfigurableEnvironment environment, int port) {
79+
MutablePropertySources sources = environment.getPropertySources();
80+
PropertySource<?> source = sources.get("server.ports");
81+
if (source == null) {
82+
source = new MapPropertySource("server.ports", new HashMap<>());
83+
sources.addFirst(source);
84+
}
85+
setPortProperty(port, source);
8186
}
82-
((Map<String, Object>) source.getSource()).put(propertyName, port);
87+
88+
@SuppressWarnings("unchecked")
89+
private void setPortProperty(int port, PropertySource<?> source) {
90+
((Map<String, Object>) source.getSource()).put(PROPERTY_NAME, port);
91+
}
92+
8393
}
8494

8595
}

spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ org.springframework.context.ApplicationContextInitializer=\
1616
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
1717
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
1818
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
19-
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer,\
20-
org.springframework.boot.web.context.RSocketPortInfoApplicationContextInitializer
19+
org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
20+
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
2121

2222
# Application Listeners
2323
org.springframework.context.ApplicationListener=\
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.web.server;
17+
package org.springframework.boot.rsocket.context;
1818

1919
import org.junit.jupiter.api.Test;
2020
import org.junit.jupiter.api.extension.ExtendWith;

0 commit comments

Comments
 (0)