1 22 package org.jboss.util; 23 24 import java.io.PrintWriter ; 25 import java.io.PrintStream ; 26 import java.io.Serializable ; 27 28 import org.jboss.logging.Logger; 29 30 import org.jboss.util.platform.Java; 31 32 33 39 public interface NestedThrowable 40 extends Serializable 41 { 42 51 boolean PARENT_TRACE_ENABLED = Util.getBoolean("parentTraceEnabled", true); 52 53 69 boolean NESTED_TRACE_ENABLED = Util.getBoolean("nestedTraceEnabled", 70 (Java.isCompatible(Java.VERSION_1_4) && 71 !PARENT_TRACE_ENABLED) || 72 !Java.isCompatible(Java.VERSION_1_4)); 73 74 83 boolean DETECT_DUPLICATE_NESTING = Util.getBoolean("detectDuplicateNesting", true); 84 85 90 Throwable getNested(); 91 92 99 Throwable getCause(); 100 101 102 106 110 final class Util 111 { 112 private static Logger log = Logger.getLogger(NestedThrowable.class); 114 115 123 private static Logger getLogger() 124 { 125 if (log == null) 126 log = Logger.getLogger(NestedThrowable.class); 127 128 return log; 129 } 130 131 132 protected static boolean getBoolean(String name, boolean defaultValue) 133 { 134 name = NestedThrowable.class.getName() + "." + name; 135 String value = System.getProperty(name, String.valueOf(defaultValue)); 136 137 log = getLogger(); 139 140 log.debug(name + "=" + value); 141 142 return new Boolean (value).booleanValue(); 143 } 144 145 149 public static void checkNested(final NestedThrowable parent, 150 final Throwable child) 151 { 152 if (!DETECT_DUPLICATE_NESTING || parent == null || child == null) return; 153 154 Class parentType = parent.getClass(); 155 Class childType = child.getClass(); 156 157 161 if (parentType.isAssignableFrom(childType)) { 162 log = getLogger(); 164 165 log.warn("Duplicate throwable nesting of same base type: " + 166 parentType + " is assignable from: " + childType); 167 } 168 } 169 170 178 public static String getMessage(final String msg, 179 final Throwable nested) 180 { 181 StringBuffer buff = new StringBuffer (msg == null ? "" : msg); 182 183 if (nested != null) { 184 buff.append(msg == null ? "- " : "; - ") 185 .append("nested throwable: (") 186 .append(nested) 187 .append(")"); 188 } 189 190 return buff.toString(); 191 } 192 193 199 public static void print(final Throwable nested, 200 final PrintStream stream) 201 { 202 if (stream == null) 203 throw new NullArgumentException("stream"); 204 205 if (NestedThrowable.NESTED_TRACE_ENABLED && nested != null) { 206 synchronized (stream) { 207 if (NestedThrowable.PARENT_TRACE_ENABLED) { 208 stream.print(" + nested throwable: "); 209 } 210 else { 211 stream.print("[ parent trace omitted ]: "); 212 } 213 214 nested.printStackTrace(stream); 215 } 216 } 217 } 218 219 225 public static void print(final Throwable nested, 226 final PrintWriter writer) 227 { 228 if (writer == null) 229 throw new NullArgumentException("writer"); 230 231 if (NestedThrowable.NESTED_TRACE_ENABLED && nested != null) { 232 synchronized (writer) { 233 if (NestedThrowable.PARENT_TRACE_ENABLED) { 234 writer.print(" + nested throwable: "); 235 } 236 else { 237 writer.print("[ parent trace omitted ]: "); 238 } 239 240 nested.printStackTrace(writer); 241 } 242 } 243 } 244 } 245 } 246 | Popular Tags |