001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 Mobile Intelligence Corp
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: package org.cougaar.community.test;
027:
028: import java.util.Set;
029:
030: import org.cougaar.util.log.LoggerFactory;
031:
032: import org.cougaar.community.CommunityCache;
033: import org.cougaar.community.CommunityImpl;
034: import org.cougaar.core.service.community.FindCommunityCallback;
035: import org.cougaar.core.service.wp.Callback;
036: import org.cougaar.community.CommunityUpdateListener;
037:
038: import org.cougaar.community.manager.AbstractCommunityManager;
039:
040: import org.cougaar.core.service.community.Community;
041:
042: /**
043: * Tests CommunityManager operations.
044: */
045: public class CommunityManagerTestImpl extends AbstractCommunityManager {
046:
047: protected CommunityUpdateListener updateListener;
048: static private CommunityManagerTestImpl instance;
049: static protected CommunityCache cache;
050:
051: /**
052: * Construct CommunityManager component capable of communicating with remote
053: * agents via Blackboard Relays.
054: * @param bs BindingSite
055: */
056: private CommunityManagerTestImpl(String agentName,
057: CommunityCache cache, CommunityUpdateListener cul) {
058: logger = LoggerFactory.getInstance().createLogger(
059: CommunityManagerTestImpl.class);
060: super .agentName = agentName;
061: this .cache = cache;
062: this .updateListener = cul;
063: }
064:
065: /**
066: * Tests whether this agent is the manager for the specified community.
067: * @param communityName String
068: * @return boolean
069: */
070: protected boolean isManager(String communityName) {
071: return communities.containsKey(communityName);
072: }
073:
074: /**
075: * Add agents to distribution list for community updates.
076: * @param communityName
077: * @param targetName String
078: */
079: protected void addTargets(String communityName, Set targets) {
080: // Intentionally empty
081: }
082:
083: /**
084: * Remove agents from distribution list for community updates.
085: * @param communityName
086: * @param targetName String
087: */
088: protected void removeTargets(String communityName, Set targets) {
089: // Intentionally empty
090: }
091:
092: /**
093: * Send updated Community info to agents on distribution.
094: * @param String communityName
095: */
096: protected void distributeUpdates(String communityName) {
097: if (updateListener != null) {
098: CommunityImpl community = (CommunityImpl) communities
099: .get(communityName);
100: community.setLastUpdate(System.currentTimeMillis());
101: updateListener.updateCommunity((CommunityImpl) community
102: .clone());
103: }
104: }
105:
106: /**
107: * Get name of community manager.
108: * @param communityName String
109: * @param fmcb FindManagerCallback
110: * @return String
111: */
112: public void findManager(String communityName,
113: FindCommunityCallback fccb) {
114: fccb.execute(isManager(communityName) ? agentName : null);
115: }
116:
117: /**
118: * Asserts community manager role.
119: * @param communityName Community to manage
120: */
121: protected void assertCommunityManagerRole(String communityName) {
122: // Intentionally empty
123: }
124:
125: protected static CommunityManagerTestImpl getInstance(
126: String agentName, CommunityCache cache,
127: CommunityUpdateListener cul) {
128: if (instance == null) {
129: instance = new CommunityManagerTestImpl(agentName, cache,
130: cul);
131: }
132: return instance;
133: }
134:
135: protected static CommunityManagerTestImpl getInstance() {
136: return instance;
137: }
138:
139: protected void reset() {
140: communities.clear();
141: }
142:
143: /**
144: * Sets community state for testing.
145: */
146: protected void addCommunity(Community community) {
147: cache.update(community);
148: communities.put(community.getName(), community);
149: distributeUpdates(community.getName());
150: }
151:
152: protected void removeCommunity(String communityName) {
153: cache.remove(communityName);
154: communities.remove(communityName);
155: distributeUpdates(communityName);
156: }
157:
158: }
|