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 javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.inject.Singleton;
31
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.plugin.descriptor.PluginDescriptor;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.toolchain.model.ToolchainModel;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import static java.util.Objects.requireNonNull;
40
41
42
43
44 @Named
45 @Singleton
46 public class DefaultToolchainManager
47 implements ToolchainManager
48 {
49 protected final Logger logger;
50
51 final Map<String, ToolchainFactory> factories;
52
53 @Inject
54 public DefaultToolchainManager( Map<String, ToolchainFactory> factories )
55 {
56 this.factories = factories;
57 this.logger = LoggerFactory.getLogger( DefaultToolchainManager.class );
58 }
59
60
61
62
63 DefaultToolchainManager( Map<String, ToolchainFactory> factories, Logger logger )
64 {
65 this.factories = factories;
66 this.logger = requireNonNull( logger );
67 }
68
69 @Override
70 public Toolchain getToolchainFromBuildContext( String type, MavenSession session )
71 {
72 Map<String, Object> context = retrieveContext( session );
73
74 ToolchainModel model = (ToolchainModel) context.get( getStorageKey( type ) );
75
76 if ( model != null )
77 {
78 List<Toolchain> toolchains = selectToolchains( Collections.singletonList( model ), type, null );
79
80 if ( !toolchains.isEmpty() )
81 {
82 return toolchains.get( 0 );
83 }
84 }
85
86 return null;
87 }
88
89 @Override
90 public List<Toolchain> getToolchains( MavenSession session, String type, Map<String, String> requirements )
91 {
92 List<ToolchainModel> models = session.getRequest().getToolchains().get( type );
93
94 return selectToolchains( models, type, requirements );
95 }
96
97 private List<Toolchain> selectToolchains( List<ToolchainModel> models, String type,
98 Map<String, String> requirements )
99 {
100 List<Toolchain> toolchains = new ArrayList<>();
101
102 if ( models != null )
103 {
104 ToolchainFactory fact = factories.get( type );
105
106 if ( fact == null )
107 {
108 logger.error( "Missing toolchain factory for type: " + type
109 + ". Possibly caused by misconfigured project." );
110 }
111 else
112 {
113 for ( ToolchainModel model : models )
114 {
115 try
116 {
117 ToolchainPrivate toolchain = fact.createToolchain( model );
118 if ( requirements == null || toolchain.matchesRequirements( requirements ) )
119 {
120 toolchains.add( toolchain );
121 }
122 }
123 catch ( MisconfiguredToolchainException ex )
124 {
125 logger.error( "Misconfigured toolchain.", ex );
126 }
127 }
128 }
129 }
130 return toolchains;
131 }
132
133 Map<String, Object> retrieveContext( MavenSession session )
134 {
135 Map<String, Object> context = null;
136
137 if ( session != null )
138 {
139 PluginDescriptor desc = new PluginDescriptor();
140 desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
141 desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( "toolchains" ) );
142
143 MavenProject current = session.getCurrentProject();
144
145 if ( current != null )
146 {
147
148 context = session.getPluginContext( desc, current );
149 }
150 }
151
152 return ( context != null ) ? context : new HashMap<>();
153 }
154
155 public static final String getStorageKey( String type )
156 {
157 return "toolchain-" + type;
158 }
159
160 }