a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: LoggerFactory.java,v 1.2 2009/10/22 21:04:36 veiming Exp $
402cd5da45d9182b81c16a13c3568faf78701827Andrew Forrest *
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington * Portions Copyrighted 2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.entitlement.log;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
402cd5da45d9182b81c16a13c3568faf78701827Andrew Forrestimport org.forgerock.openam.entitlement.PolicyConstants;
402cd5da45d9182b81c16a13c3568faf78701827Andrew Forrest
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.logging.LogManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.logging.Logger;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This factory returns the logger object (java.util.logging.Logger or its
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * super class). This factory instantiates a class that is set in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "com.sun.identity.log.loggerprovider" system property. And this class
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is responsible for returning the Logger object. com.sun.identity.log.Logger
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * will be returned if this system property is not set. Otherwise,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * java.util.logging.LogManager.getLogManager.getLogger will be returned if
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * com.sun.identity.log.Logger is absent.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class LoggerFactory {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static LoggerFactory instance = new LoggerFactory();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ILoggerProvider provider;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private LoggerFactory() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prop = System.getProperty(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ILoggerProvider.SYSTEM_PROPERTY_LOG_PROVIDER);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((prop != null) && (prop.trim().length() > 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster provider = getLoggerProvider(prop);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (provider == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster provider = getLoggerProvider(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "com.sun.identity.log.LoggerProvider");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ILoggerProvider getLoggerProvider(String className) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster className = className.trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Class clazz = Class.forName(className);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (ILoggerProvider) clazz.newInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (InstantiationException ex) {
402cd5da45d9182b81c16a13c3568faf78701827Andrew Forrest PolicyConstants.DEBUG.error("LoggerFactory.<init>", ex);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IllegalAccessException ex) {
402cd5da45d9182b81c16a13c3568faf78701827Andrew Forrest PolicyConstants.DEBUG.error("LoggerFactory.<init>", ex);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (ClassNotFoundException ex) {
402cd5da45d9182b81c16a13c3568faf78701827Andrew Forrest PolicyConstants.DEBUG.error("LoggerFactory.<init>", ex);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns logger object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name Name of logger.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return logger object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Logger getLoggEr(String name) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (provider != null) ? provider.getLogger(name)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster : LogManager.getLogManager().getLogger(name);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns logger object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name Name of logger.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return logger object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Logger getLogger(String name) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return instance.getLoggEr(name);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}