001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2016 Richard "Shred" Körber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j.exception;
015
016import static java.util.Objects.requireNonNull;
017
018import java.io.Serial;
019import java.net.URL;
020
021import org.shredzone.acme4j.AcmeResource;
022
023/**
024 * A runtime exception that is thrown when an {@link AcmeException} occured while trying
025 * to lazy-load a resource from the ACME server. It contains the original cause of the
026 * exception and a reference to the resource that could not be lazy-loaded. It is usually
027 * thrown by getter methods, so the API is not polluted with checked exceptions.
028 */
029public class AcmeLazyLoadingException extends RuntimeException {
030    @Serial
031    private static final long serialVersionUID = 1000353433913721901L;
032
033    private final Class<? extends AcmeResource> type;
034    private final URL location;
035
036    /**
037     * Creates a new {@link AcmeLazyLoadingException}.
038     *
039     * @param resource
040     *            {@link AcmeResource} to be loaded
041     * @param cause
042     *            {@link AcmeException} that was raised
043     */
044    public AcmeLazyLoadingException(AcmeResource resource, AcmeException cause) {
045        this(requireNonNull(resource).getClass(), requireNonNull(resource).getLocation(), cause);
046    }
047
048    /**
049     * Creates a new {@link AcmeLazyLoadingException}.
050     * <p>
051     * This constructor is used if there is no actual instance of the resource.
052     *
053     * @param type
054     *         {@link AcmeResource} type to be loaded
055     * @param location
056     *         Resource location
057     * @param cause
058     *         {@link AcmeException} that was raised
059     * @since 2.8
060     */
061    public AcmeLazyLoadingException(Class<? extends AcmeResource> type, URL location, AcmeException cause) {
062        super(requireNonNull(type).getSimpleName() + " " + requireNonNull(location), requireNonNull(cause));
063        this.type = type;
064        this.location = location;
065    }
066
067    /**
068     * Returns the {@link AcmeResource} type of the resource that could not be loaded.
069     */
070    public Class<? extends AcmeResource> getType() {
071        return type;
072    }
073
074    /**
075     * Returns the location of the resource that could not be loaded.
076     */
077    public URL getLocation() {
078        return location;
079    }
080
081}