001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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: package org.cougaar.community.manager;
027:
028: import java.io.ObjectInputStream;
029: import java.io.Serializable;
030: import java.util.HashSet;
031: import java.util.Set;
032: import java.util.Collections;
033: import javax.naming.directory.ModificationItem;
034:
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.relay.Relay;
037: import org.cougaar.core.util.UID;
038: import org.cougaar.core.service.community.CommunityResponse;
039: import org.cougaar.core.service.community.CommunityResponseListener;
040: import org.cougaar.core.service.community.Entity;
041:
042: /**
043: * Blackboard Relay used by CommunityService to send request to remote
044: * Community Manager. This class should only be used by Community
045: * Service implementations, it is not intended for use by clients.
046: **/
047: public class RequestImpl implements Request, Relay.Source,
048: Relay.Target, Serializable {
049:
050: protected final UID uid;
051: protected final MessageAddress source;
052: protected final MessageAddress target;
053:
054: protected String communityName;
055: protected int requestType = UNDEFINED;
056: protected Entity entity;
057:
058: protected CommunityResponse resp;
059: protected ModificationItem[] mods;
060:
061: private transient Set targets;
062: private transient Set listeners;
063:
064: public RequestImpl(MessageAddress source, MessageAddress target,
065: String communityName, int reqType, Entity entity,
066: ModificationItem[] attrMods, UID uid,
067: CommunityResponseListener crl) {
068: this .source = source;
069: this .target = target;
070: this .communityName = communityName;
071: this .requestType = reqType;
072: this .entity = entity;
073: this .mods = attrMods;
074: this .uid = uid;
075: addCommunityResponseListener(crl);
076: cacheTargets();
077: }
078:
079: public String getCommunityName() {
080: return communityName;
081: }
082:
083: /**
084: * Defines the type of request.
085: * @param reqType Request type
086: */
087: public void setRequestType(int reqType) {
088: this .requestType = reqType;
089: }
090:
091: public int getRequestType() {
092: return this .requestType;
093: }
094:
095: /**
096: * Entity for requests requiring one, such as a JOIN and LEAVE.
097: * @param entity Affected entity
098: */
099: public void setEntity(Entity entity) {
100: this .entity = entity;
101: }
102:
103: public Entity getEntity() {
104: return this .entity;
105: }
106:
107: public void setResponse(CommunityResponse resp) {
108: this .resp = resp;
109: }
110:
111: public void setAttributeModifications(ModificationItem[] mods) {
112: this .mods = mods;
113: }
114:
115: public ModificationItem[] getAttributeModifications() {
116: return mods;
117: }
118:
119: public void addCommunityResponseListener(
120: CommunityResponseListener crl) {
121: if (listeners == null)
122: listeners = new HashSet();
123: listeners.add(crl);
124: }
125:
126: public Set getCommunityResponseListeners() {
127: return (listeners != null) ? listeners : Collections.EMPTY_SET;
128: }
129:
130: public String getRequestTypeAsString(int type) {
131: switch (type) {
132: case Request.UNDEFINED:
133: return "UNDEFINED";
134: case Request.JOIN:
135: return "JOIN";
136: case Request.LEAVE:
137: return "LEAVE";
138: case Request.GET_COMMUNITY_DESCRIPTOR:
139: return "GET_COMMUNITY_DESCRIPTOR";
140: case Request.MODIFY_ATTRIBUTES:
141: return "MODIFY_ATTRIBUTES";
142: case Request.LIST:
143: return "LIST";
144: }
145: return "INVALID_VALUE";
146: }
147:
148: /**
149: * Returns a string representation of the request
150: * @return String - a string representation of the request.
151: **/
152: public String toString() {
153: return "request=" + getRequestTypeAsString(requestType)
154: + " community=" + communityName + " entity="
155: + (entity == null ? "null" : entity.getName())
156: + " resp=" + resp + " source=" + this .getSource()
157: + " uid=" + uid;
158: }
159:
160: public UID getUID() {
161: return uid;
162: }
163:
164: public void setUID(UID uid) {
165: throw new UnsupportedOperationException();
166: }
167:
168: public MessageAddress getTarget() {
169: return target;
170: }
171:
172: // Relay.Source:
173:
174: private void cacheTargets() {
175: targets = ((target != null) ? Collections.singleton(target)
176: : Collections.EMPTY_SET);
177: }
178:
179: public Set getTargets() {
180: return targets;
181: }
182:
183: public Object getContent() {
184: return this ;
185: }
186:
187: public Relay.TargetFactory getTargetFactory() {
188: return RequestImplFactory.INSTANCE;
189: }
190:
191: public int updateResponse(MessageAddress t, Object response) {
192: this .resp = (CommunityResponse) response;
193: return Relay.RESPONSE_CHANGE;
194: }
195:
196: // Relay.Target:
197:
198: public MessageAddress getSource() {
199: return source;
200: }
201:
202: public Object getResponse() {
203: return resp;
204: }
205:
206: public int updateContent(Object content, Token token) {
207: return Relay.NO_CHANGE;
208: }
209:
210: public boolean equals(Object o) {
211: if (o == this ) {
212: return true;
213: } else if (!(o instanceof RequestImpl)) {
214: return false;
215: } else {
216: UID u = ((RequestImpl) o).uid;
217: return uid.equals(u);
218: }
219: }
220:
221: public int hashCode() {
222: return uid.hashCode();
223: }
224:
225: private void readObject(ObjectInputStream stream)
226: throws ClassNotFoundException, java.io.IOException {
227: stream.defaultReadObject();
228: cacheTargets();
229: }
230:
231: protected RequestImpl target_copy() {
232: return new RequestImpl(source, null, communityName,
233: requestType, entity, mods, uid, null);
234: }
235:
236: /**
237: * Simple factory implementation.
238: */
239: private static class RequestImplFactory implements
240: Relay.TargetFactory, Serializable {
241:
242: public static RequestImplFactory INSTANCE = new RequestImplFactory();
243:
244: public Relay.Target create(UID uid, MessageAddress source,
245: Object content, Relay.Token token) {
246: RequestImpl ati = (RequestImpl) content;
247: return ati.target_copy();
248: }
249:
250: private Object readResolve() {
251: return INSTANCE;
252: }
253: }
254:
255: }
|