-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
InvalidConfigDataPropertyException.java
153 lines (136 loc) · 5.49 KB
/
InvalidConfigDataPropertyException.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
/*
* 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.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.springframework.boot.context.properties.source.ConfigurationProperty;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.core.env.AbstractEnvironment;
/**
* Exception thrown if an invalid property is found when processing config data.
*
* @author Phillip Webb
* @author Madhura Bhave
* @since 2.4.0
*/
public class InvalidConfigDataPropertyException extends ConfigDataException {
private static final Map<ConfigurationPropertyName, ConfigurationPropertyName> WARNINGS;
static {
Map<ConfigurationPropertyName, ConfigurationPropertyName> warnings = new LinkedHashMap<>();
warnings.put(ConfigurationPropertyName.of("spring.profiles"),
ConfigurationPropertyName.of("spring.config.activate.on-profile"));
WARNINGS = Collections.unmodifiableMap(warnings);
}
private static final Set<ConfigurationPropertyName> PROFILE_SPECIFIC_ERRORS;
static {
Set<ConfigurationPropertyName> errors = new LinkedHashSet<>();
errors.add(Profiles.INCLUDE_PROFILES);
errors.add(ConfigurationPropertyName.of(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME));
errors.add(ConfigurationPropertyName.of(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME));
errors.add(ConfigurationPropertyName.of("spring.config.activate.on-profile"));
errors.add(ConfigurationPropertyName.of("spring.profiles"));
PROFILE_SPECIFIC_ERRORS = Collections.unmodifiableSet(errors);
}
private final ConfigurationProperty property;
private final ConfigurationPropertyName replacement;
private final ConfigDataResource location;
InvalidConfigDataPropertyException(ConfigurationProperty property, boolean profileSpecific,
ConfigurationPropertyName replacement, ConfigDataResource location) {
super(getMessage(property, profileSpecific, replacement, location), null);
this.property = property;
this.replacement = replacement;
this.location = location;
}
/**
* Return source property that caused the exception.
* @return the invalid property
*/
public ConfigurationProperty getProperty() {
return this.property;
}
/**
* Return the {@link ConfigDataResource} of the invalid property or {@code null} if
* the source was not loaded from {@link ConfigData}.
* @return the config data location or {@code null}
*/
public ConfigDataResource getLocation() {
return this.location;
}
/**
* Return the replacement property that should be used instead or {@code null} if not
* replacement is available.
* @return the replacement property name
*/
public ConfigurationPropertyName getReplacement() {
return this.replacement;
}
/**
* Throw a {@link InvalidConfigDataPropertyException} or log a warning if the given
* {@link ConfigDataEnvironmentContributor} contains any invalid property. A warning
* is logged if the property is still supported, but not recommended. An error is
* thrown if the property is completely unsupported.
* @param logger the logger to use for warnings
* @param contributor the contributor to check
*/
static void throwOrWarn(Log logger, ConfigDataEnvironmentContributor contributor) {
ConfigurationPropertySource propertySource = contributor.getConfigurationPropertySource();
if (propertySource != null) {
WARNINGS.forEach((name, replacement) -> {
ConfigurationProperty property = propertySource.getConfigurationProperty(name);
if (property != null) {
logger.warn(getMessage(property, false, replacement, contributor.getResource()));
}
});
if (contributor.isProfileSpecific() && contributor.isNotIgnoringProfiles()) {
PROFILE_SPECIFIC_ERRORS.forEach((name) -> {
ConfigurationProperty property = propertySource.getConfigurationProperty(name);
if (property != null) {
throw new InvalidConfigDataPropertyException(property, true, null, contributor.getResource());
}
});
}
}
}
private static String getMessage(ConfigurationProperty property, boolean profileSpecific,
ConfigurationPropertyName replacement, ConfigDataResource location) {
StringBuilder message = new StringBuilder("Property '");
message.append(property.getName());
if (location != null) {
message.append("' imported from location '");
message.append(location);
}
message.append("' is invalid");
if (profileSpecific) {
message.append(" in a profile specific resource");
}
if (replacement != null) {
message.append(" and should be replaced with '");
message.append(replacement);
message.append("'");
}
if (property.getOrigin() != null) {
message.append(" [origin: ");
message.append(property.getOrigin());
message.append("]");
}
return message.toString();
}
}