001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.policy.privateutil;
038:
039: import java.lang.reflect.Field;
040: import java.util.logging.Level;
041:
042: import static com.sun.xml.ws.policy.privateutil.PolicyUtils.Commons.getCallerMethodName;
043:
044: /**
045: * This is a helper class that provides some conveniece methods wrapped around the
046: * standard {@link java.util.logging.Logger} interface.
047: *
048: * @author Marek Potociar
049: */
050: public final class PolicyLogger {
051: /**
052: * If we run with JAX-WS, we are using its logging domain (appended with ".wspolicy").
053: * Otherwise we default to "wspolicy".
054: */
055: private static final String LOGGING_SUBSYSTEM_NAME;
056: private static final Level METHOD_CALL_LEVEL_VALUE = Level.FINEST;
057:
058: static {
059: String loggingSubsystemName = "wspolicy";
060: try {
061: // Looking up JAX-WS class at run-time, so that we don't need to depend
062: // on it at compile-time.
063: Class jaxwsConstants = Class
064: .forName("com.sun.xml.ws.util.Constants");
065: Field loggingDomainField = jaxwsConstants
066: .getField("LoggingDomain");
067: Object loggingDomain = loggingDomainField.get(null);
068: loggingSubsystemName = loggingDomain.toString().concat(
069: ".wspolicy");
070: } catch (Exception e) {
071: // if we catch an exception, we fall back to default name
072: loggingSubsystemName = "wspolicy";
073: } finally {
074: LOGGING_SUBSYSTEM_NAME = loggingSubsystemName;
075: }
076: }
077:
078: private final String componentClassName;
079: private final java.util.logging.Logger logger;
080:
081: /**
082: * Prevents creation of a new instance of this PolicyLogger
083: */
084: private PolicyLogger(final String componentName) {
085: this .componentClassName = "[" + componentName + "] ";
086: this .logger = java.util.logging.Logger
087: .getLogger(LOGGING_SUBSYSTEM_NAME);
088: }
089:
090: /**
091: * The factory method returns preconfigured PolicyLogger wrapper for the class. Since there is no caching implemented,
092: * it is advised that the method is called only once per a class in order to initialize a final static logger variable,
093: * which is then used through the class to perform actual logging tasks.
094: *
095: * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
096: * @return logger instance preconfigured for use with the component
097: * @throws NullPointerException if the componentClass parameter is {@code null}.
098: */
099: public static PolicyLogger getLogger(final Class componentClass) {
100: return new PolicyLogger(componentClass.getName());
101: }
102:
103: public void log(final Level level, final String message) {
104: if (!this .logger.isLoggable(level)) {
105: return;
106: }
107: logger.logp(level, componentClassName, getCallerMethodName(),
108: message);
109: }
110:
111: public void log(final Level level, final String message,
112: final Throwable thrown) {
113: if (!this .logger.isLoggable(level)) {
114: return;
115: }
116: logger.logp(level, componentClassName, getCallerMethodName(),
117: message, thrown);
118: }
119:
120: public void finest(final String message) {
121: if (!this .logger.isLoggable(Level.FINEST)) {
122: return;
123: }
124: logger.logp(Level.FINEST, componentClassName,
125: getCallerMethodName(), message);
126: }
127:
128: public void finest(final String message, final Throwable thrown) {
129: if (!this .logger.isLoggable(Level.FINEST)) {
130: return;
131: }
132: logger.logp(Level.FINEST, componentClassName,
133: getCallerMethodName(), message, thrown);
134: }
135:
136: public void finer(final String message) {
137: if (!this .logger.isLoggable(Level.FINER)) {
138: return;
139: }
140: logger.logp(Level.FINER, componentClassName,
141: getCallerMethodName(), message);
142: }
143:
144: public void finer(final String message, final Throwable thrown) {
145: if (!this .logger.isLoggable(Level.FINER)) {
146: return;
147: }
148: logger.logp(Level.FINER, componentClassName,
149: getCallerMethodName(), message, thrown);
150: }
151:
152: public void fine(final String message) {
153: if (!this .logger.isLoggable(Level.FINE)) {
154: return;
155: }
156: logger.logp(Level.FINE, componentClassName,
157: getCallerMethodName(), message);
158: }
159:
160: public void fine(final String message, final Throwable thrown) {
161: if (!this .logger.isLoggable(Level.FINE)) {
162: return;
163: }
164: logger.logp(Level.FINE, componentClassName,
165: getCallerMethodName(), message, thrown);
166: }
167:
168: public void info(final String message) {
169: if (!this .logger.isLoggable(Level.INFO)) {
170: return;
171: }
172: logger.logp(Level.INFO, componentClassName,
173: getCallerMethodName(), message);
174: }
175:
176: public void info(final String message, final Throwable thrown) {
177: if (!this .logger.isLoggable(Level.INFO)) {
178: return;
179: }
180: logger.logp(Level.INFO, componentClassName,
181: getCallerMethodName(), message, thrown);
182: }
183:
184: public void config(final String message) {
185: if (!this .logger.isLoggable(Level.CONFIG)) {
186: return;
187: }
188: logger.logp(Level.CONFIG, componentClassName,
189: getCallerMethodName(), message);
190: }
191:
192: public void config(final String message, final Throwable thrown) {
193: if (!this .logger.isLoggable(Level.CONFIG)) {
194: return;
195: }
196: logger.logp(Level.CONFIG, componentClassName,
197: getCallerMethodName(), message, thrown);
198: }
199:
200: public void warning(final String message) {
201: if (!this .logger.isLoggable(Level.WARNING)) {
202: return;
203: }
204: logger.logp(Level.WARNING, componentClassName,
205: getCallerMethodName(), message);
206: }
207:
208: public void warning(final String message, final Throwable thrown) {
209: if (!this .logger.isLoggable(Level.WARNING)) {
210: return;
211: }
212: logger.logp(Level.WARNING, componentClassName,
213: getCallerMethodName(), message, thrown);
214: }
215:
216: public void severe(final String message) {
217: if (!this .logger.isLoggable(Level.SEVERE)) {
218: return;
219: }
220: logger.logp(Level.SEVERE, componentClassName,
221: getCallerMethodName(), message);
222: }
223:
224: public void severe(final String message, final Throwable thrown) {
225: if (!this .logger.isLoggable(Level.SEVERE)) {
226: return;
227: }
228: logger.logp(Level.SEVERE, componentClassName,
229: getCallerMethodName(), message, thrown);
230: }
231:
232: public boolean isMethodCallLoggable() {
233: return this .logger.isLoggable(METHOD_CALL_LEVEL_VALUE);
234: }
235:
236: public boolean isLoggable(final Level level) {
237: return this .logger.isLoggable(level);
238: }
239:
240: public void setLevel(final Level level) {
241: this .logger.setLevel(level);
242: }
243:
244: public void entering() {
245: if (!this .logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
246: return;
247: }
248:
249: logger.entering(componentClassName, getCallerMethodName());
250: }
251:
252: public void entering(final Object... parameters) {
253: if (!this .logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
254: return;
255: }
256:
257: logger.entering(componentClassName, getCallerMethodName(),
258: parameters);
259: }
260:
261: public void exiting() {
262: if (!this .logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
263: return;
264: }
265: logger.exiting(componentClassName, getCallerMethodName());
266: }
267:
268: public void exiting(final Object result) {
269: if (!this .logger.isLoggable(METHOD_CALL_LEVEL_VALUE)) {
270: return;
271: }
272: logger.exiting(componentClassName, getCallerMethodName(),
273: result);
274: }
275:
276: /**
277: * Method logs {@code exception}'s message as a {@code SEVERE} logging level
278: * message.
279: * <p/>
280: * If {@code cause} parameter is not {@code null}, it is logged as well and
281: * {@code exception} original cause is initialized with instance referenced
282: * by {@code cause} parameter.
283: *
284: * @param exception exception whose message should be logged. Must not be
285: * {@code null}.
286: * @param cause initial cause of the exception that should be logged as well
287: * and set as {@code exception}'s original cause. May be {@code null}.
288: * @return the same exception instance that was passed in as the {@code exception}
289: * parameter.
290: */
291: public <T extends Throwable> T logSevereException(
292: final T exception, final Throwable cause) {
293: if (this .logger.isLoggable(Level.SEVERE)) {
294: if (cause == null) {
295: logger.logp(Level.SEVERE, componentClassName,
296: getCallerMethodName(), exception.getMessage());
297: } else {
298: exception.initCause(cause);
299: logger.logp(Level.SEVERE, componentClassName,
300: getCallerMethodName(), exception.getMessage(),
301: cause);
302: }
303: }
304:
305: return exception;
306: }
307:
308: /**
309: * Method logs {@code exception}'s message as a {@code SEVERE} logging level
310: * message.
311: * <p/>
312: * If {@code logCause} parameter is {@code true}, {@code exception}'s original
313: * cause is logged as well (if exists). This may be used in cases when
314: * {@code exception}'s class provides constructor to initialize the original
315: * cause. In such case you do not need to use
316: * {@link #logSevereException(Throwable, Throwable)}
317: * method version but you might still want to log the original cause as well.
318: *
319: * @param exception exception whose message should be logged. Must not be
320: * {@code null}.
321: * @param logCause deterimnes whether initial cause of the exception should
322: * be logged as well
323: * @return the same exception instance that was passed in as the {@code exception}
324: * parameter.
325: */
326: public <T extends Throwable> T logSevereException(
327: final T exception, final boolean logCause) {
328: if (this .logger.isLoggable(Level.SEVERE)) {
329: if (logCause && exception.getCause() != null) {
330: logger.logp(Level.SEVERE, componentClassName,
331: getCallerMethodName(), exception.getMessage(),
332: exception.getCause());
333: } else {
334: logger.logp(Level.SEVERE, componentClassName,
335: getCallerMethodName(), exception.getMessage());
336: }
337: }
338:
339: return exception;
340: }
341:
342: /**
343: * Same as {@link #logSevereException(Throwable, boolean) logSevereException(exception, true)}.
344: */
345: public <T extends Throwable> T logSevereException(final T exception) {
346: if (this .logger.isLoggable(Level.SEVERE)) {
347: if (exception.getCause() == null) {
348: logger.logp(Level.SEVERE, componentClassName,
349: getCallerMethodName(), exception.getMessage());
350: } else {
351: logger.logp(Level.SEVERE, componentClassName,
352: getCallerMethodName(), exception.getMessage(),
353: exception.getCause());
354: }
355: }
356:
357: return exception;
358: }
359:
360: /**
361: * Method logs {@code exception}'s message at the logging level specified by the
362: * {@code level} argument.
363: * <p/>
364: * If {@code cause} parameter is not {@code null}, it is logged as well and
365: * {@code exception} original cause is initialized with instance referenced
366: * by {@code cause} parameter.
367: *
368: * @param exception exception whose message should be logged. Must not be
369: * {@code null}.
370: * @param cause initial cause of the exception that should be logged as well
371: * and set as {@code exception}'s original cause. May be {@code null}.
372: * @param level loging level which should be used for logging
373: * @return the same exception instance that was passed in as the {@code exception}
374: * parameter.
375: */
376: public <T extends Throwable> T logException(final T exception,
377: final Throwable cause, final Level level) {
378: if (this .logger.isLoggable(level)) {
379: if (cause == null) {
380: logger.logp(level, componentClassName,
381: getCallerMethodName(), exception.getMessage());
382: } else {
383: exception.initCause(cause);
384: logger.logp(level, componentClassName,
385: getCallerMethodName(), exception.getMessage(),
386: cause);
387: }
388: }
389:
390: return exception;
391: }
392:
393: /**
394: * Method logs {@code exception}'s message at the logging level specified by the
395: * {@code level} argument.
396: * <p/>
397: * If {@code logCause} parameter is {@code true}, {@code exception}'s original
398: * cause is logged as well (if exists). This may be used in cases when
399: * {@code exception}'s class provides constructor to initialize the original
400: * cause. In such case you do not need to use
401: * {@link #logException(Throwable, Throwable, Level) logException(exception, cause, level)}
402: * method version but you might still want to log the original cause as well.
403: *
404: * @param exception exception whose message should be logged. Must not be
405: * {@code null}.
406: * @param logCause deterimnes whether initial cause of the exception should
407: * be logged as well
408: * @param level loging level which should be used for logging
409: * @return the same exception instance that was passed in as the {@code exception}
410: * parameter.
411: */
412: public <T extends Throwable> T logException(final T exception,
413: final boolean logCause, final Level level) {
414: if (this .logger.isLoggable(level)) {
415: if (logCause && exception.getCause() != null) {
416: logger.logp(level, componentClassName,
417: getCallerMethodName(), exception.getMessage(),
418: exception.getCause());
419: } else {
420: logger.logp(level, componentClassName,
421: getCallerMethodName(), exception.getMessage());
422: }
423: }
424:
425: return exception;
426: }
427:
428: /**
429: * Same as {@link #logException(Throwable, Throwable, Level)
430: * logException(exception, true, level)}.
431: */
432: public <T extends Throwable> T logException(final T exception,
433: final Level level) {
434: if (this.logger.isLoggable(level)) {
435: if (exception.getCause() == null) {
436: logger.logp(level, componentClassName,
437: getCallerMethodName(), exception.getMessage());
438: } else {
439: logger.logp(level, componentClassName,
440: getCallerMethodName(), exception.getMessage(),
441: exception.getCause());
442: }
443: }
444:
445: return exception;
446: }
447: }
|