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.List;
024import java.util.Map;
025
026import org.apache.maven.execution.MavenSession;
027import org.apache.maven.toolchain.model.PersistedToolchains;
028import org.apache.maven.toolchain.model.ToolchainModel;
029import org.codehaus.plexus.component.annotations.Component;
030import org.codehaus.plexus.component.annotations.Requirement;
031
032/**
033 * @author mkleint
034 */
035@Component( role = ToolchainManagerPrivate.class )
036public class DefaultToolchainManagerPrivate
037    extends DefaultToolchainManager
038    implements ToolchainManagerPrivate
039{
040
041    @Requirement
042    private ToolchainsBuilder toolchainsBuilder;
043
044    public ToolchainPrivate[] getToolchainsForType( String type, MavenSession context )
045        throws MisconfiguredToolchainException
046    {
047        PersistedToolchains pers = toolchainsBuilder.build( context.getRequest().getUserToolchainsFile() );
048
049        List<ToolchainPrivate> toRet = new ArrayList<ToolchainPrivate>();
050
051        if ( pers != null )
052        {
053            List<ToolchainModel> lst = pers.getToolchains();
054            if ( lst != null )
055            {
056                for ( ToolchainModel toolchainModel : lst )
057                {
058                    ToolchainFactory fact = factories.get( toolchainModel.getType() );
059                    if ( fact != null )
060                    {
061                        toRet.add( fact.createToolchain( toolchainModel ) );
062                    }
063                    else
064                    {
065                        logger.error( "Missing toolchain factory for type: " + toolchainModel.getType()
066                            + ". Possibly caused by misconfigured project." );
067                    }
068                }
069            }
070        }
071
072        for ( ToolchainFactory toolchainFactory : factories.values() )
073        {
074            ToolchainPrivate tool = toolchainFactory.createDefaultToolchain();
075            if ( tool != null )
076            {
077                toRet.add( tool );
078            }
079        }
080
081        return toRet.toArray( new ToolchainPrivate[toRet.size()] );
082    }
083
084    public void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession session )
085    {
086        Map<String, Object> context = retrieveContext( session );
087        context.put( getStorageKey( toolchain.getType() ), toolchain.getModel() );
088    }
089
090}