View Javadoc

    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 java.io.IOException;
    21  import java.io.LineNumberReader;
    22  import java.io.PrintWriter;
    23  import java.io.StringReader;
    24  import java.io.StringWriter;
    25  import java.util.ArrayList;
    26
    27  /***
    28    * ThrowableInformation is log4j's internal representation of
    29    * throwables. It essentially consists of a string array, called
    30    * 'rep', where the first element, that is rep[0], represents the
    31    * string representation of the throwable (i.e. the value you get
    32    * when you do throwable.toString()) and subsequent elements
    33    * correspond the stack trace with the top most entry of the stack
    34    * corresponding to the second entry of the 'rep' array that is
    35    * rep[1].
    36    *
    37    * @author Ceki G&uuml;lc&uuml;
    38    *
    39    * */
    40  public class ThrowableInformation implements java.io.Serializable {
    41
    42    static final long serialVersionUID = -4748765566864322735L;
    43
    44    private transient Throwable throwable;
    45    private String[] rep;
    46
    47    public
    48    ThrowableInformation(Throwable throwable) {
    49      this.throwable = throwable;
    50    }
    51
    52      /***
    53       * Create new instance.
    54       * @since 1.2.15
    55       * @param r String representation of throwable.
    56       */
    57    public ThrowableInformation(final String[] r) {
    58        if (r != null) {
    59          rep = (String[]) r.clone();
    60        }
    61    }
    62
    63
    64    public
    65    Throwable getThrowable() {
    66      return throwable;
    67    }
    68
    69    public
    70    String[] getThrowableStrRep() {
    71      if(rep != null) {
    72        return (String[]) rep.clone();
    73      } else {
    74        StringWriter sw = new StringWriter();
    75        PrintWriter pw = new PrintWriter(sw);
    76        throwable.printStackTrace(pw);
    77        pw.flush();
    78        LineNumberReader reader = new LineNumberReader(
    79                new StringReader(sw.toString()));
    80        ArrayList lines = new ArrayList();
    81        try {
    82          String line = reader.readLine();
    83          while(line != null) {
    84            lines.add(line);
    85            line = reader.readLine();
    86          }
    87        } catch(IOException ex) {
    88            lines.add(ex.toString());
    89        }
    90        rep = new String[lines.size()];
    91        lines.toArray(rep);
    92      }
    93      return rep;
    94    }
    95  }
    96
    97