View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transfer;
20  
21  import org.eclipse.aether.RepositoryException;
22  import org.eclipse.aether.artifact.Artifact;
23  import org.eclipse.aether.repository.RemoteRepository;
24  
25  /**
26   * Thrown when an artifact could not be uploaded/downloaded to/from a particular remote repository.
27   */
28  public class ArtifactTransferException extends RepositoryException {
29  
30      private final transient Artifact artifact;
31  
32      private final transient RemoteRepository repository;
33  
34      private final boolean fromCache;
35  
36      static String getString(String prefix, RemoteRepository repository) {
37          if (repository == null) {
38              return "";
39          } else {
40              return prefix + repository.getId() + " (" + repository.getUrl() + ")";
41          }
42      }
43  
44      /**
45       * Creates a new exception with the specified artifact, repository and detail message.
46       *
47       * @param artifact The untransferable artifact, may be {@code null}.
48       * @param repository The involved remote repository, may be {@code null}.
49       * @param message The detail message, may be {@code null}.
50       */
51      public ArtifactTransferException(Artifact artifact, RemoteRepository repository, String message) {
52          this(artifact, repository, message, false);
53      }
54  
55      /**
56       * Creates a new exception with the specified artifact, repository and detail message.
57       *
58       * @param artifact The untransferable artifact, may be {@code null}.
59       * @param repository The involved remote repository, may be {@code null}.
60       * @param message The detail message, may be {@code null}.
61       * @param fromCache {@code true} if the exception was played back from the error cache, {@code false} if the
62       *            exception actually just occurred.
63       */
64      public ArtifactTransferException(
65              Artifact artifact, RemoteRepository repository, String message, boolean fromCache) {
66          super(message);
67          this.artifact = artifact;
68          this.repository = repository;
69          this.fromCache = fromCache;
70      }
71  
72      /**
73       * Creates a new exception with the specified artifact, repository and cause.
74       *
75       * @param artifact The untransferable artifact, may be {@code null}.
76       * @param repository The involved remote repository, may be {@code null}.
77       * @param cause The exception that caused this one, may be {@code null}.
78       */
79      public ArtifactTransferException(Artifact artifact, RemoteRepository repository, Throwable cause) {
80          this(
81                  artifact,
82                  repository,
83                  "Could not transfer artifact " + artifact + getString(" from/to ", repository)
84                          + getMessage(": ", cause),
85                  cause);
86      }
87  
88      /**
89       * Creates a new exception with the specified artifact, repository, detail message and cause.
90       *
91       * @param artifact The untransferable artifact, may be {@code null}.
92       * @param repository The involved remote repository, may be {@code null}.
93       * @param message The detail message, may be {@code null}.
94       * @param cause The exception that caused this one, may be {@code null}.
95       */
96      public ArtifactTransferException(Artifact artifact, RemoteRepository repository, String message, Throwable cause) {
97          super(message, cause);
98          this.artifact = artifact;
99          this.repository = repository;
100         this.fromCache = false;
101     }
102 
103     /**
104      * Gets the artifact that could not be transferred.
105      *
106      * @return The troublesome artifact or {@code null} if unknown.
107      */
108     public Artifact getArtifact() {
109         return artifact;
110     }
111 
112     /**
113      * Gets the remote repository involved in the transfer.
114      *
115      * @return The involved remote repository or {@code null} if unknown.
116      */
117     public RemoteRepository getRepository() {
118         return repository;
119     }
120 
121     /**
122      * Indicates whether this exception actually just occurred or was played back from the error cache.
123      *
124      * @return {@code true} if the exception was played back from the error cache, {@code false} if the exception
125      *         actually occurred just now.
126      */
127     public boolean isFromCache() {
128         return fromCache;
129     }
130 }