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 java.util.Observable;
030: import java.util.Observer;
031:
032: import org.cougaar.bootstrap.SystemProperties;
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.qos.metrics.Metric;
035: import org.cougaar.core.qos.metrics.MetricImpl;
036: import org.cougaar.core.qos.metrics.MetricsService;
037: import org.cougaar.core.qos.metrics.MetricsUpdateService;
038:
039: import org.cougaar.mts.base.SendQueue;
040: import org.cougaar.mts.base.SendQueueDelegateImplBase;
041: import org.cougaar.mts.base.StandardAspect;
042:
043: /**
044: * This test Aspect queries and updates the metric service on every
045: * message send. The query is as given by @property
046: * "org.cougaar.metrics.query. The update key is as given
047: * by @property org.cougaar.metrics.key. If @property
048: * org.cougaar.metrics.callback is supplied it will also subscribe to
049: * the specified formula.
050: */
051: public class MetricsTestAspect extends StandardAspect implements
052: Observer {
053:
054: MetricsUpdateService update;
055: MetricsService svc;
056: long lastUpdate = 0;
057:
058: public Object getDelegate(Object delegatee, Class type) {
059: if (type == SendQueue.class) {
060: return new DummySendQueue((SendQueue) delegatee);
061: } else {
062: return null;
063: }
064: }
065:
066: public void load() {
067: super .load();
068: ServiceBroker sb = getServiceBroker();
069: update = (MetricsUpdateService) sb.getService(this ,
070: MetricsUpdateService.class, null);
071: svc = (MetricsService) sb.getService(this ,
072: MetricsService.class, null);
073:
074: String path = SystemProperties
075: .getProperty("org.cougaar.metrics.callback");
076: if (path != null) {
077: svc.subscribeToValue(path, this );
078: System.out.println("Subscribed to " + path);
079: }
080: }
081:
082: public void update(Observable o, Object arg) {
083: long now = System.currentTimeMillis();
084: long updateDelta = now - lastUpdate;
085: // long value = ((Metric) arg).longValue();
086:
087: System.out.println("Update Time=" + updateDelta + " Value ="
088: + arg);
089: }
090:
091: private class DummySendQueue extends SendQueueDelegateImplBase {
092: DummySendQueue(SendQueue delegatee) {
093: super (delegatee);
094: }
095:
096: public void sendMessage(AttributedMessage message) {
097: runTest();
098: super .sendMessage(message);
099: }
100:
101: }
102:
103: public void runTest() {
104: String path = SystemProperties
105: .getProperty("org.cougaar.metrics.query");
106: if (path != null) {
107: Metric val = svc.getValue(path);
108: System.out.println(path + "=" + val);
109: }
110:
111: String key = SystemProperties
112: .getProperty("org.cougaar.metrics.key");
113: if (key != null) {
114: Metric m = new MetricImpl(new Long(System
115: .currentTimeMillis()), 0.3, "", "MetricsTestAspect");
116: System.out.println("Published " + key + "=" + m);
117: update.updateValue(key, m);
118: lastUpdate = System.currentTimeMillis();
119: }
120:
121: }
122:
123: }
|