1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.log4j.spi; 19 20 import org.apache.log4j.Category; 21 import org.apache.log4j.DefaultThrowableRenderer; 22 23 /** 24 * ThrowableInformation is log4j's internal representation of 25 * throwables. It essentially consists of a string array, called 26 * 'rep', where the first element, that is rep[0], represents the 27 * string representation of the throwable (i.e. the value you get 28 * when you do throwable.toString()) and subsequent elements 29 * correspond the stack trace with the top most entry of the stack 30 * corresponding to the second entry of the 'rep' array that is 31 * rep[1]. 32 * 33 * @author Ceki Gülcü 34 * 35 * */ 36 public class ThrowableInformation implements java.io.Serializable { 37 38 static final long serialVersionUID = -4748765566864322735L; 39 40 private transient Throwable throwable; 41 private transient Category category; 42 private String[] rep; 43 44 public 45 ThrowableInformation(Throwable throwable) { 46 this.throwable = throwable; 47 } 48 49 /** 50 * Create a new instance. 51 * @param throwable throwable, may not be null. 52 * @param category category used to obtain ThrowableRenderer, may be null. 53 * @since 1.2.16 54 */ 55 public ThrowableInformation(Throwable throwable, Category category) { 56 this.throwable = throwable; 57 this.category = category; 58 } 59 60 /** 61 * Create new instance. 62 * @since 1.2.15 63 * @param r String representation of throwable. 64 */ 65 public ThrowableInformation(final String[] r) { 66 if (r != null) { 67 rep = (String[]) r.clone(); 68 } 69 } 70 71 72 public 73 Throwable getThrowable() { 74 return throwable; 75 } 76 77 public synchronized String[] getThrowableStrRep() { 78 if(rep == null) { 79 ThrowableRenderer renderer = null; 80 if (category != null) { 81 LoggerRepository repo = category.getLoggerRepository(); 82 if (repo instanceof ThrowableRendererSupport) { 83 renderer = ((ThrowableRendererSupport) repo).getThrowableRenderer(); 84 } 85 } 86 if (renderer == null) { 87 rep = DefaultThrowableRenderer.render(throwable); 88 } else { 89 rep = renderer.doRender(throwable); 90 } 91 } 92 return (String[]) rep.clone(); 93 } 94 } 95 96