View Javadoc

1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2005, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.spi;
11  
12  import java.util.Arrays;
13  
14  import ch.qos.logback.core.CoreConstants;
15  
16  public class ThrowableProxy implements java.io.Serializable {
17  
18    private static final long serialVersionUID = 6307784764626694851L;
19    private ThrowableDataPoint[] tdpArray;
20    private transient final Throwable throwable;
21    private transient PackagingDataCalculator packagingDataCalculator;
22    private boolean calculatedPackageData = false;
23    
24    public ThrowableProxy(Throwable throwable) {
25      this.throwable = throwable;
26      this.tdpArray = ThrowableToDataPointArray.convert(throwable);
27    }
28  
29    public Throwable getThrowable() {
30      return throwable;
31    } 
32  
33    public PackagingDataCalculator getPackagingDataCalculator() {
34      // if original instance (non-deserialized), and packagingDataCalculator
35      // is not already initialized, then create an instance.
36      // here we assume that (throwable == null) for deserialized instances
37      if (throwable != null && packagingDataCalculator == null) {
38        packagingDataCalculator = new PackagingDataCalculator();
39      }
40      return packagingDataCalculator;
41    }
42  
43    public void calculatePackagingData() {
44      if(calculatedPackageData) {
45        return;
46      }
47      PackagingDataCalculator pdc = this.getPackagingDataCalculator();
48      if(pdc != null) {
49        calculatedPackageData = true;
50        pdc.calculate(tdpArray);
51      }
52    }
53  	
54    /**
55     * The data point representation of the throwable proxy.
56     */
57    public ThrowableDataPoint[] getThrowableDataPointArray() {
58      return tdpArray;
59    }
60  
61    @Override
62    public int hashCode() {
63      final int PRIME = 31;
64      int result = 1;
65      result = PRIME * result + Arrays.hashCode(tdpArray);
66      return result;
67    }
68  
69    @Override
70    public boolean equals(Object obj) {
71      if (this == obj)
72        return true;
73      if (obj == null)
74        return false;
75      if (getClass() != obj.getClass())
76        return false;
77      final ThrowableProxy other = (ThrowableProxy) obj;
78      if (!Arrays.equals(tdpArray, other.tdpArray))
79        return false;
80      return true;
81    }
82  
83    public void fullDump() {
84      StringBuilder builder = new StringBuilder();
85      for (ThrowableDataPoint tdp : getThrowableDataPointArray()) {
86        String string = tdp.toString();
87        builder.append(string);
88        extraData(builder, tdp);
89        builder.append(CoreConstants.LINE_SEPARATOR);
90      }
91      System.out.println(builder.toString());
92    }
93  
94    protected void extraData(StringBuilder builder, ThrowableDataPoint tdp) {
95      StackTraceElementProxy step = tdp.getStackTraceElementProxy();
96      if (step != null) {
97        ClassPackagingData cpd = step.getClassPackagingData();
98        if (cpd != null) {
99          if(!cpd.isExact()){
100           builder.append(" ~[")  ;
101         } else {
102           builder.append(" [")  ;
103         }
104         builder.append(cpd.getCodeLocation()).append(':').append(
105             cpd.getVersion()).append(']');
106       }
107     }
108   }
109 }