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.ca;
028:
029: import java.util.HashMap;
030: import java.util.Observable;
031: import java.util.Observer;
032:
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.qos.metrics.Metric;
035: import org.cougaar.core.qos.metrics.MetricNotificationQualifier;
036: import org.cougaar.core.qos.metrics.MetricsService;
037: import org.cougaar.core.qos.metrics.VariableEvaluator;
038: import org.cougaar.core.service.BlackboardService;
039:
040: /**
041: * CA interface to the MetricsService
042: */
043: abstract public class MetricsReaderFacet extends FacetImpl {
044:
045: private MetricsService metricsService;
046: private HashMap subscriptionKeys;
047:
048: protected MetricsReaderFacet(CoordinationArtifact owner,
049: ServiceBroker sb, ConnectionSpec spec, RolePlayer player) {
050: super (owner, sb, spec, player);
051: subscriptionKeys = new HashMap();
052: metricsService = (MetricsService) sb.getService(this ,
053: MetricsService.class, null);
054: linkPlayer();
055: }
056:
057: abstract protected String getPath(Object fact);
058:
059: abstract protected MetricNotificationQualifier getQualifier(
060: Object fact);
061:
062: abstract protected VariableEvaluator getEvaluator(Object fact);
063:
064: abstract protected void assertDataFact(Metric value, String path);
065:
066: private class SubscriptionObserver implements Observer {
067: String path;
068:
069: SubscriptionObserver(String path) {
070: this .path = path;
071: }
072:
073: public void update(Observable xxx, Object update) {
074: assertDataFact((Metric) update, path);
075: }
076: }
077:
078: private void subscribe(Object fact) {
079: String path = getPath(fact);
080: VariableEvaluator evaluator = getEvaluator(fact);
081: MetricNotificationQualifier qualifier = getQualifier(fact);
082: Observer observer = new SubscriptionObserver(path);
083: Object key = metricsService.subscribeToValue(path, observer,
084: evaluator, qualifier);
085: synchronized (subscriptionKeys) {
086: subscriptionKeys.put(path, key);
087: }
088: }
089:
090: private void unsubscribe(Object fact) {
091: String path = getPath(fact);
092: Object key = null;
093: synchronized (subscriptionKeys) {
094: key = subscriptionKeys.get(path);
095: subscriptionKeys.remove(path);
096: }
097: if (key != null)
098: metricsService.unsubscribeToValue(key);
099:
100: }
101:
102: public void setupSubscriptions(BlackboardService blackboard) {
103: }
104:
105: public void execute(BlackboardService blackboard) {
106: }
107:
108: public void processFactBase(BlackboardService blackboard) {
109: if (!factsHaveChanged())
110: return;
111: for (FactRevision frev = nextFact(); frev != null; frev = nextFact()) {
112: Object fact = frev.getFact();
113: if (frev instanceof FactAssertion) {
114: subscribe(fact);
115: } else if (frev instanceof FactRetraction) {
116: unsubscribe(fact);
117: }
118: }
119: }
120:
121: }
|