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.metadata.Metadata;
23  import org.eclipse.aether.repository.RemoteRepository;
24  
25  /**
26   * Thrown when metadata could not be uploaded/downloaded to/from a particular remote repository.
27   */
28  public class MetadataTransferException extends RepositoryException {
29  
30      private final transient Metadata metadata;
31  
32      private final transient RemoteRepository repository;
33  
34      /** Whether this exception was played back from the error cache. */
35      private final boolean fromCache;
36  
37      static String getString(String prefix, RemoteRepository repository) {
38          if (repository == null) {
39              return "";
40          } else {
41              return prefix + repository.getId() + " (" + repository.getUrl() + ")";
42          }
43      }
44  
45      /**
46       * Creates a new exception with the specified metadata, repository and detail message.
47       *
48       * @param metadata The untransferable metadata, may be {@code null}.
49       * @param repository The involved remote repository, may be {@code null}.
50       * @param message The detail message, may be {@code null}.
51       */
52      public MetadataTransferException(Metadata metadata, RemoteRepository repository, String message) {
53          this(metadata, repository, message, false);
54      }
55  
56      /**
57       * Creates a new exception with the specified metadata, repository and detail message.
58       *
59       * @param metadata The untransferable metadata, may be {@code null}.
60       * @param repository The involved remote repository, may be {@code null}.
61       * @param message The detail message, may be {@code null}.
62       * @param fromCache {@code true} if the exception was played back from the error cache, {@code false} if the
63       *            exception actually just occurred.
64       */
65      public MetadataTransferException(
66              Metadata metadata, RemoteRepository repository, String message, boolean fromCache) {
67          super(message);
68          this.metadata = metadata;
69          this.repository = repository;
70          this.fromCache = fromCache;
71      }
72  
73      /**
74       * Creates a new exception with the specified metadata, repository and cause.
75       *
76       * @param metadata The untransferable metadata, may be {@code null}.
77       * @param repository The involved remote repository, may be {@code null}.
78       * @param cause The exception that caused this one, may be {@code null}.
79       */
80      public MetadataTransferException(Metadata metadata, RemoteRepository repository, Throwable cause) {
81          this(
82                  metadata,
83                  repository,
84                  "Could not transfer metadata " + metadata + getString(" from/to ", repository)
85                          + getMessage(": ", cause),
86                  cause);
87      }
88  
89      /**
90       * Creates a new exception with the specified metadata, repository, detail message and cause.
91       *
92       * @param metadata The untransferable metadata, may be {@code null}.
93       * @param repository The involved remote repository, may be {@code null}.
94       * @param message The detail message, may be {@code null}.
95       * @param cause The exception that caused this one, may be {@code null}.
96       */
97      public MetadataTransferException(Metadata metadata, RemoteRepository repository, String message, Throwable cause) {
98          super(message, cause);
99          this.metadata = metadata;
100         this.repository = repository;
101         this.fromCache = false;
102     }
103 
104     /**
105      * Gets the metadata that could not be transferred.
106      *
107      * @return The troublesome metadata or {@code null} if unknown.
108      */
109     public Metadata getMetadata() {
110         return metadata;
111     }
112 
113     /**
114      * Gets the remote repository involved in the transfer.
115      *
116      * @return The involved remote repository or {@code null} if unknown.
117      */
118     public RemoteRepository getRepository() {
119         return repository;
120     }
121 
122     /**
123      * Indicates whether this exception actually just occurred or was played back from the error cache.
124      *
125      * @return {@code true} if the exception was played back from the error cache, {@code false} if the exception
126      *         actually occurred just now.
127      */
128     public boolean isFromCache() {
129         return fromCache;
130     }
131 }