001: // Copyright (c) 2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * Util.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL.
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license terms.
009: *
010: */
011: package com.sun.jbi.common;
012:
013: import com.sun.jbi.StringTranslator;
014: import com.sun.jbi.common.management.ManagementMessageBuilder;
015: import com.sun.jbi.common.management.ManagementMessageBuilderImpl;
016:
017: import java.io.PrintWriter;
018: import java.io.StringWriter;
019:
020: import java.util.HashMap;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: /**
025: * This is an utility class which provides an interface to access the string tran
026: *
027: * @author Srinivas Kondapalli
028: */
029: public class Util {
030: /**
031: * Table of handles to StringTranslators.
032: */
033: private static HashMap sStringTranslators;
034:
035: /**
036: * StringTranslator for the common utility.
037: */
038: private static StringTranslator sTranslator;
039:
040: /**
041: * Handle to the Logger instance.
042: */
043: private static Logger sLogger;
044:
045: static {
046: sStringTranslators = new HashMap();
047: sLogger = Logger.getLogger("com.sun.jbi.common");
048: sTranslator = new StringTranslatorImpl("com.sun.jbi.common",
049: null);
050: sStringTranslators.put("com.sun.jbi.common", sTranslator);
051: }
052:
053: /**
054: * A helper method to efficiently log an internationalized message. The method uses
055: * the StringTranslator to get the internationalized only if the configured log
056: * level is greater than or equal to the prescribed log level. It is recommended
057: * that clients use
058: * <pre><code>
059: * Util.log( stringTranslator, logger, Level.FINE, key);
060: * </code></pre>
061: * instead of
062: * <pre> <code>
063: * logger.fine(stringTranslator.getString(key));
064: * </code></pre>
065: *
066: * @param stringTranslator the stringTranslator implementation to be used
067: * @param logger the logger instance to be used
068: * @param level the prescribed log level
069: * @param key the key to the localized string in the resource bundle
070: */
071: public static void log(StringTranslator stringTranslator,
072: Logger logger, Level level, String key) {
073: if (logger.isLoggable(level)) {
074: logger.log(level, stringTranslator.getString(key));
075: }
076: }
077:
078: /**
079: * A helper method to efficiently log an internationalized message. The method uses
080: * the StringTranslator to get the internationalized only if the configured log
081: * level is greater than or equal to the prescribed log level. This is a helper
082: * method which can be used when the inserts length is 1.
083: *
084: * @param stringTranslator the stringTranslator implementation to be used
085: * @param logger the logger instance to be used
086: * @param level the prescribed log level
087: * @param key the key to the localized string in the resource bundle
088: * @param insert the object value to be inserted.
089: */
090: public static void log(StringTranslator stringTranslator,
091: Logger logger, Level level, String key, Object insert) {
092: if (logger.isLoggable(level)) {
093: Object[] inserts = new Object[1];
094: inserts[0] = insert;
095: logger.log(level, stringTranslator.getString(key, inserts));
096: }
097: }
098:
099: /**
100: * A helper method to efficiently log an internationalized message. The method uses
101: * the StringTranslator to get the internationalized only if the configured log
102: * level is greater than or equal to the prescribed log level. This is a helper
103: * method which can be used when the inserts length is 2.
104: *
105: * @param stringTranslator the stringTranslator implementation to be used
106: * @param logger the logger instance to be used
107: * @param level the prescribed log level
108: * @param key the key to the localized string in the resource bundle
109: * @param insert1 the first object value to be inserted.
110: * @param insert2 the second object value to be inserted.
111: */
112: public static void log(StringTranslator stringTranslator,
113: Logger logger, Level level, String key, Object insert1,
114: Object insert2) {
115: if (logger.isLoggable(level)) {
116: Object[] inserts = new Object[2];
117: inserts[0] = insert1;
118: inserts[1] = insert2;
119: logger.log(level, stringTranslator.getString(key, inserts));
120: }
121: }
122:
123: /**
124: * A helper method to efficiently log an internationalized message. The method uses
125: * the StringTranslator to get the internationalized only if the configured log
126: * level is greater than or equal to the prescribed log level. It is recommended
127: * that clients use
128: * <pre><code>
129: * Util.log( stringTranslator, logger, Level.FINE, key, inserts);
130: * </code></pre>
131: * instead of
132: * <pre> <code>
133: * logger.fine(stringTranslator.getString(key, inserts));
134: * </code></pre>
135: *
136: * @param stringTranslator the stringTranslator implementation to be used
137: * @param logger the logger instance to be used
138: * @param level the prescribed log level
139: * @param key the key to the localized string in the resource bundle
140: * @param inserts the array of message inserts.
141: */
142: public static void log(StringTranslator stringTranslator,
143: Logger logger, Level level, String key, Object[] inserts) {
144: if (logger.isLoggable(level)) {
145: logger.log(level, stringTranslator.getString(key, inserts));
146: }
147: }
148:
149: /**
150: * Get the StringTranslator for a specified package name.
151: *
152: * @param packageName - the name of the package containing the resource bundle to be
153: * used by this StringTranslator.
154: *
155: * @return The StringTranslator instance.
156: */
157: public static synchronized StringTranslator getStringTranslator(
158: String packageName) {
159: sLogger.fine(sTranslator.getString(
160: "EC_STRING_TRANSLATOR_REQUESTED", packageName));
161:
162: StringTranslator translator = (StringTranslator) sStringTranslators
163: .get(packageName);
164:
165: if (null == translator) {
166: translator = new StringTranslatorImpl(packageName, null);
167: sStringTranslators.put(packageName, translator);
168: sLogger.finer(sTranslator.getString(
169: "EC_STRING_TRANSLATOR_CREATED", packageName));
170: }
171:
172: return translator;
173: }
174:
175: /**
176: * Get the StringTranslator for a specified object.
177: *
178: * @param object - an object in the package that contains the resource bundle to be
179: * used for this StringTranslator.
180: *
181: * @return The StringTranslator instance.
182: */
183: public static StringTranslator getStringTranslatorFor(Object object) {
184: return getStringTranslator(object.getClass().getPackage()
185: .getName());
186: }
187:
188: /**
189: * Logs the exception stack trace using the defined logger instance at the prescribed
190: * level.
191: *
192: * @param exception exception instance thrown by the application.
193: * @param logger logger instance to be used.
194: * @param level prescribed level.
195: */
196: public static void logExceptionTrace(Exception exception,
197: Logger logger, Level level) {
198: if (logger.isLoggable(level)) {
199: StringWriter stringWriter = new StringWriter();
200: PrintWriter printWriter = new PrintWriter(stringWriter);
201: exception.printStackTrace(printWriter);
202: logger.log(level, stringWriter.toString());
203: printWriter.close();
204: }
205: }
206:
207: /**
208: * Logs the exception stack trace using the defined logger. It logs the
209: * exception trace using a default level of SEVERE.
210: *
211: * @param exception exception instance thrown by the application.
212: * @param logger logger instance to be used
213: */
214: public static void logExceptionTrace(Exception exception,
215: Logger logger) {
216: logExceptionTrace(exception, logger, Level.SEVERE);
217: }
218:
219: /**
220: * Creates a new management message builder instance.
221: *
222: * @return a management message builder implementation instance.
223: */
224: public static ManagementMessageBuilder createManagementMessageBuilder() {
225: return new ManagementMessageBuilderImpl();
226: }
227:
228: }
|