001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.log;
031:
032: import com.caucho.loader.EnvironmentLocal;
033:
034: import java.lang.ref.SoftReference;
035: import java.util.Collections;
036: import java.util.Enumeration;
037: import java.util.HashMap;
038: import java.util.logging.LogManager;
039: import java.util.logging.Logger;
040:
041: /**
042: * Implementation of the log manager.
043: */
044: public class LogManagerImpl extends LogManager {
045: private static final String LOG_LOCAL = "caucho.log.manager";
046:
047: private static final HashMap<String, SoftReference<EnvironmentLogger>> _envLoggers = new HashMap<String, SoftReference<EnvironmentLogger>>();
048:
049: // custom loggers set by the user.
050: private static final EnvironmentLocal<HashMap<String, Logger>> _localLoggers = new EnvironmentLocal<HashMap<String, Logger>>();
051:
052: public LogManagerImpl() {
053: }
054:
055: /**
056: * Adds a logger.
057: */
058: public synchronized boolean addLogger(Logger logger) {
059: String name = logger.getName();
060:
061: EnvironmentLogger envLogger = null;
062:
063: SoftReference<EnvironmentLogger> loggerRef = _envLoggers
064: .get(name);
065: if (loggerRef != null)
066: envLogger = loggerRef.get();
067:
068: if (envLogger == null) {
069: envLogger = new EnvironmentLogger(name, logger
070: .getResourceBundleName());
071:
072: _envLoggers.put(name, new SoftReference<EnvironmentLogger>(
073: envLogger));
074:
075: EnvironmentLogger parent = buildParentTree(name);
076:
077: if (parent != null)
078: envLogger.setParent(parent);
079: }
080:
081: // handle custom logger
082: if (!logger.getClass().equals(Logger.class)) {
083: return envLogger.addLogger(logger);
084: }
085:
086: return false;
087: }
088:
089: /**
090: * Recursively builds the parents of the logger.
091: */
092: private EnvironmentLogger buildParentTree(String childName) {
093: if (childName.equals(""))
094: return null;
095:
096: int p = childName.lastIndexOf('.');
097:
098: String parentName;
099:
100: if (p > 0)
101: parentName = childName.substring(0, p);
102: else
103: parentName = "";
104:
105: EnvironmentLogger parent = null;
106:
107: SoftReference<EnvironmentLogger> parentRef = _envLoggers
108: .get(parentName);
109: if (parentRef != null)
110: parent = parentRef.get();
111:
112: if (parent != null)
113: return parent;
114: else {
115: parent = new EnvironmentLogger(parentName, null);
116: _envLoggers.put(parentName,
117: new SoftReference<EnvironmentLogger>(parent));
118:
119: EnvironmentLogger grandparent = buildParentTree(parentName);
120:
121: if (grandparent != null)
122: parent.setParent(grandparent);
123:
124: return parent;
125: }
126: }
127:
128: /**
129: * Returns the named logger.
130: */
131: public synchronized Logger getLogger(String name) {
132: SoftReference<EnvironmentLogger> envLoggerRef = _envLoggers
133: .get(name);
134:
135: EnvironmentLogger envLogger = null;
136: if (envLoggerRef != null)
137: envLogger = envLoggerRef.get();
138:
139: if (envLogger == null)
140: return null;
141:
142: Logger customLogger = envLogger.getLogger();
143:
144: if (customLogger != null)
145: return customLogger;
146: else
147: return envLogger;
148: }
149:
150: /**
151: * Returns an enumeration of loggers.
152: */
153: public Enumeration<String> getLoggerNames() {
154: return Collections.enumeration(_envLoggers.keySet());
155: }
156:
157: /**
158: * Returns an enumeration of loggers.
159: */
160: public void reset() {
161: }
162: }
|