2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.protocol;
60 import java.util.Collection;
61 import net.jxta.document.ExtendableAdvertisement;
62 import net.jxta.endpoint.EndpointAddress;
63 import net.jxta.peer.PeerID;
65 import java.util.Enumeration;
66 import java.util.List;
67 import java.util.Vector;
71 * Provides a simple association of a {@code PeerID} to an ordered list of
72 * {@code EndpointAddress} entries. Each {@code EndpointAddress} defines one
73 * Message Transport address by which the peer may be reached. The addresses
74 * are sorted in the preferred order (which may refer to performance, cost,
75 * efficiency, etc.) which they should be used.
77 * The Access Point Advertisement is most commonly used as part of other
78 * Advertisements such as {@code RouteAdvertisement}.
80 * @see net.jxta.protocol.PeerAdvertisement
81 * @see net.jxta.protocol.RouteAdvertisement
83 public abstract class AccessPointAdvertisement extends ExtendableAdvertisement implements Cloneable {
86 * The peer id of the peer with these endpoints. May be {@code null}
87 * if the APA is used as a sub-element of a structure in which the context
88 * peerid is already known.
90 private PeerID pid = null;
93 * The EndpointAddresses associated with the specified peer in preferred
97 * <li>Values are, sadly, {@link java.lang.String} of
98 * {@link net.jxta.endpoint.EndpointAddress}.</li>
101 private Vector<String> endpointAddresses = new Vector<String>();
106 * <p/>Make a deep copy.
109 public AccessPointAdvertisement clone() {
111 AccessPointAdvertisement a = (AccessPointAdvertisement) super.clone();
113 a.setPeerID(getPeerID());
114 a.addEndpointAddresses(endpointAddresses);
117 } catch (CloneNotSupportedException impossible) {
118 throw new Error("Object.clone() threw CloneNotSupportedException", impossible);
125 * Equals means the same PID and the same endpoint addresses.
128 public boolean equals(Object target) {
130 if (this == target) {
134 if (!(target instanceof AccessPointAdvertisement)) {
138 AccessPointAdvertisement ap = (AccessPointAdvertisement) target;
140 if ((null == getPeerID()) && (null != ap.getPeerID())) {
144 if ((null != getPeerID())) {
145 if (!getPeerID().equals(ap.getPeerID())) {
149 if (endpointAddresses.size() != ap.endpointAddresses.size()) {
153 // XXX 20061127 bondolo This eventually should be an ordered comparison.
155 for (String anEA : endpointAddresses) {
156 if (!ap.endpointAddresses.contains(anEA)) {
168 public int hashCode() {
170 return pid.hashCode();
172 // force all incomplete advertisements to hash to the same place.
178 * Returns the identifying type of this Advertisement.
180 * @return String the type of advertisement
182 public static String getAdvertisementType() {
190 public final String getBaseAdvType() {
191 return getAdvertisementType();
195 * Gets the PeerID for this access point.
197 * @return PeerID The peer id associated with the endpoint addresses or
198 * {@code null} if no peer has been directly associated.
200 public PeerID getPeerID() {
205 * Sets the PeerID for this access point.
207 * @param pid The peer id associated with the endpoint addresses or
208 * {@code null} if no peer is directly associated.
210 public void setPeerID(PeerID pid) {
215 * Add all of the provided EndpointAddresses.
217 * @param addrs Add all of the specified endpoint addresses.
219 public void addEndpointAddresses(List<EndpointAddress> addrs) {
220 for (EndpointAddress addr : addrs) {
221 addEndpointAddress(addr);
226 * Clears all EndpointAddresses.
228 public void clearEndpointAddresses() {
229 endpointAddresses.clear();
233 * Remove the specified EndpointAddress.
235 * @param addr EndpointAddress to remove.
237 public void removeEndpointAddress(EndpointAddress addr) {
238 endpointAddresses.remove(addr.toString());
242 * Remove the specified EndpointAddresses.
244 * @param addrs EndpointAddresses to remove.
246 public void removeEndpointAddresses(Collection<EndpointAddress> addrs) {
247 for (EndpointAddress addr : addrs) {
248 endpointAddresses.remove(addr.toString());
253 * Returns the endpoint addresses associated with this access point.
255 * @return The endpoint addresses associated with this access point
256 * represented as {@link java.lang.String}.
258 public Enumeration<String> getEndpointAddresses() {
259 return endpointAddresses.elements();
263 * Returns the vector of endpoint addresses associated with this access
264 * point. The result is a vector of endpoint addresses represented as
265 * {@code String}. <strong>The Vector contains the "live" data of this
266 * advertisement. It should be modified only with great care.</strong>
268 * @return The endpoint addresses associated with this access point
269 * represented as {@link java.lang.String}.
270 * @deprecated Returning the Vector is dangerous and unwise. This feature
274 public Vector<String> getVectorEndpointAddresses() {
275 return endpointAddresses;
279 * Sets the list of endpoint addresses associated with this access point.
281 * @param addresses Vector of EndpointAddresses represented as
282 * {@link java.lang.String}. <b>The Vector is not copied!</b>
283 * @deprecated This method causes the AccessPointAdvertisement to reference
284 * the provided array. This means that subsequent changes to the array will
285 * alter the endpoint addresses which are part of the
286 * {@code AcccessPointAdvertisement}.
289 public void setEndpointAddresses(Vector<String> addresses) {
290 endpointAddresses = addresses;
294 * Add a new list of EndpointAddresses to the access point.
296 * @param addresses List of EndpointAddresses represented as
297 * {@link java.lang.String}.
299 * @deprecated Use {@link #addEndpointAddresses(List)} instead.
302 public void addEndpointAddresses(Vector<String> addresses) {
303 for (String toAdd : addresses) {
304 addEndpointAddress(toAdd);
309 * Add a new EndpointAddresses to the access point
311 * @param address An EndpointAddress
313 public void addEndpointAddress(EndpointAddress address) {
314 String toAdd = address.toString();
316 if (!endpointAddresses.contains(toAdd)) {
317 endpointAddresses.add(toAdd);
322 * add a new EndpointAddresses to the access point
324 * @param address EndpointAddress represented as {@link java.lang.String}.
326 public void addEndpointAddress(String address) {
327 if (!endpointAddresses.contains(address)) {
328 endpointAddresses.add(address);
333 * remove a list of EndpointAddresses from the access point
335 * @param addresses List of EndpointAddresses represented as
336 * {@link java.lang.String}.
338 public void removeEndpointAddresses(List<String> addresses) {
339 endpointAddresses.removeAll(addresses);
343 * return number of endpoint addresses
345 * @return size number of endpointAddress in the hop
348 return endpointAddresses.size();
352 * Check if the EndpointAddress is already associated with this access point
354 * @param addr endpoint address to check
355 * @return true if the EndpointAddress is already associated with this access point
357 public boolean contains(EndpointAddress addr) {
358 return endpointAddresses.contains(addr.toString());
362 * Generate a string that displays an access point
363 * information for logging or debugging purpose
365 * @return String return a string containing the access point advertisement
367 public String display() {
368 StringBuilder routeBuf = new StringBuilder();
370 routeBuf.append("PID=");
372 PeerID peerId = getPeerID();
374 if (peerId == null) {
375 routeBuf.append("<null>");
377 routeBuf.append(peerId.toString());
380 Enumeration e = getEndpointAddresses();
382 while (e.hasMoreElements()) {
383 routeBuf.append("\n Addr=").append(e.nextElement());
385 return routeBuf.toString();