001package org.eclipse.aether.transfer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.RepositoryException;
023import org.eclipse.aether.metadata.Metadata;
024import org.eclipse.aether.repository.RemoteRepository;
025
026/**
027 * Thrown when metadata could not be uploaded/downloaded to/from a particular remote repository.
028 */
029public class MetadataTransferException
030    extends RepositoryException
031{
032
033    private final transient Metadata metadata;
034
035    private final transient RemoteRepository repository;
036
037    private final boolean fromCache;
038
039    static String getString( String prefix, RemoteRepository repository )
040    {
041        if ( repository == null )
042        {
043            return "";
044        }
045        else
046        {
047            return prefix + repository.getId() + " (" + repository.getUrl() + ")";
048        }
049    }
050
051    /**
052     * Creates a new exception with the specified metadata, repository and detail message.
053     * 
054     * @param metadata The untransferable metadata, may be {@code null}.
055     * @param repository The involved remote repository, may be {@code null}.
056     * @param message The detail message, may be {@code null}.
057     */
058    public MetadataTransferException( Metadata metadata, RemoteRepository repository, String message )
059    {
060        this( metadata, repository, message, false );
061    }
062
063    /**
064     * Creates a new exception with the specified metadata, repository and detail message.
065     * 
066     * @param metadata The untransferable metadata, may be {@code null}.
067     * @param repository The involved remote repository, may be {@code null}.
068     * @param message The detail message, may be {@code null}.
069     * @param fromCache {@code true} if the exception was played back from the error cache, {@code false} if the
070     *            exception actually just occurred.
071     */
072    public MetadataTransferException( Metadata metadata, RemoteRepository repository, String message,
073                                      boolean fromCache )
074    {
075        super( message );
076        this.metadata = metadata;
077        this.repository = repository;
078        this.fromCache = fromCache;
079    }
080
081    /**
082     * Creates a new exception with the specified metadata, repository and cause.
083     * 
084     * @param metadata The untransferable metadata, may be {@code null}.
085     * @param repository The involved remote repository, may be {@code null}.
086     * @param cause The exception that caused this one, may be {@code null}.
087     */
088    public MetadataTransferException( Metadata metadata, RemoteRepository repository, Throwable cause )
089    {
090        this( metadata, repository, "Could not transfer metadata " + metadata + getString( " from/to ", repository )
091            + getMessage( ": ", cause ), cause );
092    }
093
094    /**
095     * Creates a new exception with the specified metadata, repository, detail message and cause.
096     * 
097     * @param metadata The untransferable metadata, may be {@code null}.
098     * @param repository The involved remote repository, may be {@code null}.
099     * @param message The detail message, may be {@code null}.
100     * @param cause The exception that caused this one, may be {@code null}.
101     */
102    public MetadataTransferException( Metadata metadata, RemoteRepository repository, String message, Throwable cause )
103    {
104        super( message, cause );
105        this.metadata = metadata;
106        this.repository = repository;
107        this.fromCache = false;
108    }
109
110    /**
111     * Gets the metadata that could not be transferred.
112     * 
113     * @return The troublesome metadata or {@code null} if unknown.
114     */
115    public Metadata getMetadata()
116    {
117        return metadata;
118    }
119
120    /**
121     * Gets the remote repository involved in the transfer.
122     * 
123     * @return The involved remote repository or {@code null} if unknown.
124     */
125    public RemoteRepository getRepository()
126    {
127        return repository;
128    }
129
130    /**
131     * Indicates whether this exception actually just occurred or was played back from the error cache.
132     * 
133     * @return {@code true} if the exception was played back from the error cache, {@code false} if the exception
134     *         actually occurred just now.
135     */
136    public boolean isFromCache()
137    {
138        return fromCache;
139    }
140
141}