|
1 | 1 | /* |
2 | | - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
31 | 31 | import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
32 | 32 | import org.springframework.core.io.support.SpringFactoriesLoader; |
33 | 33 | import org.springframework.core.log.LogMessage; |
34 | | -import org.springframework.util.Assert; |
35 | 34 | import org.springframework.util.ClassUtils; |
36 | 35 | import org.springframework.util.ReflectionUtils; |
37 | 36 |
|
@@ -61,42 +60,50 @@ final class FailureAnalyzers implements SpringBootExceptionReporter { |
61 | 60 | } |
62 | 61 |
|
63 | 62 | FailureAnalyzers(ConfigurableApplicationContext context, ClassLoader classLoader) { |
64 | | - Assert.notNull(context, "Context must not be null"); |
65 | | - this.classLoader = (classLoader != null) ? classLoader : context.getClassLoader(); |
66 | | - this.analyzers = loadFailureAnalyzers(this.classLoader); |
67 | | - prepareFailureAnalyzers(this.analyzers, context); |
| 63 | + this.classLoader = (classLoader != null) ? classLoader : getClassLoader(context); |
| 64 | + this.analyzers = loadFailureAnalyzers(context, this.classLoader); |
68 | 65 | } |
69 | 66 |
|
70 | | - private List<FailureAnalyzer> loadFailureAnalyzers(ClassLoader classLoader) { |
71 | | - List<String> analyzerNames = SpringFactoriesLoader.loadFactoryNames(FailureAnalyzer.class, classLoader); |
| 67 | + private ClassLoader getClassLoader(ConfigurableApplicationContext context) { |
| 68 | + return (context != null) ? context.getClassLoader() : null; |
| 69 | + } |
| 70 | + |
| 71 | + private List<FailureAnalyzer> loadFailureAnalyzers(ConfigurableApplicationContext context, |
| 72 | + ClassLoader classLoader) { |
| 73 | + List<String> classNames = SpringFactoriesLoader.loadFactoryNames(FailureAnalyzer.class, classLoader); |
72 | 74 | List<FailureAnalyzer> analyzers = new ArrayList<>(); |
73 | | - for (String analyzerName : analyzerNames) { |
| 75 | + for (String className : classNames) { |
74 | 76 | try { |
75 | | - Constructor<?> constructor = ClassUtils.forName(analyzerName, classLoader).getDeclaredConstructor(); |
76 | | - ReflectionUtils.makeAccessible(constructor); |
77 | | - analyzers.add((FailureAnalyzer) constructor.newInstance()); |
| 77 | + FailureAnalyzer analyzer = createAnalyzer(context, className); |
| 78 | + if (analyzer != null) { |
| 79 | + analyzers.add(analyzer); |
| 80 | + } |
78 | 81 | } |
79 | 82 | catch (Throwable ex) { |
80 | | - logger.trace(LogMessage.format("Failed to load %s", analyzerName), ex); |
| 83 | + logger.trace(LogMessage.format("Failed to load %s", className), ex); |
81 | 84 | } |
82 | 85 | } |
83 | 86 | AnnotationAwareOrderComparator.sort(analyzers); |
84 | 87 | return analyzers; |
85 | 88 | } |
86 | 89 |
|
87 | | - private void prepareFailureAnalyzers(List<FailureAnalyzer> analyzers, ConfigurableApplicationContext context) { |
88 | | - for (FailureAnalyzer analyzer : analyzers) { |
89 | | - prepareAnalyzer(context, analyzer); |
90 | | - } |
91 | | - } |
92 | | - |
93 | | - private void prepareAnalyzer(ConfigurableApplicationContext context, FailureAnalyzer analyzer) { |
94 | | - if (analyzer instanceof BeanFactoryAware) { |
95 | | - ((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory()); |
96 | | - } |
97 | | - if (analyzer instanceof EnvironmentAware) { |
98 | | - ((EnvironmentAware) analyzer).setEnvironment(context.getEnvironment()); |
| 90 | + private FailureAnalyzer createAnalyzer(ConfigurableApplicationContext context, String className) throws Exception { |
| 91 | + Constructor<?> constructor = ClassUtils.forName(className, this.classLoader).getDeclaredConstructor(); |
| 92 | + ReflectionUtils.makeAccessible(constructor); |
| 93 | + FailureAnalyzer analyzer = (FailureAnalyzer) constructor.newInstance(); |
| 94 | + if (analyzer instanceof BeanFactoryAware || analyzer instanceof EnvironmentAware) { |
| 95 | + if (context == null) { |
| 96 | + logger.trace(LogMessage.format("Skipping %s due to missing context", className)); |
| 97 | + return null; |
| 98 | + } |
| 99 | + if (analyzer instanceof BeanFactoryAware) { |
| 100 | + ((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory()); |
| 101 | + } |
| 102 | + if (analyzer instanceof EnvironmentAware) { |
| 103 | + ((EnvironmentAware) analyzer).setEnvironment(context.getEnvironment()); |
| 104 | + } |
99 | 105 | } |
| 106 | + return analyzer; |
100 | 107 | } |
101 | 108 |
|
102 | 109 | @Override |
|
0 commit comments