Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit a490008

Browse files
olegzericbottard
authored andcommitted
GH-1495 general code cleanup
- gathered repetative code from implementations of various strategies (RecoveryStrategy, ExpansionStrategy) into ProposalsCollectorSupport - refactored UnfinishedConfigurationPropertyNameTaskRecoveryStrategy, UnfinishedConfigurationPropertyNameRecoveryStrategy, ConfigurationPropertyNameAfterDashDashTaskRecoveryStrategy, ConfigurationPropertyNameAfterDashDashRecoveryStrategy, AddAppOptionsTaskExpansionStrategy and AddAppOptionsExpansionStrategy to use the mentioned support class
1 parent 876db98 commit a490008

8 files changed

+153
-285
lines changed

spring-cloud-dataflow-completion/src/main/java/org/springframework/cloud/dataflow/completion/AddAppOptionsExpansionStrategy.java

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,79 +20,39 @@
2020
import java.util.List;
2121
import java.util.Set;
2222

23-
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
2423
import org.springframework.cloud.dataflow.configuration.metadata.ApplicationConfigurationMetadataResolver;
25-
import org.springframework.cloud.dataflow.core.ApplicationType;
2624
import org.springframework.cloud.dataflow.core.StreamAppDefinition;
2725
import org.springframework.cloud.dataflow.core.StreamDefinition;
2826
import org.springframework.cloud.dataflow.registry.AppRegistration;
2927
import org.springframework.cloud.dataflow.registry.AppRegistry;
30-
import org.springframework.core.io.Resource;
31-
32-
import static org.springframework.cloud.dataflow.completion.CompletionProposal.expanding;
3328

3429
/**
3530
* Adds missing application configuration properties at the end of a well formed stream
3631
* definition.
3732
*
3833
* @author Eric Bottard
3934
* @author Mark Fisher
35+
* @author Oleg Zhurakousky
4036
*/
4137
class AddAppOptionsExpansionStrategy implements ExpansionStrategy {
4238

43-
private final AppRegistry appRegistry;
44-
45-
private final ApplicationConfigurationMetadataResolver metadataResolver;
39+
private final ProposalsCollectorSupportUtils collectorSupport;
4640

4741
public AddAppOptionsExpansionStrategy(AppRegistry appRegistry,
4842
ApplicationConfigurationMetadataResolver metadataResolver) {
49-
this.appRegistry = appRegistry;
50-
this.metadataResolver = metadataResolver;
43+
this.collectorSupport = new ProposalsCollectorSupportUtils(appRegistry, metadataResolver);
5144
}
5245

5346
@Override
5447
public boolean addProposals(String text, StreamDefinition streamDefinition, int detailLevel,
5548
List<CompletionProposal> collector) {
5649
StreamAppDefinition lastApp = streamDefinition.getDeploymentOrderIterator().next();
50+
AppRegistration appRegistration = this.collectorSupport.findAppRegistration(lastApp.getName(), CompletionUtils.determinePotentialTypes(lastApp));
5751

58-
String lastAppName = lastApp.getName();
59-
AppRegistration lastAppRegistration = null;
60-
for (ApplicationType appType : CompletionUtils.determinePotentialTypes(lastApp)) {
61-
lastAppRegistration = this.appRegistry.find(lastAppName, appType);
62-
if (lastAppRegistration != null) {
63-
break;
64-
}
65-
}
66-
if (lastAppRegistration == null) {
67-
// Not a valid app name, do nothing
68-
return false;
69-
}
70-
Set<String> alreadyPresentOptions = new HashSet<>(lastApp.getProperties().keySet());
71-
72-
Resource metadataResource = lastAppRegistration.getMetadataResource();
73-
74-
CompletionProposal.Factory proposals = expanding(text);
75-
76-
// For whitelisted properties, use their simple name
77-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource)) {
78-
if (!alreadyPresentOptions.contains(property.getName())) {
79-
collector.add(
80-
proposals.withSeparateTokens("--" + property.getName() + "=", property.getShortDescription()));
81-
}
82-
}
83-
84-
// For other properties (including WL'ed in full form), use their id
85-
if (detailLevel > 1) {
86-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource, true)) {
87-
if (!alreadyPresentOptions.contains(property.getId())) {
88-
collector.add(proposals.withSeparateTokens("--" + property.getId() + "=",
89-
property.getShortDescription()));
90-
}
91-
}
92-
52+
if (appRegistration != null) {
53+
Set<String> alreadyPresentOptions = new HashSet<>(lastApp.getProperties().keySet());
54+
this.collectorSupport.doAddProposals(text, "", appRegistration, alreadyPresentOptions, collector, detailLevel);
9355
}
94-
9556
return false;
9657
}
97-
9858
}

