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.HashMap;
23  import java.util.Map;
24  
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.plugin.descriptor.PluginDescriptor;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.toolchain.model.ToolchainModel;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.component.annotations.Requirement;
31  import org.codehaus.plexus.logging.Logger;
32  
33  /**
34   * @author mkleint
35   */
36  @Component( role = ToolchainManager.class )
37  public class DefaultToolchainManager
38      implements ToolchainManager
39  {
40      @Requirement
41      Logger logger;
42  
43      @Requirement( role = ToolchainFactory.class )
44      Map<String, ToolchainFactory> factories;
45  
46      public Toolchain getToolchainFromBuildContext( String type, MavenSession session )
47      {
48          Map<String, Object> context = retrieveContext( session );
49  
50          ToolchainModel model = (ToolchainModel) context.get( getStorageKey( type ) );
51  
52          if ( model != null )
53          {
54              try
55              {
56                  ToolchainFactory fact = factories.get( type );
57                  if ( fact != null )
58                  {
59                      return fact.createToolchain( model );
60                  }
61                  else
62                  {
63                      logger.error( "Missing toolchain factory for type: " + type
64                          + ". Possibly caused by misconfigured project." );
65                  }
66              }
67              catch ( MisconfiguredToolchainException ex )
68              {
69                  logger.error( "Misconfigured toolchain.", ex );
70              }
71          }
72  
73          return null;
74      }
75  
76      Map<String, Object> retrieveContext( MavenSession session )
77      {
78          Map<String, Object> context = null;
79  
80          if ( session != null )
81          {
82              PluginDescriptor desc = new PluginDescriptor();
83              desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
84              desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( "toolchains" ) );
85  
86              MavenProject current = session.getCurrentProject();
87  
88              if ( current != null )
89              {
90                  //TODO: why is this using the context
91                  context = session.getPluginContext( desc, current );
92              }
93          }
94  
95          return ( context != null ) ? context : new HashMap<String, Object>();
96      }
97  
98      public static final String getStorageKey( String type )
99      {
100         return "toolchain-" + type; // NOI18N
101     }
102 
103 }