1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.util;
17
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20 import java.lang.reflect.Constructor;
21 import java.util.Map;
22
23 import org.apache.log4j.Logger;
24 import org.springframework.aop.ThrowsAdvice;
25 import org.springframework.core.Ordered;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class ExceptionMappingAdvice implements ThrowsAdvice, Ordered {
70 private int order = 500;
71 private static final long serialVersionUID = 1L;
72 private Map<Class<? extends Exception>, Class<? extends Exception>> exceptionMapping;
73 private Class<? extends Exception> defaultException;
74 final Logger logger = Logger.getLogger(ExceptionMappingAdvice.class);
75
76
77
78
79
80
81
82
83 public void afterThrowing(Exception ex) throws Exception {
84 Class<? extends Exception> mappedExceptionClass = exceptionMapping
85 .get(ex.getClass());
86
87 if (mappedExceptionClass != null) {
88 logger.debug("Mapping exception "+ex.getClass()+" to "+mappedExceptionClass);
89 Constructor<? extends Exception> c = mappedExceptionClass
90 .getConstructor(String.class);
91 Exception mappedException = c.newInstance(ex.getMessage());
92 throw mappedException;
93 }
94
95
96 if(ex instanceof RuntimeException){
97 logger.trace("No mapping available, throwing default exception "+defaultException);
98 if (defaultException != null) {
99
100 StringWriter traceWriter = new StringWriter();
101 PrintWriter printWriter = new PrintWriter(traceWriter, false);
102 logger.error(printWriter, ex);
103 printWriter.close();
104 String faultMessage = traceWriter.getBuffer().toString();
105 logger.error(faultMessage);
106
107 try{
108 Constructor<? extends Exception> c = defaultException
109 .getConstructor(String.class, Throwable.class);
110 throw c.newInstance(ex.getMessage(), ex);
111 }catch(NoSuchMethodException e){
112 Constructor<? extends Exception> c = defaultException
113 .getConstructor(String.class);
114 throw c.newInstance(ex.getMessage());
115 }
116 }
117
118 logger.debug("No mapping or default exception available. Exception "+ex.getClass());
119 throw new RuntimeException("Could Not Map Exception: " + ex.toString());
120 }
121 }
122
123 @Override
124 public int getOrder() {
125 return order;
126 }
127
128
129
130
131
132 public void setOrder(int order) {
133 this.order = order;
134 }
135
136
137
138
139 public Map<Class<? extends Exception>, Class<? extends Exception>> getExceptionMapping() {
140 return exceptionMapping;
141 }
142
143
144
145
146
147 public void setExceptionMapping(
148 Map<Class<? extends Exception>, Class<? extends Exception>> exceptionMapping) {
149 this.exceptionMapping = exceptionMapping;
150 }
151
152
153
154
155 public Class<? extends Exception> getDefaultException() {
156 return defaultException;
157 }
158
159
160
161
162
163 public void setDefaultException(Class<? extends Exception> defaultException) {
164 this.defaultException = defaultException;
165 }
166
167 }