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.impl.protocol;
60 import net.jxta.document.Advertisement;
61 import net.jxta.document.AdvertisementFactory;
62 import net.jxta.document.Attribute;
63 import net.jxta.document.Document;
64 import net.jxta.document.Element;
65 import net.jxta.document.MimeMediaType;
66 import net.jxta.document.StructuredDocument;
67 import net.jxta.document.StructuredDocumentUtils;
68 import net.jxta.document.XMLElement;
69 import net.jxta.id.IDFactory;
70 import net.jxta.logging.Logging;
71 import net.jxta.peer.PeerID;
72 import net.jxta.protocol.AccessPointAdvertisement;
73 import net.jxta.protocol.RouteAdvertisement;
76 import java.net.URISyntaxException;
77 import java.util.Enumeration;
78 import java.util.Vector;
79 import java.util.logging.Level;
80 import java.util.logging.Logger;
84 * This class implements the basic Route advertisement.
87 * <xs:complexType name="RA">
89 * <xs:element name="DstPID" type="jxta:JXTAID" minOccurs="0"/>
90 * <xs:element name="Dst">
93 * <xs:element ref="jxta:APA" />
95 * </xs:complexType>
97 * <xs:element name="Hops" minOccurs="0">
100 * <xs:element ref="jxta:APA" maxOccurs="unbounded" />
102 * </xs:complexType>
105 * </xs:complexType>
108 * @see net.jxta.protocol.RouteAdvertisement
110 public class RouteAdv extends RouteAdvertisement implements Cloneable {
115 private static final Logger LOG = Logger.getLogger(RouteAdv.class.getName());
117 private static final String[] INDEX_FIELDS = {DEST_PID_TAG};
120 * Instantiator for our advertisement
122 public static class Instantiator implements AdvertisementFactory.Instantiator {
127 public String getAdvertisementType() {
128 return RouteAdv.getAdvertisementType();
134 public Advertisement newInstance() {
135 return new RouteAdv();
141 public Advertisement newInstance(Element root) {
142 if (!XMLElement.class.isInstance(root)) {
143 throw new IllegalArgumentException(getAdvertisementType() + " only supports XLMElement");
146 return new RouteAdv((XMLElement) root);
151 * Private constructor. Use instantiator
153 private RouteAdv() {}
156 * Private constructor. Use instantiator
158 * @param doc the element
160 private RouteAdv(XMLElement doc) {
161 String doctype = doc.getName();
163 String typedoctype = "";
164 Attribute itsType = doc.getAttribute("type");
166 if (null != itsType) {
167 typedoctype = itsType.getValue();
170 if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
171 throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
174 Enumeration<XMLElement> elements = doc.getChildren();
176 while (elements.hasMoreElements()) {
177 XMLElement elem = elements.nextElement();
179 if (!handleElement(elem)) {
180 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
181 LOG.fine("Unhandled Element: " + elem.toString());
186 // Compatibility hack
187 setDestPeerID(getDestPeerID());
191 throw new IllegalArgumentException("Route contains a loop!");
199 public RouteAdv clone() {
200 return (RouteAdv) super.clone();
207 public String getAdvType() {
208 return getAdvertisementType();
215 protected boolean handleElement(Element raw) {
217 if (super.handleElement(raw)) {
221 XMLElement elem = (XMLElement) raw;
223 if (DEST_PID_TAG.equals(elem.getName())) {
225 URI pID = new URI(elem.getTextValue());
227 setDestPeerID((PeerID) IDFactory.fromURI(pID));
228 } catch (URISyntaxException badID) {
229 throw new IllegalArgumentException("Bad PeerID in advertisement");
230 } catch (ClassCastException badID) {
231 throw new IllegalArgumentException("ID in advertisement is not a peer id");
236 if ("Dst".equals(elem.getName())) {
237 for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements();) {
238 XMLElement aXpt = (XMLElement) eachXpt.nextElement();
240 AccessPointAdvertisement xptAdv = (AccessPointAdvertisement)
241 AdvertisementFactory.newAdvertisement(aXpt);
248 if ("Hops".equals(elem.getName())) {
249 Vector<AccessPointAdvertisement> hops = new Vector<AccessPointAdvertisement>();
251 for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements();) {
252 XMLElement aXpt = (XMLElement) eachXpt.nextElement();
254 AccessPointAdvertisement xptAdv = (AccessPointAdvertisement)
255 AdvertisementFactory.newAdvertisement(aXpt);
257 hops.addElement(xptAdv);
270 public Document getDocument(MimeMediaType encodeAs) {
271 StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
274 throw new IllegalStateException("I won't write a doc for a route with a loop");
277 PeerID pid = getDestPeerID();
280 Element e0 = adv.createElement(DEST_PID_TAG, pid.toString());
285 Element e1 = adv.createElement("Dst");
289 AccessPointAdvertisement dest = getDest();
291 // create a copy without the PID if necessary (the pid is redundant)
292 AccessPointAdvertisement destAPA = dest;
293 if(null != dest.getPeerID()) {
294 destAPA = dest.clone();
295 destAPA.setPeerID(null);
298 StructuredDocument xptDoc = (StructuredDocument) destAPA.getDocument(encodeAs);
299 StructuredDocumentUtils.copyElements(adv, e1, xptDoc);
301 Enumeration<AccessPointAdvertisement> eachHop = getHops();
303 // only include hops if we have some
304 if (eachHop.hasMoreElements()) {
305 Element e2 = adv.createElement("Hops");
309 while (eachHop.hasMoreElements()) {
310 AccessPointAdvertisement hop = eachHop.nextElement();
312 if (null == hop.getPeerID()) {
313 // Refuse to write illegal hops.
317 xptDoc = (StructuredDocument) hop.getDocument(encodeAs);
319 StructuredDocumentUtils.copyElements(adv, e2, xptDoc);
329 public String[] getIndexFields() {