001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 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.lib.web.micro.mts;
028:
029: import java.io.ByteArrayOutputStream;
030: import java.util.List;
031: import java.util.Map;
032:
033: import org.cougaar.core.mts.Message;
034: import org.cougaar.core.mts.MessageAddress;
035: import org.cougaar.core.util.UID;
036:
037: /**
038: * A message between input/output pipe deliverers.
039: */
040: public class PipeMessage extends Message {
041:
042: private final String type;
043: private final UID sessionId;
044: private final int counter;
045: private final Map metaData;
046: private final List data;
047:
048: public PipeMessage(MessageAddress source, MessageAddress target,
049: String type, UID sessionId, int counter, Map metaData,
050: List data) {
051: super (source, target);
052: this .type = type;
053: this .sessionId = sessionId;
054: this .counter = counter;
055: this .metaData = metaData;
056: this .data = data;
057:
058: String s = (type == null ? "null type"
059: : sessionId == null ? "null sessionId"
060: : counter < 0 ? ("negative counter: " + counter)
061: : data == null ? "null data" : null);
062: if (s != null) {
063: throw new IllegalArgumentException(s);
064: }
065: }
066:
067: /**
068: * @return sender type, used to distinguish between client and server
069: * messages.
070: */
071: public String getType() {
072: return type;
073: }
074:
075: /** @return unique session id */
076: public UID getSessionId() {
077: return sessionId;
078: }
079:
080: /** @return positive sequence counter */
081: public int getCounter() {
082: return counter;
083: }
084:
085: /** @see Deliverer#deliver */
086: public Map getMetaData() {
087: return metaData;
088: }
089:
090: /** @see Deliverer#deliver */
091: public List getData() {
092: return data;
093: }
094:
095: /**
096: * Get a string representation of the data.
097: *
098: * @param prefix optional line prefix, e.g. " ".
099: * @param limit optional result string limit, or -1 for no limit
100: * @return the {@link #getData} as a formatted string.
101: */
102: public String getDataAsString(String prefix, int limit) {
103: String pre = (prefix == null ? "" : prefix);
104: StringBuffer buf = new StringBuffer();
105: for (int i = 0; i < data.size(); i++) {
106: Object o = data.get(i);
107: if (i > 0) {
108: buf.append("\n");
109: }
110: if (o instanceof ByteArrayOutputStream) {
111: o = ((ByteArrayOutputStream) o).toByteArray();
112: }
113: if (o instanceof byte[]) {
114: byte[] b = (byte[]) o;
115: buf.append(pre).append("** byte[").append(b.length)
116: .append("] **\n");
117: int len = b.length;
118: if (limit >= 0 && len > (limit - buf.length())) {
119: len = Math.max(0, limit - buf.length());
120: }
121: String s = new String(b, 0, len);
122: if (pre.length() > 0 && s.indexOf('\n') >= 0) {
123: s = s.replaceAll("\n", "\n" + pre);
124: }
125: buf.append(pre).append(s);
126: if (len < b.length) {
127: buf.append("\n");
128: if (pre.length() > 0) {
129: buf.append(pre);
130: }
131: buf.append("..");
132: break;
133: }
134: } else if (o == Tokens.NOOP) {
135: buf.append(pre).append("** noop **");
136: } else if (o == Tokens.FLUSH) {
137: buf.append(pre).append("** flush **");
138: } else if (o == Tokens.CLOSE) {
139: buf.append(pre).append("** close **");
140: } else {
141: buf.append(pre).append("** ? **");
142: buf.append(o == null ? "null" : o.getClass().getName());
143: }
144: }
145: return buf.toString();
146: }
147:
148: public String toString() {
149: return toString(100);
150: }
151:
152: public String toString(int limit) {
153: return "(pipe-message"
154: + "\n source="
155: + getOriginator()
156: + "\n target="
157: + getTarget()
158: + "\n type="
159: + type
160: + "\n sessionId="
161: + sessionId
162: + "\n counter="
163: + counter
164: + "\n metaData="
165: + (metaData == null ? "null" : "Map[" + metaData.size()
166: + "]")
167: + "\n data=List["
168: + data.size()
169: + "]"
170: + "\n"
171: + getDataAsString(" ", limit)
172: + "\n closed="
173: + (!data.isEmpty() && data.get(data.size() - 1) == Tokens.CLOSE)
174: + ")";
175: }
176: }
|