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.Observable;
030: import java.util.Observer;
031:
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.service.community.Community;
034: import org.cougaar.core.service.community.CommunityService;
035: import org.cougaar.multicast.AttributeBasedAddress;
036: import org.cougaar.util.UnaryPredicate;
037:
038: /**
039: * Default implementation for Facets that deal with ABAs and
040: * Communities.
041: */
042: abstract public class CommunityFacetImpl extends FacetImpl implements
043: Observer {
044: private Community community;
045: private String communityName;
046: private CommunityFinder finder;
047: private AttributeBasedAddress aba;
048: private CommunityService commService;
049:
050: /**
051: * Hook for domain-specific Facet implementations to construct an
052: * ABA given a community.
053: */
054: public abstract AttributeBasedAddress makeABA(String communityName);
055:
056: protected CommunityFacetImpl(CoordinationArtifact owner,
057: ServiceBroker sb, ConnectionSpec spec, RolePlayer player) {
058: super (owner, sb, spec, player);
059: commService = (CommunityService) sb.getService(this ,
060: CommunityService.class, null);
061:
062: }
063:
064: protected AttributeBasedAddress getABA() {
065: return aba;
066: }
067:
068: protected Community getCommunity() {
069: return community;
070: }
071:
072: // Community helpers
073: protected void findCommunityForAny(String filter,
074: UnaryPredicate predicate) {
075: finder = new CommunityFinder.ForAny(commService, filter,
076: predicate);
077: finder.addObserver(this );
078: }
079:
080: protected void findCommunityForAgent(String filter,
081: UnaryPredicate predicate) {
082: finder = new CommunityFinder.ForAgent(commService, filter,
083: predicate, getAgentID());
084: finder.addObserver(this );
085: }
086:
087: // Observer. Used for CommunityFinder callbacks.
088: public void update(Observable obs, Object value) {
089: if (log.isDebugEnabled())
090: log.debug("CommunityFinder " + obs + " callback returned "
091: + value);
092: if (obs instanceof CommunityFinder) {
093: CommunityFinder finder = (CommunityFinder) obs;
094: this .communityName = (String) value;
095: this .community = finder.getCommunity();
096: this .aba = makeABA(communityName);
097: linkPlayer();
098: }
099: }
100:
101: protected Receptacle makeReceptacle() {
102: return new CommunityReceptacleImpl();
103: }
104:
105: protected class CommunityReceptacleImpl extends ReceptacleImpl {
106: public Community getCommunity() {
107: return community;
108: }
109:
110: }
111:
112: }
|