/
FailureAnalyzers.java
142 lines (125 loc) · 4.96 KB
/
FailureAnalyzers.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
/*
* 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.diagnostics;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.boot.SpringBootExceptionReporter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.log.LogMessage;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Utility to trigger {@link FailureAnalyzer} and {@link FailureAnalysisReporter}
* instances loaded from {@code spring.factories}.
* <p>
* A {@code FailureAnalyzer} that requires access to the {@link BeanFactory} in order to
* perform its analysis can implement {@code BeanFactoryAware} to have the
* {@code BeanFactory} injected prior to {@link FailureAnalyzer#analyze(Throwable)} being
* called.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @author Stephane Nicoll
*/
final class FailureAnalyzers implements SpringBootExceptionReporter {
private static final Log logger = LogFactory.getLog(FailureAnalyzers.class);
private final ClassLoader classLoader;
private final List<FailureAnalyzer> analyzers;
FailureAnalyzers(ConfigurableApplicationContext context) {
this(context, null);
}
FailureAnalyzers(ConfigurableApplicationContext context, ClassLoader classLoader) {
this.classLoader = (classLoader != null) ? classLoader : getClassLoader(context);
this.analyzers = loadFailureAnalyzers(context, this.classLoader);
}
private ClassLoader getClassLoader(ConfigurableApplicationContext context) {
return (context != null) ? context.getClassLoader() : null;
}
private List<FailureAnalyzer> loadFailureAnalyzers(ConfigurableApplicationContext context,
ClassLoader classLoader) {
List<String> classNames = SpringFactoriesLoader.loadFactoryNames(FailureAnalyzer.class, classLoader);
List<FailureAnalyzer> analyzers = new ArrayList<>();
for (String className : classNames) {
try {
FailureAnalyzer analyzer = createAnalyzer(context, className);
if (analyzer != null) {
analyzers.add(analyzer);
}
}
catch (Throwable ex) {
logger.trace(LogMessage.format("Failed to load %s", className), ex);
}
}
AnnotationAwareOrderComparator.sort(analyzers);
return analyzers;
}
private FailureAnalyzer createAnalyzer(ConfigurableApplicationContext context, String className) throws Exception {
Constructor<?> constructor = ClassUtils.forName(className, this.classLoader).getDeclaredConstructor();
ReflectionUtils.makeAccessible(constructor);
FailureAnalyzer analyzer = (FailureAnalyzer) constructor.newInstance();
if (analyzer instanceof BeanFactoryAware || analyzer instanceof EnvironmentAware) {
if (context == null) {
logger.trace(LogMessage.format("Skipping %s due to missing context", className));
return null;
}
if (analyzer instanceof BeanFactoryAware) {
((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory());
}
if (analyzer instanceof EnvironmentAware) {
((EnvironmentAware) analyzer).setEnvironment(context.getEnvironment());
}
}
return analyzer;
}
@Override
public boolean reportException(Throwable failure) {
FailureAnalysis analysis = analyze(failure, this.analyzers);
return report(analysis, this.classLoader);
}
private FailureAnalysis analyze(Throwable failure, List<FailureAnalyzer> analyzers) {
for (FailureAnalyzer analyzer : analyzers) {
try {
FailureAnalysis analysis = analyzer.analyze(failure);
if (analysis != null) {
return analysis;
}
}
catch (Throwable ex) {
logger.trace(LogMessage.format("FailureAnalyzer %s failed", analyzer), ex);
}
}
return null;
}
private boolean report(FailureAnalysis analysis, ClassLoader classLoader) {
List<FailureAnalysisReporter> reporters = SpringFactoriesLoader.loadFactories(FailureAnalysisReporter.class,
classLoader);
if (analysis == null || reporters.isEmpty()) {
return false;
}
for (FailureAnalysisReporter reporter : reporters) {
reporter.report(analysis);
}
return true;
}
}