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.qos.metrics;
028:
029: import org.cougaar.core.component.ServiceBroker;
030: import org.cougaar.core.plugin.ComponentPlugin;
031: import org.cougaar.core.service.LoggingService;
032: import org.cougaar.core.service.PersistenceMetricsService;
033: import org.cougaar.core.service.ThreadService;
034: import org.cougaar.core.thread.Schedulable;
035:
036: /**
037: * Gathers persistence metrics from the PersistenceMetricsService and
038: * publishes them as Metrics into the MetricsUpdateService. Should be
039: * loaded into every Agent.
040: *
041: * @see PersistenceMetricsService
042: */
043: public class PersistenceAdapterPlugin extends ComponentPlugin implements
044: Runnable, Constants {
045: private PersistenceMetricsService pms;
046: private LoggingService loggingService;
047: private MetricsUpdateService mus;
048: private Schedulable schedulable;
049: private String key;
050:
051: public void load() {
052: super .load();
053:
054: ServiceBroker sb = getServiceBroker();
055:
056: loggingService = (LoggingService) sb.getService(this ,
057: LoggingService.class, null);
058:
059: pms = (PersistenceMetricsService) sb.getService(this ,
060: PersistenceMetricsService.class, null);
061: if (pms == null) {
062: if (loggingService.isErrorEnabled())
063: loggingService
064: .error("Couldn't get PersistenceMetricsService");
065: return;
066: }
067:
068: mus = (MetricsUpdateService) sb.getService(this ,
069: MetricsUpdateService.class, null);
070: if (mus == null) {
071: if (loggingService.isErrorEnabled())
072: loggingService
073: .error("Couldn't get MetricsUpdateService");
074: return;
075: }
076:
077: ThreadService tsvc = (ThreadService) sb.getService(this ,
078: ThreadService.class, null);
079: if (tsvc == null) {
080: if (loggingService.isErrorEnabled())
081: loggingService.error("Couldn't get ThreadService");
082: return;
083: }
084:
085: key = "Agent" + KEY_SEPR + getAgentIdentifier() + KEY_SEPR
086: + PERSIST_SIZE_LAST;
087:
088: schedulable = tsvc.getThread(this , this , "PersistenceAdapter");
089: schedulable.schedule(0, 10000);
090:
091: sb.releaseService(this , ThreadService.class, tsvc);
092:
093: }
094:
095: // Runnable
096: public void run() {
097: PersistenceMetricsService.Metric[] metrics = pms
098: .getAll(PersistenceMetricsService.FULL);
099: long maxSize = 0;
100: for (int i = 0, n = metrics.length; i < n; i++) {
101: maxSize = Math.max(maxSize, metrics[i].getSize());
102: }
103:
104: if (maxSize > 0) {
105: Metric metric = new MetricImpl(maxSize,
106: SECOND_MEAS_CREDIBILITY, "bytes",
107: "PersistenceMetricsService");
108: mus.updateValue(key, metric);
109: if (loggingService.isDebugEnabled())
110: loggingService.debug("Updating " + key + " to "
111: + metric);
112: } else {
113: if (loggingService.isDebugEnabled())
114: loggingService.debug(key + " is still 0 ");
115: }
116: }
117:
118: // Plugin methods
119: protected void setupSubscriptions() {
120: // None in this example
121: }
122:
123: public void execute() {
124: // Not relevant in this example
125: }
126:
127: }
|