package org.apache.maven.toolchain;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.model.ToolchainModel;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;

/**
 * @author mkleint
 */
@Component( role = ToolchainManagerPrivate.class )
public class DefaultToolchainManagerPrivate
    extends DefaultToolchainManager
    implements ToolchainManagerPrivate
{

    @Requirement
    private ToolchainsBuilder toolchainsBuilder;

    public ToolchainPrivate[] getToolchainsForType( String type, MavenSession context )
        throws MisconfiguredToolchainException
    {
        PersistedToolchains pers = toolchainsBuilder.build( context.getRequest().getUserToolchainsFile() );

        List<ToolchainPrivate> toRet = new ArrayList<ToolchainPrivate>();

        if ( pers != null )
        {
            List<ToolchainModel> lst = pers.getToolchains();
            if ( lst != null )
            {
                for ( ToolchainModel toolchainModel : lst )
                {
                    ToolchainFactory fact = factories.get( toolchainModel.getType() );
                    if ( fact != null )
                    {
                        toRet.add( fact.createToolchain( toolchainModel ) );
                    }
                    else
                    {
                        logger.error( "Missing toolchain factory for type: " + toolchainModel.getType()
                            + ". Possibly caused by misconfigured project." );
                    }
                }
            }
        }

        for ( ToolchainFactory toolchainFactory : factories.values() )
        {
            ToolchainPrivate tool = toolchainFactory.createDefaultToolchain();
            if ( tool != null )
            {
                toRet.add( tool );
            }
        }

        return toRet.toArray( new ToolchainPrivate[toRet.size()] );
    }

    public void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession session )
    {
        Map<String, Object> context = retrieveContext( session );
        context.put( getStorageKey( toolchain.getType() ), toolchain.getModel() );
    }

}

@@ -1804,96 +1804,52 @@
     
-if ( pers != null )%0A        %7B%0A            List%3CToolchainModel%3E lst = pers.getToolchains(
+ToolchainFactory fact = factories.get( type 
 );%0A 
@@ -1859,22 +1859,19 @@
     
-    
 if ( 
-lst !
+fact =
 = nu
@@ -1867,36 +1867,32 @@
  fact == null )%0A
-    
         %7B%0A      
@@ -1901,55 +1901,134 @@
     
-    f
+logger.err
 or
- 
 ( 
-ToolchainModel toolchainModel : lst
+%22Missing toolchain factory for type: %22 + type%0A                + %22. Possibly caused by misconfigured project.%22
  )
+;
 %0A   
@@ -2028,32 +2028,34 @@
  );%0A        
+%7D%0A
         
 %7B%0A          
@@ -2034,33 +2034,56 @@
       %7D%0A        
-%7B
+else if ( pers != null )
 %0A               
@@ -2067,32 +2067,34 @@
  null )%0A        
+%7B%0A
             Tool
@@ -2081,32 +2081,37 @@
   %7B%0A            
+List%3C
 ToolchainFactory
@@ -2107,47 +2107,38 @@
 hain
-Factory fact = factorie
+Model%3E lst = per
 s.get
-( t
+T
 oolchain
 Mode
@@ -2137,31 +2137,13 @@
 hain
-Model.getType() );%0A    
+s();%0A
     
@@ -2150,28 +2150,23 @@
         
-    
 if ( 
-fac
+ls
 t != nul
@@ -2165,32 +2165,24 @@
 t != null )%0A
-        
             
@@ -2203,38 +2203,14 @@
     
-        toRet.add( fact.create
+for ( 
 Tool
@@ -2214,17 +2214,21 @@
 oolchain
-(
+Model
  toolcha
@@ -2235,20 +2235,23 @@
 inModel 
-)
+: lst
  )
-;
 %0A       
@@ -2251,37 +2251,33 @@
                 
-    %7D
+%7B
 %0A               
@@ -2281,20 +2281,62 @@
         
-else
+if ( type.equals( toolchainModel.getType() ) )
 %0A       
@@ -2378,163 +2378,57 @@
     
-logger.error( %22Missing toolchain factory for type: %22 + toolchainModel.getType()%0A                            + %22. Possibly caused by misconfigured project.%22
+toRet.add( fact.createToolchain( toolchainModel )
  );%0A

return toolchains that are of expected type