KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > JMXExceptionTranslator


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 package com.sun.enterprise.admin.common;
25
26 //JMX imports
27
import javax.management.ObjectName JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29
30 import com.sun.enterprise.admin.util.ArgChecker;
31 import com.sun.enterprise.admin.common.exception.*;
32 import com.sun.enterprise.admin.common.ObjectNameHelper;
33
34 public class JMXExceptionTranslator
35 {
36     private static final String JavaDoc NULL_ARGUMENT = "null-arg";
37
38     public static AFException translate(Exception JavaDoc e)
39     {
40         ArgChecker.check((e != null), NULL_ARGUMENT);
41
42         AFException afe = new AFException(e.getMessage());
43         if (e instanceof javax.management.MBeanException JavaDoc)
44         {
45             Exception JavaDoc targetException =
46                 ((javax.management.MBeanException JavaDoc)e).getTargetException();
47             if (targetException instanceof AFException)
48             {
49                 afe = (AFException) targetException;
50             }
51             // <addition> srini@sun.com server.xml verifier
52
else if(targetException instanceof AFRuntimeException)
53             {
54                    throw (AFRuntimeException)targetException;
55             }
56         else if (targetException instanceof javax.management.MBeanException JavaDoc)
57         {
58                 Exception JavaDoc excpn =
59             ((javax.management.MBeanException JavaDoc)targetException).getTargetException();
60         if (excpn != null) {
61                    if (excpn instanceof javax.management.InvalidAttributeValueException JavaDoc) {
62                       afe = new com.sun.enterprise.admin.common.exception.InvalidAttributeValueException(
63                 excpn.getLocalizedMessage());
64            }
65         }
66         }
67             // </addition>
68
else
69             {
70                 afe = new AFOtherException(targetException);
71             }
72         }
73         else if (e instanceof javax.management.InstanceNotFoundException JavaDoc)
74         {
75             String JavaDoc msg = convertInstanceNotFoundExceptionMessage(e);
76             afe = new AFTargetNotFoundException(msg);
77         }
78         else if (e instanceof javax.management.ReflectionException JavaDoc)
79         {
80             afe = new AFOtherException(e);
81         }
82         else if (e instanceof javax.management.AttributeNotFoundException JavaDoc)
83         {
84             afe = new AttributeNotFoundException(e.getLocalizedMessage());
85         }
86         else if (e instanceof javax.management.InvalidAttributeValueException JavaDoc)
87         {
88             afe = new InvalidAttributeValueException(e.getLocalizedMessage());
89         }
90         else if (e instanceof java.lang.RuntimeException JavaDoc)
91         {
92             throw (java.lang.RuntimeException JavaDoc) e;
93         }
94         return afe;
95     }
96
97     private static String JavaDoc convertInstanceNotFoundExceptionMessage(Exception JavaDoc e)
98     {
99         /* Here we are trying to extract type and name from exception
100          if message is valid ObjectName string representation */

101         String JavaDoc type = null;
102         String JavaDoc name = null;
103         try
104         {
105             ObjectName JavaDoc objectName = new ObjectName JavaDoc(e.getMessage());
106             type = ObjectNameHelper.getType(objectName);
107             name = ObjectNameHelper.getName(objectName);
108         }
109         catch (MalformedObjectNameException JavaDoc mfone)
110         {
111         }
112         String JavaDoc msg;
113         if(type!=null)
114         {
115             if(name!=null)
116                 msg = type + " '" + name +"' is not found.";
117             else
118                 msg = type + " is not found.";
119         }
120         else
121         {
122             //object name is malformed - leave original message
123
msg = e.getLocalizedMessage();
124         }
125         return msg;
126     }
127 }
128
Popular Tags