001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.mts;
028:
029: import java.io.IOException;
030: import java.io.ObjectInput;
031: import java.io.ObjectOutput;
032:
033: import org.cougaar.util.log.Logger;
034: import org.cougaar.util.log.Logging;
035:
036: /**
037: * A wrapped {@link MessageAddress} with added {@link
038: * MessageAttributes}.
039: */
040: public class MessageAddressWithAttributes extends MessageAddress {
041: private transient MessageAddress delegate;
042: private transient MessageAttributes attributes;
043:
044: public MessageAddressWithAttributes() {
045: }
046:
047: protected MessageAddressWithAttributes(MessageAddress delegate,
048: MessageAttributes attributes) {
049: this .delegate = delegate;
050: this .attributes = attributes;
051: }
052:
053: /** @deprecated Why would you want a MessageAddress that only has attributes? */
054: protected MessageAddressWithAttributes(MessageAttributes attributes) {
055: Logger logger = Logging.getLogger(getClass().getName());
056: if (logger.isErrorEnabled())
057: logger
058: .error("Creating a MessageAddress with attributes but no name!");
059: delegate = null;
060: this .attributes = attributes;
061: }
062:
063: protected MessageAddressWithAttributes(String addr,
064: MessageAttributes attrs) {
065: delegate = MessageAddress.getMessageAddress(addr);
066: attributes = attrs;
067: }
068:
069: /** @return The MessageAddress without the MessageAtributes */
070: public final MessageAddress getPrimary() {
071: return delegate == null ? null : delegate.getPrimary();
072: }
073:
074: /**
075: * @return The Parent MessageAddress. This is usually the same
076: * as the result of getPrimary();
077: */
078: public final MessageAddress getDelegate() {
079: return delegate;
080: }
081:
082: public final String toAddress() {
083: return (delegate == null) ? null : delegate.toAddress();
084: }
085:
086: public final MessageAttributes getMessageAttributes() {
087: return attributes;
088: }
089:
090: public static final MessageAddress getMessageAddressWithAttributes(
091: MessageAddress ma, MessageAttributes mas) {
092: return new MessageAddressWithAttributes(ma, mas);
093: }
094:
095: public static final MessageAddress getMessageAddressWithAttributes(
096: String address, MessageAttributes mas) {
097: MessageAddress ma = MessageAddress.getMessageAddress(address);
098: return new MessageAddressWithAttributes(ma, mas);
099: }
100:
101: /** @deprecated Why would you want a MessageAddress that only has attributes? */
102: public static final MessageAddress getMessageAddressWithAttributes(
103: MessageAttributes mas) {
104: return new MessageAddressWithAttributes(mas);
105: }
106:
107: //
108: // io
109: //
110:
111: // should never be used - see writeReplace
112: public void writeExternal(ObjectOutput out) throws IOException {
113: out.writeObject(delegate);
114: // attributes are transient
115: }
116:
117: public void readExternal(ObjectInput in)
118: throws ClassNotFoundException, IOException {
119: delegate = (MessageAddress) in.readObject();
120: // attributes are transient
121: }
122:
123: private Object writeReplace() {
124: return delegate;
125: }
126:
127: }
|