Commit 4b0fb8ff authored by Phillip Webb's avatar Phillip Webb

Polish 'Add @LocalRSocketServerPort support'

See gh-18287
parent 3c8fa3bb
......@@ -19,12 +19,12 @@ package org.springframework.boot.autoconfigure.rsocket;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer;
import org.springframework.boot.rsocket.server.RSocketServerBootstrap;
import org.springframework.boot.rsocket.server.RSocketServerFactory;
import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.web.context.RSocketPortInfoApplicationContextInitializer;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.web.server;
package org.springframework.boot.rsocket.context;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
......
......@@ -14,13 +14,12 @@
* limitations under the License.
*/
package org.springframework.boot.web.context;
package org.springframework.boot.rsocket.context;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.rsocket.context.RSocketServerInitializedEvent;
import org.springframework.boot.rsocket.server.RSocketServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
......@@ -45,41 +44,52 @@ import org.springframework.core.env.PropertySource;
* @since 2.2.0
*/
public class RSocketPortInfoApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext>,
ApplicationListener<RSocketServerInitializedEvent> {
private ConfigurableApplicationContext applicationContext;
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(this);
this.applicationContext = applicationContext;
applicationContext.addApplicationListener(new Listener(applicationContext));
}
@Override
public void onApplicationEvent(RSocketServerInitializedEvent event) {
String propertyName = "local.rsocket.server.port";
setPortProperty(this.applicationContext, propertyName, event.getrSocketServer().address().getPort());
}
private static class Listener implements ApplicationListener<RSocketServerInitializedEvent> {
private static final String PROPERTY_NAME = "local.rsocket.server.port";
private ConfigurableApplicationContext applicationContext;
Listener(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
private void setPortProperty(ApplicationContext context, String propertyName, int port) {
if (context instanceof ConfigurableApplicationContext) {
setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(), propertyName, port);
@Override
public void onApplicationEvent(RSocketServerInitializedEvent event) {
setPortProperty(this.applicationContext, event.getrSocketServer().address().getPort());
}
if (context.getParent() != null) {
setPortProperty(context.getParent(), propertyName, port);
private void setPortProperty(ApplicationContext context, int port) {
if (context instanceof ConfigurableApplicationContext) {
setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(), port);
}
if (context.getParent() != null) {
setPortProperty(context.getParent(), port);
}
}
}
@SuppressWarnings("unchecked")
private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) {
MutablePropertySources sources = environment.getPropertySources();
PropertySource<?> source = sources.get("server.ports");
if (source == null) {
source = new MapPropertySource("server.ports", new HashMap<>());
sources.addFirst(source);
private void setPortProperty(ConfigurableEnvironment environment, int port) {
MutablePropertySources sources = environment.getPropertySources();
PropertySource<?> source = sources.get("server.ports");
if (source == null) {
source = new MapPropertySource("server.ports", new HashMap<>());
sources.addFirst(source);
}
setPortProperty(port, source);
}
((Map<String, Object>) source.getSource()).put(propertyName, port);
@SuppressWarnings("unchecked")
private void setPortProperty(int port, PropertySource<?> source) {
((Map<String, Object>) source.getSource()).put(PROPERTY_NAME, port);
}
}
}
......@@ -16,8 +16,8 @@ org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer,\
org.springframework.boot.web.context.RSocketPortInfoApplicationContextInitializer
org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
# Application Listeners
org.springframework.context.ApplicationListener=\
......
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.web.server;
package org.springframework.boot.rsocket.context;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment