01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.exception;
05:
06: /**
07: * Wrap exception in a nice block message surrounded by ****s
08: */
09: public class ExceptionWrapperImpl implements ExceptionWrapper {
10:
11: private final int MAX_STAR_COUNT = 79;
12:
13: public String wrap(String message) {
14: message = (message == null) ? String.valueOf(message) : message;
15: int starCount = longestLineCharCount(message);
16: if (starCount > MAX_STAR_COUNT) {
17: starCount = MAX_STAR_COUNT;
18: }
19: return "\n" + getStars(starCount) + "\n" + message + "\n"
20: + getStars(starCount) + "\n";
21: }
22:
23: private String getStars(int starCount) {
24: StringBuffer stars = new StringBuffer();
25: while (starCount-- > 0) {
26: stars.append('*');
27: }
28: return stars.toString();
29: }
30:
31: private int longestLineCharCount(String message) {
32: int count = 0;
33: int sidx = 0, eidx = 0;
34: while ((eidx = message.indexOf('\n', sidx)) >= 0) {
35: if (count < (eidx - sidx)) {
36: count = (eidx - sidx);
37: }
38: sidx = eidx + 1;
39: }
40: if (sidx < message.length()
41: && count < (message.length() - sidx)) {
42: count = (message.length() - sidx);
43: }
44: return count;
45: }
46:
47: }
|