1 /* 2 * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.exceptions; 17 18 import java.util.Date; 19 20 import de.smartics.exceptions.core.Code; 21 import de.smartics.exceptions.core.CodeMessageFormatter; 22 import de.smartics.exceptions.core.ExceptionId; 23 import de.smartics.exceptions.core.ThrowableHandleMode; 24 import de.smartics.exceptions.runtime.FallbackClassLoader; 25 import de.smartics.exceptions.runtime.ExceptionContextManager; 26 27 /** 28 * The base implementation of the core exception that supports unique 29 * identifying and exception codes for checked exceptions. 30 */ 31 public abstract class AbstractCoreException extends Exception implements 32 CoreException 33 { 34 // ********************************* Fields ********************************* 35 36 // --- constants ------------------------------------------------------------ 37 38 /** 39 * The class version identifier. 40 */ 41 private static final long serialVersionUID = 4L; 42 43 // --- members -------------------------------------------------------------- 44 45 /** 46 * Reference to common exception information. Currently this is the exception 47 * identifier and the exception code. 48 * 49 * @serial 50 */ 51 protected final ExceptionInfo info; 52 53 // ****************************** Initializer ******************************* 54 55 // ****************************** Constructors ****************************** 56 57 /** 58 * Constructor. 59 * 60 * @param code the error or exception code of the exception. 61 * @see AbstractCoreException#AbstractCoreException(String, Throwable, Code) 62 */ 63 protected AbstractCoreException(final Code code) 64 { 65 this(null, null, code); 66 } 67 68 /** 69 * Constructor. 70 * 71 * @param message the detail message (which is saved for later retrieval by 72 * the {@link #getMessage()} method). Only used if there is no 73 * localized message in the bundle. 74 * @param code the error or exception code of the exception. 75 * @see AbstractCoreException#AbstractCoreException(String, Throwable, Code) 76 */ 77 protected AbstractCoreException(final String message, final Code code) 78 { 79 this(message, null, code); 80 } 81 82 /** 83 * Constructor. 84 * 85 * @param cause the cause (which is saved for later retrieval by the 86 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 87 * and indicates that the cause is nonexistent or unknown.) 88 * @param code the error or exception code of the exception. 89 * @see AbstractCoreException#AbstractCoreException(String, Throwable, Code) 90 */ 91 protected AbstractCoreException(final Throwable cause, final Code code) 92 { 93 this(null, cause, code); 94 } 95 96 /** 97 * Constructor. 98 * 99 * @param message the detail message (which is saved for later retrieval by 100 * the {@link #getMessage()} method). Only used if there is no 101 * localized message in the bundle. 102 * @param cause the cause (which is saved for later retrieval by the 103 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 104 * and indicates that the cause is nonexistent or unknown.) 105 * @param code the error or exception code of the exception. 106 * @throws NullPointerException if the code is <code>null</code>. 107 * @see java.lang.Exception#Exception(java.lang.String,java.lang.Throwable) 108 */ 109 protected AbstractCoreException(final String message, final Throwable cause, 110 final Code code) throws NullPointerException 111 { 112 super(message, Helper.provideException(cause)); 113 this.info = new ExceptionInfo(cause, code); 114 } 115 116 // ****************************** Inner Classes ***************************** 117 118 // ********************************* Methods ******************************** 119 120 // --- init ----------------------------------------------------------------- 121 122 // --- get&set -------------------------------------------------------------- 123 124 @Override 125 public final ExceptionId<?> getId() 126 { 127 return info.getId(); 128 } 129 130 @Override 131 public final Code getCode() 132 { 133 return info.getCode(); 134 } 135 136 @Override 137 public final Date getTime() 138 { 139 return info.getTime(); 140 } 141 142 @Override 143 public final Throwable getCause() 144 { 145 if (Helper.getThrowableHandleMode() == ThrowableHandleMode.NORMAL) 146 { 147 return super.getCause(); 148 } 149 else 150 { 151 return info.removeableCause; 152 } 153 } 154 155 // --- business ------------------------------------------------------------- 156 157 @Override 158 public final void truncateCause() 159 { 160 Helper.truncateCause(this, this.info); 161 } 162 163 // --- object basics -------------------------------------------------------- 164 165 /** 166 * Returns a class loader to provide resources from the class path. 167 * 168 * @return a class loader to provide resources from the class path. 169 */ 170 protected final ClassLoader getClassLoader() 171 { 172 final ClassLoader loader = 173 new FallbackClassLoader(Thread.currentThread() 174 .getContextClassLoader(), getClass().getClassLoader()); // NOPMD 175 return loader; 176 } 177 178 /** 179 * Returns the string representation of the exception. 180 * <p> 181 * May be overridden by subclasses in an application (not a library) to change 182 * the string representation. Usually an implementation of 183 * {@code CodeMessageFormatter} should be set to the 184 * {@link de.smartics.exceptions.ExceptionContext}. 185 * </p> 186 * 187 * @return the string representation of the object. 188 */ 189 @Override 190 public String toString() 191 { 192 final CodeMessageFormatter formatter = 193 ExceptionContextManager.getFormatter(getClassLoader()); 194 final String string = formatter.format(this); 195 return string; 196 } 197 }