001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: import java.util.Observable;
032: import java.util.Observer;
033: import org.cougaar.qos.ResourceStatus.RSSSubscriber;
034: import org.cougaar.qos.ResourceStatus.ResourceNode;
035: import org.cougaar.util.log.Logger;
036:
037: class RSSSubscriberProxy implements Observer {
038: private BoundDataFormula bdf;
039: private RSSSubscriber subscriber;
040: private final ResourceStatusServiceImpl service;
041: private final int callback_id;
042: Logger logger;
043:
044: private class Updater implements Runnable {
045: private final org.cougaar.qos.ResourceStatus.DataValue corbaDataValue;
046:
047: Updater(org.cougaar.qos.ResourceStatus.DataValue value) {
048: this .corbaDataValue = value;
049: }
050:
051: public void run() {
052: synchronized (RSSSubscriberProxy.this ) {
053: if (subscriber != null) {
054: try {
055: subscriber.dataUpdate(callback_id,
056: corbaDataValue);
057: } catch (Exception ex) {
058: // silently assume remote object is dead
059: service.unsubscribe(subscriber, bdf
060: .getDescription());
061: }
062: }
063: }
064: }
065: }
066:
067: RSSSubscriberProxy(BoundDataFormula bdf, RSSSubscriber subscriber,
068: int callback_id, ResourceStatusServiceImpl service) {
069: this .bdf = bdf;
070: this .subscriber = subscriber;
071: this .callback_id = callback_id;
072: this .service = service;
073: logger = Logging.getLogger(RSSSubscriberProxy.class);
074: bdf.addObserver(this );
075: }
076:
077: public void update(Observable o, Object value) {
078: DataValue v = (DataValue) value;
079: if (logger.isDebugEnabled()) {
080: logger.debug("Update: " + value);
081: }
082: org.cougaar.qos.ResourceStatus.DataValue corbaDataValue = v
083: .getCorbaValue();
084: // Do the actual CORBA call in a dedicated thread, so as not
085: // to tie up the caller's thread.
086: RSSUtils.schedule(new Updater(corbaDataValue), 0);
087: }
088:
089: boolean hasPath(ResourceNode[] path) {
090: ResourceNode[] candidate = bdf.getDescription();
091: if (candidate.length != path.length) {
092: return false;
093: }
094: for (int i = 0; i < candidate.length; i++) {
095: if (!candidate[i].equals(path[i])) {
096: return false;
097: }
098: }
099: return true;
100: }
101:
102: synchronized void unbind() {
103: if (bdf != null) {
104: bdf.deleteObserver(this);
105: bdf.unsubscribe();
106: bdf = null;
107: subscriber = null;
108: }
109: }
110:
111: }
|