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