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