]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/eclipse/aether/resolution/ArtifactResolutionException.java
Merge branch 'master' of https://github.com/argeo/argeo-slc.git
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / eclipse / aether / resolution / ArtifactResolutionException.java
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
7 *
8 * Contributors:
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.aether.resolution;
12
13 import java.util.Collections;
14 import java.util.List;
15
16 import org.eclipse.aether.RepositoryException;
17 import org.eclipse.aether.transfer.ArtifactNotFoundException;
18 import org.eclipse.aether.transfer.RepositoryOfflineException;
19
20 /**
21 * Thrown in case of a unresolvable artifacts.
22 */
23 public class ArtifactResolutionException
24 extends RepositoryException
25 {
26
27 private final transient List<ArtifactResult> results;
28
29 /**
30 * Creates a new exception with the specified results.
31 *
32 * @param results The resolution results at the point the exception occurred, may be {@code null}.
33 */
34 public ArtifactResolutionException( List<ArtifactResult> results )
35 {
36 super( getMessage( results ), getCause( results ) );
37 this.results = ( results != null ) ? results : Collections.<ArtifactResult> emptyList();
38 }
39
40 /**
41 * Creates a new exception with the specified results and detail message.
42 *
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}.
45 */
46 public ArtifactResolutionException( List<ArtifactResult> results, String message )
47 {
48 super( message, getCause( results ) );
49 this.results = ( results != null ) ? results : Collections.<ArtifactResult> emptyList();
50 }
51
52 /**
53 * Creates a new exception with the specified results, detail message and cause.
54 *
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}.
58 */
59 public ArtifactResolutionException( List<ArtifactResult> results, String message, Throwable cause )
60 {
61 super( message, cause );
62 this.results = ( results != null ) ? results : Collections.<ArtifactResult> emptyList();
63 }
64
65 /**
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.
68 *
69 * @return The resolution results or {@code null} if unknown.
70 */
71 public List<ArtifactResult> getResults()
72 {
73 return results;
74 }
75
76 /**
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.
79 *
80 * @return The (first) resolution result or {@code null} if none.
81 */
82 public ArtifactResult getResult()
83 {
84 return ( results != null && !results.isEmpty() ) ? results.get( 0 ) : null;
85 }
86
87 private static String getMessage( List<? extends ArtifactResult> results )
88 {
89 StringBuilder buffer = new StringBuilder( 256 );
90
91 buffer.append( "The following artifacts could not be resolved: " );
92
93 int unresolved = 0;
94
95 String sep = "";
96 for ( ArtifactResult result : results )
97 {
98 if ( !result.isResolved() )
99 {
100 unresolved++;
101
102 buffer.append( sep );
103 buffer.append( result.getRequest().getArtifact() );
104 sep = ", ";
105 }
106 }
107
108 Throwable cause = getCause( results );
109 if ( cause != null )
110 {
111 if ( unresolved == 1 )
112 {
113 buffer.setLength( 0 );
114 buffer.append( cause.getMessage() );
115 }
116 else
117 {
118 buffer.append( ": " ).append( cause.getMessage() );
119 }
120 }
121
122 return buffer.toString();
123 }
124
125 private static Throwable getCause( List<? extends ArtifactResult> results )
126 {
127 for ( ArtifactResult result : results )
128 {
129 if ( !result.isResolved() )
130 {
131 Throwable notFound = null, offline = null;
132 for ( Throwable t : result.getExceptions() )
133 {
134 if ( t instanceof ArtifactNotFoundException )
135 {
136 if ( notFound == null )
137 {
138 notFound = t;
139 }
140 if ( offline == null && t.getCause() instanceof RepositoryOfflineException )
141 {
142 offline = t;
143 }
144 }
145 else
146 {
147 return t;
148 }
149
150 }
151 if ( offline != null )
152 {
153 return offline;
154 }
155 if ( notFound != null )
156 {
157 return notFound;
158 }
159 }
160 }
161 return null;
162 }
163
164 }