1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.commands;
22
23 import org.apache.commons.chain.Catalog;
24 import org.apache.commons.chain.CatalogFactory;
25 import org.apache.commons.chain.Command;
26 import org.apache.commons.chain.Filter;
27 import org.apache.struts.chain.contexts.ActionContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35
36 public class ExceptionCatcher extends ActionCommandBase implements Filter<ActionContext> {
37
38
39
40
41 private final Logger log =
42 LoggerFactory.getLogger(ExceptionCatcher.class);
43
44
45
46
47
48
49 private String catalogName = null;
50
51
52
53
54 private String exceptionCommand = null;
55
56
57
58
59
60
61
62
63
64 public String getCatalogName() {
65 return (this.catalogName);
66 }
67
68
69
70
71
72
73
74 public void setCatalogName(String catalogName) {
75 this.catalogName = catalogName;
76 }
77
78
79
80
81
82
83 public String getExceptionCommand() {
84 return (this.exceptionCommand);
85 }
86
87
88
89
90
91
92 public void setExceptionCommand(String exceptionCommand) {
93 this.exceptionCommand = exceptionCommand;
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108 @Override
109 protected boolean execute_(ActionContext actionCtx)
110 throws Exception {
111 actionCtx.setException(null);
112
113 return CONTINUE_PROCESSING;
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public boolean postprocess(ActionContext actionCtx, Exception exception) {
132
133 if (exception == null) {
134 return (false);
135 }
136
137
138 log.debug("Attempting to handle a thrown exception");
139
140 actionCtx.setException(exception);
141
142
143 try {
144 Command<ActionContext> command = lookupExceptionCommand();
145
146 if (command == null) {
147 log.error("Cannot find exceptionCommand '{}'",
148 exceptionCommand);
149 throw new IllegalStateException(
150 "Cannot find exceptionCommand '" + exceptionCommand + "'");
151 }
152
153 log.trace("Calling exceptionCommand '{}'", exceptionCommand);
154
155 command.execute(actionCtx);
156 } catch (Exception e) {
157 log.warn("Exception from exceptionCommand '{}'",
158 exceptionCommand, e);
159 IllegalStateException e2 = new IllegalStateException("Exception chain threw exception");
160 e2.initCause(e);
161 throw e2;
162 }
163
164 return true;
165 }
166
167
168
169
170
171
172
173
174
175 protected Command<ActionContext> lookupExceptionCommand() {
176 final String catalogName = getCatalogName();
177
178 final CatalogFactory<ActionContext> catalogFactory =
179 CatalogFactory.getInstance();
180
181 final Catalog<ActionContext> catalog;
182
183 if (catalogName == null) {
184 catalog = catalogFactory.getCatalog();
185
186 if (catalog == null) {
187 log.error("Cannot find default catalog");
188 throw new IllegalArgumentException(
189 "Cannot find default catalog");
190 }
191 } else {
192 catalog = catalogFactory.getCatalog(catalogName);
193
194 if (catalog == null) {
195 log.error("Cannot find catalog '{}'", catalogName);
196 throw new IllegalArgumentException("Cannot find catalog '"
197 + catalogName + "'");
198 }
199 }
200
201 String exceptionCommand = getExceptionCommand();
202
203 if (exceptionCommand == null) {
204 log.error("No exceptionCommand property specified");
205 throw new IllegalStateException(
206 "No exceptionCommand property specfied");
207 }
208
209 return catalog.getCommand(exceptionCommand);
210 }
211 }