001package org.apache.maven.toolchain; 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.util.ArrayList; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.maven.execution.MavenSession; 029import org.apache.maven.plugin.descriptor.PluginDescriptor; 030import org.apache.maven.project.MavenProject; 031import org.apache.maven.toolchain.model.ToolchainModel; 032import org.codehaus.plexus.component.annotations.Component; 033import org.codehaus.plexus.component.annotations.Requirement; 034import org.codehaus.plexus.logging.Logger; 035 036/** 037 * @author mkleint 038 */ 039@Component( role = ToolchainManager.class ) 040public class DefaultToolchainManager 041 implements ToolchainManager 042{ 043 @Requirement 044 Logger logger; 045 046 @Requirement( role = ToolchainFactory.class ) 047 Map<String, ToolchainFactory> factories; 048 049 @Override 050 public Toolchain getToolchainFromBuildContext( String type, MavenSession session ) 051 { 052 Map<String, Object> context = retrieveContext( session ); 053 054 ToolchainModel model = (ToolchainModel) context.get( getStorageKey( type ) ); 055 056 if ( model != null ) 057 { 058 List<Toolchain> toolchains = selectToolchains( Collections.singletonList( model ), type, null ); 059 060 if ( !toolchains.isEmpty() ) 061 { 062 return toolchains.get( 0 ); 063 } 064 } 065 066 return null; 067 } 068 069 @Override 070 public List<Toolchain> getToolchains( MavenSession session, String type, Map<String, String> requirements ) 071 { 072 List<ToolchainModel> models = session.getRequest().getToolchains().get( type ); 073 074 return selectToolchains( models, type, requirements ); 075 } 076 077 private List<Toolchain> selectToolchains( List<ToolchainModel> models, String type, 078 Map<String, String> requirements ) 079 { 080 List<Toolchain> toolchains = new ArrayList<>(); 081 082 if ( models != null ) 083 { 084 ToolchainFactory fact = factories.get( type ); 085 086 if ( fact == null ) 087 { 088 logger.error( "Missing toolchain factory for type: " + type 089 + ". Possibly caused by misconfigured project." ); 090 } 091 else 092 { 093 for ( ToolchainModel model : models ) 094 { 095 try 096 { 097 ToolchainPrivate toolchain = fact.createToolchain( model ); 098 if ( requirements == null || toolchain.matchesRequirements( requirements ) ) 099 { 100 toolchains.add( toolchain ); 101 } 102 } 103 catch ( MisconfiguredToolchainException ex ) 104 { 105 logger.error( "Misconfigured toolchain.", ex ); 106 } 107 } 108 } 109 } 110 return toolchains; 111 } 112 113 Map<String, Object> retrieveContext( MavenSession session ) 114 { 115 Map<String, Object> context = null; 116 117 if ( session != null ) 118 { 119 PluginDescriptor desc = new PluginDescriptor(); 120 desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() ); 121 desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( "toolchains" ) ); 122 123 MavenProject current = session.getCurrentProject(); 124 125 if ( current != null ) 126 { 127 //TODO: why is this using the context 128 context = session.getPluginContext( desc, current ); 129 } 130 } 131 132 return ( context != null ) ? context : new HashMap<String, Object>(); 133 } 134 135 public static final String getStorageKey( String type ) 136 { 137 return "toolchain-" + type; // NOI18N 138 } 139 140}