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.HashMap;
023import java.util.Map;
024
025import org.apache.maven.execution.MavenSession;
026import org.apache.maven.plugin.descriptor.PluginDescriptor;
027import org.apache.maven.project.MavenProject;
028import org.apache.maven.toolchain.model.ToolchainModel;
029import org.codehaus.plexus.component.annotations.Component;
030import org.codehaus.plexus.component.annotations.Requirement;
031import org.codehaus.plexus.logging.Logger;
032
033/**
034 * @author mkleint
035 */
036@Component( role = ToolchainManager.class )
037public class DefaultToolchainManager
038    implements ToolchainManager
039{
040    @Requirement
041    Logger logger;
042
043    @Requirement( role = ToolchainFactory.class )
044    Map<String, ToolchainFactory> factories;
045
046    public Toolchain getToolchainFromBuildContext( String type, MavenSession session )
047    {
048        Map<String, Object> context = retrieveContext( session );
049
050        ToolchainModel model = (ToolchainModel) context.get( getStorageKey( type ) );
051
052        if ( model != null )
053        {
054            try
055            {
056                ToolchainFactory fact = factories.get( type );
057                if ( fact != null )
058                {
059                    return fact.createToolchain( model );
060                }
061                else
062                {
063                    logger.error( "Missing toolchain factory for type: " + type
064                        + ". Possibly caused by misconfigured project." );
065                }
066            }
067            catch ( MisconfiguredToolchainException ex )
068            {
069                logger.error( "Misconfigured toolchain.", ex );
070            }
071        }
072
073        return null;
074    }
075
076    Map<String, Object> retrieveContext( MavenSession session )
077    {
078        Map<String, Object> context = null;
079
080        if ( session != null )
081        {
082            PluginDescriptor desc = new PluginDescriptor();
083            desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
084            desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( "toolchains" ) );
085
086            MavenProject current = session.getCurrentProject();
087            
088            if ( current != null )
089            {
090                //TODO: why is this using the context
091                context = session.getPluginContext( desc, current );
092            }
093        }
094
095        return ( context != null ) ? context : new HashMap<String, Object>();
096    }
097
098    public static final String getStorageKey( String type )
099    {
100        return "toolchain-" + type; // NOI18N
101    }
102
103}