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.tx.common;
038:
039: import java.lang.reflect.Field;
040: import java.util.logging.Level;
041:
042: /**
043: * This is a helper class that provides some convenience methods wrapped around the
044: * standard {@link java.util.logging.Logger} interface.
045: * <p/>
046: * Logging domains:
047: * wstx // general for all web service transactions
048: * wstx.wscoord // for ws-coordination logging
049: * wstx.at // for ws-atomic transaction logging.
050: *
051: * @see #getLogger(Class)
052: * @see #getATLogger(Class)
053: * @see #getCoordLogger(Class)
054: */
055: public final class TxLogger {
056: /**
057: * If we run with JAX-WS, we are using its logging domain (appended with ".wstx").
058: * Otherwise we default to "wstx".
059: */
060: private final static String LOGGING_SUBSYSTEM_NAME;
061:
062: static {
063: String loggingSubsystemName = null;
064: try {
065: // Looking up JAX-WS class at run-time, so that we don't need to depend
066: // on it at compile-time.
067: Class jaxwsConstants = Class
068: .forName("com.sun.xml.ws.util.Constants");
069: Field loggingDomainField = jaxwsConstants
070: .getField("LoggingDomain");
071: Object loggingDomain = loggingDomainField.get(null);
072: loggingSubsystemName = loggingDomain.toString().concat(
073: ".wstx");
074: } catch (Exception e) {
075: // If we don't manage to extract the logging domain from JAX-WS, we
076: // fall back to a default.
077: loggingSubsystemName = "wstx";
078: } finally {
079: LOGGING_SUBSYSTEM_NAME = loggingSubsystemName;
080: }
081: }
082:
083: private final String componentClassName;
084: private final java.util.logging.Logger logger;
085:
086: /**
087: * Prevents creation of a new instance of this TxLogger
088: */
089: private TxLogger(String componentName) {
090: this .componentClassName = "[" + componentName + "] ";
091: this .logger = java.util.logging.Logger
092: .getLogger(LOGGING_SUBSYSTEM_NAME);
093: }
094:
095: private TxLogger(String componentName, String subsystem) {
096: this .componentClassName = "[" + componentName + "] ";
097:
098: this .logger = java.util.logging.Logger
099: .getLogger(LOGGING_SUBSYSTEM_NAME + subsystem);
100: }
101:
102: /**
103: * The factory method returns preconfigured TxLogger wrapper for the class. Since there is no caching implemented,
104: * it is advised that the method is called only once per a class in order to initialize a final static logger variable,
105: * which is then used through the class to perform actual logging tasks.
106: *
107: * @param componentClass class of the component that will use the logger instance. Must not be {@code null}.
108: * @return logger instance preconfigured for use with the component
109: * @throws NullPointerException if the componentClass parameter is {@code null}.
110: */
111: public static TxLogger getLogger(final Class componentClass) {
112: return new TxLogger(componentClass.getName());
113: }
114:
115: /**
116: * Logging specifically for *.wstx.wsat subsystem.
117: */
118: public static TxLogger getATLogger(final Class componentClass) {
119: return new TxLogger(componentClass.getName(), ".wsat");
120: }
121:
122: /**
123: * Logging specifically for *.wstx.wscoord subsystem.
124: */
125: public static TxLogger getCoordLogger(final Class componentClass) {
126: return new TxLogger(componentClass.getName(), ".wscoord");
127: }
128:
129: public void log(final Level level, final String methodName,
130: final String message) {
131: logger.logp(level, componentClassName, methodName, message);
132: }
133:
134: public void log(final Level level, final String methodName,
135: final String message, final Throwable thrown) {
136: logger.logp(level, componentClassName, methodName, message,
137: thrown);
138: }
139:
140: public void finest(final String methodName, final String message) {
141: logger.logp(Level.FINEST, componentClassName, methodName,
142: message);
143: }
144:
145: public void finest(final String methodName, final String message,
146: final Throwable thrown) {
147: logger.logp(Level.FINEST, componentClassName, methodName,
148: message, thrown);
149: }
150:
151: public void finer(final String methodName, final String message) {
152: logger.logp(Level.FINER, componentClassName, methodName,
153: message);
154: }
155:
156: public void finer(final String methodName, final String message,
157: final Throwable thrown) {
158: logger.logp(Level.FINER, componentClassName, methodName,
159: message, thrown);
160: }
161:
162: public void fine(final String methodName, final String message) {
163: logger
164: .logp(Level.FINE, componentClassName, methodName,
165: message);
166: }
167:
168: public void fine(final String methodName, final String message,
169: final Throwable thrown) {
170: logger.logp(Level.FINE, componentClassName, methodName,
171: message, thrown);
172: }
173:
174: public void info(final String methodName, final String message) {
175: logger
176: .logp(Level.INFO, componentClassName, methodName,
177: message);
178: }
179:
180: public void info(final String methodName, final String message,
181: final Throwable thrown) {
182: logger.logp(Level.INFO, componentClassName, methodName,
183: message, thrown);
184: }
185:
186: public void config(final String methodName, final String message) {
187: logger.logp(Level.CONFIG, componentClassName, methodName,
188: message);
189: }
190:
191: public void config(final String methodName, final String message,
192: final Throwable thrown) {
193: logger.logp(Level.CONFIG, componentClassName, methodName,
194: message, thrown);
195: }
196:
197: public void warning(final String methodName, final String message) {
198: logger.logp(Level.WARNING, componentClassName, methodName,
199: message);
200: }
201:
202: public void warning(final String methodName, final String message,
203: final Throwable thrown) {
204: logger.logp(Level.WARNING, componentClassName, methodName,
205: message, thrown);
206: }
207:
208: public void severe(final String methodName, final String message) {
209: logger.logp(Level.SEVERE, componentClassName, methodName,
210: message);
211: }
212:
213: public void severe(final String methodName, final String message,
214: final Throwable thrown) {
215: logger.logp(Level.SEVERE, componentClassName, methodName,
216: message, thrown);
217: }
218:
219: public void entering(final String methodName) {
220: logger.entering(componentClassName, methodName);
221: }
222:
223: public void entering(final String methodName, final Object parameter) {
224: logger.entering(componentClassName, methodName, parameter);
225: }
226:
227: public void entering(final String methodName,
228: final Object[] parameters) {
229: logger.entering(componentClassName, methodName, parameters);
230: }
231:
232: public void exiting(final String methodName) {
233: logger.exiting(componentClassName, methodName);
234: }
235:
236: public void exiting(final String methodName, final Object result) {
237: logger.exiting(componentClassName, methodName, result);
238: }
239:
240: public boolean isLogging(final Level level) {
241: return logger.isLoggable(level);
242: }
243: }
|