1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.store

File TransactionException.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

10
44
12
1
224
108
17
0.39
3.67
12
1.42

Classes

Class Line # Actions
TransactionException 37 44 0% 17 18
0.7272727572.7%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.store;
21   
22    import java.io.PrintStream;
23    import java.io.PrintWriter;
24    import java.io.StringWriter;
25    import java.io.Writer;
26    import java.util.ArrayList;
27    import java.util.List;
28   
29    /**
30    * An exception made up of a group of exceptions.
31    * This class satisfies the use case when multiple things which may throw exceptions
32    * must be done, each may throw an exception but the others must run all the same.
33    *
34    * @version $Id: 90c1bc7251586cb1683af6cb4c2e3667d0f81d0f $
35    * @since 3.0M2
36    */
 
37    public class TransactionException extends Exception
38    {
39    /**
40    * The platform dependent newline string.
41    */
42    private static final String NEWLINE = System.getProperty("line.separator");
43   
44    /**
45    * A tab character which will be used to tab in the messages from nested exceptions.
46    */
47    private static final String TAB = "\t";
48   
49    /**
50    * The list of exceptions which caused this to be thrown.
51    */
52    private final List<Throwable> causes;
53   
54    /**
55    * Does this failure mean that the storage engine may be corrupt?
56    */
57    private final boolean isNonRecoverable;
58   
59    /**
60    * Total number of exceptions under this one.
61    */
62    private final int exceptionCount;
63   
64    /**
65    * Constructor with message specified.
66    *
67    * @param causes the list of Throwables which caused this exception.
68    */
 
69  4 toggle public TransactionException(final List<Throwable> causes)
70    {
71  4 this(null, causes);
72    }
73   
74    /**
75    * Constructor with message specified.
76    *
77    * @param message the message to give with the exception.
78    * @param causes the list of Throwables which caused this exception.
79    */
 
80  4 toggle public TransactionException(final String message, final List<Throwable> causes)
81    {
82  4 this(message, causes, false);
83    }
84   
85    /**
86    * Constructor with message specified.
87    *
88    * @param message the message to give with the exception.
89    * @param causes the list of Throwables which caused this exception.
90    * @param isNonRecoverable true if the storage engine could not recover from this
91    * exception and corruption might have resulted.
92    */
 
93  27 toggle public TransactionException(final String message,
94    final List<Throwable> causes,
95    final boolean isNonRecoverable)
96    {
97  27 super(message);
98  27 this.causes = new ArrayList<Throwable>(causes);
99  27 boolean nonRecoverable = isNonRecoverable;
100   
101  27 int total = 0;
102  27 for (Throwable cause : this.causes) {
103  36 if (cause instanceof TransactionException) {
104  3 final TransactionException teCause = (TransactionException) cause;
105   
106  3 total += teCause.exceptionCount();
107  3 if (teCause.isNonRecoverable()) {
108  2 nonRecoverable = true;
109    }
110    } else {
111  33 total++;
112    }
113    }
114  27 this.exceptionCount = total;
115  27 this.isNonRecoverable = nonRecoverable;
116    }
117   
118    /**
119    * @return all of the exceptions which caused this exception to be thrown.
120    */
 
121  12 toggle public List<Throwable> getCauses()
122    {
123  12 return new ArrayList<Throwable>(this.causes);
124    }
125   
126    /**
127    * @return the total number of exceptions which caused this exception to be thrown.
128    */
 
129  8 toggle public int exceptionCount()
130    {
131  8 return this.exceptionCount;
132    }
133   
134    /**
135    * @return true if the storage engine could not recover from an exception in this group and
136    * corruption of the storage engine might have resulted.
137    */
 
138  11 toggle public boolean isNonRecoverable()
139    {
140  11 return this.isNonRecoverable;
141    }
142   
143    /**
144    * {@inheritDoc}
145    * <p>
146    * In this implementation the message is also included in the
147    * stack trace so calling both is redundant.
148    * </p>
149    *
150    * @see java.lang.Throwable#getMessage()
151    */
 
152  5 toggle public String getMessage()
153    {
154  5 final Writer writer = new StringWriter();
155  5 final PrintWriter printer = new PrintWriter(writer);
156  5 printInfo(printer, false);
157  5 return writer.toString();
158    }
159   
160    /**
161    * Utility method to get the stack trace for this exception as a string.
162    *
163    * @return what {@link #printStackTrace()} would have printed.
164    */
 
165  0 toggle public String getStackTraceString()
166    {
167  0 final Writer writer = new StringWriter();
168  0 final PrintWriter printer = new PrintWriter(writer);
169  0 this.printStackTrace(printer);
170  0 return writer.toString();
171    }
172   
 
173  0 toggle @Override
174    public void printStackTrace()
175    {
176  0 this.printStackTrace(System.err);
177    }
178   
 
179  0 toggle @Override
180    public void printStackTrace(final PrintStream writeTo)
181    {
182  0 this.printStackTrace(new PrintWriter(writeTo));
183    }
184   
 
185  0 toggle @Override
186    public void printStackTrace(final PrintWriter writeTo)
187    {
188  0 this.printInfo(writeTo, true);
189    }
190   
191    /**
192    * Get the exception message or stack trace.
193    *
194    * @param writeTo the PrintWriter to write the output to.
195    * @param includeStackTrace if true then an integrated message and stack trace is produced.
196    */
 
197  5 toggle private void printInfo(final PrintWriter writeTo, final boolean includeStackTrace)
198    {
199  5 if (super.getMessage() != null) {
200  1 writeTo.println(super.getMessage());
201    }
202  5 writeTo.println("Caused by:");
203   
204  5 for (Throwable cause : causes) {
205   
206  13 writeTo.println(cause.getClass().getName());
207  13 writeTo.print(TAB);
208  13 writeTo.print(("" + cause.getMessage()).replaceAll(NEWLINE, NEWLINE + TAB));
209  13 writeTo.print(NEWLINE);
210   
211  13 if (includeStackTrace) {
212    // Include the stack trace tabbed in for each so they are recognizable as different.
213    // TODO End the stack trace at the frame which caused the TransactionException to throw.
214  0 final Writer stw = new StringWriter();
215  0 final PrintWriter stpw = new PrintWriter(stw);
216  0 cause.printStackTrace(stpw);
217  0 writeTo.print(stw.toString().replaceAll(NEWLINE, NEWLINE + TAB));
218    }
219    }
220  5 if (includeStackTrace) {
221  0 super.printStackTrace(writeTo);
222    }
223    }
224    }