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.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.descriptor.PluginDescriptor;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.toolchain.model.ToolchainModel;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.codehaus.plexus.logging.Logger;
35  
36  /**
37   * @author mkleint
38   */
39  @Component( role = ToolchainManager.class )
40  public class DefaultToolchainManager
41      implements ToolchainManager
42  {
43      @Requirement
44      Logger logger;
45  
46      @Requirement( role = ToolchainFactory.class )
47      Map<String, ToolchainFactory> factories;
48      
49      @Override
50      public Toolchain getToolchainFromBuildContext( String type, MavenSession session )
51      {
52          Map<String, Object> context = retrieveContext( session );
53  
54          ToolchainModel model = (ToolchainModel) context.get( getStorageKey( type ) );
55  
56          if ( model != null )
57          {
58              List<Toolchain> toolchains = selectToolchains( Collections.singletonList( model ), type, null );
59              
60              if ( !toolchains.isEmpty() )
61              {
62                  return toolchains.get( 0 );
63              }
64          }
65  
66          return null;
67      }
68  
69      @Override
70      public List<Toolchain> getToolchains( MavenSession session, String type, Map<String, String> requirements )
71      {
72          List<ToolchainModel> models = session.getRequest().getToolchains().get( type );
73  
74          return selectToolchains( models, type, requirements );
75      }
76  
77      private List<Toolchain> selectToolchains( List<ToolchainModel> models, String type,
78                                                Map<String, String> requirements )
79      {
80          List<Toolchain> toolchains = new ArrayList<>();
81  
82          if ( models != null )
83          {
84              ToolchainFactory fact = factories.get( type );
85  
86              if ( fact == null )
87              {
88                  logger.error( "Missing toolchain factory for type: " + type
89                      + ". Possibly caused by misconfigured project." );
90              }
91              else
92              {
93                  for ( ToolchainModel model : models )
94                  {
95                      try
96                      {
97                          ToolchainPrivate toolchain = fact.createToolchain( model );
98                          if ( requirements == null || toolchain.matchesRequirements( requirements ) )
99                          {
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 }