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.ExceptionContextManager; 25 26 /** 27 * The base implementation of the core exception that supports unique 28 * identifying and exception codes for checked exceptions. 29 */ 30 public abstract class AbstractCoreException extends Exception implements 31 CoreException 32 { 33 // ********************************* Fields ********************************* 34 35 // --- constants ------------------------------------------------------------ 36 37 /** 38 * The class version identifier. 39 */ 40 private static final long serialVersionUID = 4L; 41 42 // --- members -------------------------------------------------------------- 43 44 /** 45 * Reference to common exception information. Currently this is the exception 46 * identifier and the exception code. 47 * 48 * @serial 49 */ 50 protected final ExceptionInfo info; 51 52 // ****************************** Initializer ******************************* 53 54 // ****************************** Constructors ****************************** 55 56 /** 57 * Constructor. 58 * 59 * @param code the error or exception code of the exception. 60 * @see AbstractCoreException#AbstractCoreException(String, Throwable, Code) 61 */ 62 protected AbstractCoreException(final Code code) 63 { 64 this(null, null, code); 65 } 66 67 /** 68 * Constructor. 69 * 70 * @param message the detail message (which is saved for later retrieval by 71 * the {@link #getMessage()} method). Only used if there is no 72 * localized message in the bundle. 73 * @param code the error or exception code of the exception. 74 * @see AbstractCoreException#AbstractCoreException(String, Throwable, Code) 75 */ 76 protected AbstractCoreException(final String message, final Code code) 77 { 78 this(message, null, code); 79 } 80 81 /** 82 * Constructor. 83 * 84 * @param cause the cause (which is saved for later retrieval by the 85 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 86 * and indicates that the cause is nonexistent or unknown.) 87 * @param code the error or exception code of the exception. 88 * @see AbstractCoreException#AbstractCoreException(String, Throwable, Code) 89 */ 90 protected AbstractCoreException(final Throwable cause, final Code code) 91 { 92 this(null, cause, code); 93 } 94 95 /** 96 * Constructor. 97 * 98 * @param message the detail message (which is saved for later retrieval by 99 * the {@link #getMessage()} method). Only used if there is no 100 * localized message in the bundle. 101 * @param cause the cause (which is saved for later retrieval by the 102 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 103 * and indicates that the cause is nonexistent or unknown.) 104 * @param code the error or exception code of the exception. 105 * @throws NullPointerException if the code is <code>null</code>. 106 * @see java.lang.Exception#Exception(java.lang.String,java.lang.Throwable) 107 */ 108 protected AbstractCoreException(final String message, final Throwable cause, 109 final Code code) throws NullPointerException 110 { 111 super(message, Helper.provideException(cause)); 112 this.info = new ExceptionInfo(cause, code); 113 } 114 115 // ****************************** Inner Classes ***************************** 116 117 // ********************************* Methods ******************************** 118 119 // --- init ----------------------------------------------------------------- 120 121 // --- get&set -------------------------------------------------------------- 122 123 @Override 124 public final ExceptionId<?> getId() 125 { 126 return info.getId(); 127 } 128 129 @Override 130 public final Code getCode() 131 { 132 return info.getCode(); 133 } 134 135 @Override 136 public final Date getTime() 137 { 138 return info.getTime(); 139 } 140 141 @Override 142 public final Throwable getCause() 143 { 144 if (Helper.getThrowableHandleMode() == ThrowableHandleMode.NORMAL) 145 { 146 return super.getCause(); 147 } 148 else 149 { 150 return info.removeableCause; 151 } 152 } 153 154 // --- business ------------------------------------------------------------- 155 156 @Override 157 public final void truncateCause() 158 { 159 Helper.truncateCause(this, this.info); 160 } 161 162 // --- object basics -------------------------------------------------------- 163 164 /** 165 * Returns the string representation of the exception. 166 * <p> 167 * May be overridden by subclasses in an application (not a library) to change 168 * the string representation. Usually an implementation of 169 * {@code CodeMessageFormatter} should be set to the 170 * {@link de.smartics.exceptions.ExceptionContext}. 171 * </p> 172 * 173 * @return the string representation of the object. 174 */ 175 @Override 176 public String toString() 177 { 178 final CodeMessageFormatter formatter = 179 ExceptionContextManager.getFormatter(Thread.currentThread() 180 .getContextClassLoader()); 181 final String string = formatter.format(this); 182 return string; 183 } 184 }