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.core.blackboard;
028:
029: import java.util.List;
030: import java.util.Map;
031: import java.util.WeakHashMap;
032: import org.cougaar.util.StackElements;
033:
034: /**
035: * An envelope that records the subscriber name, time of
036: * "openTransaction()", and time of "closeTransaction()".
037: *
038: * @see Subscriber option system property that must be enabled
039: * for these Envelopes to be used.
040: */
041: public class TimestampedEnvelope extends Envelope {
042:
043: // weakly cache "publishAdd" stacks
044: private static final Map stacks = new WeakHashMap();
045:
046: private String name;
047: private long openTime;
048: private long closeTime;
049:
050: public TimestampedEnvelope() {
051: }
052:
053: public Envelope newInstance() {
054: TimestampedEnvelope ret = new TimestampedEnvelope();
055: ret.name = name;
056: ret.openTime = openTime;
057: ret.closeTime = closeTime;
058: return ret;
059: }
060:
061: public final void setName(String name) {
062: this .name = name;
063: }
064:
065: AddEnvelopeTuple newAddEnvelopeTuple(Object o) {
066: return new Add(o, captureStack());
067: }
068:
069: ChangeEnvelopeTuple newChangeEnvelopeTuple(Object o, List changes) {
070: return new Change(o, changes, captureStack());
071: }
072:
073: RemoveEnvelopeTuple newRemoveEnvelopeTuple(Object o) {
074: return new Remove(o, captureStack());
075: }
076:
077: private static StackElements captureStack() {
078: StackElements se = new StackElements(new Throwable());
079: synchronized (stacks) {
080: StackElements cached_se = (StackElements) stacks.get(se);
081: if (cached_se == null) {
082: stacks.put(se, se);
083: } else {
084: se = cached_se;
085: }
086: }
087: return se;
088: }
089:
090: public final void setTransactionOpenTime(long openTime) {
091: this .openTime = openTime;
092: }
093:
094: public final void setTransactionCloseTime(long closeTime) {
095: this .closeTime = closeTime;
096: }
097:
098: /**
099: * @return true if the envelope is from the blackboard (LPs)
100: */
101: public boolean isBlackboard() {
102: return false;
103: }
104:
105: /**
106: * @return the name of the subscriber that created this envelope
107: */
108: public final String getName() {
109: return name;
110: }
111:
112: /**
113: * @return time in milliseconds when the transaction was opened
114: */
115: public final long getTransactionOpenTime() {
116: return openTime;
117: }
118:
119: /**
120: * @return time in milliseconds when the transaction was closed
121: */
122: public final long getTransactionCloseTime() {
123: return closeTime;
124: }
125:
126: // package-private subscription needs to see this
127: boolean get_isVisible() {
128: return isVisible();
129: }
130:
131: public String toString() {
132: return super .toString() + " ("
133: + (isBlackboard() ? "blackboard, " : "client, ") + name
134: + ", " + openTime + " + " + (closeTime - openTime)
135: + ")";
136: }
137:
138: private static final class Add extends AddEnvelopeTuple {
139: private StackElements se;
140:
141: public Add(Object o, StackElements se) {
142: super (o);
143: this .se = se;
144: }
145:
146: public StackElements getStack() {
147: return se;
148: }
149: }
150:
151: private static final class Change extends ChangeEnvelopeTuple {
152: private StackElements se;
153:
154: public Change(Object o, List changes, StackElements se) {
155: super (o, changes);
156: this .se = se;
157: }
158:
159: public StackElements getStack() {
160: return se;
161: }
162: }
163:
164: private static final class Remove extends RemoveEnvelopeTuple {
165: private StackElements se;
166:
167: public Remove(Object o, StackElements se) {
168: super (o);
169: this .se = se;
170: }
171:
172: public StackElements getStack() {
173: return se;
174: }
175: }
176: }
|