View Javadoc
1   package org.apache.maven.toolchain;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.inject.Inject;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.api.toolchain.ToolchainModel;
32  import org.slf4j.Logger;
33  
34  /**
35   * TODO: refactor this, component extending component is bad practice.
36   *
37   * @author mkleint
38   * @author Robert Scholte
39   */
40  @Named
41  @Singleton
42  public class DefaultToolchainManagerPrivate
43      extends DefaultToolchainManager
44      implements ToolchainManagerPrivate
45  {
46      @Inject
47      public DefaultToolchainManagerPrivate( Map<String, ToolchainFactory> factories )
48      {
49          super( factories );
50      }
51  
52      /**
53       * Ctor needed for UT.
54       */
55      DefaultToolchainManagerPrivate( Map<String, ToolchainFactory> factories, Logger logger )
56      {
57          super( factories, logger );
58      }
59  
60      @Override
61      public ToolchainPrivate[] getToolchainsForType( String type, MavenSession session )
62          throws MisconfiguredToolchainException
63      {
64          List<ToolchainPrivate> toRet = new ArrayList<>();
65  
66          ToolchainFactory fact = factories.get( type );
67          if ( fact == null )
68          {
69              logger.error( "Missing toolchain factory for type: " + type
70                  + ". Possibly caused by misconfigured project." );
71          }
72          else
73          {
74              List<ToolchainModel> availableToolchains =
75                      org.apache.maven.toolchain.model.ToolchainModel.toolchainModelToApiV4(
76                              session.getRequest().getToolchains().get( type ) );
77  
78              if ( availableToolchains != null )
79              {
80                  for ( ToolchainModel toolchainModel : availableToolchains )
81                  {
82                      org.apache.maven.toolchain.model.ToolchainModel tm =
83                              new org.apache.maven.toolchain.model.ToolchainModel( toolchainModel );
84                      toRet.add( fact.createToolchain( tm ) );
85                  }
86              }
87  
88              // add default toolchain
89              ToolchainPrivate tool = fact.createDefaultToolchain();
90              if ( tool != null )
91              {
92                  toRet.add( tool );
93              }
94          }
95  
96          return toRet.toArray( new ToolchainPrivate[0] );
97      }
98  
99      @Override
100     public void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession session )
101     {
102         Map<String, Object> context = retrieveContext( session );
103         context.put( getStorageKey( toolchain.getType() ), toolchain.getModel() );
104     }
105 
106 }