View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.core.modules.api;
17  
18  import java.util.List;
19  
20  /**
21   * @author Christian Ohr
22   */
23  @SuppressWarnings("serial")
24  public class ValidationException extends RuntimeException {
25  
26      private Throwable[] causes;
27  
28      public ValidationException(Throwable cause) {
29          super(cause);
30          causes = new Throwable[] { cause };
31      }
32  
33      public ValidationException(String message, Throwable cause) {
34          super(message, cause);
35          causes = new Throwable[] { cause };
36      }
37  
38      public ValidationException(String message, Throwable... causes) {
39          super(message, ((causes == null) || (causes.length == 0)) ? null : causes[0]);
40          this.causes = causes;
41      }
42  
43      public ValidationException(String message, List<? extends Throwable> causes) {
44          this(message, causes.toArray(new Throwable[causes.size()]));
45      }
46  
47      public ValidationException(String message) {
48          super(message);
49      }
50  
51      public ValidationException(Throwable... causes) {
52          this(null, causes);
53      }
54  
55      public ValidationException(List<? extends Throwable> causes) {
56          this(null, causes);
57      }
58  
59      public Throwable[] getCauses() {
60          return causes;
61      }
62  
63      @Override
64      public String getMessage() {
65          StringBuilder sb = new StringBuilder();
66          
67          String msg = super.getMessage();
68          if(msg != null) {
69              sb.append(msg).append('\n');
70          }
71  
72          if(causes != null) {
73              for(Throwable t : causes) {
74                  if(t != null) {
75                      msg = t.getMessage();
76                      sb.append((msg == null) ? t.getClass().getName() : msg).append('\n');
77                  }
78              }
79          }
80  
81          int len = sb.length(); 
82          return (len == 0) ? getClass().getName() : sb.deleteCharAt(len - 1).toString();
83      }
84  
85  }