1 package org.apache.maven.toolchain;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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;
101 }
102
103 }