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.core.wp;
028:
029: import org.cougaar.core.mts.MessageAddress;
030: import org.cougaar.core.mts.MessageAttributes;
031: import org.cougaar.core.mts.SimpleMessageAttributes;
032:
033: /**
034: * A utility class to attach an MTS timeout attribute to a message
035: * address.
036: */
037: public final class MessageTimeoutUtils {
038:
039: /**
040: * Attribute constants, to be moved into the core MTS (bug 3213).
041: */
042: private static final String SEND_TIMEOUT = "MessageSendTimeout";
043: private static final String SEND_DEADLINE = "MessageSendDeadline";
044:
045: private MessageTimeoutUtils() {
046: }
047:
048: /**
049: * Get the absolute deadline on an address, for example
050: * <code>1060280361356</code> milliseconds.
051: */
052: public static long getDeadline(MessageAddress addr) {
053: Number value = get(addr, SEND_DEADLINE);
054: return (value == null ? -1 : value.longValue());
055: }
056:
057: /** Tag an address with a relative timeout. */
058: public static MessageAddress setDeadline(MessageAddress addr,
059: long deadline) {
060: return (deadline <= 0 ? (addr) : set(addr, SEND_DEADLINE,
061: new Long(deadline)));
062: }
063:
064: /**
065: * Get the relative timeout on an address, for example
066: * <code>5000</code> milliseconds.
067: */
068: public static long getTimeout(MessageAddress addr) {
069: Number value = get(addr, SEND_TIMEOUT);
070: return (value == null ? -1 : value.longValue());
071: }
072:
073: /** Tag an address with a relative timeout. */
074: public static MessageAddress setTimeout(MessageAddress addr,
075: long timeout) {
076: int t = (int) timeout;
077: return (t <= 0 ? (addr) : set(addr, SEND_TIMEOUT,
078: new Integer(t)));
079: }
080:
081: // get a number attribute
082: private static Number get(MessageAddress addr, String name) {
083: if (addr == null) {
084: return null;
085: }
086: MessageAttributes attrs = addr.getMessageAttributes();
087: if (attrs == null) {
088: return null;
089: }
090: Object o = attrs.getAttribute(name);
091: if (!(o instanceof Number)) {
092: return null;
093: }
094: return ((Number) o);
095: }
096:
097: // set a number attribute
098: private static MessageAddress set(MessageAddress addr, String name,
099: Number value) {
100: if (addr == null) {
101: return null;
102: }
103: MessageAttributes attrs = addr.getMessageAttributes();
104: if (attrs == null) {
105: attrs = new SimpleMessageAttributes();
106: addr = MessageAddress.getMessageAddress(addr, attrs);
107: }
108: attrs.setAttribute(name, value);
109: return addr;
110: }
111: }
|