View Javadoc
1   package org.apache.maven.toolchain.building;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Robert Scholte
44   * @since 3.3.0
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 }