1 package org.apache.maven.toolchain.building;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.inject.Singleton;
30
31 import org.apache.maven.building.Problem;
32 import org.apache.maven.building.ProblemCollector;
33 import org.apache.maven.building.ProblemCollectorFactory;
34 import org.apache.maven.building.Source;
35 import org.apache.maven.toolchain.io.ToolchainsParseException;
36 import org.apache.maven.toolchain.io.ToolchainsReader;
37 import org.apache.maven.toolchain.merge.MavenToolchainMerger;
38 import org.apache.maven.toolchain.model.PersistedToolchains;
39 import org.apache.maven.toolchain.model.TrackableBase;
40
41
42
43
44
45
46 @Named
47 @Singleton
48 public class DefaultToolchainsBuilder
49 implements ToolchainsBuilder
50 {
51 private MavenToolchainMerger toolchainsMerger = new MavenToolchainMerger();
52
53 @Inject
54 private ToolchainsReader toolchainsReader;
55
56 @Override
57 public ToolchainsBuildingResult build( ToolchainsBuildingRequest request )
58 throws ToolchainsBuildingException
59 {
60 ProblemCollector problems = ProblemCollectorFactory.newInstance( null );
61
62 PersistedToolchains globalToolchains = readToolchains( request.getGlobalToolchainsSource(), request, problems );
63
64 PersistedToolchains userToolchains = readToolchains( request.getUserToolchainsSource(), request, problems );
65
66 toolchainsMerger.merge( userToolchains, globalToolchains, TrackableBase.GLOBAL_LEVEL );
67
68 problems.setSource( "" );
69
70 if ( hasErrors( problems.getProblems() ) )
71 {
72 throw new ToolchainsBuildingException( problems.getProblems() );
73 }
74
75
76 return new DefaultToolchainsBuildingResult( userToolchains, problems.getProblems() );
77 }
78
79 private PersistedToolchains readToolchains( Source toolchainsSource, ToolchainsBuildingRequest request,
80 ProblemCollector problems )
81 {
82 if ( toolchainsSource == null )
83 {
84 return new PersistedToolchains();
85 }
86
87 PersistedToolchains toolchains;
88
89 try
90 {
91 Map<String, ?> options = Collections.singletonMap( ToolchainsReader.IS_STRICT, Boolean.TRUE );
92
93 try
94 {
95 toolchains = toolchainsReader.read( toolchainsSource.getInputStream(), options );
96 }
97 catch ( ToolchainsParseException e )
98 {
99 options = Collections.singletonMap( ToolchainsReader.IS_STRICT, Boolean.FALSE );
100
101 toolchains = toolchainsReader.read( toolchainsSource.getInputStream(), options );
102
103 problems.add( Problem.Severity.WARNING, e.getMessage(), e.getLineNumber(), e.getColumnNumber(),
104 e );
105 }
106 }
107 catch ( ToolchainsParseException e )
108 {
109 problems.add( Problem.Severity.FATAL, "Non-parseable toolchains " + toolchainsSource.getLocation()
110 + ": " + e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
111 return new PersistedToolchains();
112 }
113 catch ( IOException e )
114 {
115 problems.add( Problem.Severity.FATAL, "Non-readable toolchains " + toolchainsSource.getLocation()
116 + ": " + e.getMessage(), -1, -1, e );
117 return new PersistedToolchains();
118 }
119
120 return toolchains;
121 }
122
123 private boolean hasErrors( List<Problem> problems )
124 {
125 if ( problems != null )
126 {
127 for ( Problem problem : problems )
128 {
129 if ( Problem.Severity.ERROR.compareTo( problem.getSeverity() ) >= 0 )
130 {
131 return true;
132 }
133 }
134 }
135
136 return false;
137 }
138 }