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: /*
038: * LoggingHelper.java
039: *
040: *
041: * @author Mike Grogan
042: * Created on January 8, 2007, 1:50 PM
043: *
044: */
045:
046: package com.sun.xml.ws.rm.jaxws.util;
047:
048: import java.util.logging.Logger;
049: import java.util.logging.Level;
050: import java.lang.reflect.Constructor;
051:
052: /**
053: * Helper methods for logging.
054: */
055: public class LoggingHelper {
056:
057: private final Logger logger;
058:
059: public static final String logRoot = "javax.enterprise.xml.webservices";
060:
061: public static final String packageRoot = "com\\.sun\\.xml\\.ws";
062:
063: public LoggingHelper(Logger logger) {
064: this .logger = logger;
065: }
066:
067: /**
068: * Throws an Exception of a given type with a given message. A log
069: * entry logged at the specified level with the same message is also
070: * written
071: *
072: * @param exceptionClass The type of exception to throw
073: * @param level The logging level to use
074: * @param message The message in the Exception and log entry
075: * @param stackTrace If true, the stack trace for the exception is logged.. If false
076: * the stackTrace is not logged.
077: * @throws An instance of the exceptionClass argument
078: */
079: @SuppressWarnings("unchecked")
080: public <T extends Throwable> void throwAndLog(
081: Class<T> exceptionClass, Level level, String message,
082: boolean stackTrace) throws T {
083:
084: //look for exceptionClass ctors of the form Exception() and Exception(String)
085: Constructor oneArg = null;
086: Constructor zeroArg = null;
087: Constructor[] ctors = exceptionClass.getConstructors();
088:
089: for (Constructor ctor : ctors) {
090: Class[] params = ctor.getParameterTypes();
091: if (params.length == 0) {
092: zeroArg = ctor;
093: } else if (params.length == 1
094: && params[0].equals(String.class)) {
095: oneArg = ctor;
096: }
097: }
098:
099: //Construct the exception
100: T exception = null;
101:
102: try {
103: if (oneArg != null) {
104: //use the 1-arg ctor if possible
105: exception = (T) oneArg.newInstance(message);
106: } else if (zeroArg == null) {
107: exception = (T) zeroArg.newInstance();
108: }
109: } catch (Throwable e) {
110: }
111:
112: //write the log entry
113: if (logger.isLoggable(level)) {
114: if (stackTrace && exception != null) {
115: logger.log(level, message, exception);
116: } else {
117: logger.log(level, message);
118: }
119: }
120:
121: //throw the exception
122: if (exception != null) {
123: throw exception;
124: }
125:
126: }
127:
128: /**
129: * Same as throwAndLog(Class, Level, String, boolean) with last argument
130: * set to true.
131: */
132: public <T extends Throwable> void throwAndLog(
133: Class<T> exceptionClass, Level level, String message)
134: throws T {
135:
136: throwAndLog(exceptionClass, level, message, true);
137: }
138:
139: /**
140: * Forms a logger name by replacing "com.sun.xml.ws" in the name
141: * of the specified class by "javax.enterprise.xml.webservices"
142: *
143: * @param clasz The Class to use for forming the logger name
144: * @return The logger name
145: */
146: public static String getLoggerName(Class clasz) {
147: return clasz.getName().replaceFirst(packageRoot, logRoot);
148: }
149:
150: }
|