001package org.apache.maven.toolchain.building; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.IOException; 023import java.util.Collections; 024import java.util.List; 025import java.util.Map; 026 027import javax.inject.Inject; 028import javax.inject.Named; 029import javax.inject.Singleton; 030 031import org.apache.maven.building.Problem; 032import org.apache.maven.building.ProblemCollector; 033import org.apache.maven.building.ProblemCollectorFactory; 034import org.apache.maven.building.Source; 035import org.apache.maven.toolchain.io.ToolchainsParseException; 036import org.apache.maven.toolchain.io.ToolchainsReader; 037import org.apache.maven.toolchain.merge.MavenToolchainMerger; 038import org.apache.maven.toolchain.model.PersistedToolchains; 039import org.apache.maven.toolchain.model.TrackableBase; 040 041/** 042 * 043 * @author Robert Scholte 044 * @since 3.3.0 045 */ 046@Named 047@Singleton 048public class DefaultToolchainsBuilder 049 implements ToolchainsBuilder 050{ 051 private MavenToolchainMerger toolchainsMerger = new MavenToolchainMerger(); 052 053 @Inject 054 private ToolchainsReader toolchainsReader; 055 056 @Override 057 public ToolchainsBuildingResult build( ToolchainsBuildingRequest request ) 058 throws ToolchainsBuildingException 059 { 060 ProblemCollector problems = ProblemCollectorFactory.newInstance( null ); 061 062 PersistedToolchains globalToolchains = readToolchains( request.getGlobalToolchainsSource(), request, problems ); 063 064 PersistedToolchains userToolchains = readToolchains( request.getUserToolchainsSource(), request, problems ); 065 066 toolchainsMerger.merge( userToolchains, globalToolchains, TrackableBase.GLOBAL_LEVEL ); 067 068 problems.setSource( "" ); 069 070 if ( hasErrors( problems.getProblems() ) ) 071 { 072 throw new ToolchainsBuildingException( problems.getProblems() ); 073 } 074 075 076 return new DefaultToolchainsBuildingResult( userToolchains, problems.getProblems() ); 077 } 078 079 private PersistedToolchains readToolchains( Source toolchainsSource, ToolchainsBuildingRequest request, 080 ProblemCollector problems ) 081 { 082 if ( toolchainsSource == null ) 083 { 084 return new PersistedToolchains(); 085 } 086 087 PersistedToolchains toolchains; 088 089 try 090 { 091 Map<String, ?> options = Collections.singletonMap( ToolchainsReader.IS_STRICT, Boolean.TRUE ); 092 093 try 094 { 095 toolchains = toolchainsReader.read( toolchainsSource.getInputStream(), options ); 096 } 097 catch ( ToolchainsParseException e ) 098 { 099 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}