aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.policy.privateutil; aoqi@0: aoqi@0: import com.sun.istack.internal.logging.Logger; aoqi@0: import java.lang.reflect.Field; aoqi@0: aoqi@0: /** aoqi@0: * This is a helper class that provides some conveniece methods wrapped around the aoqi@0: * standard {@link java.util.logging.Logger} interface. aoqi@0: * aoqi@0: * @author Marek Potociar aoqi@0: * @author Fabian Ritzmann aoqi@0: */ aoqi@0: public final class PolicyLogger extends Logger { aoqi@0: aoqi@0: /** aoqi@0: * If we run with JAX-WS, we are using its logging domain (appended with ".wspolicy"). aoqi@0: * Otherwise we default to "wspolicy". aoqi@0: */ aoqi@0: private static final String POLICY_PACKAGE_ROOT = "com.sun.xml.internal.ws.policy"; aoqi@0: aoqi@0: /** aoqi@0: * Make sure this class cannot be instantiated by client code. aoqi@0: * aoqi@0: * @param policyLoggerName The name of the subsystem to be logged. aoqi@0: * @param className The fully qualified class name. aoqi@0: */ aoqi@0: private PolicyLogger(final String policyLoggerName, final String className) { aoqi@0: super(policyLoggerName, className); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The factory method returns preconfigured PolicyLogger wrapper for the class. Since there is no caching implemented, aoqi@0: * it is advised that the method is called only once per a class in order to initialize a final static logger variable, aoqi@0: * which is then used through the class to perform actual logging tasks. aoqi@0: * aoqi@0: * @param componentClass class of the component that will use the logger instance. Must not be {@code null}. aoqi@0: * @return logger instance preconfigured for use with the component aoqi@0: * @throws NullPointerException if the componentClass parameter is {@code null}. aoqi@0: */ aoqi@0: public static PolicyLogger getLogger(final Class componentClass) { aoqi@0: final String componentClassName = componentClass.getName(); aoqi@0: aoqi@0: if (componentClassName.startsWith(POLICY_PACKAGE_ROOT)) { aoqi@0: return new PolicyLogger(getLoggingSubsystemName() + componentClassName.substring(POLICY_PACKAGE_ROOT.length()), aoqi@0: componentClassName); aoqi@0: } else { aoqi@0: return new PolicyLogger(getLoggingSubsystemName() + "." + componentClassName, componentClassName); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static String getLoggingSubsystemName() { aoqi@0: String loggingSubsystemName = "wspolicy"; aoqi@0: try { aoqi@0: // Looking up JAX-WS class at run-time, so that we don't need to depend aoqi@0: // on it at compile-time. aoqi@0: Class jaxwsConstants = Class.forName("com.sun.xml.internal.ws.util.Constants"); aoqi@0: Field loggingDomainField = jaxwsConstants.getField("LoggingDomain"); aoqi@0: Object loggingDomain = loggingDomainField.get(null); aoqi@0: loggingSubsystemName = loggingDomain.toString().concat(".wspolicy"); aoqi@0: } catch (RuntimeException e) { aoqi@0: // if we catch an exception, we stick with the default name aoqi@0: // this catch is redundant but works around a Findbugs warning aoqi@0: } catch (Exception e) { aoqi@0: // if we catch an exception, we stick with the default name aoqi@0: } aoqi@0: return loggingSubsystemName; aoqi@0: } aoqi@0: aoqi@0: }