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:
027: package org.cougaar.servicediscovery.transaction;
028:
029: import java.util.Collection;
030: import java.util.Collections;
031:
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.core.relay.Relay;
034: import org.cougaar.util.log.Logger;
035: import org.cougaar.util.log.Logging;
036:
037: /**
038: * Relay used to request command chain lineage
039: **/
040: public class LineageRelay extends RelayAdapter {
041: private static Logger myLogger = Logging
042: .getLogger(LineageRelay.class);
043:
044: private String myAgentName;
045: private Collection myLineages;
046: private transient String myToString = null;
047:
048: public LineageRelay() {
049: super ();
050: }
051:
052: /**
053: * Gets the name of the Agent whose lineage is reported.
054: *
055: * @return String Name of the agent
056: */
057: public String getAgentName() {
058: return myAgentName;
059: }
060:
061: /**
062: * Returns the lineage lists for the target agent
063: *
064: * @return Collection Lineages for the targent agent
065: */
066: public Collection getLineages() {
067: Collection lists = (myLineages == null) ? Collections.EMPTY_LIST
068: : Collections.unmodifiableCollection(myLineages);
069:
070: return lists;
071: }
072:
073: /**
074: * Sets the Collection of lineages for the target agent
075: *
076: * @param lineages Collection of lineages for the agent
077: */
078: public void setLineages(Collection lineages) {
079: myLineages = lineages;
080: myToString = null;
081: }
082:
083: /**
084: * Add a target message address.
085: * @param target the address of the target agent.
086: **/
087: public void addTarget(MessageAddress target) {
088: if ((myTargetSet != null) && (myTargetSet.size() > 0)) {
089: myLogger.error("Attempt to set multiple targets.");
090: return;
091: }
092:
093: super .addTarget(target);
094:
095: myAgentName = target.toString();
096: }
097:
098: /**
099: * Set the response that was sent from a target. For LP use only.
100: * This implementation assumes that response is always different.
101: * or used.
102: **/
103: public int updateResponse(MessageAddress target, Object response) {
104: LineageRelay lineageRelay = (LineageRelay) response;
105:
106: setLineages(lineageRelay.getLineages());
107:
108: return Relay.RESPONSE_CHANGE;
109: }
110:
111: // Relay.Target:
112:
113: public Object getResponse() {
114: return (myLineages != null ? this : null);
115: }
116:
117: public String toString() {
118: if (myToString == null) {
119: myToString = getClass() + ": agent=<" + getAgentName()
120: + ">, lineages=<" + getLineages() + ">, UID=<"
121: + getUID() + ">";
122: }
123:
124: return myToString;
125: }
126:
127: }
|