spring-cloud-dataflow-completion/src/main/java/org/springframework/cloud/dataflow/completion/AddAppOptionsTaskExpansionStrategy.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@
2020
import java.util.List;
2121
import java.util.Set;
2222

23-
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
2423
import org.springframework.cloud.dataflow.configuration.metadata.ApplicationConfigurationMetadataResolver;
2524
import org.springframework.cloud.dataflow.core.ApplicationType;
2625
import org.springframework.cloud.dataflow.core.TaskDefinition;
2726
import org.springframework.cloud.dataflow.registry.AppRegistration;
2827
import org.springframework.cloud.dataflow.registry.AppRegistry;
29-
import org.springframework.core.io.Resource;
30-
31-
import static org.springframework.cloud.dataflow.completion.CompletionProposal.expanding;
3228

3329
/**
3430
* Adds missing application configuration properties at the end of a well formed task
@@ -37,52 +33,28 @@
3733
* @author Eric Bottard
3834
* @author Mark Fisher
3935
* @author Andy Clement
36+
* @author Oleg Zhurakousky
4037
*/
4138
class AddAppOptionsTaskExpansionStrategy implements TaskExpansionStrategy {
4239

43-
private final AppRegistry appRegistry;
44-
45-
private final ApplicationConfigurationMetadataResolver metadataResolver;
40+
private final ProposalsCollectorSupportUtils collectorSupport;
4641

4742
public AddAppOptionsTaskExpansionStrategy(AppRegistry appRegistry,
4843
ApplicationConfigurationMetadataResolver metadataResolver) {
49-
this.appRegistry = appRegistry;
50-
this.metadataResolver = metadataResolver;
44+
this.collectorSupport = new ProposalsCollectorSupportUtils(appRegistry, metadataResolver);
5145
}
5246

5347
@Override
5448
public boolean addProposals(String text, TaskDefinition taskDefinition, int detailLevel,
5549
List<CompletionProposal> collector) {
5650
String appName = taskDefinition.getRegisteredAppName();
57-
AppRegistration appRegistration = this.appRegistry.find(appName, ApplicationType.task);
58-
if (appRegistration == null) {
59-
// Not a valid app, do nothing
60-
return false;
61-
}
62-
Set<String> alreadyPresentOptions = new HashSet<>(taskDefinition.getProperties().keySet());
63-
Resource metadataResource = appRegistration.getMetadataResource();
64-
CompletionProposal.Factory proposals = expanding(text);
6551

66-
// For whitelisted properties, use their simple name
67-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource)) {
68-
if (!alreadyPresentOptions.contains(property.getName())) {
69-
collector.add(
70-
proposals.withSeparateTokens("--" + property.getName() + "=", property.getShortDescription()));
71-
}
72-
}
73-
74-
// For other properties (including WL'ed in full form), use their id
75-
if (detailLevel > 1) {
76-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource, true)) {
77-
if (!alreadyPresentOptions.contains(property.getId())) {
78-
collector.add(proposals.withSeparateTokens("--" + property.getId() + "=",
79-
property.getShortDescription()));
80-
}
81-
}
52+
AppRegistration appRegistration = this.collectorSupport.findAppRegistration(appName, ApplicationType.task);
8253

54+
if (appRegistration != null) {
55+
Set<String> alreadyPresentOptions = new HashSet<>(taskDefinition.getProperties().keySet());
56+
this.collectorSupport.doAddProposals(text, "", appRegistration, alreadyPresentOptions, collector, detailLevel);
8357
}
84-
8558
return false;
8659
}
87-
8860
}

