1/* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.xml.internal.ws.policy.privateutil; 27 28import com.sun.istack.internal.logging.Logger; 29import java.lang.reflect.Field; 30 31/** 32 * This is a helper class that provides some conveniece methods wrapped around the 33 * standard {@link java.util.logging.Logger} interface. 34 * 35 * @author Marek Potociar 36 * @author Fabian Ritzmann 37 */ 38public final class PolicyLogger extends Logger { 39 40 /** 41 * If we run with JAX-WS, we are using its logging domain (appended with ".wspolicy"). 42 * Otherwise we default to "wspolicy". 43 */ 44 private static final String POLICY_PACKAGE_ROOT = "com.sun.xml.internal.ws.policy"; 45 46 /** 47 * Make sure this class cannot be instantiated by client code. 48 * 49 * @param policyLoggerName The name of the subsystem to be logged. 50 * @param className The fully qualified class name. 51 */ 52 private PolicyLogger(final String policyLoggerName, final String className) { 53 super(policyLoggerName, className); 54 } 55 56 /** 57 * The factory method returns preconfigured PolicyLogger wrapper for the class. Since there is no caching implemented, 58 * it is advised that the method is called only once per a class in order to initialize a final static logger variable, 59 * which is then used through the class to perform actual logging tasks. 60 * 61 * @param componentClass class of the component that will use the logger instance. Must not be {@code null}. 62 * @return logger instance preconfigured for use with the component 63 * @throws NullPointerException if the componentClass parameter is {@code null}. 64 */ 65 public static PolicyLogger getLogger(final Class<?> componentClass) { 66 final String componentClassName = componentClass.getName(); 67 68 if (componentClassName.startsWith(POLICY_PACKAGE_ROOT)) { 69 return new PolicyLogger(getLoggingSubsystemName() + componentClassName.substring(POLICY_PACKAGE_ROOT.length()), 70 componentClassName); 71 } else { 72 return new PolicyLogger(getLoggingSubsystemName() + "." + componentClassName, componentClassName); 73 } 74 } 75 76 private static String getLoggingSubsystemName() { 77 String loggingSubsystemName = "wspolicy"; 78 try { 79 // Looking up JAX-WS class at run-time, so that we don't need to depend 80 // on it at compile-time. 81 Class jaxwsConstants = Class.forName("com.sun.xml.internal.ws.util.Constants"); 82 Field loggingDomainField = jaxwsConstants.getField("LoggingDomain"); 83 Object loggingDomain = loggingDomainField.get(null); 84 loggingSubsystemName = loggingDomain.toString().concat(".wspolicy"); 85 } catch (RuntimeException e) { 86 // if we catch an exception, we stick with the default name 87 // this catch is redundant but works around a Findbugs warning 88 } catch (Exception e) { 89 // if we catch an exception, we stick with the default name 90 } 91 return loggingSubsystemName; 92 } 93 94} 95