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 Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,515 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, 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 convenience 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, Object param1) { 1.136 + if (!this.logger.isLoggable(level)) { 1.137 + return; 1.138 + } 1.139 + logger.logp(level, componentClassName, getCallerMethodName(), message, param1); 1.140 + } 1.141 + 1.142 + public void log(final Level level, final String message, Object[] params) { 1.143 + if (!this.logger.isLoggable(level)) { 1.144 + return; 1.145 + } 1.146 + logger.logp(level, componentClassName, getCallerMethodName(), message, params); 1.147 + } 1.148 + 1.149 + public void log(final Level level, final String message, final Throwable thrown) { 1.150 + if (!this.logger.isLoggable(level)) { 1.151 + return; 1.152 + } 1.153 + logger.logp(level, componentClassName, getCallerMethodName(), message, thrown); 1.154 + } 1.155 + 1.156 + public void finest(final String message) { 1.157 + if (!this.logger.isLoggable(Level.FINEST)) { 1.158 + return; 1.159 + } 1.160 + logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message); 1.161 + } 1.162 + 1.163 + public void finest(final String message, Object[] params) { 1.164 + if (!this.logger.isLoggable(Level.FINEST)) { 1.165 + return; 1.166 + } 1.167 + logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, params); 1.168 + } 1.169 + 1.170 + public void finest(final String message, final Throwable thrown) { 1.171 + if (!this.logger.isLoggable(Level.FINEST)) { 1.172 + return; 1.173 + } 1.174 + logger.logp(Level.FINEST, componentClassName, getCallerMethodName(), message, thrown); 1.175 + } 1.176 + 1.177 + public void finer(final String message) { 1.178 + if (!this.logger.isLoggable(Level.FINER)) { 1.179 + return; 1.180 + } 1.181 + logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message); 1.182 + } 1.183 + 1.184 + public void finer(final String message, Object[] params) { 1.185 + if (!this.logger.isLoggable(Level.FINER)) { 1.186 + return; 1.187 + } 1.188 + logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, params); 1.189 + } 1.190 + 1.191 + public void finer(final String message, final Throwable thrown) { 1.192 + if (!this.logger.isLoggable(Level.FINER)) { 1.193 + return; 1.194 + } 1.195 + logger.logp(Level.FINER, componentClassName, getCallerMethodName(), message, thrown); 1.196 + } 1.197 + 1.198 + public void fine(final String message) { 1.199 + if (!this.logger.isLoggable(Level.FINE)) { 1.200 + return; 1.201 + } 1.202 + logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message); 1.203 + } 1.204 + 1.205 + public void fine(final String message, final Throwable thrown) { 1.206 + if (!this.logger.isLoggable(Level.FINE)) { 1.207 + return; 1.208 + } 1.209 + logger.logp(Level.FINE, componentClassName, getCallerMethodName(), message, thrown); 1.210 + } 1.211 + 1.212 + public void info(final String message) { 1.213 + if (!this.logger.isLoggable(Level.INFO)) { 1.214 + return; 1.215 + } 1.216 + logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message); 1.217 + } 1.218 + 1.219 + public void info(final String message, Object[] params) { 1.220 + if (!this.logger.isLoggable(Level.INFO)) { 1.221 + return; 1.222 + } 1.223 + logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, params); 1.224 + } 1.225 + 1.226 + public void info(final String message, final Throwable thrown) { 1.227 + if (!this.logger.isLoggable(Level.INFO)) { 1.228 + return; 1.229 + } 1.230 + logger.logp(Level.INFO, componentClassName, getCallerMethodName(), message, thrown); 1.231 + } 1.232 + 1.233 + public void config(final String message) { 1.234 + if (!this.logger.isLoggable(Level.CONFIG)) { 1.235 + return; 1.236 + } 1.237 + logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message); 1.238 + } 1.239 + 1.240 + public void config(final String message, Object[] params) { 1.241 + if (!this.logger.isLoggable(Level.CONFIG)) { 1.242 + return; 1.243 + } 1.244 + logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, params); 1.245 + } 1.246 + 1.247 + public void config(final String message, final Throwable thrown) { 1.248 + if (!this.logger.isLoggable(Level.CONFIG)) { 1.249 + return; 1.250 + } 1.251 + logger.logp(Level.CONFIG, componentClassName, getCallerMethodName(), message, thrown); 1.252 + } 1.253 + 1.254 + public void warning(final String message) { 1.255 + if (!this.logger.isLoggable(Level.WARNING)) { 1.256 + return; 1.257 + } 1.258 + logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message); 1.259 + } 1.260 + 1.261 + public void warning(final String message, Object[] params) { 1.262 + if (!this.logger.isLoggable(Level.WARNING)) { 1.263 + return; 1.264 + } 1.265 + logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, params); 1.266 + } 1.267 + 1.268 + public void warning(final String message, final Throwable thrown) { 1.269 + if (!this.logger.isLoggable(Level.WARNING)) { 1.270 + return; 1.271 + } 1.272 + logger.logp(Level.WARNING, componentClassName, getCallerMethodName(), message, thrown); 1.273 + } 1.274 + 1.275 + public void severe(final String message) { 1.276 + if (!this.logger.isLoggable(Level.SEVERE)) { 1.277 + return; 1.278 + } 1.279 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message); 1.280 + } 1.281 + 1.282 + public void severe(final String message, Object[] params) { 1.283 + if (!this.logger.isLoggable(Level.SEVERE)) { 1.284 + return; 1.285 + } 1.286 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, params); 1.287 + } 1.288 + 1.289 + public void severe(final String message, final Throwable thrown) { 1.290 + if (!this.logger.isLoggable(Level.SEVERE)) { 1.291 + return; 1.292 + } 1.293 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), message, thrown); 1.294 + } 1.295 + 1.296 + public boolean isMethodCallLoggable() { 1.297 + return this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE); 1.298 + } 1.299 + 1.300 + public boolean isLoggable(final Level level) { 1.301 + return this.logger.isLoggable(level); 1.302 + } 1.303 + 1.304 + public void setLevel(final Level level) { 1.305 + this.logger.setLevel(level); 1.306 + } 1.307 + 1.308 + public void entering() { 1.309 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.310 + return; 1.311 + } 1.312 + 1.313 + logger.entering(componentClassName, getCallerMethodName()); 1.314 + } 1.315 + 1.316 + public void entering(final Object... parameters) { 1.317 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.318 + return; 1.319 + } 1.320 + 1.321 + logger.entering(componentClassName, getCallerMethodName(), parameters); 1.322 + } 1.323 + 1.324 + public void exiting() { 1.325 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.326 + return; 1.327 + } 1.328 + logger.exiting(componentClassName, getCallerMethodName()); 1.329 + } 1.330 + 1.331 + public void exiting(final Object result) { 1.332 + if (!this.logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) { 1.333 + return; 1.334 + } 1.335 + logger.exiting(componentClassName, getCallerMethodName(), result); 1.336 + } 1.337 + 1.338 + /** 1.339 + * Method logs {@code exception}'s message as a {@code SEVERE} logging level 1.340 + * message. 1.341 + * <p/> 1.342 + * If {@code cause} parameter is not {@code null}, it is logged as well and 1.343 + * {@code exception} original cause is initialized with instance referenced 1.344 + * by {@code cause} parameter. 1.345 + * 1.346 + * @param exception exception whose message should be logged. Must not be 1.347 + * {@code null}. 1.348 + * @param cause initial cause of the exception that should be logged as well 1.349 + * and set as {@code exception}'s original cause. May be {@code null}. 1.350 + * @return the same exception instance that was passed in as the {@code exception} 1.351 + * parameter. 1.352 + */ 1.353 + public <T extends Throwable> T logSevereException(final T exception, final Throwable cause) { 1.354 + if (this.logger.isLoggable(Level.SEVERE)) { 1.355 + if (cause == null) { 1.356 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 1.357 + } else { 1.358 + exception.initCause(cause); 1.359 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 1.360 + } 1.361 + } 1.362 + 1.363 + return exception; 1.364 + } 1.365 + 1.366 + /** 1.367 + * Method logs {@code exception}'s message as a {@code SEVERE} logging level 1.368 + * message. 1.369 + * <p/> 1.370 + * If {@code logCause} parameter is {@code true}, {@code exception}'s original 1.371 + * cause is logged as well (if exists). This may be used in cases when 1.372 + * {@code exception}'s class provides constructor to initialize the original 1.373 + * cause. In such case you do not need to use 1.374 + * {@link #logSevereException(Throwable, Throwable)} 1.375 + * method version but you might still want to log the original cause as well. 1.376 + * 1.377 + * @param exception exception whose message should be logged. Must not be 1.378 + * {@code null}. 1.379 + * @param logCause deterimnes whether initial cause of the exception should 1.380 + * be logged as well 1.381 + * @return the same exception instance that was passed in as the {@code exception} 1.382 + * parameter. 1.383 + */ 1.384 + public <T extends Throwable> T logSevereException(final T exception, final boolean logCause) { 1.385 + if (this.logger.isLoggable(Level.SEVERE)) { 1.386 + if (logCause && exception.getCause() != null) { 1.387 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.388 + } else { 1.389 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 1.390 + } 1.391 + } 1.392 + 1.393 + return exception; 1.394 + } 1.395 + 1.396 + /** 1.397 + * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}. 1.398 + */ 1.399 + public <T extends Throwable> T logSevereException(final T exception) { 1.400 + if (this.logger.isLoggable(Level.SEVERE)) { 1.401 + if (exception.getCause() == null) { 1.402 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage()); 1.403 + } else { 1.404 + logger.logp(Level.SEVERE, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.405 + } 1.406 + } 1.407 + 1.408 + return exception; 1.409 + } 1.410 + 1.411 + /** 1.412 + * Method logs {@code exception}'s message at the logging level specified by the 1.413 + * {@code level} argument. 1.414 + * <p/> 1.415 + * If {@code cause} parameter is not {@code null}, it is logged as well and 1.416 + * {@code exception} original cause is initialized with instance referenced 1.417 + * by {@code cause} parameter. 1.418 + * 1.419 + * @param exception exception whose message should be logged. Must not be 1.420 + * {@code null}. 1.421 + * @param cause initial cause of the exception that should be logged as well 1.422 + * and set as {@code exception}'s original cause. May be {@code null}. 1.423 + * @param level loging level which should be used for logging 1.424 + * @return the same exception instance that was passed in as the {@code exception} 1.425 + * parameter. 1.426 + */ 1.427 + public <T extends Throwable> T logException(final T exception, final Throwable cause, final Level level) { 1.428 + if (this.logger.isLoggable(level)) { 1.429 + if (cause == null) { 1.430 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 1.431 + } else { 1.432 + exception.initCause(cause); 1.433 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), cause); 1.434 + } 1.435 + } 1.436 + 1.437 + return exception; 1.438 + } 1.439 + 1.440 + /** 1.441 + * Method logs {@code exception}'s message at the logging level specified by the 1.442 + * {@code level} argument. 1.443 + * <p/> 1.444 + * If {@code logCause} parameter is {@code true}, {@code exception}'s original 1.445 + * cause is logged as well (if exists). This may be used in cases when 1.446 + * {@code exception}'s class provides constructor to initialize the original 1.447 + * cause. In such case you do not need to use 1.448 + * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)} 1.449 + * method version but you might still want to log the original cause as well. 1.450 + * 1.451 + * @param exception exception whose message should be logged. Must not be 1.452 + * {@code null}. 1.453 + * @param logCause deterimnes whether initial cause of the exception should 1.454 + * be logged as well 1.455 + * @param level loging level which should be used for logging 1.456 + * @return the same exception instance that was passed in as the {@code exception} 1.457 + * parameter. 1.458 + */ 1.459 + public <T extends Throwable> T logException(final T exception, final boolean logCause, final Level level) { 1.460 + if (this.logger.isLoggable(level)) { 1.461 + if (logCause && exception.getCause() != null) { 1.462 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.463 + } else { 1.464 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 1.465 + } 1.466 + } 1.467 + 1.468 + return exception; 1.469 + } 1.470 + 1.471 + /** 1.472 + * Same as {@link #logException(Throwable, Throwable, Level) 1.473 + * logException(exception, true, level)}. 1.474 + */ 1.475 + public <T extends Throwable> T logException(final T exception, final Level level) { 1.476 + if (this.logger.isLoggable(level)) { 1.477 + if (exception.getCause() == null) { 1.478 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage()); 1.479 + } else { 1.480 + logger.logp(level, componentClassName, getCallerMethodName(), exception.getMessage(), exception.getCause()); 1.481 + } 1.482 + } 1.483 + 1.484 + return exception; 1.485 + } 1.486 + 1.487 + /** 1.488 + * Function returns the name of the caller method for the method executing this 1.489 + * function. 1.490 + * 1.491 + * @return caller method name from the call stack of the current {@link Thread}. 1.492 + */ 1.493 + private static String getCallerMethodName() { 1.494 + return getStackMethodName(5); 1.495 + } 1.496 + 1.497 + /** 1.498 + * Method returns the name of the method that is on the {@code methodIndexInStack} 1.499 + * position in the call stack of the current {@link Thread}. 1.500 + * 1.501 + * @param methodIndexInStack index to the call stack to get the method name for. 1.502 + * @return the name of the method that is on the {@code methodIndexInStack} 1.503 + * position in the call stack of the current {@link Thread}. 1.504 + */ 1.505 + private static String getStackMethodName(final int methodIndexInStack) { 1.506 + final String methodName; 1.507 + 1.508 + final StackTraceElement[] stack = Thread.currentThread().getStackTrace(); 1.509 + if (stack.length > methodIndexInStack + 1) { 1.510 + methodName = stack[methodIndexInStack].getMethodName(); 1.511 + } else { 1.512 + methodName = "UNKNOWN METHOD"; 1.513 + } 1.514 + 1.515 + return methodName; 1.516 + } 1.517 + 1.518 +}