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.lib.aggagent.plugin;
027:
028: import java.util.Collections;
029: import java.util.Set;
030:
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.relay.Relay;
033: import org.cougaar.core.util.UID;
034:
035: /**
036: * This is an example of a simple relay object.
037: */
038: public class AggRelay implements Relay.Source, Relay.Target {
039: private MessageAddress source;
040: private MessageAddress target;
041: private UID uid;
042:
043: private XMLMessage content;
044: private XMLMessage response;
045:
046: private transient Set _targets;
047:
048: /** Holds value of property local. */
049: private boolean local = false;
050:
051: /**
052: * @param content initial content
053: * @param response initial response
054: */
055: public AggRelay(UID uid, MessageAddress source,
056: MessageAddress target, XMLMessage content,
057: XMLMessage response) {
058: this .uid = uid;
059: this .source = source;
060: this .target = target;
061:
062: this .content = content;
063: this .response = response;
064:
065: this ._targets = ((target != null) ? Collections
066: .singleton(target) : Collections.EMPTY_SET);
067:
068: }
069:
070: // UniqueObject interface
071:
072: public void setUID(UID uid) {
073: throw new RuntimeException("Attempt to change UID");
074: }
075:
076: public UID getUID() {
077: return uid;
078: }
079:
080: // Source interface
081:
082: public Set getTargets() {
083: return _targets;
084: }
085:
086: public Object getContent() {
087: return content;
088: }
089:
090: private static final class SimpleRelayFactory implements
091: TargetFactory, java.io.Serializable {
092:
093: public static final SimpleRelayFactory INSTANCE = new SimpleRelayFactory();
094:
095: private SimpleRelayFactory() {
096: }
097:
098: public Relay.Target create(UID uid, MessageAddress source,
099: Object content, Token token) {
100: AggRelay ar = new AggRelay(uid, source, null,
101: (XMLMessage) content, null);
102: ar.setLocal(false);
103: return ar;
104: }
105:
106: private Object readResolve() {
107: return INSTANCE;
108: }
109: };
110:
111: public TargetFactory getTargetFactory() {
112: return SimpleRelayFactory.INSTANCE;
113: }
114:
115: public int updateResponse(MessageAddress addr, Object o) {
116: if (!(o instanceof XMLMessage))
117: throw new RuntimeException(
118: "AggRelay: Response is not an XMLMessage");
119: return updateResponse(addr, (XMLMessage) o);
120: }
121:
122: public int updateResponse(MessageAddress t, XMLMessage response) {
123: // assert response != null
124: if (!(response.equals(this .response))) {
125: this .response = response;
126: return Relay.RESPONSE_CHANGE;
127: }
128: return Relay.NO_CHANGE;
129: }
130:
131: // Target interface
132:
133: public MessageAddress getSource() {
134: return source;
135: }
136:
137: public Object getResponse() {
138: return response;
139: }
140:
141: public int updateContent(Object o, Token token) {
142: if (!(o instanceof XMLMessage))
143: throw new RuntimeException(
144: "AggRelay: Content is not an XMLMessage");
145: return updateContent((XMLMessage) o, token);
146: }
147:
148: public int updateContent(XMLMessage content, Token token) {
149: // assert content != null
150: if (!(content.equals(this .content))) {
151: this .content = content;
152: return Relay.CONTENT_CHANGE;
153: }
154: return Relay.NO_CHANGE;
155: }
156:
157: public boolean equals(Object o) {
158: if (o == this ) {
159: return true;
160: } else if (!(o instanceof AggRelay)) {
161: return false;
162: } else {
163: UID u = ((AggRelay) o).getUID();
164: return uid.equals(u);
165: }
166: }
167:
168: public int hashCode() {
169: return uid.hashCode();
170: }
171:
172: public String toString() {
173: return "(" + uid + ", " + content + ", " + response + ")";
174: }
175:
176: /** Getter for property local.
177: * @return Value of property local.
178: */
179: public boolean isLocal() {
180: return local;
181: }
182:
183: /** Setter for property local.
184: * @param local New value of property local.
185: */
186: public void setLocal(boolean local) {
187: this .local = local;
188: }
189:
190: private void readObject(java.io.ObjectInputStream os)
191: throws ClassNotFoundException, java.io.IOException {
192: os.defaultReadObject();
193: this._targets = ((target != null) ? Collections
194: .singleton(target) : Collections.EMPTY_SET);
195: }
196: }
|