1 package org.apache.maven.report.projectinfo.dependencies;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.net.UnknownHostException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.ArtifactUtils;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.manager.WagonConfigurationException;
30 import org.apache.maven.artifact.manager.WagonManager;
31 import org.apache.maven.artifact.metadata.ArtifactMetadata;
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.artifact.repository.metadata.Metadata;
34 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
35 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
36 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
37 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
38 import org.apache.maven.artifact.resolver.ArtifactResolver;
39 import org.apache.maven.plugin.logging.Log;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.project.MavenProjectBuilder;
42 import org.apache.maven.project.ProjectBuildingException;
43 import org.apache.maven.settings.Proxy;
44 import org.apache.maven.settings.Settings;
45 import org.apache.maven.wagon.ConnectionException;
46 import org.apache.maven.wagon.TransferFailedException;
47 import org.apache.maven.wagon.UnsupportedProtocolException;
48 import org.apache.maven.wagon.Wagon;
49 import org.apache.maven.wagon.authentication.AuthenticationException;
50 import org.apache.maven.wagon.authentication.AuthenticationInfo;
51 import org.apache.maven.wagon.authorization.AuthorizationException;
52 import org.apache.maven.wagon.observers.Debug;
53 import org.apache.maven.wagon.proxy.ProxyInfo;
54 import org.apache.maven.wagon.repository.Repository;
55 import org.codehaus.plexus.util.StringUtils;
56
57
58
59
60
61
62
63 public class RepositoryUtils
64 {
65 private static final List<String> UNKNOWN_HOSTS = new ArrayList<String>();
66
67 private final Log log;
68
69 private final WagonManager wagonManager;
70
71 private final Settings settings;
72
73 private final MavenProjectBuilder mavenProjectBuilder;
74
75 private final ArtifactFactory factory;
76
77 private final List<ArtifactRepository> remoteRepositories;
78
79 private final List<ArtifactRepository> pluginRepositories;
80
81 private final ArtifactResolver resolver;
82
83 private final ArtifactRepository localRepository;
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public RepositoryUtils( Log log, WagonManager wagonManager, Settings settings,
98 MavenProjectBuilder mavenProjectBuilder, ArtifactFactory factory,
99 ArtifactResolver resolver, List<ArtifactRepository> remoteRepositories,
100 List<ArtifactRepository> pluginRepositories, ArtifactRepository localRepository,
101 RepositoryMetadataManager repositoryMetadataManager )
102 {
103 this.log = log;
104 this.wagonManager = wagonManager;
105 this.settings = settings;
106 this.mavenProjectBuilder = mavenProjectBuilder;
107 this.factory = factory;
108 this.resolver = resolver;
109 this.remoteRepositories = remoteRepositories;
110 this.pluginRepositories = pluginRepositories;
111 this.localRepository = localRepository;
112 }
113
114
115
116
117 public ArtifactRepository getLocalRepository()
118 {
119 return localRepository;
120 }
121
122
123
124
125 public List<ArtifactRepository> getRemoteArtifactRepositories()
126 {
127 return remoteRepositories;
128 }
129
130
131
132
133 public List<ArtifactRepository> getPluginArtifactRepositories()
134 {
135 return pluginRepositories;
136 }
137
138
139
140
141
142
143
144 public void resolve( Artifact artifact )
145 throws ArtifactResolutionException, ArtifactNotFoundException
146 {
147 List<ArtifactRepository> repos =
148 new ArrayList<ArtifactRepository>( pluginRepositories.size() + remoteRepositories.size() );
149 repos.addAll( pluginRepositories );
150 repos.addAll( remoteRepositories );
151
152 resolver.resolve( artifact, repos, localRepository );
153 }
154
155
156
157
158
159
160
161 public boolean dependencyExistsInRepo( ArtifactRepository repo, Artifact artifact )
162 {
163 if ( repo.isBlacklisted() )
164 {
165 if ( log.isDebugEnabled() )
166 {
167 log.debug( "The repo '" + repo.getId() + "' is black listed - Ignored it" );
168 }
169 return false;
170 }
171
172 if ( UNKNOWN_HOSTS.contains( repo.getUrl() ) )
173 {
174 if ( log.isDebugEnabled() )
175 {
176 log.debug( "The repo url '" + repo.getUrl() + "' is unknowned - Ignored it" );
177 }
178 return false;
179 }
180
181 repo = wagonManager.getMirrorRepository( repo );
182
183 String id = repo.getId();
184 Repository repository = new Repository( id, repo.getUrl() );
185
186 Wagon wagon;
187 try
188 {
189 wagon = wagonManager.getWagon( repository );
190 }
191 catch ( UnsupportedProtocolException e )
192 {
193 log.error( "Unsupported protocol: '" + repo.getProtocol() + "'", e );
194 return false;
195 }
196 catch ( WagonConfigurationException e )
197 {
198 log.error( "Unsupported protocol: '" + repo.getProtocol() + "'", e );
199 return false;
200 }
201
202 wagon.setTimeout( 1000 );
203
204 if ( log.isDebugEnabled() )
205 {
206 Debug debug = new Debug();
207
208 wagon.addSessionListener( debug );
209 wagon.addTransferListener( debug );
210 }
211
212 try
213 {
214
215 AuthenticationInfo auth = wagonManager.getAuthenticationInfo( repo.getId() );
216
217 ProxyInfo proxyInfo = getProxyInfo();
218 if ( proxyInfo != null )
219 {
220 wagon.connect( repository, auth, proxyInfo );
221 }
222 else
223 {
224 wagon.connect( repository, auth );
225 }
226
227 return wagon.resourceExists( StringUtils.replace( getDependencyUrlFromRepository( artifact, repo ),
228 repo.getUrl(), "" ) );
229 }
230 catch ( ConnectionException e )
231 {
232 if ( log.isDebugEnabled() )
233 {
234 log.error( "Unable to connect to: " + repo.getUrl(), e );
235 }
236 else
237 {
238 log.error( "Unable to connect to: " + repo.getUrl() );
239 }
240 return false;
241 }
242 catch ( AuthenticationException e )
243 {
244 if ( log.isDebugEnabled() )
245 {
246 log.error( "Unable to connect to: " + repo.getUrl(), e );
247 }
248 else
249 {
250 log.error( "Unable to connect to: " + repo.getUrl() );
251 }
252 return false;
253 }
254 catch ( TransferFailedException e )
255 {
256 if ( e.getCause() instanceof UnknownHostException )
257 {
258 log.error( "Unknown host " + e.getCause().getMessage() + " - ignored it" );
259 UNKNOWN_HOSTS.add( repo.getUrl() );
260 }
261 else
262 {
263 if ( log.isDebugEnabled() )
264 {
265 log.error( "Unable to determine if resource " + artifact + " exists in " + repo.getUrl(), e );
266 }
267 else
268 {
269 log.error( "Unable to determine if resource " + artifact + " exists in " + repo.getUrl() );
270 }
271 }
272 return false;
273 }
274 catch ( AuthorizationException e )
275 {
276 if ( log.isDebugEnabled() )
277 {
278 log.error( "Unable to connect to: " + repo.getUrl(), e );
279 }
280 else
281 {
282 log.error( "Unable to connect to: " + repo.getUrl() );
283 }
284 return false;
285 }
286 catch ( AbstractMethodError e )
287 {
288 log.error( "Wagon " + wagon.getClass().getName() + " does not support the resourceExists method" );
289 return false;
290 }
291 finally
292 {
293 try
294 {
295 wagon.disconnect();
296 }
297 catch ( ConnectionException e )
298 {
299 if ( log.isDebugEnabled() )
300 {
301 log.error( "Error disconnecting wagon - ignored", e );
302 }
303 else
304 {
305 log.error( "Error disconnecting wagon - ignored" );
306 }
307 }
308 }
309 }
310
311
312
313
314
315
316
317
318 public MavenProject getMavenProjectFromRepository( Artifact artifact )
319 throws ProjectBuildingException
320 {
321 Artifact projectArtifact = artifact;
322
323 boolean allowStubModel = false;
324 if ( !"pom".equals( artifact.getType() ) )
325 {
326 projectArtifact = factory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
327 artifact.getVersion(), artifact.getScope() );
328 allowStubModel = true;
329 }
330
331
332 return mavenProjectBuilder.buildFromRepository( projectArtifact, remoteRepositories, localRepository,
333 allowStubModel );
334 }
335
336
337
338
339
340
341
342 public String getDependencyUrlFromRepository( Artifact artifact, ArtifactRepository repo )
343 {
344 if ( repo.isBlacklisted() )
345 {
346 return null;
347 }
348
349 Artifact copyArtifact = ArtifactUtils.copyArtifact( artifact );
350
351 if ( ( artifact.isSnapshot() && repo.getSnapshots().isEnabled() ) )
352 {
353 if ( artifact.getBaseVersion().equals( artifact.getVersion() ) )
354 {
355
356 if ( artifact.getMetadataList() == null || artifact.getMetadataList().isEmpty() )
357 {
358 try
359 {
360 resolve( artifact );
361 }
362 catch ( ArtifactResolutionException e )
363 {
364 log.error( "Artifact: " + artifact.getId() + " could not be resolved." );
365 }
366 catch ( ArtifactNotFoundException e )
367 {
368 log.error( "Artifact: " + artifact.getId() + " was not found." );
369 }
370 }
371
372 for ( ArtifactMetadata m : artifact.getMetadataList() )
373 {
374 if ( m instanceof SnapshotArtifactRepositoryMetadata )
375 {
376 SnapshotArtifactRepositoryMetadata snapshotMetadata = (SnapshotArtifactRepositoryMetadata) m;
377
378 Metadata metadata = snapshotMetadata.getMetadata();
379 if ( metadata.getVersioning() == null || metadata.getVersioning().getSnapshot() == null
380 || metadata.getVersioning().getSnapshot().isLocalCopy()
381 || metadata.getVersioning().getSnapshot().getTimestamp() == null )
382 {
383 continue;
384 }
385
386
387 String version =
388 StringUtils.replace( copyArtifact.getVersion(), Artifact.SNAPSHOT_VERSION,
389 metadata.getVersioning().getSnapshot().getTimestamp() )
390 + "-" + metadata.getVersioning().getSnapshot().getBuildNumber();
391 copyArtifact.setVersion( version );
392 }
393 }
394 }
395 }
396
397 return repo.getUrl() + "/" + repo.pathOf( copyArtifact );
398 }
399
400
401
402
403
404
405
406
407
408
409
410 private ProxyInfo getProxyInfo()
411 {
412 ProxyInfo proxyInfo = null;
413 if ( settings != null && settings.getActiveProxy() != null )
414 {
415 Proxy settingsProxy = settings.getActiveProxy();
416
417 proxyInfo = new ProxyInfo();
418 proxyInfo.setHost( settingsProxy.getHost() );
419 proxyInfo.setType( settingsProxy.getProtocol() );
420 proxyInfo.setPort( settingsProxy.getPort() );
421 proxyInfo.setNonProxyHosts( settingsProxy.getNonProxyHosts() );
422 proxyInfo.setUserName( settingsProxy.getUsername() );
423 proxyInfo.setPassword( settingsProxy.getPassword() );
424 }
425
426 return proxyInfo;
427 }
428 }