KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > MBeanExceptionFormatter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: MBeanExceptionFormatter.java,v 1.3 2005/12/25 03:42:23 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 import javax.management.MBeanException JavaDoc;
31
32 /**
33  * Helper class that formats the chained exception messages.
34  * The format of the returned message will be cause1(cause2(...)).
35  * It also initializes the cause of the MBeanException which otherwise
36  * is not initialized during MBeanException creation.
37  */

38 public final class MBeanExceptionFormatter
39 {
40     /**
41      * This method returns an MBeanException instance with the message formatted
42      * as msg(msg1(msg2...)). It also inits the cause of the MBeanException.
43      * @param e The cause.
44      * @param msg
45      * @return A new instance of MBeanException.
46      */

47     public static MBeanException JavaDoc toMBeanException( final Exception JavaDoc e,
48                                                     final String JavaDoc msg )
49     {
50         final String JavaDoc s = getMessage(e, msg);
51         final Exception JavaDoc cause = (e != null) ? e : new Exception JavaDoc(s);
52         MBeanException JavaDoc mbe = new MBeanException JavaDoc(cause, s);
53         mbe.initCause(cause);
54         return mbe;
55     }
56
57     /**
58      * @param t
59      * @param cause1
60      * @return Returns the formatted message cause1(cause2(...))
61      */

62     public static String JavaDoc getMessage(final Throwable JavaDoc t, final String JavaDoc cause1)
63     {
64         if (null == t) { return cause1; }
65         final String JavaDoc cause = t.getMessage();
66         final Throwable JavaDoc x = (t instanceof MBeanException JavaDoc) ?
67             ((MBeanException JavaDoc)t).getTargetException() : t.getCause();
68         final String JavaDoc s = getMessage(x, cause);
69         return formatMessage(cause1, s);
70     }
71
72     private static String JavaDoc formatMessage(String JavaDoc a, String JavaDoc b)
73     {
74         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
75         boolean isValidA = (null != a) && (a.length() > 0);
76         boolean isValidB = (null != b) && (b.length() > 0);
77         if (isValidA) { sb.append(a); }
78         if (isValidA && isValidB) { sb.append('('); }
79         if (isValidB) { sb.append(b); }
80         if (isValidA && isValidB) { sb.append(')'); }
81         return sb.toString();
82     }
83 }
84
Popular Tags