001: /**
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.log.common;
013:
014: import java.util.Collections;
015: import java.util.HashSet;
016: import java.util.Set;
017: import java.util.logging.Handler;
018: import java.util.logging.Logger;
019:
020: /**
021: * Use this API to get the portal specific logger. It does additional house-keeping
022: * jobs on loggers so as to overcome container-specific settings.
023: * This is just a proxy to the Logger class.
024: */
025:
026: // Any changes to this class. Also check ABLogger, SSOAdapterLogger
027: public class PortalLogger {
028:
029: static String DEBUG_NAME = "debug";
030:
031: // Maintains a list of loggers
032: static Set loggerSet = Collections.synchronizedSet(new HashSet());
033:
034: static PortalLogManager manager = new PortalLogManager(); //forced classloading
035:
036: /**
037: * Add the root logger to the list of loggers and also to the LoggersList
038: *
039: * @param rootLoggerName
040: */
041: protected static void addRootLogger(String rootLoggerName) {
042: loggerSet.add(rootLoggerName);
043: LoggersList.add(rootLoggerName);
044: }
045:
046: /**
047: * Configure the logger.
048: * Set the logger to use the parent handlers.
049: * Remove any handlers added by the system.
050: * When a level is set on a particular logger. All the child loggers of that
051: * logger do not inherit the level of the parent logger. Bug in Webserver ???
052: * Hence its explicitly set to that of the default. i.e All the loggers are
053: * set to a default level. This is done only once as any change in the level
054: * has to be retained.
055: */
056: private static Logger config(Logger logger) {
057: String loggerName = logger.getName();
058:
059: if (!loggerSet.contains(loggerName)) {
060: if (!manager.useServerLogs(loggerName)) {
061: logger.setUseParentHandlers(true);
062: // Set the level to the default level
063: logger.setLevel(manager.getLevel(loggerName));
064: // Remove the handlers added by the system
065: Handler[] handlers = logger.getHandlers();
066: for (int i = 0; i < handlers.length; i++) {
067: logger.removeHandler(handlers[i]);
068: }
069: }
070: loggerSet.add(loggerName);
071: }
072: LoggersList.add(loggerName);
073: return logger;
074: }
075:
076: /**
077: * Returns the Logger for the class object.
078: * Derives the package name from the class object and uses it
079: * to create the Logger.
080: *
081: * @param cls a class object
082: * @return the Logger
083: */
084: public static Logger getLogger(Class cls) {
085: return config(Logger.getLogger(getName(cls)));
086: }
087:
088: /**
089: * Returns the Logger for the specified name.
090: * This method is used by those classes that do not follow
091: * com.sun.portal namespace. This is used in place if the
092: * above method <code>getLogger(Class cls)</code>.
093: *
094: * @param name the package name
095: * @return the Logger
096: */
097: public static Logger getLogger(String name) {
098: return config(Logger.getLogger(getName(name)));
099: }
100:
101: /**
102: * Derives the package name from the class object and prepends it
103: * with "debug".
104: *
105: * @param cls a class object
106: * @return the package name prepended with 'debug'.
107: */
108: private static String getName(Class cls) {
109: Package pkg = cls.getPackage();
110: String packageName = (pkg == null) ? getDefaultPkgName(cls)
111: : pkg.getName();
112: String correctName = correct(packageName);
113: return correctName;
114: }
115:
116: private static String getDefaultPkgName(Class cls) {
117: String className = cls.getName();
118: String pkgName = DEBUG_NAME;
119: int index = -1;
120: if (className != null) {
121: index = className.lastIndexOf(".");
122: }
123: if (index != -1) {
124: pkgName = className.substring(0, index);
125: }
126: return pkgName;
127: }
128:
129: /**
130: * Prepends the package name with "debug".
131: *
132: * @param name the package name
133: * @return the package name prepended with 'debug'.
134: */
135: private static String getName(String name) {
136: String correctName = correct(name);
137: return correctName;
138: }
139:
140: /**
141: * Prepends the name with "debug" if its not present.
142: *
143: * @param name the package name
144: * @return the package name prepended with 'debug'.
145: */
146: private static String correct(String name) {
147: if (name.indexOf(DEBUG_NAME) != 0) {
148: StringBuffer csuffix = new StringBuffer();
149: csuffix.append(DEBUG_NAME);
150: csuffix.append(".");
151: csuffix.append(name);
152: return csuffix.toString();
153: } else {
154: return name;
155: }
156: }
157: }
|