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:
027: package org.cougaar.community.test;
028:
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.Iterator;
032: import java.util.Set;
033:
034: import javax.naming.directory.ModificationItem;
035:
036: import org.cougaar.util.log.LoggerFactory;
037:
038: import org.cougaar.core.service.community.CommunityResponse;
039: import org.cougaar.core.service.community.CommunityResponseListener;
040: import org.cougaar.core.service.community.CommunityService;
041: import org.cougaar.core.service.community.Community;
042: import org.cougaar.core.service.community.Entity;
043: import org.cougaar.core.service.community.FindCommunityCallback;
044:
045: import org.cougaar.community.AbstractCommunityService;
046: import org.cougaar.community.CommunityMemberships;
047: import org.cougaar.community.MembershipWatcher;
048: import org.cougaar.community.manager.CommunityManager;
049: import org.cougaar.community.CommunityUpdateListener;
050: import org.cougaar.community.CommunityResponseImpl;
051:
052: /**
053: * Implementation of CommunityService for testing outside of a running
054: * cougaar society.
055: **/
056: public class CommunityServiceTestImpl extends AbstractCommunityService
057: implements CommunityService, java.io.Serializable {
058:
059: /**
060: * Constructor.
061: */
062: public CommunityServiceTestImpl(String agentName) {
063: super .agentName = agentName;
064: log = LoggerFactory.getInstance().createLogger(
065: CommunityServiceTestImpl.class);
066: communityUpdateListener = new MyCommunityUpdateListener();
067: if (cache == null)
068: cache = new CommunityCacheTestImpl();
069: communityManager = CommunityManagerTestImpl.getInstance(
070: agentName, cache, communityUpdateListener);
071: myCommunities = new CommunityMemberships();
072: membershipWatcher = new MembershipWatcher(agentName, this ,
073: myCommunities);
074: }
075:
076: protected CommunityCacheTestImpl getCache() {
077: return (CommunityCacheTestImpl) cache;
078: }
079:
080: protected CommunityMemberships getCommunityMemberships() {
081: return myCommunities;
082: }
083:
084: protected MembershipWatcher getMembershipWatcher() {
085: return membershipWatcher;
086: }
087:
088: protected void queueCommunityRequest(String communityName,
089: int requestType, Entity entity,
090: ModificationItem[] attrMods, CommunityResponseListener crl,
091: long timeout, long delay) {
092: log.debug(agentName + ": queueCommunityRequest: "
093: + " community=" + communityName + " type="
094: + requestType + " entity=" + entity + " attrMods="
095: + attrMods);
096: CommunityResponse resp = communityManager
097: .processRequest(agentName, communityName, requestType,
098: entity, attrMods);
099: handleResponse(communityName, resp, Collections.singleton(crl));
100: }
101:
102: public Collection listParentCommunities(String member,
103: String filter, CommunityResponseListener crl) {
104: return listParentCommunities(member, filter);
105: }
106:
107: public Collection listParentCommunities(String member,
108: CommunityResponseListener crl) {
109: // FIXME - bug 3910 stub
110: return listParentCommunities(member);
111: }
112:
113: protected void sendResponse(CommunityResponse resp, Set listeners) {
114: log.debug("sendResponse: resp=" + resp + " listeners="
115: + listeners);
116: for (Iterator it = listeners.iterator(); it.hasNext();) {
117: CommunityResponseListener crl = (CommunityResponseListener) it
118: .next();
119: if (crl != null) {
120: crl.getResponse(resp);
121: }
122: }
123: }
124:
125: protected CommunityManager getCommunityManager() {
126: return communityManager;
127: }
128:
129: protected String getAgentName() {
130: return agentName;
131: }
132:
133: /**
134: * Lists all communities in White pages.
135: * @return Collection of community names
136: */
137: public Collection listAllCommunities() {
138: return cache.listAll();
139: }
140:
141: public void listAllCommunities(CommunityResponseListener crl) {
142: crl.getResponse(new CommunityResponseImpl(
143: CommunityResponse.SUCCESS, listAllCommunities()));
144: }
145:
146: public void findCommunity(String communityName,
147: FindCommunityCallback findMgrCB, long timeout) {
148: if (communityManager != null) {
149: communityManager.findManager(communityName, findMgrCB);
150: } else {
151: findMgrCB.execute(null);
152: }
153: }
154:
155: class MyCommunityUpdateListener implements CommunityUpdateListener {
156:
157: public void updateCommunity(Community community) {
158: log.debug("updateCommunity: community=" + community);
159: cache.update(community);
160: }
161:
162: public void removeCommunity(Community community) {
163: cache.remove(community.getName());
164: }
165:
166: }
167:
168: }
|