KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > logging > MessageLogger


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.logging;
59
60 import java.util.Locale JavaDoc;
61 import java.text.MessageFormat JavaDoc;
62 import java.util.MissingResourceException JavaDoc;
63 import java.util.ResourceBundle JavaDoc;
64 import org.apache.commons.logging.Log;
65 import org.apache.commons.logging.LogFactory;
66
67 /**
68  * @author Mark Whitlock <whitlock@apache.org>
69  */

70 public class MessageLogger {
71     private static Log log = LogFactory.getLog("wsif");
72
73     /**
74      * Message resource bundle.
75      */

76     private static ResourceBundle JavaDoc messages = null;
77
78     /**
79      * Private constructor so no one can instantiate this class.
80      */

81     private MessageLogger() {
82         Trc.entry(this);
83         Trc.exit();
84     }
85     
86     public static boolean isInfoEnabled() {
87         return log.isInfoEnabled();
88     }
89
90     public static boolean isWarnEnabled() {
91         return log.isWarnEnabled();
92     }
93
94     public static boolean isErrorEnabled() {
95         return log.isErrorEnabled();
96     }
97
98     public static boolean isFatalEnabled() {
99         return log.isFatalEnabled();
100     }
101
102     public static void log(String JavaDoc key) {
103         Trc.entry(null,key);
104         try {
105             logIt(key.charAt(key.length() - 1), getMessage(key));
106         } catch (MissingResourceException JavaDoc mre) {
107             Trc.exception(mre);
108             handleMissingResourceException(mre, key, null);
109         }
110         Trc.exit();
111     }
112
113     /**
114      * Logs the message with the given key. If an argument is
115      * specified in the message (in the format of "{0}") then
116      * fill in that argument with the value of var.
117      */

118     public static void log(String JavaDoc key, Object JavaDoc var) {
119         Trc.entry(null,key,var);
120         String JavaDoc[] args = { var == null ? "<null>" : var.toString()};
121         try {
122             logIt(
123                 key.charAt(key.length() - 1),
124                 MessageFormat.format(getMessage(key), args));
125         } catch (MissingResourceException JavaDoc mre) {
126             Trc.exception(mre);
127             handleMissingResourceException(mre, key, args);
128         }
129         Trc.exit();
130     }
131
132     /**
133      * Logs the message with the given key. If arguments are
134      * specified in the message (in the format of "{0} {1}")
135      * then fill them in with the values of var1 and var2, respectively.
136      */

137     public static void log(String JavaDoc key, Object JavaDoc var1, Object JavaDoc var2) {
138         Trc.entry(null,key,var1,var2);
139         String JavaDoc[] args = {
140             var1 == null ? "<null>" : var1.toString(),
141             var2 == null ? "<null>" : var2.toString()};
142         try {
143             logIt(
144                 key.charAt(key.length() - 1),
145                 MessageFormat.format(getMessage(key), args));
146         } catch (MissingResourceException JavaDoc mre) {
147             Trc.exception(mre);
148             handleMissingResourceException(mre, key, args);
149         }
150         Trc.exit();
151     }
152
153     /**
154      * Logs the message with the given key. Replace each "{X}"
155      * in the message with vars[X]. If there are more vars than
156      * {X}'s, then the extra vars are ignored. If there are more {X}'s
157      * than vars, then a java.text.ParseException (subclass of
158      * RuntimeException) is thrown.
159      */

160     public static void log(String JavaDoc key, Object JavaDoc[] vars) {
161         Trc.entry(null,key,vars);
162         String JavaDoc[] args;
163         if (vars == null) {
164             args = new String JavaDoc[1];
165             args[0] = "<null>";
166         } else {
167             args = new String JavaDoc[vars.length];
168             for (int i = 0; i < vars.length; i++)
169                 args[i] = (vars[i] == null ? "<null>" : vars[i].toString());
170         }
171
172         try {
173             logIt(
174                 key.charAt(key.length() - 1),
175                 MessageFormat.format(getMessage(key), args));
176         } catch (MissingResourceException JavaDoc mre) {
177             Trc.exception(mre);
178             handleMissingResourceException(mre, key, args);
179         }
180         Trc.exit();
181     }
182
183     /**
184      * Get the message with the given key.
185      * Load the ResourceBundle here instead of in a static initialiser so it
186      * won't be loaded if we never log a message.
187      */

188     private static String JavaDoc getMessage(String JavaDoc key) {
189         if (messages == null)
190             messages = ResourceBundle.getBundle("org.apache.wsif.catalog.Messages");
191
192         return messages.getString(key);
193     }
194
195     private static void logIt(char severity, String JavaDoc text) {
196         if (severity == 'I') {
197             if (isInfoEnabled())
198                 log.info(text);
199         } else if (severity == 'W') {
200             if (isWarnEnabled())
201                 log.warn(text);
202         } else if (severity == 'F') {
203             if (isFatalEnabled())
204                 log.fatal(text);
205         } else {
206             // default is error
207
if (isErrorEnabled())
208                 log.error(text);
209         }
210     }
211
212     private static void handleMissingResourceException(
213         MissingResourceException JavaDoc mre,
214         String JavaDoc key,
215         String JavaDoc[] args) {
216
217         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("WSIF: Unable to display message ");
218         sb.append(key);
219         if (args != null && args.length > 0) {
220             sb.append(" with arguments ");
221             for (int i = 0; i < args.length; i++) {
222                 if (i != 0)
223                     sb.append(", ");
224                 sb.append(args[i]);
225             }
226         }
227         sb.append(" because WSIF MessageLogger caught ");
228         sb.append(mre.toString());
229         log.error(sb.toString());
230     }
231 }
232
Popular Tags