1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program 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
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17
18 package ch.elca.el4j.tests.services.exceptionhandler;
19
20 import org.aopalliance.intercept.MethodInvocation;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import ch.elca.el4j.services.exceptionhandler.AbstractExceptionHandlerInterceptor;
25 import ch.elca.el4j.services.exceptionhandler.handler.AbstractExceptionHandler;
26
27 /**
28 * This simple exception handler logs exception messages in a dialogue,
29 * printed to the standard out.
30 *
31 * @svnLink $Revision: 3880 $;$Date: 2009-08-04 15:17:52 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/exception_handling/src/test/java/ch/elca/el4j/tests/services/exceptionhandler/MessageBoxExceptionHandler.java $
32 *
33 * @author Andreas Bur (ABU)
34 */
35 public class MessageBoxExceptionHandler extends AbstractExceptionHandler {
36
37 /**
38 * Logger.
39 */
40 private static Logger s_logger
41 = LoggerFactory.getLogger(MessageBoxExceptionHandler.class);
42
43 /** Number of handle calls. */
44 public static int s_numberOfHandleCalls = 0;
45
46 /**
47 * {@inheritDoc}
48 */
49 protected Object handleException(Throwable t,
50 AbstractExceptionHandlerInterceptor exceptionInvoker,
51 MethodInvocation invocation, Logger logger) throws Throwable {
52
53 s_numberOfHandleCalls++;
54
55 String s = t.getMessage();
56 StringBuffer buffer = new StringBuffer('\n');
57 drawLine(s.length(), buffer);
58 buffer.append("| ");
59 buffer.append(s);
60 buffer.append(" |\n");
61 drawLine(s.length(), buffer);
62
63 s_logger.info(buffer.toString());
64
65 return null;
66 }
67
68 /**
69 * Draws a line consisting of hyphens.
70 *
71 * @param length
72 * The number of hyphens to print.
73 *
74 * @param buffer
75 * The buffer to write the hyphens to.
76 */
77 private void drawLine(int length, StringBuffer buffer) {
78 // Checkstyle: MagicNumber off
79 for (int i = 0; i < length + 4; i++) {
80 buffer.append("-");
81 }
82 buffer.append('\n');
83 // Checkstyle: MagicNumber on
84 }
85 }