1 package org.apache.maven.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.inject.Singleton;
25
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30
31 import org.apache.maven.api.Session;
32 import org.apache.maven.api.Toolchain;
33 import org.apache.maven.api.services.ToolchainManager;
34 import org.apache.maven.api.services.ToolchainManagerException;
35 import org.apache.maven.execution.MavenSession;
36 import org.apache.maven.toolchain.DefaultToolchainManagerPrivate;
37 import org.apache.maven.toolchain.MisconfiguredToolchainException;
38 import org.apache.maven.toolchain.ToolchainPrivate;
39
40 @Named
41 @Singleton
42 public class DefaultToolchainManager implements ToolchainManager
43 {
44 private final DefaultToolchainManagerPrivate toolchainManagerPrivate;
45
46 @Inject
47 public DefaultToolchainManager( DefaultToolchainManagerPrivate toolchainManagerPrivate )
48 {
49 this.toolchainManagerPrivate = toolchainManagerPrivate;
50 }
51
52 @Override
53 public List<Toolchain> getToolchains( Session session, String type, Map<String, String> requirements )
54 throws ToolchainManagerException
55 {
56 MavenSession s = ( ( DefaultSession ) session ).getMavenSession();
57 List<org.apache.maven.toolchain.Toolchain> toolchains =
58 toolchainManagerPrivate.getToolchains( s, type, requirements );
59 return new MappedList<>( toolchains, this::toToolchain );
60 }
61
62 @Override
63 public Optional<Toolchain> getToolchainFromBuildContext( Session session, String type )
64 throws ToolchainManagerException
65 {
66 MavenSession s = ( ( DefaultSession ) session ).getMavenSession();
67 return Optional.ofNullable( toolchainManagerPrivate.getToolchainFromBuildContext( type, s ) )
68 .map( this::toToolchain );
69 }
70
71 @Override
72 public List<Toolchain> getToolchainsForType( Session session, String type )
73 throws ToolchainManagerException
74 {
75 try
76 {
77 MavenSession s = ( (DefaultSession) session ).getMavenSession();
78 ToolchainPrivate[] toolchains = toolchainManagerPrivate.getToolchainsForType( type, s );
79 return new MappedList<>( Arrays.asList( toolchains ), this::toToolchain );
80 }
81 catch ( MisconfiguredToolchainException e )
82 {
83 throw new ToolchainManagerException( "Unable to get toochains for type " + type, e );
84 }
85 }
86
87 @Override
88 public void storeToolchainToBuildContext( Session session, Toolchain toolchain )
89 throws ToolchainManagerException
90 {
91 MavenSession s = ( ( DefaultSession ) session ).getMavenSession();
92 org.apache.maven.toolchain.ToolchainPrivate tc =
93 (org.apache.maven.toolchain.ToolchainPrivate) ( (ToolchainWrapper) toolchain ).toolchain;
94 toolchainManagerPrivate.storeToolchainToBuildContext( tc, s );
95 }
96
97 private Toolchain toToolchain( org.apache.maven.toolchain.Toolchain toolchain )
98 {
99 return new ToolchainWrapper( toolchain );
100 }
101
102 private static class ToolchainWrapper implements Toolchain
103 {
104 private final org.apache.maven.toolchain.Toolchain toolchain;
105
106 ToolchainWrapper( org.apache.maven.toolchain.Toolchain toolchain )
107 {
108 this.toolchain = toolchain;
109 }
110
111 @Override
112 public String getType()
113 {
114 return toolchain.getType();
115 }
116
117 @Override
118 public String findTool( String toolName )
119 {
120 return toolchain.findTool( toolName );
121 }
122
123 @Override
124 public boolean matchesRequirements( Map<String, String> requirements )
125 {
126 return ( (ToolchainPrivate) toolchain ).matchesRequirements( requirements );
127 }
128 }
129 }