spring-cloud-dataflow-completion/src/main/java/org/springframework/cloud/dataflow/completion/ConfigurationPropertyNameAfterDashDashRecoveryStrategy.java

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,30 @@
2020
import java.util.List;
2121
import java.util.Set;
2222

23-
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
2423
import org.springframework.cloud.dataflow.configuration.metadata.ApplicationConfigurationMetadataResolver;
25-
import org.springframework.cloud.dataflow.core.ApplicationType;
2624
import org.springframework.cloud.dataflow.core.StreamAppDefinition;
2725
import org.springframework.cloud.dataflow.core.StreamDefinition;
2826
import org.springframework.cloud.dataflow.core.dsl.CheckPointedParseException;
2927
import org.springframework.cloud.dataflow.registry.AppRegistration;
3028
import org.springframework.cloud.dataflow.registry.AppRegistry;
31-
import org.springframework.core.io.Resource;
32-
33-
import static org.springframework.cloud.dataflow.completion.CompletionProposal.expanding;
3429

3530
/**
3631
* Provides completion proposals when the user has typed the two dashes that precede an
3732
* app configuration property.
3833
*
3934
* @author Eric Bottard
4035
* @author Mark Fisher
36+
* @author Oleg Zhurakousky
4137
*/
4238
class ConfigurationPropertyNameAfterDashDashRecoveryStrategy
4339
extends StacktraceFingerprintingRecoveryStrategy<CheckPointedParseException> {
4440

45-
private final AppRegistry appRegistry;
46-
47-
private final ApplicationConfigurationMetadataResolver metadataResolver;
41+
private final ProposalsCollectorSupportUtils collectorSupport;
4842

4943
ConfigurationPropertyNameAfterDashDashRecoveryStrategy(AppRegistry appRegistry,
5044
ApplicationConfigurationMetadataResolver metadataResolver) {
5145
super(CheckPointedParseException.class, "file --", "file | foo --");
52-
this.appRegistry = appRegistry;
53-
this.metadataResolver = metadataResolver;
46+
this.collectorSupport = new ProposalsCollectorSupportUtils(appRegistry, metadataResolver);
5447
}
5548

5649
@Override
@@ -61,38 +54,10 @@ public void addProposals(String dsl, CheckPointedParseException exception, int d
6154
StreamDefinition streamDefinition = new StreamDefinition("__dummy", safe);
6255
StreamAppDefinition lastApp = streamDefinition.getDeploymentOrderIterator().next();
6356

64-
String lastAppName = lastApp.getName();
65-
AppRegistration lastAppRegistration = null;
66-
for (ApplicationType appType : CompletionUtils.determinePotentialTypes(lastApp)) {
67-
lastAppRegistration = appRegistry.find(lastAppName, appType);
68-
if (lastAppRegistration != null) {
69-
break;
70-
}
71-
}
72-
if (lastAppRegistration == null) {
73-
// Not a valid app name, do nothing
74-
return;
75-
}
76-
Set<String> alreadyPresentOptions = new HashSet<>(lastApp.getProperties().keySet());
77-
78-
Resource metadataResource = lastAppRegistration.getMetadataResource();
79-
80-
CompletionProposal.Factory proposals = expanding(dsl);
81-
82-
// For whitelisted properties, use their shortname
83-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource)) {
84-
if (!alreadyPresentOptions.contains(property.getName())) {
85-
collector.add(proposals.withSuffix(property.getName() + "=", property.getShortDescription()));
86-
}
87-
}
88-
89-
// For other properties, use their fully qualified name
90-
if (detailLevel > 1) {
91-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource, true)) {
92-
if (!alreadyPresentOptions.contains(property.getId())) {
93-
collector.add(proposals.withSuffix(property.getId() + "=", property.getShortDescription()));
94-
}
95-
}
57+
AppRegistration appRegistration = this.collectorSupport.findAppRegistration(lastApp.getName(), CompletionUtils.determinePotentialTypes(lastApp));
58+
if (appRegistration != null) {
59+
Set<String> alreadyPresentOptions = new HashSet<>(lastApp.getProperties().keySet());
60+
this.collectorSupport.doAddProposals(safe, "", appRegistration, alreadyPresentOptions, collector, detailLevel);
9661
}
9762
}
9863
}

