Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.istack.internal.logging;
28 import com.sun.istack.internal.NotNull;
30 import java.util.StringTokenizer;
31 import java.util.logging.Level;
33 /**
34 * This is a helper class that provides some conveniece methods wrapped around the
35 * standard {@link java.util.logging.Logger} interface.
36 *
37 * The class also makes sure that logger names of each Metro subsystem are consistent
38 * with each other.
39 *
40 * @author Marek Potociar <marek.potociar at sun.com>
41 * @author Fabian Ritzmann
42 */
43 public class Logger {
45 private static final String WS_LOGGING_SUBSYSTEM_NAME_ROOT = "com.sun.metro";
46 private static final String ROOT_WS_PACKAGE = "com.sun.xml.internal.ws.";
47 //
48 private static final Level METHOD_CALL_LEVEL_VALUE = Level.FINEST;
49 //
50 private final String componentClassName;
51 private final java.util.logging.Logger logger;
53 /**
54 * Prevents creation of a new instance of this Logger unless used by a subclass.
55 */
56 protected Logger(final String systemLoggerName, final String componentName) {
57 this.componentClassName = "[" + componentName + "] ";
58 this.logger = java.util.logging.Logger.getLogger(systemLoggerName);
59 }
61 /**
62 * <p>
63 * The factory method returns preconfigured Logger wrapper for the class. Method calls
64 * {@link #getSystemLoggerName(java.lang.Class)} to generate default logger name.
65 * </p>
66 * <p>
67 * Since there is no caching implemented, it is advised that the method is called only once
68 * per a class in order to initialize a final static logger variable, which is then used
69 * through the class to perform actual logging tasks.
70 * </p>
71 *
72 * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
73 * @return logger instance preconfigured for use with the component
74 * @throws NullPointerException if the componentClass parameter is {@code null}.
75 */
76 public static @NotNull Logger getLogger(final @NotNull Class<?> componentClass) {
77 return new Logger(getSystemLoggerName(componentClass), componentClass.getName());
78 }
80 /**
81 * The factory method returns preconfigured Logger wrapper for the class. Since there is no caching implemented,
82 * it is advised that the method is called only once per a class in order to initialize a final static logger variable,
83 * which is then used through the class to perform actual logging tasks.
84 *
85 * This method should be only used in a special cases when overriding of a default logger name derived from the
86 * package of the component class is needed. For all common use cases please use {@link #getLogger(java.lang.Class)}
87 * method.
88 *
89 * @param customLoggerName custom name of the logger.
90 * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
91 * @return logger instance preconfigured for use with the component
92 * @throws NullPointerException if the componentClass parameter is {@code null}.
93 *
94 * @see #getLogger(java.lang.Class)
95 */
96 public static @NotNull Logger getLogger(final @NotNull String customLoggerName, final @NotNull Class<?> componentClass) {
97 return new Logger(customLoggerName, componentClass.getName());
98 }
100 /**
101 * Calculates the subsystem suffix based on the package of the component class
102 * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
103 * @return system logger name for the given {@code componentClass} instance
104 */
105 static final String getSystemLoggerName(@NotNull Class<?> componentClass) {
106 StringBuilder sb = new StringBuilder(componentClass.getPackage().getName());
107 final int lastIndexOfWsPackage = sb.lastIndexOf(ROOT_WS_PACKAGE);
108 if (lastIndexOfWsPackage > -1) {
109 sb.replace(0, lastIndexOfWsPackage + ROOT_WS_PACKAGE.length(), "");
111 StringTokenizer st = new StringTokenizer(sb.toString(), ".");
112 sb = new StringBuilder(WS_LOGGING_SUBSYSTEM_NAME_ROOT).append(".");
113 if (st.hasMoreTokens()) {
114 String token = st.nextToken();
115 if ("api".equals(token)) {
116 token = st.nextToken();
117 }
118 sb.append(token);
119 }
120 }
122 return sb.toString();
123 }
125 public void log(final Level level, final String message) {
126 if (!this.logger.isLoggable(level)) {
127 return;
128 }
129 logger.logp(level, componentClassName, getCallerMethodName(), message);
130 }
132 public void log(final Level level, final String message, final Throwable thrown) {
133 if (!this.logger.isLoggable(level)) {
134 return;
135 }
136 logger.logp(level, componentClassName, getCallerMethodName(), message, thrown);
137 }
139 public void finest(final String message) {
140 if (!this.logger.isLoggable(Level.FINEST)) {
141 return;
142 }
143 logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message);
144 }
146 public void finest(final String message, final Throwable thrown) {
147 if (!this.logger.isLoggable(Level.FINEST)) {
148 return;
149 }
150 logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, thrown);
151 }
153 public void finer(final String message) {
154 if (!this.logger.isLoggable(Level.FINER)) {
155 return;
156 }
157 logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message);
158 }
160 public void finer(final String message, final Throwable thrown) {
161 if (!this.logger.isLoggable(Level.FINER)) {
162 return;
163 }
164 logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, thrown);
165 }
167 public void fine(final String message) {
168 if (!this.logger.isLoggable(Level.FINE)) {
169 return;
170 }
171 logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message);
172 }
174 public void fine(final String message, final Throwable thrown) {
175 if (!this.logger.isLoggable(Level.FINE)) {
176 return;
177 }
178 logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message, thrown);
179 }
181 public void info(final String message) {
182 if (!this.logger.isLoggable(Level.INFO)) {
183 return;
184 }
185 logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message);
186 }
188 public void info(final String message, final Throwable thrown) {
189 if (!this.logger.isLoggable(Level.INFO)) {
190 return;
191 }
192 logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, thrown);
193 }
195 public void config(final String message) {
196 if (!this.logger.isLoggable(Level.CONFIG)) {
197 return;
198 }
199 logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message);
200 }
202 public void config(final String message, final Throwable thrown) {
203 if (!this.logger.isLoggable(Level.CONFIG)) {
204 return;
205 }
206 logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, thrown);
207 }
209 public void warning(final String message) {
210 if (!this.logger.isLoggable(Level.WARNING)) {
211 return;
212 }
213 logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message);
214 }
216 public void warning(final String message, final Throwable thrown) {
217 if (!this.logger.isLoggable(Level.WARNING)) {
218 return;
219 }
220 logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, thrown);
221 }
223 public void severe(final String message) {
224 if (!this.logger.isLoggable(Level.SEVERE)) {
225 return;
226 }
227 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message);
228 }
230 public void severe(final String message, final Throwable thrown) {
231 if (!this.logger.isLoggable(Level.SEVERE)) {
232 return;
233 }
234 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, thrown);
235 }
237 public boolean isMethodCallLoggable() {
238 return this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE);
239 }
241 public boolean isLoggable(final Level level) {
242 return this.logger.isLoggable(level);
243 }
245 public void setLevel(final Level level) {
246 this.logger.setLevel(level);
247 }
249 public void entering() {
250 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
251 return;
252 }
254 logger.entering(componentClassName, getCallerMethodName());
255 }
257 public void entering(final Object... parameters) {
258 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
259 return;
260 }
262 logger.entering(componentClassName, getCallerMethodName(), parameters);
263 }
265 public void exiting() {
266 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
267 return;
268 }
269 logger.exiting(componentClassName, getCallerMethodName());
270 }
272 public void exiting(final Object result) {
273 if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
274 return;
275 }
276 logger.exiting(componentClassName, getCallerMethodName(), result);
277 }
279 /**
280 * Method logs {@code exception}'s message as a {@code SEVERE} logging level
281 * message.
282 * <p/>
283 * If {@code cause} parameter is not {@code null}, it is logged as well and
284 * {@code exception} original cause is initialized with instance referenced
285 * by {@code cause} parameter.
286 *
287 * @param exception exception whose message should be logged. Must not be
288 * {@code null}.
289 * @param cause initial cause of the exception that should be logged as well
290 * and set as {@code exception}'s original cause. May be {@code null}.
291 * @return the same exception instance that was passed in as the {@code exception}
292 * parameter.
293 */
294 public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) {
295 if (this.logger.isLoggable(Level.SEVERE)) {
296 if (cause == null) {
297 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
298 } else {
299 exception.initCause(cause);
300 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause);
301 }
302 }
304 return exception;
305 }
307 /**
308 * Method logs {@code exception}'s message as a {@code SEVERE} logging level
309 * message.
310 * <p/>
311 * If {@code logCause} parameter is {@code true}, {@code exception}'s original
312 * cause is logged as well (if exists). This may be used in cases when
313 * {@code exception}'s class provides constructor to initialize the original
314 * cause. In such case you do not need to use
315 * {@link #logSevereException(Throwable, Throwable)}
316 * method version but you might still want to log the original cause as well.
317 *
318 * @param exception exception whose message should be logged. Must not be
319 * {@code null}.
320 * @param logCause deterimnes whether initial cause of the exception should
321 * be logged as well
322 * @return the same exception instance that was passed in as the {@code exception}
323 * parameter.
324 */
325 public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) {
326 if (this.logger.isLoggable(Level.SEVERE)) {
327 if (logCause && exception.getCause() != null) {
328 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
329 } else {
330 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
331 }
332 }
334 return exception;
335 }
337 /**
338 * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}.
339 */
340 public <T extends Throwable> T logSevereException(final T exception) {
341 if (this.logger.isLoggable(Level.SEVERE)) {
342 if (exception.getCause() == null) {
343 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage());
344 } else {
345 logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
346 }
347 }
349 return exception;
350 }
352 /**
353 * Method logs {@code exception}'s message at the logging level specified by the
354 * {@code level} argument.
355 * <p/>
356 * If {@code cause} parameter is not {@code null}, it is logged as well and
357 * {@code exception} original cause is initialized with instance referenced
358 * by {@code cause} parameter.
359 *
360 * @param exception exception whose message should be logged. Must not be
361 * {@code null}.
362 * @param cause initial cause of the exception that should be logged as well
363 * and set as {@code exception}'s original cause. May be {@code null}.
364 * @param level loging level which should be used for logging
365 * @return the same exception instance that was passed in as the {@code exception}
366 * parameter.
367 */
368 public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) {
369 if (this.logger.isLoggable(level)) {
370 if (cause == null) {
371 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
372 } else {
373 exception.initCause(cause);
374 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause);
375 }
376 }
378 return exception;
379 }
381 /**
382 * Method logs {@code exception}'s message at the logging level specified by the
383 * {@code level} argument.
384 * <p/>
385 * If {@code logCause} parameter is {@code true}, {@code exception}'s original
386 * cause is logged as well (if exists). This may be used in cases when
387 * {@code exception}'s class provides constructor to initialize the original
388 * cause. In such case you do not need to use
389 * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)}
390 * method version but you might still want to log the original cause as well.
391 *
392 * @param exception exception whose message should be logged. Must not be
393 * {@code null}.
394 * @param logCause deterimnes whether initial cause of the exception should
395 * be logged as well
396 * @param level loging level which should be used for logging
397 * @return the same exception instance that was passed in as the {@code exception}
398 * parameter.
399 */
400 public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) {
401 if (this.logger.isLoggable(level)) {
402 if (logCause && exception.getCause() != null) {
403 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
404 } else {
405 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
406 }
407 }
409 return exception;
410 }
412 /**
413 * Same as {@link #logException(Throwable, Throwable, Level)
414 * logException(exception, true, level)}.
415 */
416 public <T extends Throwable> T logException(final T exception, final Level level) {
417 if (this.logger.isLoggable(level)) {
418 if (exception.getCause() == null) {
419 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage());
420 } else {
421 logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause());
422 }
423 }
425 return exception;
426 }
428 /**
429 * Function returns the name of the caller method for the method executing this
430 * function.
431 *
432 * @return caller method name from the call stack of the current {@link Thread}.
433 */
434 private static String getCallerMethodName() {
435 return getStackMethodName(5);
436 }
438 /**
439 * Method returns the name of the method that is on the {@code methodIndexInStack}
440 * position in the call stack of the current {@link Thread}.
441 *
442 * @param methodIndexInStack index to the call stack to get the method name for.
443 * @return the name of the method that is on the {@code methodIndexInStack}
444 * position in the call stack of the current {@link Thread}.
445 */
446 private static String getStackMethodName(final int methodIndexInStack) {
447 final String methodName;
449 final StackTraceElement[] stack = Thread.currentThread().getStackTrace();
450 if (stack.length > methodIndexInStack + 1) {
451 methodName = stack[methodIndexInStack].getMethodName();
452 } else {
453 methodName = "UNKNOWN METHOD";
454 }
456 return methodName;
457 }
459 }