Skip to content

Commit ff91175

Browse files
committed
Introduce RequestContext
1 parent fd68d22 commit ff91175

25 files changed

+459
-231
lines changed

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AbstractScmEnvironmentRepository.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.cloud.config.environment.Environment;
2222
import org.springframework.cloud.config.server.support.AbstractScmAccessor;
2323
import org.springframework.cloud.config.server.support.AbstractScmAccessorProperties;
24+
import org.springframework.cloud.config.server.support.RequestContext;
2425
import org.springframework.core.Ordered;
2526
import org.springframework.core.env.ConfigurableEnvironment;
2627

@@ -57,17 +58,18 @@ public synchronized Environment findOne(String application, String profile, Stri
5758

5859
@Override
5960
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin) {
60-
return findOne(application, profile, label, includeOrigin, false);
61+
return findOne(application, profile, label, includeOrigin,
62+
new RequestContext.Builder().forceRefresh(false).build());
6163
}
6264

6365
@Override
6466
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin,
65-
boolean forceRefresh) {
67+
RequestContext ctx) {
6668
NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(getEnvironment(),
6769
new NativeEnvironmentProperties(), this.observationRegistry);
68-
Locations locations = getLocations(application, profile, label, forceRefresh);
70+
Locations locations = getLocations(application, profile, label, ctx);
6971
delegate.setSearchLocations(locations.getLocations());
70-
Environment result = delegate.findOne(application, profile, "", includeOrigin, forceRefresh);
72+
Environment result = delegate.findOne(application, profile, "", includeOrigin, ctx);
7173
result.setVersion(locations.getVersion());
7274
result.setLabel(label);
7375
return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(), getUri());

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/CompositeEnvironmentRepository.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.commons.logging.LogFactory;
2626

2727
import org.springframework.cloud.config.environment.Environment;
28+
import org.springframework.cloud.config.server.support.RequestContext;
2829
import org.springframework.core.OrderComparator;
2930