spring-cloud-dataflow-completion/src/main/java/org/springframework/cloud/dataflow/completion/ConfigurationPropertyNameAfterDashDashTaskRecoveryStrategy.java

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@
2020
import java.util.List;
2121
import java.util.Set;
2222

23-
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
2423
import org.springframework.cloud.dataflow.configuration.metadata.ApplicationConfigurationMetadataResolver;
2524
import org.springframework.cloud.dataflow.core.ApplicationType;
2625
import org.springframework.cloud.dataflow.core.TaskDefinition;
2726
import org.springframework.cloud.dataflow.core.dsl.CheckPointedParseException;
2827
import org.springframework.cloud.dataflow.registry.AppRegistration;
2928
import org.springframework.cloud.dataflow.registry.AppRegistry;
30-
import org.springframework.core.io.Resource;
31-
32-
import static org.springframework.cloud.dataflow.completion.CompletionProposal.expanding;
3329

3430
/**
3531
* Provides completion proposals when the user has typed the two dashes that precede an
@@ -38,19 +34,17 @@
3834
* @author Eric Bottard
3935
* @author Mark Fisher
4036
* @author Andy Clement
37+
* @author Oleg Zhurakousky
4138
*/
4239
class ConfigurationPropertyNameAfterDashDashTaskRecoveryStrategy
4340
extends StacktraceFingerprintingTaskRecoveryStrategy<CheckPointedParseException> {
4441

45-
private final AppRegistry appRegistry;
46-
47-
private final ApplicationConfigurationMetadataResolver metadataResolver;
42+
private final ProposalsCollectorSupportUtils collectorSupport;
4843

4944
ConfigurationPropertyNameAfterDashDashTaskRecoveryStrategy(AppRegistry appRegistry,
5045
ApplicationConfigurationMetadataResolver metadataResolver) {
5146
super(CheckPointedParseException.class, "file --");
52-
this.appRegistry = appRegistry;
53-
this.metadataResolver = metadataResolver;
47+
this.collectorSupport = new ProposalsCollectorSupportUtils(appRegistry, metadataResolver);
5448
}
5549

5650
@Override
@@ -60,32 +54,11 @@ public void addProposals(String dsl, CheckPointedParseException exception, int d
6054
String safe = exception.getExpressionStringUntilCheckpoint();
6155
TaskDefinition taskDefinition = new TaskDefinition("__dummy", safe);
6256

63-
String appName = taskDefinition.getRegisteredAppName();
64-
AppRegistration appRegistration = appRegistry.find(appName, ApplicationType.task);
65-
if (appRegistration == null) {
66-
// Not a valid app name, do nothing
67-
return;
68-
}
69-
Set<String> alreadyPresentOptions = new HashSet<>(taskDefinition.getProperties().keySet());
70-
71-
Resource metadataResource = appRegistration.getMetadataResource();
72-
73-
CompletionProposal.Factory proposals = expanding(dsl);
74-
75-
// For whitelisted properties, use their shortname
76-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource)) {
77-
if (!alreadyPresentOptions.contains(property.getName())) {
78-
collector.add(proposals.withSuffix(property.getName() + "=", property.getShortDescription()));
79-
}
80-
}
57+
AppRegistration appRegistration = this.collectorSupport.findAppRegistration(taskDefinition.getRegisteredAppName(), ApplicationType.task);
8158

82-
// For other properties, use their fully qualified name
83-
if (detailLevel > 1) {
84-
for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource, true)) {
85-
if (!alreadyPresentOptions.contains(property.getId())) {
86-
collector.add(proposals.withSuffix(property.getId() + "=", property.getShortDescription()));
87-
}
88-
}
59+
if (appRegistration != null) {
60+
Set<String> alreadyPresentOptions = new HashSet<>(taskDefinition.getProperties().keySet());
61+
this.collectorSupport.doAddProposals(safe, "", appRegistration, alreadyPresentOptions, collector, detailLevel);
8962
}
9063
}
9164
}

0 commit comments

Comments
 (0)