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.net.URL;
019
020import org.shredzone.acme4j.AcmeResource;
021
022/**
023 * A runtime exception that is thrown when an {@link AcmeException} occured while trying
024 * to lazy-load a resource from the ACME server. It contains the original cause of the
025 * exception and a reference to the resource that could not be lazy-loaded. It is usually
026 * thrown by getter methods, so the API is not polluted with checked exceptions.
027 */
028public class AcmeLazyLoadingException extends RuntimeException {
029    private static final long serialVersionUID = 1000353433913721901L;
030
031    private final Class<? extends AcmeResource> type;
032    private final URL location;
033
034    /**
035     * Creates a new {@link AcmeLazyLoadingException}.
036     *
037     * @param resource
038     *            {@link AcmeResource} to be loaded
039     * @param cause
040     *            {@link AcmeException} that was raised
041     */
042    public AcmeLazyLoadingException(AcmeResource resource, AcmeException cause) {
043        this(requireNonNull(resource).getClass(), requireNonNull(resource).getLocation(), cause);
044    }
045
046    /**
047     * Creates a new {@link AcmeLazyLoadingException}.
048     * <p>
049     * This constructor is used if there is no actual instance of the resource.
050     *
051     * @param type
052     *         {@link AcmeResource} type to be loaded
053     * @param location
054     *         Resource location
055     * @param cause
056     *         {@link AcmeException} that was raised
057     * @since 2.8
058     */
059    public AcmeLazyLoadingException(Class<? extends AcmeResource> type, URL location, AcmeException cause) {
060        super(requireNonNull(type).getSimpleName() + " " + requireNonNull(location), requireNonNull(cause));
061        this.type = type;
062        this.location = location;
063    }
064
065    /**
066     * Returns the {@link AcmeResource} type of the resource that could not be loaded.
067     */
068    public Class<? extends AcmeResource> getType() {
069        return type;
070    }
071
072    /**
073     * Returns the location of the resource that could not be loaded.
074     */
075    public URL getLocation() {
076        return location;
077    }
078
079}