3031
/**
@@ -77,25 +78,25 @@ public Environment findOne(String application, String profile, String label) {
7778

7879
@Override
7980
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
80-
return findOne(application, profile, label, includeOrigin, false);
81+
return findOne(application, profile, label, includeOrigin,
82+
new RequestContext.Builder().forceRefresh(false).build());
8183
}
8284

8385
@Override
8486
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
85-
boolean forceRefresh) {
87+
RequestContext ctx) {
8688
Environment env = new Environment(application, new String[] { profile }, label, null, null);
8789
if (this.environmentRepositories.size() == 1) {
8890
Environment envRepo = this.environmentRepositories.get(0).findOne(application, profile, label,
89-
includeOrigin, forceRefresh);
91+
includeOrigin, ctx);
9092
env.addAll(envRepo.getPropertySources());
9193
env.setVersion(envRepo.getVersion());
9294
env.setState(envRepo.getState());
9395
}
9496
else {
9597
for (EnvironmentRepository repo : environmentRepositories) {
9698
try {
97-
env.addAll(repo.findOne(application, profile, label, includeOrigin, forceRefresh)
98-
.getPropertySources());
99+
env.addAll(repo.findOne(application, profile, label, includeOrigin, ctx).getPropertySources());
99100
}
100101
catch (Exception e) {
101102
if (failOnError) {

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.cloud.config.environment.EnvironmentMediaType;
3838
import org.springframework.cloud.config.environment.PropertySource;
3939
import org.springframework.cloud.config.server.support.PathUtils;
40+
import org.springframework.cloud.config.server.support.RequestContext;
4041
import org.springframework.http.HttpHeaders;
4142
import org.springframework.http.HttpStatus;
4243
import org.springframework.http.MediaType;
@@ -108,34 +109,38 @@ public void setAcceptEmpty(boolean acceptEmpty) {
108109
produces = MediaType.APPLICATION_JSON_VALUE)
109110
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles,
110111
@RequestParam(defaultValue = "false") boolean forceRefresh) {
111-
return getEnvironment(name, profiles, null, false, forceRefresh);
112+
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
113+
return getEnvironment(name, profiles, null, false, ctx);
112114
}
113115

114116
@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
115117
produces = EnvironmentMediaType.V2_JSON)
116118
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
117119
@RequestParam(defaultValue = "false") boolean forceRefresh) {
118-
return getEnvironment(name, profiles, null, true, forceRefresh);
120+
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
121+
return getEnvironment(name, profiles, null, true, ctx);
119122
}
120123

121124
@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = MediaType.APPLICATION_JSON_VALUE)
122125
public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label,
123126
@RequestParam(defaultValue = "false") boolean forceRefresh) {
124-
return getEnvironment(name, profiles, label, false, forceRefresh);
127+
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
128+
return getEnvironment(name, profiles, label, false, ctx);
125129
}
126130

127131
@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = EnvironmentMediaType.V2_JSON)
128132
public Environment labelledIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
129133
@PathVariable String label, @RequestParam(defaultValue = "false") boolean forceRefresh) {
130-
return getEnvironment(name, profiles, label, true, forceRefresh);
134+
RequestContext ctx = new RequestContext.Builder().forceRefresh(forceRefresh).build();
135+
return getEnvironment(name, profiles, label, true, ctx);
131136
}
132137

133138
public Environment getEnvironment(String name, String profiles, String label, boolean includeOrigin,
134-
boolean forceRefresh) {
139+
RequestContext ctx) {
135140
try {
136141
name = normalize(name);
137142
label = normalize(label);
138-
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin, forceRefresh);
143+
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin, ctx);
139144
if (!this.acceptEmpty && (environment == null || environment.getPropertySources().isEmpty())) {
140145
throw new EnvironmentNotFoundException("Profile Not found");
141146
}

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentEncryptorEnvironmentRepository.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.cloud.config.environment.PropertySource;
2828
import org.springframework.cloud.config.environment.PropertyValueDescriptor;
2929
import org.springframework.cloud.config.server.encryption.EnvironmentEncryptor;
30+
import org.springframework.cloud.config.server.support.RequestContext;
3031

3132
/**
3233
* A delegating {@link EnvironmentRepository} that can decrypt the properties if an
@@ -61,13 +62,12 @@ public Environment findOne(String name, String profiles, String label) {
6162

6263
@Override
6364
public Environment findOne(String name, String profiles, String label, boolean includeOrigin) {
64-
return findOne(name, profiles, label, includeOrigin, false);
65+
return findOne(name, profiles, label, includeOrigin, new RequestContext.Builder().forceRefresh(false).build());
6566
}
6667

6768
@Override
68-
public Environment findOne(String name, String profiles, String label, boolean includeOrigin,
69-
boolean forceRefresh) {
70-
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin, forceRefresh);
69+
public Environment findOne(String name, String profiles, String label, boolean includeOrigin, RequestContext ctx) {
70+
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin, ctx);
7171
if (this.environmentEncryptors != null) {
7272
for (EnvironmentEncryptor environmentEncryptor : environmentEncryptors) {
7373
environment = environmentEncryptor.decrypt(environment);

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.cloud.config.server.environment;
1818

1919
import org.springframework.cloud.config.environment.Environment;
20+
import org.springframework.cloud.config.server.support.RequestContext;
2021

2122
/**
2223
* @author Dave Syer
@@ -31,7 +32,7 @@ default Environment findOne(String application, String profile, String label, bo
3132
}
3233

3334
default Environment findOne(String application, String profile, String label, boolean includeOrigin,
34-
boolean forceRefresh) {
35+
RequestContext ctx) {
3536
return findOne(application, profile, label, includeOrigin);
3637
}
3738

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
import org.springframework.beans.factory.InitializingBean;
5959
import org.springframework.cloud.config.server.support.GitCredentialsProviderFactory;
60+
import org.springframework.cloud.config.server.support.RequestContext;
6061
import org.springframework.core.env.ConfigurableEnvironment;
6162
import org.springframework.core.io.UrlResource;
6263
import org.springframework.util.Assert;
@@ -269,24 +270,24 @@ public void setSkipSslValidation(boolean skipSslValidation) {
269270

270271
@Override
271272
public synchronized Locations getLocations(String application, String profile, String label) {
272-
return getLocations(application, profile, label, false);
273+
return getLocations(application, profile, label, new RequestContext.Builder().forceRefresh(false).build());
273274
}
274275

275276
@Override
276-
public synchronized Locations getLocations(String application, String profile, String label, boolean forceRefresh) {
277+
public synchronized Locations getLocations(String application, String profile, String label, RequestContext ctx) {
277278
if (label == null) {
278279
label = this.defaultLabel;
279280
}
280281
String version;
281282
try {
282-
version = refresh(label, forceRefresh);
283+
version = refresh(label, ctx.getForceRefresh());
283284
}
284285
catch (Exception e) {
285286
if (this.defaultLabel.equals(label) && JGitEnvironmentProperties.MAIN_LABEL.equals(this.defaultLabel)
286287
&& tryMasterBranch) {
287288
logger.info("Could not refresh default label " + label, e);
288289
logger.info("Will try to refresh master label instead.");
289-
version = refresh(JGitEnvironmentProperties.MASTER_LABEL, forceRefresh);
290+
version = refresh(JGitEnvironmentProperties.MASTER_LABEL, ctx.getForceRefresh());
290291
}
291292
else {
292293
throw e;
@@ -488,6 +489,7 @@ private Ref checkout(Git git, String label) throws GitAPIException {
488489
}
489490

490491
protected boolean shouldPull(Git git, boolean forceRefresh) throws GitAPIException {
492+
System.out.println(forceRefresh);
491493
boolean shouldPull;
492494
if (!(this.allowForceRefresh && forceRefresh) && (this.refreshRate < 0 || (this.refreshRate > 0
493495
&& System.currentTimeMillis() - this.lastRefresh < (this.refreshRate * 1000)))) {

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/MultipleJGitEnvironmentRepository.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.beans.BeanUtils;
3030
import org.springframework.cloud.config.environment.Environment;
31+
import org.springframework.cloud.config.server.support.RequestContext;
3132
import org.springframework.core.env.ConfigurableEnvironment;
3233
import org.springframework.util.PatternMatchUtils;
3334
import org.springframework.util.StringUtils;
@@ -119,14 +120,14 @@ public void setRepos(Map<String, PatternMatchingJGitEnvironmentRepository> repos
119120
}
120121

121122
@Override
122-
public Locations getLocations(String application, String profile, String label, boolean forceRefresh) {
123+
public Locations getLocations(String application, String profile, String label, RequestContext ctx) {
123124
for (PatternMatchingJGitEnvironmentRepository repository : this.repos.values()) {
124125
if (repository.matches(application, profile, label)) {
125126
for (JGitEnvironmentRepository candidate : getRepositories(repository, application, profile, label)) {
126127
try {
127-
Environment source = candidate.findOne(application, profile, label, false, forceRefresh);
128+
Environment source = candidate.findOne(application, profile, label, false, ctx);
128129
if (source != null) {
129-
return candidate.getLocations(application, profile, label, forceRefresh);
130+
return candidate.getLocations(application, profile, label, ctx);
130131
}
131132
}
132133
catch (Exception e) {
@@ -141,23 +142,22 @@ public Locations getLocations(String application, String profile, String label,
141142
}
142143
JGitEnvironmentRepository candidate = getRepository(this, application, profile, label);
143144
if (candidate == this) {
144-
return super.getLocations(application, profile, label, forceRefresh);
145+
return super.getLocations(application, profile, label, ctx);
145146
}
146-
return candidate.getLocations(application, profile, label, forceRefresh);
147+
return candidate.getLocations(application, profile, label, ctx);
147148
}
148149

149150
@Override
150151
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
151-
boolean forceRefresh) {
152+
RequestContext ctx) {
152153
for (PatternMatchingJGitEnvironmentRepository repository : this.repos.values()) {
153154
if (repository.matches(application, profile, label)) {
154155
for (JGitEnvironmentRepository candidate : getRepositories(repository, application, profile, label)) {
155156
try {
156157
if (label == null) {
157158
label = candidate.getDefaultLabel();
158159
}
159-
Environment source = candidate.findOne(application, profile, label, includeOrigin,
160-
forceRefresh);
160+
Environment source = candidate.findOne(application, profile, label, includeOrigin, ctx);
161161
if (source != null) {
162162
return source;
163163
}
@@ -177,26 +177,26 @@ public Environment findOne(String application, String profile, String label, boo
177177
label = candidate.getDefaultLabel();
178178
}
179179
try {
180-
return findOneFromCandidate(candidate, application, profile, label, includeOrigin, forceRefresh);
180+
return findOneFromCandidate(candidate, application, profile, label, includeOrigin, ctx);
181181
}
182182
catch (Exception e) {
183183
if (MultipleJGitEnvironmentProperties.MAIN_LABEL.equals(label) && isTryMasterBranch()) {
184184
logger.info("Cannot find Environment with default label " + getDefaultLabel(), e);
185185
logger.info("Will try to find Environment master label instead.");
186186
candidate = getRepository(this, application, profile, MultipleJGitEnvironmentProperties.MASTER_LABEL);
187187
return findOneFromCandidate(candidate, application, profile,
188-
MultipleJGitEnvironmentProperties.MASTER_LABEL, includeOrigin, forceRefresh);
188+
MultipleJGitEnvironmentProperties.MASTER_LABEL, includeOrigin, ctx);
189189
}
190190
throw e;
191191
}
192192
}
193193

194194
private Environment findOneFromCandidate(JGitEnvironmentRepository candidate, String application, String profile,
195-
String label, boolean includeOrigin, boolean forceRefresh) {
195+
String label, boolean includeOrigin, RequestContext ctx) {
196196
if (candidate == this) {
197-
return super.findOne(application, profile, label, includeOrigin, forceRefresh);
197+
return super.findOne(application, profile, label, includeOrigin, ctx);
198198
}
199-
return candidate.findOne(application, profile, label, includeOrigin, forceRefresh);
199+
return candidate.findOne(application, profile, label, includeOrigin, ctx);
200200
}
201201

202202
private List<JGitEnvironmentRepository> getRepositories(JGitEnvironmentRepository repository, String application,
@@ -298,14 +298,14 @@ public boolean matches(String application, String profile, String label) {
298298

299299
@Override
300300
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
301-
boolean forceRefresh) {
301+
RequestContext ctx) {
302302

303303
if (this.pattern == null || this.pattern.length == 0) {
304304
return null;
305305
}
306306

307307
if (PatternMatchUtils.simpleMatch(this.pattern, application + "/" + profile)) {
308-
return super.findOne(application, profile, label, includeOrigin, forceRefresh);
308+
return super.findOne(application, profile, label, includeOrigin, ctx);
309309
}
310310

311311
return null;

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.boot.context.config.StandardConfigDataResource;
3838
import org.springframework.cloud.config.environment.Environment;
3939
import org.springframework.cloud.config.environment.PropertySource;
40+
import org.springframework.cloud.config.server.support.RequestContext;
4041
import org.springframework.core.NestedExceptionUtils;
4142
import org.springframework.core.Ordered;
4243
import org.springframework.core.env.ConfigurableEnvironment;
@@ -137,12 +138,11 @@ public Environment findOne(String config, String profile, String label) {
137138

138139
@Override
139140
public Environment findOne(String config, String profile, String label, boolean includeOrigin) {
140-
return findOne(config, profile, label, includeOrigin, false);
141+
return findOne(config, profile, label, includeOrigin, new RequestContext.Builder().forceRefresh(false).build());
141142
}
142143

143144
@Override
144-
public Environment findOne(String config, String profile, String label, boolean includeOrigin,
145-
boolean forceRefresh) {
145+
public Environment findOne(String config, String profile, String label, boolean includeOrigin, RequestContext ctx) {
146146

147147
try {
148148
ConfigurableEnvironment environment = getEnvironment(config, profile, label);
@@ -161,7 +161,7 @@ public void onPropertySourceAdded(org.springframework.core.env.PropertySource<?>
161161
environment.getPropertySources().remove("config-data-setup");
162162
return clean(ObservationEnvironmentRepositoryWrapper
163163
.wrap(this.observationRegistry, new PassthruEnvironmentRepository(environment))
164-
.findOne(config, profile, label, includeOrigin, forceRefresh), propertySourceToConfigData);
164+
.findOne(config, profile, label, includeOrigin, ctx), propertySourceToConfigData);
165165
}
166166
catch (Exception e) {
167167
String msg = String.format("Could not construct context for config=%s profile=%s label=%s includeOrigin=%b",

Diff for: spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/ObservationEnvironmentRepositoryWrapper.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.micrometer.observation.ObservationRegistry;
2020

2121
import org.springframework.cloud.config.environment.Environment;
22+
import org.springframework.cloud.config.server.support.RequestContext;
2223

2324
/**
2425
* Wraps a {@link EnvironmentRepository} execution with observation.
@@ -68,17 +69,18 @@ public Environment findOne(String application, String profile, String label) {
6869

6970
@Override
7071
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
71-
return findOne(application, profile, label, includeOrigin, false);
72+
return findOne(application, profile, label, includeOrigin,
73+
new RequestContext.Builder().forceRefresh(false).build());
7274
}
7375

7476
@Override
7577
public Environment findOne(String application, String profile, String label, boolean includeOrigin,
76-
boolean forceRefresh) {
78+
RequestContext ctx) {
7779
ObservationEnvironmentRepositoryContext context = new ObservationEnvironmentRepositoryContext(
7880
this.delegate.getClass(), application, profile, label);
7981
return DocumentedConfigObservation.ENVIRONMENT_REPOSITORY
8082
.observation(null, CONVENTION, () -> context, this.registry)
81-
.observe(() -> this.delegate.findOne(application, profile, label, includeOrigin, forceRefresh));
83+
.observe(() -> this.delegate.findOne(application, profile, label, includeOrigin, ctx));
8284
}
8385

8486
/**

0 commit comments

Comments
 (0)