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.mts.std;
028:
029: import org.cougaar.core.component.ParameterizedComponent;
030: import org.cougaar.core.component.ServiceBroker;
031: import org.cougaar.core.component.ServiceProvider;
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.core.mts.MessageQueueDumpService;
034: import org.cougaar.core.node.NodeControlService;
035: import org.cougaar.core.node.StateDumpServiceComponent;
036: import org.cougaar.mts.base.DestinationQueueMonitorService;
037: import org.cougaar.util.log.Logger;
038:
039: /**
040: * This class and its related service have a very specialized role.
041: * They exist solely to allow one external class, the StateDumpService
042: * implementation, to dump the current message queues. No other
043: * clients can use it. The StateDumpService impl itself can't easily
044: * live in mts for security reasons.
045: */
046: public final class MessageQueueDumpServiceComponent extends
047: ParameterizedComponent implements ServiceProvider {
048: private ServiceBroker sb, rootsb;
049: private Impl impl;
050:
051: public MessageQueueDumpServiceComponent() {
052: }
053:
054: public void setNodeControlService(NodeControlService ncs) {
055: rootsb = (ncs == null) ? null : ncs.getRootServiceBroker();
056: }
057:
058: public void load() {
059: super .load();
060: impl = new Impl(sb);
061: rootsb.addService(MessageQueueDumpService.class, this );
062: }
063:
064: public Object getService(ServiceBroker sb, Object requestor,
065: Class serviceClass) {
066: if (serviceClass == MessageQueueDumpService.class
067: && requestor instanceof StateDumpServiceComponent)
068: return impl;
069: else
070: return null;
071: }
072:
073: public void releaseService(ServiceBroker sb, Object requestor,
074: Class serviceClass, Object service) {
075: }
076:
077: public final void setServiceBroker(ServiceBroker sb) {
078: this .sb = sb;
079: }
080:
081: private class Impl implements MessageQueueDumpService {
082: DestinationQueueMonitorService dqms;
083: ServiceBroker sb;
084:
085: Impl(ServiceBroker sb) {
086: this .sb = sb;
087: }
088:
089: private void dumpMessage(AttributedMessage msg, int i,
090: Logger logger) {
091: logger.warn(i + " " + msg.getOriginator() + " "
092: + msg.getTarget() + " "
093: + msg.getAttributesAsString() + " "
094: + msg.getRawMessage()/*.getClass().getName()*/);
095: }
096:
097: public int dumpQueues(Logger logger) {
098: if (dqms == null) {
099: dqms = (DestinationQueueMonitorService) sb.getService(
100: this , DestinationQueueMonitorService.class,
101: null);
102: if (dqms == null) {
103: logger
104: .warn("Couldn't get DestinationQueueMonitorService");
105: return 0;
106: }
107: }
108:
109: int count = 0;
110: MessageAddress[] addresses = dqms.getDestinations();
111: for (int i = 0; i < addresses.length; i++) {
112: MessageAddress address = addresses[i];
113: AttributedMessage[] msgs = dqms.snapshotQueue(address);
114: for (int j = 0; j < msgs.length; j++) {
115: count++;
116: AttributedMessage msg = msgs[j];
117: dumpMessage(msg, count, logger);
118: }
119: }
120: return count;
121: }
122:
123: }
124: }
|