View Javadoc

1   package org.csc.phynixx.common.exceptions;
2   
3   /*
4    * #%L
5    * phynixx-common
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import java.io.PrintStream;
25  import java.io.PrintWriter;
26  
27  public class DelegatedRuntimeException extends RuntimeException implements IDelegated {
28  
29  
30      public DelegatedRuntimeException() {
31          super();
32      }
33  
34      public DelegatedRuntimeException(String message, Throwable cause) {
35          super(message, cause);
36      }
37  
38      public DelegatedRuntimeException(String message) {
39          super(message);
40      }
41  
42      public DelegatedRuntimeException(Throwable cause) {
43          super(cause);
44      }
45  
46      /**
47       * liefert die delegierte/ausloesende Exception.
48       * Falls die Delegation rekursiv ist (d.h. die deklegierte Exception ist wiederrum eine
49       * delegation), so wird die delegation bis zur ersten Exception ausfgeloest, die nicht das IF IDelegated implementiert.
50       *
51       * @return the root cause exception.
52       */
53      public Throwable getRootCause() {
54          Throwable throwable = super.getCause();
55  
56          if (throwable instanceof IDelegated) {
57              return ((IDelegated) throwable).getRootCause();
58          }
59          if (throwable == null) {
60              return new Exception("");
61          } else {
62              return throwable;
63          }
64      }
65  
66      /**
67       * liefert den Decorierenden Inhalt.
68       */
69      public String getDecoratingMessage() {
70          String decoratingMessage = super.getMessage();
71          if (decoratingMessage == null) {
72              return "";
73          } else {
74              return decoratingMessage;
75          }
76      }
77  
78  
79      /**
80       * Liefert Message
81       *
82       * @return String
83       */
84      public String getMessage() {
85          String message = super.getMessage();
86          if (this.getCause() != null) {
87              return message + " :: " + this.getCause().getMessage();
88          } else {
89              return super.getMessage() + " DelegatedSystemExeption :: No RootCause defined";
90          }
91      }
92  
93  
94      /**
95       * Die aktuelle Verarbeitung ermittelt die ausloesende Exception und liefert deren StackTrace
96       * Eine Iteration ueber die einzelnen Stacktraces muss ggfs selbst mit Hilfe der Methode
97       * getCause() implementiert werden.
98       *
99       * @param stream PrintStream
100      */
101     public void printStackTrace(PrintStream stream) {
102         if (getRootCause() != null) {
103             stream.println(this.getMessage());
104             getRootCause().printStackTrace(stream);
105         } else {
106             this.printStackTrace(stream);
107         }
108     }
109 
110     /**
111      * Die aktuelle Verarbeitung verweistan die Ausfuehrung der delegierte Excetion
112      * Eine Iteration ueber die einzelnen Stacktraces muss ggfs selbst mit Hilfe der Methode
113      * getCause() implementiert werden.
114      *
115      * @param writer PrintWriter
116      */
117     public void printStackTrace(PrintWriter writer) {
118         if (getRootCause() != null) {
119             writer.write(getMessage());
120             getRootCause().printStackTrace(writer);
121         } else {
122             this.printStackTrace(writer);
123         }
124     }
125 
126 
127 }