1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.resolution
;
13 import java
.util
.Collections
;
14 import java
.util
.List
;
16 import org
.eclipse
.aether
.RepositoryException
;
17 import org
.eclipse
.aether
.transfer
.ArtifactNotFoundException
;
18 import org
.eclipse
.aether
.transfer
.RepositoryOfflineException
;
21 * Thrown in case of a unresolvable artifacts.
23 public class ArtifactResolutionException
24 extends RepositoryException
27 private final transient List
<ArtifactResult
> results
;
30 * Creates a new exception with the specified results.
32 * @param results The resolution results at the point the exception occurred, may be {@code null}.
34 public ArtifactResolutionException( List
<ArtifactResult
> results
)
36 super( getMessage( results
), getCause( results
) );
37 this.results
= ( results
!= null ) ? results
: Collections
.<ArtifactResult
> emptyList();
41 * Creates a new exception with the specified results and detail message.
43 * @param results The resolution results at the point the exception occurred, may be {@code null}.
44 * @param message The detail message, may be {@code null}.
46 public ArtifactResolutionException( List
<ArtifactResult
> results
, String message
)
48 super( message
, getCause( results
) );
49 this.results
= ( results
!= null ) ? results
: Collections
.<ArtifactResult
> emptyList();
53 * Creates a new exception with the specified results, detail message and cause.
55 * @param results The resolution results at the point the exception occurred, may be {@code null}.
56 * @param message The detail message, may be {@code null}.
57 * @param cause The exception that caused this one, may be {@code null}.
59 public ArtifactResolutionException( List
<ArtifactResult
> results
, String message
, Throwable cause
)
61 super( message
, cause
);
62 this.results
= ( results
!= null ) ? results
: Collections
.<ArtifactResult
> emptyList();
66 * Gets the resolution results at the point the exception occurred. Despite being incomplete, callers might want to
67 * use these results to fail gracefully and continue their operation with whatever interim data has been gathered.
69 * @return The resolution results or {@code null} if unknown.
71 public List
<ArtifactResult
> getResults()
77 * Gets the first result from {@link #getResults()}. This is a convenience method for cases where callers know only
78 * a single result/request is involved.
80 * @return The (first) resolution result or {@code null} if none.
82 public ArtifactResult
getResult()
84 return ( results
!= null && !results
.isEmpty() ) ? results
.get( 0 ) : null;
87 private static String
getMessage( List
<?
extends ArtifactResult
> results
)
89 StringBuilder buffer
= new StringBuilder( 256 );
91 buffer
.append( "The following artifacts could not be resolved: " );
96 for ( ArtifactResult result
: results
)
98 if ( !result
.isResolved() )
102 buffer
.append( sep
);
103 buffer
.append( result
.getRequest().getArtifact() );
108 Throwable cause
= getCause( results
);
111 if ( unresolved
== 1 )
113 buffer
.setLength( 0 );
114 buffer
.append( cause
.getMessage() );
118 buffer
.append( ": " ).append( cause
.getMessage() );
122 return buffer
.toString();
125 private static Throwable
getCause( List
<?
extends ArtifactResult
> results
)
127 for ( ArtifactResult result
: results
)
129 if ( !result
.isResolved() )
131 Throwable notFound
= null, offline
= null;
132 for ( Throwable t
: result
.getExceptions() )
134 if ( t
instanceof ArtifactNotFoundException
)
136 if ( notFound
== null )
140 if ( offline
== null && t
.getCause() instanceof RepositoryOfflineException
)
151 if ( offline
!= null )
155 if ( notFound
!= null )