forked from spring-projects/spring-boot
/
Profiles.java
209 lines (180 loc) · 7.23 KB
/
Profiles.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright 2012-2020 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.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
/**
* Provides access to environment profiles that have either been set directly on the
* {@link Environment} or will be set based on configuration data property values.
*
* @author Phillip Webb
* @author Madhura Bhave
* @since 2.4.0
*/
public class Profiles implements Iterable<String> {
/**
* Name of property to set to specify additionally included active profiles.
*/
public static final String INCLUDE_PROFILES_PROPERTY_NAME = "spring.profiles.include";
private static final Bindable<MultiValueMap<String, String>> STRING_STRINGS_MAP = Bindable
.of(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class));
private static final Set<String> UNSET_ACTIVE = Collections.emptySet();
private static final Set<String> UNSET_DEFAULT = Collections.singleton("default");
private final MultiValueMap<String, String> groups;
private final List<String> activeProfiles;
private final List<String> defaultProfiles;
/**
* Create a new {@link Profiles} instance based on the {@link Environment} and
* {@link Binder}.
* @param environment the source environment
* @param binder the binder for profile properties
* @param additionalProfiles any additional active profiles
*/
Profiles(Environment environment, Binder binder, Collection<String> additionalProfiles) {
this.groups = binder.bind("spring.profiles.group", STRING_STRINGS_MAP).orElseGet(LinkedMultiValueMap::new);
this.activeProfiles = expandProfiles(getActivatedProfiles(environment, binder, additionalProfiles));
this.defaultProfiles = expandProfiles(getDefaultProfiles(environment, binder));
}
private List<String> getActivatedProfiles(Environment environment, Binder binder,
Collection<String> additionalProfiles) {
return asUniqueItemList(get(environment, binder, environment::getActiveProfiles,
AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, UNSET_ACTIVE), additionalProfiles);
}
private List<String> getDefaultProfiles(Environment environment, Binder binder) {
return asUniqueItemList(get(environment, binder, environment::getDefaultProfiles,
AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, UNSET_DEFAULT));
}
private String[] get(Environment environment, Binder binder, Supplier<String[]> supplier, String propertyName,
Set<String> unset) {
String propertyValue = environment.getProperty(propertyName);
if (hasExplicit(supplier, propertyValue, unset)) {
return supplier.get();
}
return binder.bind(propertyName, String[].class).orElse(StringUtils.toStringArray(unset));
}
private boolean hasExplicit(Supplier<String[]> supplier, String propertyValue, Set<String> unset) {
Set<String> profiles = new LinkedHashSet<>(Arrays.asList(supplier.get()));
if (!StringUtils.hasLength(propertyValue)) {
return !unset.equals(profiles);
}
Set<String> propertyProfiles = StringUtils
.commaDelimitedListToSet(StringUtils.trimAllWhitespace(propertyValue));
return !propertyProfiles.equals(profiles);
}
private List<String> expandProfiles(List<String> profiles) {
Deque<String> stack = new ArrayDeque<>();
asReversedList(profiles).forEach(stack::push);
Set<String> expandedProfiles = new LinkedHashSet<>();
while (!stack.isEmpty()) {
String current = stack.pop();
expandedProfiles.add(current);
List<String> groupProfiles = asReversedList(this.groups.get(current));
Set<String> profileConflicts = getProfileConflicts(groupProfiles, expandedProfiles);
if (!profileConflicts.isEmpty()) {
String message = String.format("Profiles could not be resolved. Remove profiles %s from group: %s",
profileConflicts, current);
throw new IllegalStateException(message);
}
groupProfiles.forEach(stack::push);
}
return asUniqueItemList(StringUtils.toStringArray(expandedProfiles));
}
private Set<String> getProfileConflicts(List<String> groupProfiles, Set<String> expandedProfiles) {
return groupProfiles.stream().filter(expandedProfiles::contains).collect(Collectors.toSet());
}
private List<String> asReversedList(List<String> list) {
if (list == null || list.isEmpty()) {
return Collections.emptyList();
}
List<String> reversed = new ArrayList<>(list);
Collections.reverse(reversed);
return reversed;
}
private List<String> asUniqueItemList(String[] array) {
return asUniqueItemList(array, null);
}
private List<String> asUniqueItemList(String[] array, Collection<String> additional) {
LinkedHashSet<String> uniqueItems = new LinkedHashSet<>(Arrays.asList(array));
if (!CollectionUtils.isEmpty(additional)) {
uniqueItems.addAll(additional);
}
return Collections.unmodifiableList(new ArrayList<>(uniqueItems));
}
/**
* Return an iterator for all {@link #getAccepted() accepted profiles}.
*/
@Override
public Iterator<String> iterator() {
return getAccepted().iterator();
}
/**
* Return the active profiles.
* @return the active profiles
*/
public List<String> getActive() {
return this.activeProfiles;
}
/**
* Return the default profiles.
* @return the active profiles
*/
public List<String> getDefault() {
return this.defaultProfiles;
}
/**
* Return the accepted profiles.
* @return the accepted profiles
*/
public List<String> getAccepted() {
return (!this.activeProfiles.isEmpty()) ? this.activeProfiles : this.defaultProfiles;
}
/**
* Return if the given profile is active.
* @param profile the profile to test
* @return if the profile is active
*/
public boolean isAccepted(String profile) {
return getAccepted().contains(profile);
}
@Override
public String toString() {
ToStringCreator creator = new ToStringCreator(this);
creator.append("active", getActive().toString());
creator.append("default", getDefault().toString());
creator.append("accepted", getAccepted().toString());
return creator.toString();
}
}