1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/istack/internal/logging/Logger.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,459 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.istack.internal.logging; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 + 1.33 +import java.util.StringTokenizer; 1.34 +import java.util.logging.Level; 1.35 + 1.36 +/** 1.37 + * This is a helper class that provides some conveniece methods wrapped around the 1.38 + * standard {@link java.util.logging.Logger} interface. 1.39 + * 1.40 + * The class also makes sure that logger names of each Metro subsystem are consistent 1.41 + * with each other. 1.42 + * 1.43 + * @author Marek Potociar <marek.potociar at sun.com> 1.44 + * @author Fabian Ritzmann 1.45 + */ 1.46 +public class Logger { 1.47 + 1.48 + private static final String WS_LOGGING_SUBSYSTEM_NAME_ROOT = "com.sun.metro"; 1.49 + private static final String ROOT_WS_PACKAGE = "com.sun.xml.internal.ws."; 1.50 + // 1.51 + private static final Level METHOD_CALL_LEVEL_VALUE = Level.FINEST; 1.52 + // 1.53 + private final String componentClassName; 1.54 + private final java.util.logging.Logger logger; 1.55 + 1.56 + /** 1.57 + * Prevents creation of a new instance of this Logger unless used by a subclass. 1.58 + */ 1.59 + protected Logger(final String systemLoggerName, final String componentName) { 1.60 + this.componentClassName = "[" + componentName + "] "; 1.61 + this.logger = java.util.logging.Logger.getLogger(systemLoggerName); 1.62 + } 1.63 + 1.64 + /** 1.65 + * <p> 1.66 + * The factory method returns preconfigured Logger wrapper for the class. Method calls 1.67 + * {@link #getSystemLoggerName(java.lang.Class)} to generate default logger name. 1.68 + * </p> 1.69 + * <p> 1.70 + * Since there is no caching implemented, it is advised that the method is called only once 1.71 + * per a class in order to initialize a final static logger variable, which is then used 1.72 + * through the class to perform actual logging tasks. 1.73 + * </p> 1.74 + * 1.75 + * @param componentClass class of the component that will use the logger instance. Must not be {@code null}. 1.76 + * @return logger instance preconfigured for use with the component 1.77 + * @throws NullPointerException if the componentClass parameter is {@code null}. 1.78 + */ 1.79 + public static @NotNull Logger getLogger(final @NotNull Class<?> componentClass) { 1.80 + return new Logger(getSystemLoggerName(componentClass), componentClass.getName()); 1.81 + } 1.82 + 1.83 + /** 1.84 + * The factory method returns preconfigured Logger wrapper for the class. Since there is no caching implemented, 1.85 + * it is advised that the method is called only once per a class in order to initialize a final static logger variable, 1.86 + * which is then used through the class to perform actual logging tasks. 1.87 + * 1.88 + * This method should be only used in a special cases when overriding of a default logger name derived from the 1.89 + * package of the component class is needed. For all common use cases please use {@link #getLogger(java.lang.Class)} 1.90 + * method. 1.91 + * 1.92 + * @param customLoggerName custom name of the logger. 1.93 + * @param componentClass class of the component that will use the logger instance. Must not be {@code null}. 1.94 + * @return logger instance preconfigured for use with the component 1.95 + * @throws NullPointerException if the componentClass parameter is {@code null}. 1.96 + * 1.97 + * @see #getLogger(java.lang.Class) 1.98 + */ 1.99 + public static @NotNull Logger getLogger(final @NotNull String customLoggerName, final @NotNull Class<?> componentClass) { 1.100 + return new Logger(customLoggerName, componentClass.getName()); 1.101 + } 1.102 + 1.103 + /** 1.104 + * Calculates the subsystem suffix based on the package of the component class 1.105 + * @param componentClass class of the component that will use the logger instance. Must not be {@code null}. 1.106 + * @return system logger name for the given {@code componentClass} instance 1.107 + */ 1.108 + static final String getSystemLoggerName(@NotNull Class<?> componentClass) { 1.109 + StringBuilder sb = new StringBuilder(componentClass.getPackage().getName()); 1.110 + final int lastIndexOfWsPackage = sb.lastIndexOf(ROOT_WS_PACKAGE); 1.111 + if (lastIndexOfWsPackage > -1) { 1.112 + sb.replace(0, lastIndexOfWsPackage + ROOT_WS_PACKAGE.length(), ""); 1.113 + 1.114 + StringTokenizer st = new StringTokenizer(sb.toString(), "."); 1.115 + sb = new StringBuilder(WS_LOGGING_SUBSYSTEM_NAME_ROOT).append("."); 1.116 + if (st.hasMoreTokens()) { 1.117 + String token = st.nextToken(); 1.118 + if ("api".equals(token)) { 1.119 + token = st.nextToken(); 1.120 + } 1.121 + sb.append(token); 1.122 + } 1.123 + } 1.124 + 1.125 + return sb.toString(); 1.126 + } 1.127 + 1.128 + public void log(final Level level, final String message) { 1.129 + if (!this.logger.isLoggable(level)) { 1.130 + return; 1.131 + } 1.132 + logger.logp(level, componentClassName, getCallerMethodName(), message); 1.133 + } 1.134 + 1.135 + public void log(final Level level, final String message, final Throwable thrown) { 1.136 + if (!this.logger.isLoggable(level)) { 1.137 + return; 1.138 + } 1.139 + logger.logp(level, componentClassName, getCallerMethodName(), message, thrown); 1.140 + } 1.141 + 1.142 + public void finest(final String message) { 1.143 + if (!this.logger.isLoggable(Level.FINEST)) { 1.144 + return; 1.145 + } 1.146 + logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message); 1.147 + } 1.148 + 1.149 + public void finest(final String message, final Throwable thrown) { 1.150 + if (!this.logger.isLoggable(Level.FINEST)) { 1.151 + return; 1.152 + } 1.153 + logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, thrown); 1.154 + } 1.155 + 1.156 + public void finer(final String message) { 1.157 + if (!this.logger.isLoggable(Level.FINER)) { 1.158 + return; 1.159 + } 1.160 + logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message); 1.161 + } 1.162 + 1.163 + public void finer(final String message, final Throwable thrown) { 1.164 + if (!this.logger.isLoggable(Level.FINER)) { 1.165 + return; 1.166 + } 1.167 + logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, thrown); 1.168 + } 1.169 + 1.170 + public void fine(final String message) { 1.171 + if (!this.logger.isLoggable(Level.FINE)) { 1.172 + return; 1.173 + } 1.174 + logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message); 1.175 + } 1.176 + 1.177 + public void fine(final String message, final Throwable thrown) { 1.178 + if (!this.logger.isLoggable(Level.FINE)) { 1.179 + return; 1.180 + } 1.181 + logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message, thrown); 1.182 + } 1.183 + 1.184 + public void info(final String message) { 1.185 + if (!this.logger.isLoggable(Level.INFO)) { 1.186 + return; 1.187 + } 1.188 + logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message); 1.189 + } 1.190 + 1.191 + public void info(final String message, final Throwable thrown) { 1.192 + if (!this.logger.isLoggable(Level.INFO)) { 1.193 + return; 1.194 + } 1.195 + logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, thrown); 1.196 + } 1.197 + 1.198 + public void config(final String message) { 1.199 + if (!this.logger.isLoggable(Level.CONFIG)) { 1.200 + return; 1.201 + } 1.202 + logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message); 1.203 + } 1.204 + 1.205 + public void config(final String message, final Throwable thrown) { 1.206 + if (!this.logger.isLoggable(Level.CONFIG)) { 1.207 + return; 1.208 + } 1.209 + logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, thrown); 1.210 + } 1.211 + 1.212 + public void warning(final String message) { 1.213 + if (!this.logger.isLoggable(Level.WARNING)) { 1.214 + return; 1.215 + } 1.216 + logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message); 1.217 + } 1.218 + 1.219 + public void warning(final String message, final Throwable thrown) { 1.220 + if (!this.logger.isLoggable(Level.WARNING)) { 1.221 + return; 1.222 + } 1.223 + logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, thrown); 1.224 + } 1.225 + 1.226 + public void severe(final String message) { 1.227 + if (!this.logger.isLoggable(Level.SEVERE)) { 1.228 + return; 1.229 + } 1.230 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message); 1.231 + } 1.232 + 1.233 + public void severe(final String message, final Throwable thrown) { 1.234 + if (!this.logger.isLoggable(Level.SEVERE)) { 1.235 + return; 1.236 + } 1.237 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, thrown); 1.238 + } 1.239 + 1.240 + public boolean isMethodCallLoggable() { 1.241 + return this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE); 1.242 + } 1.243 + 1.244 + public boolean isLoggable(final Level level) { 1.245 + return this.logger.isLoggable(level); 1.246 + } 1.247 + 1.248 + public void setLevel(final Level level) { 1.249 + this.logger.setLevel(level); 1.250 + } 1.251 + 1.252 + public void entering() { 1.253 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.254 + return; 1.255 + } 1.256 + 1.257 + logger.entering(componentClassName, getCallerMethodName()); 1.258 + } 1.259 + 1.260 + public void entering(final Object... parameters) { 1.261 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.262 + return; 1.263 + } 1.264 + 1.265 + logger.entering(componentClassName, getCallerMethodName(), parameters); 1.266 + } 1.267 + 1.268 + public void exiting() { 1.269 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.270 + return; 1.271 + } 1.272 + logger.exiting(componentClassName, getCallerMethodName()); 1.273 + } 1.274 + 1.275 + public void exiting(final Object result) { 1.276 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.277 + return; 1.278 + } 1.279 + logger.exiting(componentClassName, getCallerMethodName(), result); 1.280 + } 1.281 + 1.282 + /** 1.283 + * Method logs {@code exception}'s message as a {@code SEVERE} logging level 1.284 + * message. 1.285 + * <p/> 1.286 + * If {@code cause} parameter is not {@code null}, it is logged as well and 1.287 + * {@code exception} original cause is initialized with instance referenced 1.288 + * by {@code cause} parameter. 1.289 + * 1.290 + * @param exception exception whose message should be logged. Must not be 1.291 + * {@code null}. 1.292 + * @param cause initial cause of the exception that should be logged as well 1.293 + * and set as {@code exception}'s original cause. May be {@code null}. 1.294 + * @return the same exception instance that was passed in as the {@code exception} 1.295 + * parameter. 1.296 + */ 1.297 + public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) { 1.298 + if (this.logger.isLoggable(Level.SEVERE)) { 1.299 + if (cause == null) { 1.300 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 1.301 + } else { 1.302 + exception.initCause(cause); 1.303 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 1.304 + } 1.305 + } 1.306 + 1.307 + return exception; 1.308 + } 1.309 + 1.310 + /** 1.311 + * Method logs {@code exception}'s message as a {@code SEVERE} logging level 1.312 + * message. 1.313 + * <p/> 1.314 + * If {@code logCause} parameter is {@code true}, {@code exception}'s original 1.315 + * cause is logged as well (if exists). This may be used in cases when 1.316 + * {@code exception}'s class provides constructor to initialize the original 1.317 + * cause. In such case you do not need to use 1.318 + * {@link #logSevereException(Throwable, Throwable)} 1.319 + * method version but you might still want to log the original cause as well. 1.320 + * 1.321 + * @param exception exception whose message should be logged. Must not be 1.322 + * {@code null}. 1.323 + * @param logCause deterimnes whether initial cause of the exception should 1.324 + * be logged as well 1.325 + * @return the same exception instance that was passed in as the {@code exception} 1.326 + * parameter. 1.327 + */ 1.328 + public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) { 1.329 + if (this.logger.isLoggable(Level.SEVERE)) { 1.330 + if (logCause && exception.getCause() != null) { 1.331 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.332 + } else { 1.333 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 1.334 + } 1.335 + } 1.336 + 1.337 + return exception; 1.338 + } 1.339 + 1.340 + /** 1.341 + * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}. 1.342 + */ 1.343 + public <T extends Throwable> T logSevereException(final T exception) { 1.344 + if (this.logger.isLoggable(Level.SEVERE)) { 1.345 + if (exception.getCause() == null) { 1.346 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 1.347 + } else { 1.348 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.349 + } 1.350 + } 1.351 + 1.352 + return exception; 1.353 + } 1.354 + 1.355 + /** 1.356 + * Method logs {@code exception}'s message at the logging level specified by the 1.357 + * {@code level} argument. 1.358 + * <p/> 1.359 + * If {@code cause} parameter is not {@code null}, it is logged as well and 1.360 + * {@code exception} original cause is initialized with instance referenced 1.361 + * by {@code cause} parameter. 1.362 + * 1.363 + * @param exception exception whose message should be logged. Must not be 1.364 + * {@code null}. 1.365 + * @param cause initial cause of the exception that should be logged as well 1.366 + * and set as {@code exception}'s original cause. May be {@code null}. 1.367 + * @param level loging level which should be used for logging 1.368 + * @return the same exception instance that was passed in as the {@code exception} 1.369 + * parameter. 1.370 + */ 1.371 + public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) { 1.372 + if (this.logger.isLoggable(level)) { 1.373 + if (cause == null) { 1.374 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 1.375 + } else { 1.376 + exception.initCause(cause); 1.377 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 1.378 + } 1.379 + } 1.380 + 1.381 + return exception; 1.382 + } 1.383 + 1.384 + /** 1.385 + * Method logs {@code exception}'s message at the logging level specified by the 1.386 + * {@code level} argument. 1.387 + * <p/> 1.388 + * If {@code logCause} parameter is {@code true}, {@code exception}'s original 1.389 + * cause is logged as well (if exists). This may be used in cases when 1.390 + * {@code exception}'s class provides constructor to initialize the original 1.391 + * cause. In such case you do not need to use 1.392 + * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)} 1.393 + * method version but you might still want to log the original cause as well. 1.394 + * 1.395 + * @param exception exception whose message should be logged. Must not be 1.396 + * {@code null}. 1.397 + * @param logCause deterimnes whether initial cause of the exception should 1.398 + * be logged as well 1.399 + * @param level loging level which should be used for logging 1.400 + * @return the same exception instance that was passed in as the {@code exception} 1.401 + * parameter. 1.402 + */ 1.403 + public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) { 1.404 + if (this.logger.isLoggable(level)) { 1.405 + if (logCause && exception.getCause() != null) { 1.406 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.407 + } else { 1.408 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 1.409 + } 1.410 + } 1.411 + 1.412 + return exception; 1.413 + } 1.414 + 1.415 + /** 1.416 + * Same as {@link #logException(Throwable, Throwable, Level) 1.417 + * logException(exception, true, level)}. 1.418 + */ 1.419 + public <T extends Throwable> T logException(final T exception, final Level level) { 1.420 + if (this.logger.isLoggable(level)) { 1.421 + if (exception.getCause() == null) { 1.422 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 1.423 + } else { 1.424 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.425 + } 1.426 + } 1.427 + 1.428 + return exception; 1.429 + } 1.430 + 1.431 + /** 1.432 + * Function returns the name of the caller method for the method executing this 1.433 + * function. 1.434 + * 1.435 + * @return caller method name from the call stack of the current {@link Thread}. 1.436 + */ 1.437 + private static String getCallerMethodName() { 1.438 + return getStackMethodName(5); 1.439 + } 1.440 + 1.441 + /** 1.442 + * Method returns the name of the method that is on the {@code methodIndexInStack} 1.443 + * position in the call stack of the current {@link Thread}. 1.444 + * 1.445 + * @param methodIndexInStack index to the call stack to get the method name for. 1.446 + * @return the name of the method that is on the {@code methodIndexInStack} 1.447 + * position in the call stack of the current {@link Thread}. 1.448 + */ 1.449 + private static String getStackMethodName(final int methodIndexInStack) { 1.450 + final String methodName; 1.451 + 1.452 + final StackTraceElement[] stack = Thread.currentThread().getStackTrace(); 1.453 + if (stack.length > methodIndexInStack + 1) { 1.454 + methodName = stack[methodIndexInStack].getMethodName(); 1.455 + } else { 1.456 + methodName = "UNKNOWN METHOD"; 1.457 + } 1.458 + 1.459 + return methodName; 1.460 + } 1.461 + 1.462 +}