Skip to content

Commit 769d65f

Browse files
committed
Protect against possible NullPointerException when building collections
1 parent d9d81c5 commit 769d65f

File tree

22 files changed

+325
-155
lines changed

22 files changed

+325
-155
lines changed

spring-cloud-app-broker-core/src/main/java/org/springframework/cloud/appbroker/deployer/BackingApplication.java

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.Objects;
2525
import java.util.stream.Collectors;
2626

27+
import org.springframework.util.CollectionUtils;
28+
29+
@SuppressWarnings("PMD.GodClass")
2730
public class BackingApplication {
2831

2932
private static final String VALUE_HIDDEN = "<value hidden>";
@@ -36,26 +39,6 @@ public class BackingApplication {
3639
private List<ParametersTransformerSpec> parametersTransformers;
3740
private List<CredentialProviderSpec> credentialProviders;
3841

39-
public BackingApplication(BackingApplication backingApplicationToCopy) {
40-
this.name = backingApplicationToCopy.name;
41-
this.path = backingApplicationToCopy.path;
42-
this.properties = backingApplicationToCopy.properties == null
43-
? new HashMap<>()
44-
: new HashMap<>(backingApplicationToCopy.properties);
45-
this.environment = backingApplicationToCopy.environment == null
46-
? new HashMap<>()
47-
: new HashMap<>(backingApplicationToCopy.environment);
48-
this.services = backingApplicationToCopy.services == null
49-
? new ArrayList<>()
50-
: backingApplicationToCopy.services.stream().map(ServicesSpec::new).collect(Collectors.toList());
51-
this.parametersTransformers = backingApplicationToCopy.parametersTransformers == null
52-
? new ArrayList<>()
53-
: backingApplicationToCopy.parametersTransformers.stream().map(ParametersTransformerSpec::new).collect(Collectors.toList());
54-
this.credentialProviders = backingApplicationToCopy.credentialProviders == null
55-
? new ArrayList<>()
56-
: backingApplicationToCopy.credentialProviders.stream().map(CredentialProviderSpec::new).collect(Collectors.toList());
57-
}
58-
5942
private BackingApplication() {
6043
}
6144

@@ -193,16 +176,51 @@ private Map<String, Object> sanitizeEnvironment(Map<String, Object> environment)
193176
public static class BackingApplicationBuilder {
194177

195178
private String name;
179+
196180
private String path;
181+
197182
private final Map<String, String> properties = new HashMap<>();
183+
198184
private final Map<String, Object> environment = new HashMap<>();
185+
199186
private final List<ServicesSpec> services = new ArrayList<>();
187+
200188
private final List<ParametersTransformerSpec> parameterTransformers = new ArrayList<>();
189+
201190
private final List<CredentialProviderSpec> credentialProviders = new ArrayList<>();
202191

203192
BackingApplicationBuilder() {
204193
}
205194

195+
public BackingApplicationBuilder backingApplication(BackingApplication backingApplication) {
196+
this.name(backingApplication.getName())
197+
.path(backingApplication.getPath())
198+
.properties(backingApplication.getProperties())
199+
.environment(backingApplication.getEnvironment());
200+
if (!CollectionUtils.isEmpty(backingApplication.getServices())) {
201+
this.services(backingApplication.getServices().stream()
202+
.map(spec -> ServicesSpec.builder()
203+
.spec(spec)
204+
.build())
205+
.collect(Collectors.toList()));
206+
}
207+
if (!CollectionUtils.isEmpty(backingApplication.getParametersTransformers())) {
208+
this.parameterTransformers(backingApplication.getParametersTransformers().stream()
209+
.map(spec -> ParametersTransformerSpec.builder()
210+
.spec(spec)
211+
.build())
212+
.collect(Collectors.toList()));
213+
}
214+
if (!CollectionUtils.isEmpty(backingApplication.getCredentialProviders())) {
215+
this.credentialProviders(backingApplication.getCredentialProviders().stream()
216+
.map(spec -> CredentialProviderSpec.builder()
217+
.spec(spec)
218+
.build())
219+
.collect(Collectors.toList()));
220+
}
221+
return this;
222+
}
223+
206224
public BackingApplicationBuilder name(String name) {
207225
this.name = name;
208226
return this;
@@ -214,37 +232,72 @@ public BackingApplicationBuilder path(String path) {
214232
}
215233

216234
public BackingApplicationBuilder property(String key, String value) {
217-
this.properties.put(key, value);
235+
if (key != null && value != null) {
236+
this.properties.put(key, value);
237+
}
218238
return this;
219239
}
220240

221241
public BackingApplicationBuilder properties(Map<String, String> properties) {
222-
this.properties.putAll(properties);
242+
if (!CollectionUtils.isEmpty(properties)) {
243+
this.properties.putAll(properties);
244+
}
223245
return this;
224246
}
225247

226248
public BackingApplicationBuilder environment(String key, String value) {
227-
this.environment.put(key, value);
249+
if (key != null && value != null) {
250+
this.environment.put(key, value);
251+
}
228252
return this;
229253
}
230254

231-
public BackingApplicationBuilder environment(Map<String, String> environment) {
232-
this.environment.putAll(environment);
255+
public BackingApplicationBuilder environment(Map<String, Object> environment) {
256+
if (!CollectionUtils.isEmpty(environment)) {
257+
this.environment.putAll(environment);
258+
}
259+
return this;
260+
}
261+
262+
public BackingApplicationBuilder services(List<ServicesSpec> services) {
263+
if (!CollectionUtils.isEmpty(services)) {
264+
this.services.addAll(services);
265+
}
233266
return this;
234267
}
235268

236269
public BackingApplicationBuilder services(ServicesSpec... services) {
237-
this.services.addAll(Arrays.asList(services));
270+
if (services != null) {
271+
this.services(Arrays.asList(services));
272+
}
273+
return this;
274+
}
275+
276+
public BackingApplicationBuilder parameterTransformers(List<ParametersTransformerSpec> parameterTransformers) {
277+
if (!CollectionUtils.isEmpty(parameterTransformers)) {
278+
this.parameterTransformers.addAll(parameterTransformers);
279+
}
238280
return this;
239281
}
240282

241283
public BackingApplicationBuilder parameterTransformers(ParametersTransformerSpec... parameterTransformers) {
242-
this.parameterTransformers.addAll(Arrays.asList(parameterTransformers));
284+
if (parameterTransformers != null) {
285+
this.parameterTransformers(Arrays.asList(parameterTransformers));
286+
}
287+
return this;
288+
}
289+
290+
public BackingApplicationBuilder credentialProviders(List<CredentialProviderSpec> credentialProviders) {
291+
if (!CollectionUtils.isEmpty(credentialProviders)) {
292+
this.credentialProviders.addAll(credentialProviders);
293+
}
243294
return this;
244295
}
245296

246297
public BackingApplicationBuilder credentialProviders(CredentialProviderSpec... credentialProviders) {
247-
this.credentialProviders.addAll(Arrays.asList(credentialProviders));
298+
if (credentialProviders != null) {
299+
this.credentialProviders(Arrays.asList(credentialProviders));
300+
}
248301
return this;
249302
}
250303

spring-cloud-app-broker-core/src/main/java/org/springframework/cloud/appbroker/deployer/BackingApplications.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ private BackingApplications() {
3131
super.addAll(backingApplications);
3232
}
3333

34-
public BackingApplications(BackingApplications backingApplicationsToCopy) {
35-
backingApplicationsToCopy.forEach(backingApplicationToCopy ->
36-
this.add(new BackingApplication(backingApplicationToCopy)));
37-
}
38-
3934
public static BackingApplicationsBuilder builder() {
4035
return new BackingApplicationsBuilder();
4136
}
@@ -44,13 +39,17 @@ public static class BackingApplicationsBuilder {
4439
private final List<BackingApplication> backingApplications = new ArrayList<>();
4540

4641
public BackingApplicationsBuilder backingApplication(BackingApplication backingApplication) {
47-
this.backingApplications.add(backingApplication);
42+
if (backingApplication != null) {
43+
this.backingApplications.add(backingApplication);
44+
}
4845
return this;
4946
}
5047

5148
public BackingApplicationsBuilder backingApplications(List<BackingApplication> backingApplications) {
5249
if (!CollectionUtils.isEmpty(backingApplications)) {
53-
this.backingApplications.addAll(backingApplications);
50+
backingApplications.forEach(backingApplication -> this.backingApplication(BackingApplication.builder()
51+
.backingApplication(backingApplication)
52+
.build()));
5453
}
5554
return this;
5655
}

spring-cloud-app-broker-core/src/main/java/org/springframework/cloud/appbroker/deployer/BackingService.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.Objects;
2525

26+
import org.springframework.util.CollectionUtils;
27+
2628
public class BackingService {
2729

2830
private String serviceInstanceName;
@@ -52,22 +54,6 @@ private BackingService() {
5254
this.rebindOnUpdate = rebindOnUpdate;
5355
}
5456

55-
BackingService(BackingService backingServiceToCopy) {
56-
this.serviceInstanceName = backingServiceToCopy.serviceInstanceName;
57-
this.name = backingServiceToCopy.name;
58-
this.plan = backingServiceToCopy.plan;
59-
this.parameters = backingServiceToCopy.parameters == null
60-
? new HashMap<>()
61-
: new HashMap<>(backingServiceToCopy.parameters);
62-
this.properties = backingServiceToCopy.properties == null
63-
? new HashMap<>()
64-
: new HashMap<>(backingServiceToCopy.properties);
65-
this.parametersTransformers = backingServiceToCopy.parametersTransformers == null
66-
? new ArrayList<>()
67-
: new ArrayList<>(backingServiceToCopy.parametersTransformers);
68-
this.rebindOnUpdate = backingServiceToCopy.rebindOnUpdate;
69-
}
70-
7157
public String getServiceInstanceName() {
7258
return serviceInstanceName;
7359
}
@@ -171,14 +157,24 @@ public static final class BackingServiceBuilder {
171157
private String serviceInstanceName;
172158
private String name;
173159
private String plan;
174-
private Map<String, Object> parameters = new HashMap<>();
175-
private Map<String, String> properties = new HashMap<>();
160+
private final Map<String, Object> parameters = new HashMap<>();
161+
private final Map<String, String> properties = new HashMap<>();
176162
private final List<ParametersTransformerSpec> parameterTransformers = new ArrayList<>();
177163
private boolean rebindOnUpdate;
178164

179165
BackingServiceBuilder() {
180166
}
181167

168+
public BackingServiceBuilder backingService(BackingService backingService) {
169+
return this.serviceInstanceName(backingService.getServiceInstanceName())
170+
.name(backingService.getName())
171+
.plan(backingService.getPlan())
172+
.parameters(backingService.getParameters())
173+
.properties(backingService.getProperties())
174+
.parameterTransformers(backingService.getParametersTransformers())
175+
.rebindOnUpdate(backingService.isRebindOnUpdate());
176+
}
177+
182178
public BackingServiceBuilder serviceInstanceName(String serviceInstanceName) {
183179
this.serviceInstanceName = serviceInstanceName;
184180
return this;
@@ -195,17 +191,30 @@ public BackingServiceBuilder plan(String plan) {
195191
}
196192

197193
public BackingServiceBuilder parameters(Map<String, Object> parameters) {
198-
this.parameters = parameters;
194+
if (!CollectionUtils.isEmpty(parameters)) {
195+
this.parameters.putAll(parameters);
196+
}
199197
return this;
200198
}
201199

202200
public BackingServiceBuilder properties(Map<String, String> properties) {
203-
this.properties = properties;
201+
if (!CollectionUtils.isEmpty(properties)) {
202+
this.properties.putAll(properties);
203+
}
204+
return this;
205+
}
206+
207+
public BackingServiceBuilder parameterTransformers(List<ParametersTransformerSpec> parameterTransformers) {
208+
if (!CollectionUtils.isEmpty(parameterTransformers)) {
209+
this.parameterTransformers.addAll(parameterTransformers);
210+
}
204211
return this;
205212
}
206213

207214
public BackingServiceBuilder parameterTransformers(ParametersTransformerSpec... parameterTransformers) {
208-
this.parameterTransformers.addAll(Arrays.asList(parameterTransformers));
215+
if (parameterTransformers != null) {
216+
this.parameterTransformers(Arrays.asList(parameterTransformers));
217+
}
209218
return this;
210219
}
211220

spring-cloud-app-broker-core/src/main/java/org/springframework/cloud/appbroker/deployer/BackingServices.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.springframework.util.CollectionUtils;
23+
2224
public class BackingServices extends ArrayList<BackingService> {
2325

2426
private static final long serialVersionUID = 1L;
@@ -30,11 +32,6 @@ private BackingServices() {
3032
super.addAll(backingServices);
3133
}
3234

33-
public BackingServices(BackingServices backingServicesToCopy) {
34-
backingServicesToCopy.forEach(backingServiceToCopy ->
35-
this.add(new BackingService(backingServiceToCopy)));
36-
}
37-
3835
public static BackingServicesBuilder builder() {
3936
return new BackingServicesBuilder();
4037
}
@@ -44,7 +41,18 @@ public static class BackingServicesBuilder {
4441
private final List<BackingService> backingServices = new ArrayList<>();
4542

4643
public BackingServicesBuilder backingService(BackingService backingService) {
47-
this.backingServices.add(backingService);
44+
if (backingService != null) {
45+
this.backingServices.add(backingService);
46+
}
47+
return this;
48+
}
49+
50+
public BackingServicesBuilder backingServices(BackingServices backingServices) {
51+
if (!CollectionUtils.isEmpty(backingServices)) {
52+
backingServices.forEach(backingService -> this.backingService(BackingService.builder()
53+
.backingService(backingService)
54+
.build()));
55+
}
4856
return this;
4957
}
5058

spring-cloud-app-broker-core/src/main/java/org/springframework/cloud/appbroker/deployer/BrokeredService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.Objects;
2020

21+
import org.springframework.util.CollectionUtils;
22+
2123
public class BrokeredService {
2224

2325
private String serviceName;
@@ -132,12 +134,20 @@ public BrokeredServiceBuilder planName(String planId) {
132134
}
133135

134136
public BrokeredServiceBuilder apps(BackingApplications backingApplications) {
135-
this.backingApplications = backingApplications;
137+
if (!CollectionUtils.isEmpty(backingApplications)) {
138+
this.backingApplications = BackingApplications.builder()
139+
.backingApplications(backingApplications)
140+
.build();
141+
}
136142
return this;
137143
}
138144

139145
public BrokeredServiceBuilder services(BackingServices backingServices) {
140-
this.backingServices = backingServices;
146+
if (!CollectionUtils.isEmpty(backingServices)) {
147+
this.backingServices = BackingServices.builder()
148+
.backingServices(backingServices)
149+
.build();
150+
}
141151
return this;
142152
}
143153

spring-cloud-app-broker-core/src/main/java/org/springframework/cloud/appbroker/deployer/BrokeredServices.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.springframework.util.CollectionUtils;
23+
2224
public class BrokeredServices extends ArrayList<BrokeredService> {
2325
private static final long serialVersionUID = 6303127383252611352L;
2426

@@ -37,7 +39,16 @@ public static class BrokeredServicesBuilder {
3739
private final List<BrokeredService> brokeredServices = new ArrayList<>();
3840

3941
public BrokeredServicesBuilder service(BrokeredService brokeredService) {
40-
this.brokeredServices.add(brokeredService);
42+
if (brokeredService != null) {
43+
this.brokeredServices.add(brokeredService);
44+
}
45+
return this;
46+
}
47+
48+
public BrokeredServicesBuilder services(BrokeredServices brokeredServices) {
49+
if (!CollectionUtils.isEmpty(brokeredServices)) {
50+
this.brokeredServices.addAll(brokeredServices);
51+
}
4152
return this;
4253
}
4354

0 commit comments

Comments
 (0)