|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -package org.springframework.boot.web.context; |
| 17 | +package org.springframework.boot.rsocket.context; |
18 | 18 |
|
19 | 19 | import java.util.HashMap; |
20 | 20 | import java.util.Map; |
21 | 21 |
|
22 | 22 | import org.springframework.beans.factory.annotation.Value; |
23 | | -import org.springframework.boot.rsocket.context.RSocketServerInitializedEvent; |
24 | 23 | import org.springframework.boot.rsocket.server.RSocketServer; |
25 | 24 | import org.springframework.context.ApplicationContext; |
26 | 25 | import org.springframework.context.ApplicationContextInitializer; |
|
45 | 44 | * @since 2.2.0 |
46 | 45 | */ |
47 | 46 | public class RSocketPortInfoApplicationContextInitializer |
48 | | - implements ApplicationContextInitializer<ConfigurableApplicationContext>, |
49 | | - ApplicationListener<RSocketServerInitializedEvent> { |
50 | | - |
51 | | - private ConfigurableApplicationContext applicationContext; |
| 47 | + implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
52 | 48 |
|
53 | 49 | @Override |
54 | 50 | public void initialize(ConfigurableApplicationContext applicationContext) { |
55 | | - applicationContext.addApplicationListener(this); |
56 | | - this.applicationContext = applicationContext; |
| 51 | + applicationContext.addApplicationListener(new Listener(applicationContext)); |
57 | 52 | } |
58 | 53 |
|
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 | + } |
64 | 63 |
|
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()); |
68 | 67 | } |
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 | + } |
71 | 76 | } |
72 | | - } |
73 | 77 |
|
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); |
81 | 86 | } |
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 | + |
83 | 93 | } |
84 | 94 |
|
85 | 95 | } |
0 commit comments