From 22f5b7a15e47f53ae59a2218a59da99ad16d4ed3 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 24 Feb 2016 14:07:21 +0000 Subject: [PATCH] Make AggregateApplicationBuilder more fluent So you can add a parent for instance, like this: new AggregateApplicationBuilder().from(TwitterStreamApplication.class) .args(new String[] { "--language=en" }).to(StreamTransformer.class) .parent( SpringApplication.run(ModuleApplication.class, args)) .run(); --- .../AggregateApplicationBuilder.java | 115 ++++++++---------- 1 file changed, 50 insertions(+), 65 deletions(-) diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/aggregate/AggregateApplicationBuilder.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/aggregate/AggregateApplicationBuilder.java index c038fbd8d..347f00732 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/aggregate/AggregateApplicationBuilder.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/aggregate/AggregateApplicationBuilder.java @@ -41,6 +41,8 @@ public class AggregateApplicationBuilder { private AggregateApplicationBuilder applicationBuilder = this; + ConfigurableApplicationContext parentContext; + public SourceConfigurer from(Class app) { SourceConfigurer sourceConfigurer = new SourceConfigurer(app); this.sourceConfigurer = sourceConfigurer; @@ -48,9 +50,12 @@ public class AggregateApplicationBuilder { } public void run(String[] parentArgs) { - ConfigurableApplicationContext parentContext = AggregateApplication.createParentContext(parentArgs); - SharedChannelRegistry sharedChannelRegistry = parentContext.getBean(SharedChannelRegistry.class); - List apps = new ArrayList(); + ConfigurableApplicationContext parentContext = this.parentContext != null + ? this.parentContext + : AggregateApplication.createParentContext(parentArgs); + SharedChannelRegistry sharedChannelRegistry = parentContext + .getBean(SharedChannelRegistry.class); + List> apps = new ArrayList>(); if (this.sourceConfigurer != null) { apps.add(sourceConfigurer); } @@ -66,38 +71,24 @@ public class AggregateApplicationBuilder { for (int i = 0; i < apps.size(); i++) { appsToEmbed.add(apps.get(i).getApp()); } - AggregateApplication.prepareSharedChannelRegistry(sharedChannelRegistry, appsToEmbed.toArray(new Class[0])); + AggregateApplication.prepareSharedChannelRegistry(sharedChannelRegistry, + appsToEmbed.toArray(new Class[0])); for (int i = apps.size() - 1; i >= 0; i--) { - AppConfigurer appConfigurer = apps.get(i); - appConfigurer.setParentContext(parentContext); - appConfigurer.setNamespace(AggregateApplication.getNamespace(appConfigurer.getApp().getName(), i)); + AppConfigurer appConfigurer = apps.get(i); + appConfigurer.parent(parentContext); + appConfigurer.namespace(AggregateApplication + .getNamespace(appConfigurer.getApp().getName(), i)); appConfigurer.embed(); } } - - public class SourceConfigurer extends AppConfigurer { + public class SourceConfigurer extends AppConfigurer { public SourceConfigurer(Class app) { this.app = app; sourceConfigurer = this; } - public SourceConfigurer as(String... names) { - this.names = names; - return this; - } - - public SourceConfigurer args(String... args) { - this.args = args; - return this; - } - - public SourceConfigurer profiles(String... profiles) { - this.profiles = profiles; - return this; - } - public SinkConfigurer to(Class sink) { return new SinkConfigurer(sink); } @@ -108,52 +99,22 @@ public class AggregateApplicationBuilder { } - public class SinkConfigurer extends AppConfigurer { + public class SinkConfigurer extends AppConfigurer { public SinkConfigurer(Class app) { this.app = app; sinkConfigurer = this; } - public SinkConfigurer as(String... names) { - this.names = names; - return this; - } - - public SinkConfigurer args(String... args) { - this.args = args; - return this; - } - - public SinkConfigurer profiles(String... profiles) { - this.profiles = profiles; - return this; - } - } - public class ProcessorConfigurer extends AppConfigurer { + public class ProcessorConfigurer extends AppConfigurer { public ProcessorConfigurer(Class app) { this.app = app; processorConfigurers.add(this); } - public ProcessorConfigurer as(String... names) { - this.names = names; - return this; - } - - public ProcessorConfigurer args(String... args) { - this.args = args; - return this; - } - - public ProcessorConfigurer profiles(String... profiles) { - this.profiles = profiles; - return this; - } - public SinkConfigurer to(Class sink) { return new SinkConfigurer(sink); } @@ -164,7 +125,7 @@ public class AggregateApplicationBuilder { } - private abstract class AppConfigurer { + public abstract class AppConfigurer> { Class app; @@ -174,20 +135,40 @@ public class AggregateApplicationBuilder { String[] profiles = null; - ConfigurableApplicationContext parentContext; - String namespace; Class getApp() { return this.app; } - public void setParentContext(ConfigurableApplicationContext parentContext) { - this.parentContext = parentContext; + public T as(String... names) { + this.names = names; + return getConfigurer(); } - public void setNamespace(String namespace) { + public T args(String... args) { + this.args = args; + return getConfigurer(); + } + + public T profiles(String... profiles) { + this.profiles = profiles; + return getConfigurer(); + } + + public T parent(ConfigurableApplicationContext parentContext) { + AggregateApplicationBuilder.this.parentContext = parentContext; + return getConfigurer(); + } + + @SuppressWarnings("unchecked") + private T getConfigurer() { + return (T) this; + } + + public T namespace(String namespace) { this.namespace = namespace; + return getConfigurer(); } public void run(String... args) { @@ -195,12 +176,16 @@ public class AggregateApplicationBuilder { } void embed() { - childContext(this.app, this.parentContext, this.namespace).args(this.args).config(this.names).profiles(this.profiles).run(); + childContext(this.app, AggregateApplicationBuilder.this.parentContext, + this.namespace).args(this.args).config(this.names) + .profiles(this.profiles).run(); } } - private ChildContextBuilder childContext(Class app, ConfigurableApplicationContext parentContext, String namespace) { - return new ChildContextBuilder(AggregateApplication.embedApp(parentContext, namespace, app)); + private ChildContextBuilder childContext(Class app, + ConfigurableApplicationContext parentContext, String namespace) { + return new ChildContextBuilder( + AggregateApplication.embedApp(parentContext, namespace, app)); } private class ChildContextBuilder {