1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.apache.maven.toolchain; |
21 | |
|
22 | |
import java.io.BufferedInputStream; |
23 | |
import java.io.File; |
24 | |
import java.io.FileInputStream; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.lang.reflect.InvocationTargetException; |
28 | |
import java.lang.reflect.Method; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Collections; |
31 | |
import java.util.HashMap; |
32 | |
import java.util.Iterator; |
33 | |
import java.util.List; |
34 | |
import java.util.Map; |
35 | |
import org.apache.maven.execution.MavenSession; |
36 | |
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
37 | |
import org.apache.maven.project.MavenProject; |
38 | |
import org.apache.maven.toolchain.model.PersistedToolchains; |
39 | |
import org.apache.maven.toolchain.model.ToolchainModel; |
40 | |
import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader; |
41 | |
import org.codehaus.plexus.PlexusConstants; |
42 | |
import org.codehaus.plexus.PlexusContainer; |
43 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
44 | |
import org.codehaus.plexus.context.Context; |
45 | |
import org.codehaus.plexus.context.ContextException; |
46 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
47 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class DefaultToolchainManager extends AbstractLogEnabled |
54 | |
implements ToolchainManager, |
55 | |
ToolchainManagerPrivate, |
56 | |
Contextualizable |
57 | |
{ |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private PlexusContainer container; |
63 | |
|
64 | |
public DefaultToolchainManager( ) |
65 | 0 | { |
66 | 0 | } |
67 | |
|
68 | |
public void contextualize( Context context ) |
69 | |
throws ContextException |
70 | |
{ |
71 | 0 | container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY); |
72 | 0 | } |
73 | |
|
74 | |
public ToolchainPrivate[] getToolchainsForType( String type ) |
75 | |
throws MisconfiguredToolchainException |
76 | |
{ |
77 | |
try |
78 | |
{ |
79 | 0 | PersistedToolchains pers = readToolchainSettings (); |
80 | 0 | Map factories = container.lookupMap( ToolchainFactory.ROLE ); |
81 | 0 | List toRet = new ArrayList( ); |
82 | 0 | if ( pers != null ) |
83 | |
{ |
84 | 0 | List lst = pers.getToolchains(); |
85 | 0 | if ( lst != null ) |
86 | |
{ |
87 | 0 | Iterator it = lst.iterator(); |
88 | 0 | while ( it.hasNext() ) |
89 | |
{ |
90 | 0 | ToolchainModel toolchainModel = (ToolchainModel) it.next(); |
91 | 0 | ToolchainFactory fact = (ToolchainFactory) factories.get( toolchainModel.getType() ); |
92 | 0 | if ( fact != null ) |
93 | |
{ |
94 | 0 | toRet.add( fact.createToolchain( toolchainModel ) ); |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 0 | getLogger().error("Missing toolchain factory for type:" + toolchainModel.getType() + ". Possibly caused by misconfigured project."); |
99 | |
} |
100 | 0 | } |
101 | |
} |
102 | |
} |
103 | 0 | Iterator it = factories.values().iterator(); |
104 | 0 | while ( it.hasNext() ) |
105 | |
{ |
106 | 0 | ToolchainFactory fact = (ToolchainFactory) it.next(); |
107 | 0 | ToolchainPrivate tool = fact.createDefaultToolchain(); |
108 | 0 | if ( tool != null ) |
109 | |
{ |
110 | 0 | toRet.add( tool ); |
111 | |
} |
112 | 0 | } |
113 | 0 | ToolchainPrivate[] tc = new ToolchainPrivate[ toRet.size() ]; |
114 | 0 | return (ToolchainPrivate[]) toRet.toArray(tc); |
115 | |
} |
116 | 0 | catch ( ComponentLookupException ex ) |
117 | |
{ |
118 | 0 | getLogger().fatalError("Error in component lookup", ex); |
119 | |
} |
120 | 0 | return new ToolchainPrivate[0]; |
121 | |
} |
122 | |
|
123 | |
public Toolchain getToolchainFromBuildContext( String type, |
124 | |
MavenSession session ) |
125 | |
{ |
126 | 0 | Map context = retrieveContext(session); |
127 | 0 | if ( "javac".equals( type )) |
128 | |
{ |
129 | |
|
130 | 0 | type = "jdk"; |
131 | |
} |
132 | 0 | Object obj = context.get( getStorageKey( type ) ); |
133 | 0 | ToolchainModel model = (ToolchainModel)obj; |
134 | |
|
135 | 0 | if ( model != null ) |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | ToolchainFactory fact = (ToolchainFactory) container.lookup(ToolchainFactory.ROLE, type); |
140 | 0 | return fact.createToolchain( model ); |
141 | |
} |
142 | 0 | catch ( ComponentLookupException ex ) |
143 | |
{ |
144 | 0 | getLogger().fatalError("Error in component lookup", ex); |
145 | |
} |
146 | 0 | catch ( MisconfiguredToolchainException ex ) |
147 | |
{ |
148 | 0 | getLogger().error("Misconfigured toolchain.", ex); |
149 | 0 | } |
150 | |
} |
151 | 0 | return null; |
152 | |
} |
153 | |
|
154 | |
private MavenProject getCurrentProject(MavenSession session) { |
155 | |
|
156 | |
try |
157 | |
{ |
158 | 0 | Method meth = session.getClass().getMethod("getCurrentProject", new Class[0]); |
159 | 0 | return (MavenProject) meth.invoke(session, null); |
160 | 0 | } catch (Exception ex) |
161 | |
{ |
162 | |
|
163 | |
} |
164 | 0 | return null; |
165 | |
} |
166 | |
|
167 | |
private Map retrieveContext( MavenSession session ) |
168 | |
{ |
169 | 0 | if (session == null) |
170 | |
{ |
171 | 0 | return new HashMap(); |
172 | |
} |
173 | 0 | PluginDescriptor desc = new PluginDescriptor(); |
174 | 0 | desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() ); |
175 | 0 | desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId ("toolchains") ); |
176 | 0 | MavenProject current = getCurrentProject(session); |
177 | 0 | if ( current != null ) |
178 | |
{ |
179 | 0 | return session.getPluginContext( desc, current ); |
180 | |
|
181 | |
} |
182 | 0 | return new HashMap(); |
183 | |
} |
184 | |
|
185 | |
|
186 | |
public void storeToolchainToBuildContext( ToolchainPrivate toolchain, |
187 | |
MavenSession session ) |
188 | |
{ |
189 | 0 | Map context = retrieveContext( session ); |
190 | 0 | context.put( getStorageKey( toolchain.getType() ), toolchain.getModel () ); |
191 | 0 | } |
192 | |
|
193 | |
public static final String getStorageKey( String type ) |
194 | |
{ |
195 | 0 | return "toolchain-" + type; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
private PersistedToolchains readToolchainSettings( ) |
200 | |
throws MisconfiguredToolchainException |
201 | |
{ |
202 | |
|
203 | 0 | File tch = new File( System.getProperty( "user.home" ), |
204 | |
".m2/toolchains.xml" ); |
205 | 0 | if ( tch.exists() ) |
206 | |
{ |
207 | 0 | MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader(); |
208 | 0 | InputStreamReader in = null; |
209 | |
try |
210 | |
{ |
211 | 0 | in = new InputStreamReader( new BufferedInputStream( new FileInputStream( tch ) ) ); |
212 | 0 | return reader.read( in ); |
213 | |
} |
214 | 0 | catch ( Exception ex ) |
215 | |
{ |
216 | 0 | throw new MisconfiguredToolchainException( "Cannot read toolchains file at " + tch.getAbsolutePath( ), |
217 | |
ex ); |
218 | |
} |
219 | |
finally |
220 | |
{ |
221 | 0 | if (in != null) |
222 | |
{ |
223 | |
try |
224 | |
{ |
225 | 0 | in.close(); |
226 | |
} |
227 | 0 | catch (IOException ex) |
228 | 0 | { } |
229 | |
} |
230 | |
|
231 | |
} |
232 | |
} |
233 | |
else |
234 | |
{ |
235 | |
|
236 | |
} |
237 | 0 | return null; |
238 | |
} |
239 | |
} |