001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.spi;
019
020import org.apache.log4j.Category;
021import org.apache.log4j.DefaultThrowableRenderer;
022
023/**
024  * ThrowableInformation is log4j's internal representation of
025  * throwables. It essentially consists of a string array, called
026  * 'rep', where the first element, that is rep[0], represents the
027  * string representation of the throwable (i.e. the value you get
028  * when you do throwable.toString()) and subsequent elements
029  * correspond the stack trace with the top most entry of the stack
030  * corresponding to the second entry of the 'rep' array that is
031  * rep[1].
032  *
033  * @author Ceki Gülcü
034  *
035  * */
036public class ThrowableInformation implements java.io.Serializable {
037
038  static final long serialVersionUID = -4748765566864322735L;
039
040  private transient Throwable throwable;
041  private transient Category category;
042  private String[] rep;
043
044  public
045  ThrowableInformation(Throwable throwable) {
046    this.throwable = throwable;
047  }
048
049    /**
050     * Create a new instance.
051     * @param throwable throwable, may not be null.
052     * @param category category used to obtain ThrowableRenderer, may be null.
053     * @since 1.2.16
054     */
055  public ThrowableInformation(Throwable throwable, Category category) {
056      this.throwable = throwable;
057      this.category = category;
058  }
059
060    /**
061     * Create new instance.
062     * @since 1.2.15
063     * @param r String representation of throwable.
064     */
065  public ThrowableInformation(final String[] r) {
066      if (r != null) {
067        rep = (String[]) r.clone();
068      }
069  }
070
071
072  public
073  Throwable getThrowable() {
074    return throwable;
075  }
076
077  public synchronized String[] getThrowableStrRep() {
078    if(rep == null) {
079      ThrowableRenderer renderer = null;
080      if (category != null) {
081          LoggerRepository repo = category.getLoggerRepository();
082          if (repo instanceof ThrowableRendererSupport) {
083              renderer = ((ThrowableRendererSupport) repo).getThrowableRenderer();
084          }
085      }
086      if (renderer == null) {
087          rep = DefaultThrowableRenderer.render(throwable);
088      } else {
089          rep = renderer.doRender(throwable);
090      }
091    }
092    return (String[]) rep.clone();
093  }
094}
095
096