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.bootstrap;
028:
029: import java.io.InputStream;
030: import java.io.BufferedReader;
031: import java.io.Serializable;
032: import java.util.Map;
033: import org.cougaar.core.util.UID;
034:
035: /**
036: * Bootstrap data for a single agent, including its {@link
037: * org.cougaar.core.service.wp.AddressEntry}s.
038: * <p>
039: * The {@link UID} is an optional tag to track version changes. If
040: * the server changes the data then the server-side UID for the bundle
041: * will also change.
042: * <p>
043: * This class mirrors the resolver's {@link
044: * org.cougaar.core.wp.resolver.Record} class, but is kept separate
045: * to allow greater flexability.
046: */
047: public final class Bundle implements Serializable {
048:
049: private final String name;
050: private final UID uid;
051: private final long ttd;
052: private final Map entries;
053:
054: public Bundle(String name, UID uid, long ttd, Map entries) {
055: this .name = name;
056: this .uid = uid;
057: this .ttd = ttd;
058: this .entries = entries;
059: }
060:
061: /**
062: * The agent name.
063: */
064: public String getName() {
065: return name;
066: }
067:
068: /**
069: * The UID of the Bundle, which is an optional version tracker.
070: */
071: public UID getUID() {
072: return uid;
073: }
074:
075: /**
076: * The expiration "time-to-death" relative to the base timestamp,
077: * or negative if this is a proposed modification.
078: */
079: public long getTTD() {
080: return ttd;
081: }
082:
083: /**
084: * AddressEntries for this bundle.
085: * <p>
086: * The entries must have the same name.
087: */
088: public Map getEntries() {
089: return entries;
090: }
091:
092: public boolean equals(Object o) {
093: if (o == this ) {
094: return true;
095: }
096: if (!(o instanceof Bundle)) {
097: return false;
098: }
099: Bundle b = (Bundle) o;
100: return (uid == null ? b.uid == null : uid.equals(b.uid))
101: && ttd == b.ttd
102: && (entries == null ? b.entries == null : entries
103: .equals(b.entries));
104: }
105:
106: public int hashCode() {
107: return uid.hashCode();
108: }
109:
110: public String toString() {
111: return "(bundle " + encode() + ")";
112: }
113:
114: public String encode() {
115: return BundleEncoder.encodeBundle(this );
116: }
117:
118: public static Bundle decode(String s) {
119: return BundleDecoder.decodeBundle(s);
120: }
121:
122: public static Map decodeAll(InputStream is) throws Exception {
123: return BundleDecoder.decodeBundles(is);
124: }
125:
126: public static Map decodeAll(BufferedReader br) throws Exception {
127: return BundleDecoder.decodeBundles(br);
128: }
129: }
|