001package org.conqat.lib.commons.resources;
002
003import java.io.IOException;
004
005/**
006 * Exception that signals that either loading a resource from the classpath or
007 * listing neighbouring resources for a given class file failed.
008 */
009public class ResourceException extends RuntimeException {
010
011        private static final long serialVersionUID = 1L;
012
013        private ResourceException(String message, IOException cause) {
014                super(message, cause);
015        }
016
017        /**
018         * Constructs a {@link ResourceException} for when loading a resource failed.
019         */
020        public static ResourceException newLoadFailed(Resource resource, IOException e) {
021                return new ResourceException("Failed to read resource " + resource.getAbsolutePath(), e);
022        }
023
024        /**
025         * Constructs a {@link ResourceException} for when listing neighbouring
026         * resources for a given class file failed.
027         */
028        public static ResourceException newListFailed(String dirName, Class<?> contextClass, IOException e) {
029                return new ResourceException(
030                                "Unable to list resources in for " + Resource.getAbsolutePath(contextClass, dirName), e);
031        }
032
033        /**
034         * Constructs a {@link ResourceException} for when creating a temporary
035         * directory failed.
036         */
037        public static ResourceException newTemporaryDirectoryCreationFailed(IOException e) {
038                return new ResourceException("Failed to create temporary directory: " + e.getMessage(), e);
039        }
040}