001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.node;
028:
029: import org.cougaar.bootstrap.SystemProperties;
030: import org.cougaar.core.component.Component;
031: import org.cougaar.core.component.ServiceBroker;
032: import org.cougaar.core.component.ServiceProvider;
033: import org.cougaar.core.component.ServiceRevokedListener;
034: import org.cougaar.core.logging.LoggingControlService;
035: import org.cougaar.core.logging.LoggingServiceProvider;
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.service.AgentIdentificationService;
038: import org.cougaar.core.service.LoggingService;
039: import org.cougaar.util.GenericStateModelAdapter;
040:
041: /**
042: * This component advertises the {@link LoggingService}
043: * to all agents.
044: *
045: * @property org.cougaar.core.logging.addAgentPrefix
046: * Modify the agent-level LoggingService implementation to add an
047: * "agent: " prefix to all logging lines. Options are:
048: * "true" to enable in both agents and node-agents,
049: * "agent" to only enable on agents,
050: * "node" to only enable on the node-agent,
051: * "false" to disable.
052: * Defaults to true.
053: */
054: public final class LoggingServiceComponent extends
055: GenericStateModelAdapter implements Component {
056:
057: private static final String ADD_AGENT_PREFIX = SystemProperties
058: .getProperty("org.cougaar.core.logging.addAgentPrefix",
059: "true");
060:
061: private ServiceBroker sb;
062: private ServiceBroker rootsb;
063:
064: private ServiceProvider lsp;
065:
066: public void setServiceBroker(ServiceBroker sb) {
067: this .sb = sb;
068: }
069:
070: public void load() {
071: super .load();
072:
073: NodeControlService ncs = (NodeControlService) sb.getService(
074: this , NodeControlService.class, null);
075: if (ncs != null) {
076: rootsb = ncs.getRootServiceBroker();
077: sb.releaseService(this , NodeControlService.class, ncs);
078: }
079:
080: String prefix;
081: if (shouldPrefix()) {
082: MessageAddress localAgent = find_local_agent();
083: prefix = localAgent + ": ";
084: } else {
085: prefix = null;
086: }
087:
088: lsp = new LoggingServiceProvider(prefix);
089:
090: ServiceBroker the_sb = (rootsb == null ? sb : rootsb);
091: the_sb.addService(LoggingService.class, lsp);
092: the_sb.addService(LoggingControlService.class, lsp);
093: }
094:
095: private MessageAddress find_local_agent() {
096: AgentIdentificationService ais = (AgentIdentificationService) sb
097: .getService(this , AgentIdentificationService.class,
098: null);
099: if (ais == null) {
100: return null;
101: }
102: MessageAddress ret = ais.getMessageAddress();
103: sb.releaseService(this , AgentIdentificationService.class, ais);
104: return ret;
105: }
106:
107: public void unload() {
108: super .unload();
109:
110: ServiceBroker the_sb = (rootsb == null ? sb : rootsb);
111: the_sb.revokeService(LoggingControlService.class, lsp);
112: the_sb.revokeService(LoggingService.class, lsp);
113: lsp = null;
114: }
115:
116: private boolean shouldPrefix() {
117: boolean isNode = (rootsb != null);
118: return ADD_AGENT_PREFIX.equals("true")
119: || ADD_AGENT_PREFIX.equals("both")
120: || (isNode ? ADD_AGENT_PREFIX.equals("node")
121: : ADD_AGENT_PREFIX.equals("agents"));
122: }
123: }
|