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.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
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<Toolchain>();
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
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;
138 }
139
140 }