001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.logging;
028:
029: import java.util.Properties;
030:
031: import org.cougaar.core.component.ServiceBroker;
032: import org.cougaar.core.component.ServiceProvider;
033: import org.cougaar.core.service.LoggingService;
034: import org.cougaar.util.log.Logger;
035: import org.cougaar.util.log.Logging;
036: import org.cougaar.util.log.LoggerController;
037: import org.cougaar.util.log.LoggerControllerProxy;
038: import org.cougaar.util.log.LoggerFactory;
039: import org.cougaar.util.log.LoggerProxy;
040:
041: import java.util.Map;
042: import java.util.WeakHashMap;
043:
044: /**
045: * This service provider provides the {@link LoggingService}, which
046: * is backed by the {@link LoggerFactory}.
047: */
048: public class LoggingServiceProvider implements ServiceProvider {
049:
050: // maps of prefix -> key -> logger
051: // Map<String, Map<String, LoggerProxy>>
052: private static final Map prefixToLoggerCache = new WeakHashMap(11);
053:
054: // our prefix
055: private final String prefix;
056:
057: // our logger cache
058: // value of prefixToLoggerCache.get(prefix).
059: // Map<String, LoggerProxy>
060: private final Map loggerCache;
061:
062: public LoggingServiceProvider() {
063: this (null);
064: }
065:
066: public LoggingServiceProvider(String prefix) {
067: this .prefix = prefix;
068: loggerCache = getLoggerCache(prefix);
069: }
070:
071: private static final Map getLoggerCache(String prefix) {
072: synchronized (prefixToLoggerCache) {
073: Map ret = (Map) prefixToLoggerCache.get(prefix);
074: if (ret == null) {
075: ret = new WeakHashMap(11);
076: // new String(key) keeps the entry GCable
077: String weakKey = (prefix == null ? (null) : new String(
078: prefix)); // intentional "new String"
079: prefixToLoggerCache.put(weakKey, ret);
080: }
081: return ret;
082: }
083: }
084:
085: public Object getService(ServiceBroker sb, Object requestor,
086: Class serviceClass) {
087: if (LoggingService.class.isAssignableFrom(serviceClass)) {
088: return getLoggingService(requestor);
089: } else if (LoggingControlService.class
090: .isAssignableFrom(serviceClass)) {
091: LoggerController lc = Logging
092: .getLoggerController(requestor);
093: return new LoggingControlServiceImpl(lc);
094: } else {
095: return null;
096: }
097: }
098:
099: public void releaseService(ServiceBroker sb, Object requestor,
100: Class serviceClass, Object service) {
101: }
102:
103: LoggingService getLoggingService(Object requestor) {
104: String key = Logging.getKey(requestor);
105: LoggingService ls;
106: synchronized (loggerCache) {
107: ls = (LoggingService) loggerCache.get(key);
108: if (ls == null) {
109: Logger logger = Logging.getLogger(key);
110: if (prefix == null) {
111: ls = new LoggingServiceImpl(logger);
112: } else {
113: ls = new LoggingServiceWithPrefix(logger, prefix);
114: }
115: // new String(key) keeps the entry GCable
116: String weakKey = new String(key);
117: loggerCache.put(weakKey, ls); // intentional "new String"
118: }
119: }
120: return ls;
121: }
122:
123: private static class LoggingServiceImpl extends LoggerProxy
124: implements LoggingService {
125: public LoggingServiceImpl(Logger l) {
126: super (l);
127: }
128: }
129:
130: private static class LoggingControlServiceImpl extends
131: LoggerControllerProxy implements LoggingControlService {
132: public LoggingControlServiceImpl(LoggerController lc) {
133: super(lc);
134: }
135: }
136: }
|