|
25 | 25 | */
|
26 | 26 | import javax.inject.Inject;
|
27 | 27 | import javax.inject.Named;
|
| 28 | +import javax.inject.Provider; |
28 | 29 |
|
29 | 30 | import java.util.Map;
|
30 | 31 |
|
31 | 32 | import org.codehaus.plexus.compiler.Compiler;
|
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
32 | 35 |
|
33 | 36 | /**
|
34 | 37 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
|
35 | 38 | */
|
36 | 39 | @Named
|
37 | 40 | public class DefaultCompilerManager implements CompilerManager {
|
38 | 41 | @Inject
|
39 |
| - private Map<String, Compiler> compilers; |
| 42 | + private Map<String, Provider<Compiler>> compilers; |
| 43 | + |
| 44 | + private final Logger log = LoggerFactory.getLogger(getClass()); |
40 | 45 |
|
41 | 46 | // ----------------------------------------------------------------------
|
42 | 47 | // CompilerManager Implementation
|
43 | 48 | // ----------------------------------------------------------------------
|
44 | 49 |
|
45 | 50 | public Compiler getCompiler(String compilerId) throws NoSuchCompilerException {
|
46 |
| - Compiler compiler = compilers.get(compilerId); |
| 51 | + // Provider<Class> is lazy -> presence of provider means compiler is present, but not yet constructed |
| 52 | + Provider<Compiler> compilerProvider = compilers.get(compilerId); |
47 | 53 |
|
48 |
| - if (compiler == null) { |
| 54 | + if (compilerProvider == null) { |
| 55 | + // Compiler could not be injected for some reason |
| 56 | + String errorMessage = "Compiler '{}' either was not found (e.g. wrong compiler ID, missing dependencies) " |
| 57 | + + "or its class could not be scanned. " |
| 58 | + + "Running the build with -Dsisu.debug, looking for exceptions might help. " |
| 59 | + + "A TypeNotPresentException might indicate, that the compiler needs more recent Java runtime. " |
| 60 | + + "An IllegalArgumentException in ClassReader.<init> might mean, that you need to upgrade Maven."; |
| 61 | + log.error(errorMessage, compilerId); |
49 | 62 | throw new NoSuchCompilerException(compilerId);
|
50 | 63 | }
|
51 | 64 |
|
52 |
| - return compiler; |
| 65 | + // Provider exists, but compiler was not created yet |
| 66 | + try { |
| 67 | + return compilerProvider.get(); |
| 68 | + } catch (Exception e) { |
| 69 | + // DI could not construct compiler |
| 70 | + String errorMessage = "Compiler '{}' could not be instantiated or injected properly. " |
| 71 | + + "Running the build with -Dsisu.debug, looking for exceptions might help."; |
| 72 | + log.error(errorMessage, compilerId); |
| 73 | + throw new NoSuchCompilerException(compilerId); |
| 74 | + } |
53 | 75 | }
|
54 | 76 | }
|
0 commit comments