KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ExceptionHandler.java
26  *
27  * Created on February 26, 2004, 3:39 PM
28  */

29
30 package com.sun.enterprise.admin.mbeans;
31
32 import com.sun.enterprise.admin.servermgmt.InstanceException;
33 import com.sun.enterprise.config.ConfigException;
34
35 import com.sun.enterprise.util.i18n.StringManagerBase;
36
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39
40 /**
41  *
42  * @author kebbs
43  */

44 public class ExceptionHandler {
45     
46     private Logger JavaDoc _logger;
47     
48     
49     /**
50      * Create a new exception handler with the specified logger
51      * @param logger
52      */

53     public ExceptionHandler(Logger JavaDoc logger) {
54         _logger = logger;
55     }
56      
57     protected Logger JavaDoc getLogger()
58     {
59         return _logger;
60     }
61                            
62     public InstanceException handleInstanceException(Exception JavaDoc ex, String JavaDoc messageId, String JavaDoc arg)
63     {
64         return handleInstanceException(ex, messageId, new String JavaDoc[] {arg});
65     }
66                
67     /**
68      * Convert an incoming exception to an InstanceException and log if the incoming exception
69      * is not an InstanceException.
70      * @param ex The exception
71      * @param messageId the resource bundle id of the message to log
72      * @param args arguments to be passed to the log message
73      * @return InstanceException
74      */

75     public InstanceException handleInstanceException(Exception JavaDoc ex, String JavaDoc messageId, String JavaDoc[] args)
76     {
77         InstanceException result = null;
78         Level JavaDoc level = Level.FINE;
79         if (ex instanceof InstanceException) {
80             result = (InstanceException)ex;
81         } else if (ex instanceof ConfigException) {
82             result = new InstanceException(ex);
83         } else {
84             level = Level.WARNING;
85             result = new InstanceException(ex);
86         }
87         StringManagerBase sm = StringManagerBase.getStringManager(getLogger().getResourceBundleName());
88         getLogger().log(level, sm.getString(messageId, args), ex);
89         return result;
90     }
91             
92     public ConfigException handleConfigException(Exception JavaDoc ex, String JavaDoc messageId, String JavaDoc arg)
93     {
94         return handleConfigException(ex, messageId, new String JavaDoc[] {arg});
95     }
96     
97     /**
98      * Convert an incoming exception to an ConfigException and log if the incoming exception
99      * is not a ConfigException
100      * @param ex The exception
101      * @param messageId the resource bundle id of the message to log
102      * @param args arguments to be passed to the log message
103      * @return ConfgException
104      */

105     public ConfigException handleConfigException(Exception JavaDoc ex, String JavaDoc messageId, String JavaDoc[] args)
106     {
107         ConfigException result = null;
108         Level JavaDoc level = Level.FINE;
109         if (ex instanceof ConfigException) {
110             result = (ConfigException)ex;
111         } else {
112             level = Level.WARNING;
113             result = new ConfigException(ex);
114         }
115         StringManagerBase sm = StringManagerBase.getStringManager(getLogger().getResourceBundleName());
116         getLogger().log(level, sm.getString(messageId, args), ex);
117         return result;
118     }
119 }
120
Popular Tags