-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
ConfigDataImporter.java
167 lines (145 loc) · 5.99 KB
/
ConfigDataImporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.config;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.springframework.boot.logging.DeferredLogFactory;
/**
* Imports {@link ConfigData} by {@link ConfigDataLocationResolver resolving} and
* {@link ConfigDataLoader loading} locations. {@link ConfigDataResource resources} are
* tracked to ensure that they are not imported multiple times.
*
* @author Phillip Webb
* @author Madhura Bhave
*/
class ConfigDataImporter {
private final Log logger;
private final ConfigDataLocationResolvers resolvers;
private final ConfigDataLoaders loaders;
private final ConfigDataNotFoundAction notFoundAction;
private final Set<ConfigDataResource> loaded = new HashSet<>();
private final Set<ConfigDataLocation> loadedLocations = new HashSet<>();
private final Set<ConfigDataLocation> optionalLocations = new HashSet<>();
/**
* Create a new {@link ConfigDataImporter} instance.
* @param logFactory the log factory
* @param notFoundAction the action to take when a location cannot be found
* @param resolvers the config data location resolvers
* @param loaders the config data loaders
*/
ConfigDataImporter(DeferredLogFactory logFactory, ConfigDataNotFoundAction notFoundAction,
ConfigDataLocationResolvers resolvers, ConfigDataLoaders loaders) {
this.logger = logFactory.getLog(getClass());
this.resolvers = resolvers;
this.loaders = loaders;
this.notFoundAction = notFoundAction;
}
/**
* Resolve and load the given list of locations, filtering any that have been
* previously loaded.
* @param activationContext the activation context
* @param locationResolverContext the location resolver context
* @param loaderContext the loader context
* @param locations the locations to resolve
* @param resolveProfileSpecific if profile specific resolution should be attempted
* @return a map of the loaded locations and data
*/
Map<ConfigDataResolutionResult, ConfigData> resolveAndLoad(ConfigDataActivationContext activationContext,
ConfigDataLocationResolverContext locationResolverContext, ConfigDataLoaderContext loaderContext,
List<ConfigDataLocation> locations, boolean resolveProfileSpecific) {
try {
Profiles profiles = (activationContext != null) ? activationContext.getProfiles() : null;
List<ConfigDataResolutionResult> resolved = resolve(locationResolverContext, profiles, locations,
resolveProfileSpecific);
return load(loaderContext, resolved);
}
catch (IOException ex) {
throw new IllegalStateException("IO error on loading imports from " + locations, ex);
}
}
private List<ConfigDataResolutionResult> resolve(ConfigDataLocationResolverContext locationResolverContext,
Profiles profiles, List<ConfigDataLocation> locations, boolean resolveProfileSpecific) {
List<ConfigDataResolutionResult> resolved = new ArrayList<>(locations.size());
for (ConfigDataLocation location : locations) {
resolved.addAll(resolve(locationResolverContext, profiles, location, resolveProfileSpecific));
}
return Collections.unmodifiableList(resolved);
}
private List<ConfigDataResolutionResult> resolve(ConfigDataLocationResolverContext locationResolverContext,
Profiles profiles, ConfigDataLocation location, boolean resolveProfileSpecific) {
try {
return this.resolvers.resolve(locationResolverContext, location, profiles, resolveProfileSpecific);
}
catch (ConfigDataNotFoundException ex) {
handle(ex, location, null);
return Collections.emptyList();
}
}
private Map<ConfigDataResolutionResult, ConfigData> load(ConfigDataLoaderContext loaderContext,
List<ConfigDataResolutionResult> candidates) throws IOException {
Map<ConfigDataResolutionResult, ConfigData> result = new LinkedHashMap<>();
for (int i = candidates.size() - 1; i >= 0; i--) {
ConfigDataResolutionResult candidate = candidates.get(i);
ConfigDataLocation location = candidate.getLocation();
ConfigDataResource resource = candidate.getResource();
if (resource.isOptional()) {
this.optionalLocations.add(location);
}
if (this.loaded.contains(resource)) {
this.loadedLocations.add(location);
}
else {
try {
ConfigData loaded = this.loaders.load(loaderContext, resource);
if (loaded != null) {
this.loaded.add(resource);
this.loadedLocations.add(location);
result.put(candidate, loaded);
}
}
catch (ConfigDataNotFoundException ex) {
handle(ex, location, resource);
}
}
}
return Collections.unmodifiableMap(result);
}
private void handle(ConfigDataNotFoundException ex, ConfigDataLocation location, ConfigDataResource resource) {
if (ex instanceof ConfigDataResourceNotFoundException) {
ex = ((ConfigDataResourceNotFoundException) ex).withLocation(location);
}
getNotFoundAction(location, resource).handle(this.logger, ex);
}
private ConfigDataNotFoundAction getNotFoundAction(ConfigDataLocation location, ConfigDataResource resource) {
if (location.isOptional() || (resource != null && resource.isOptional())) {
return ConfigDataNotFoundAction.IGNORE;
}
return this.notFoundAction;
}
Set<ConfigDataLocation> getLoadedLocations() {
return this.loadedLocations;
}
Set<ConfigDataLocation> getOptionalLocations() {
return this.optionalLocations;
}
}