Package org.jvnet.glassfish.comms.util

Source Code of org.jvnet.glassfish.comms.util.LogUtil

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License").  You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code.  If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license."  If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above.  However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.glassfish.comms.util;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.HashMap;
import java.util.Iterator;

import com.sun.enterprise.server.ServerContext;
import com.sun.enterprise.server.ApplicationServer;
import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.ElementProperty;

/** This class sets up a logger with a given LogDomain, or if no LogDomain is given the
* value 'javax.enterprise.system.container.sip' is used instead.
*
*/
public final class LogUtil {
   
    // General SIP log domain
    public static final String SIP_LOG_DOMAIN = "javax.enterprise.system.container.sip";
   
    // Log domain for clustered load balancer
    public static final String CLB_LOG_DOMAIN = "javax.enterprise.system.container.clb";
  
    // Log domain for application routers
    public static final String AR_LOG_DOMAIN = "javax.enterprise.system.container.ar";

    // Log domain for replication
    public static final String SSR_LOG_DOMAIN = "javax.enterprise.system.container.ssr";
   
    // Log domain for SIP message inspection
    public static final String SMI_LOG_DOMAIN = "javax.enterprise.system.container.sip.smi";

    private static final String LOG_STRING_PACKAGE = "org.jvnet.glassfish.comms.logger";

    // Log diameter message inspection
    public static final String DIAMETER_LOG_DOMAIN = "javax.enterprise.system.diameter";

    // Mapper to map loggerName and corresponding property name in domain.xml
    private static final HashMap logProps = new HashMap();
    static {
        logProps.put(SIP_LOG_DOMAIN, "sip");
        logProps.put(CLB_LOG_DOMAIN, "clb");
        logProps.put(AR_LOG_DOMAIN, "ar");
        logProps.put(SSR_LOG_DOMAIN, "ssr");
        logProps.put(SMI_LOG_DOMAIN, "smi");
        logProps.put(DIAMETER_LOG_DOMAIN, "diameter");
    }

    // Shortcut singletons to the most used domains.
    public static final LogUtil SIP_LOGGER = new LogUtil(SIP_LOG_DOMAIN);
    public static final LogUtil CLB_LOGGER = new LogUtil(CLB_LOG_DOMAIN);
    public static final LogUtil AR_LOGGER = new LogUtil(AR_LOG_DOMAIN);
    public static final LogUtil SSR_LOGGER = new LogUtil(SSR_LOG_DOMAIN);
    public static final LogUtil SMI_LOGGER = new LogUtil(SMI_LOG_DOMAIN);
    public static final LogUtil DIAMETER_LOGGER = new LogUtil(DIAMETER_LOG_DOMAIN);

    private final Logger log;

    /** This constructor is added just for backwards compatibility. Use the LogUtil()
     * constructor instead; the classname is no longer used for anything inside the
     * LogUtil class.
     */
    @Deprecated
    public LogUtil(Class c) {
        this(SIP_LOG_DOMAIN);
    }

    public LogUtil() {
        this(SIP_LOG_DOMAIN);
    }

    public LogUtil(String domain) {
        String module = domain.substring(domain.lastIndexOf('.'));
        String logStrings = LOG_STRING_PACKAGE + module + ".LogStrings";
        log = Logger.getLogger(domain, logStrings);
        Level level = Level.parse(getLogLevel(domain));
        log.setLevel(level);
    }

    public Logger getLogger() {
        return log;
    }
   
    // Get the log level from domain.xml
    public String getLogLevel(String loggerName) {
        try {
            ServerContext sc = ApplicationServer.getServerContext();
            Config cfg = ServerBeansFactory.getConfigBean(sc.getConfigContext());
            ElementProperty[] elementProperties =
                cfg.getLogService().getModuleLogLevels().getElementProperty();
            for( int i = 0; i < elementProperties.length; i++ ) {
                String level = elementProperties[i].getValue();
                String prop = getPropertyName(loggerName);
                if (prop.equals(elementProperties[i].getName()))
                    return level;
            }
        } catch(Exception e) {
            // error while accessing config bean
        }
        return "INFO";
    }

    // Given the logger name, get the domain.xml property name
    public static String getPropertyName(String loggerName) {
        String property = (String)logProps.get(loggerName);
        return (property!=null)?property:loggerName;
    }
   
    // Given the domain.xml property name, get the logger name
    public static String getLoggerName(String property) {
         if (logProps.containsValue(property)) {
            Iterator it = logProps.keySet().iterator();
            while (it.hasNext()) {
                String loggerName = (String)it.next();
                if (logProps.get(loggerName).equals(property))
                    return loggerName;
            }
        }
        return property;
    }
}
TOP

Related Classes of org.jvnet.glassfish.comms.util.LogUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.