]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/RouteAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / RouteAdv.java
1 /*
2 Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.protocol;
58
59
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;
74
75 import java.net.URI;
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;
81
82
83 /**
84  * This class implements the basic Route advertisement.
85  * <p/>
86  * <pre>
87  *    &lt;xs:complexType name="RA">
88  *      &lt;xs:sequence>
89  *          &lt;xs:element name="DstPID" type="jxta:JXTAID" minOccurs="0"/>
90  *          &lt;xs:element name="Dst">
91  *              &lt;xs:complexType>
92  *            &lt;xs:sequence>
93  *                      &lt;xs:element ref="jxta:APA" />
94  *            &lt;/xs:sequence>
95  *              &lt;/xs:complexType>
96  *        &lt;/xs:element>
97  *        &lt;xs:element name="Hops" minOccurs="0">
98  *              &lt;xs:complexType>
99  *            &lt;xs:sequence>
100  *                      &lt;xs:element ref="jxta:APA" maxOccurs="unbounded" />
101  *            &lt;/xs:sequence>
102  *              &lt;/xs:complexType>
103  *        &lt;/xs:element>
104  *      &lt;/xs:sequence>
105  *    &lt;/xs:complexType>
106  * </pre>
107  *
108  * @see net.jxta.protocol.RouteAdvertisement
109  */
110 public class RouteAdv extends RouteAdvertisement implements Cloneable {
111
112     /**
113      * Logger
114      */
115     private static final Logger LOG = Logger.getLogger(RouteAdv.class.getName());
116
117     private static final String[] INDEX_FIELDS = {DEST_PID_TAG};
118
119     /**
120      * Instantiator for our advertisement
121      */
122     public static class Instantiator implements AdvertisementFactory.Instantiator {
123
124         /**
125          * {@inheritDoc}
126          */
127         public String getAdvertisementType() {
128             return RouteAdv.getAdvertisementType();
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         public Advertisement newInstance() {
135             return new RouteAdv();
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         public Advertisement newInstance(Element root) {
142             if (!XMLElement.class.isInstance(root)) {
143                 throw new IllegalArgumentException(getAdvertisementType() + " only supports XLMElement");
144             }
145
146             return new RouteAdv((XMLElement) root);
147         }
148     }
149
150     /**
151      * Private constructor. Use instantiator
152      */
153     private RouteAdv() {}
154
155     /**
156      * Private constructor. Use instantiator
157      *
158      * @param doc the element
159      */
160     private RouteAdv(XMLElement doc) {
161         String doctype = doc.getName();
162
163         String typedoctype = "";
164         Attribute itsType = doc.getAttribute("type");
165
166         if (null != itsType) {
167             typedoctype = itsType.getValue();
168         }
169
170         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
171             throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
172         }
173
174         Enumeration<XMLElement> elements = doc.getChildren();
175
176         while (elements.hasMoreElements()) {
177             XMLElement elem = elements.nextElement();
178
179             if (!handleElement(elem)) {
180                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
181                     LOG.fine("Unhandled Element: " + elem.toString());
182                 }
183             }
184         }
185
186         // Compatibility hack
187         setDestPeerID(getDestPeerID());
188
189         // Sanity Check!!!
190         if (hasALoop()) {
191             throw new IllegalArgumentException("Route contains a loop!");
192         }
193     }
194
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public RouteAdv clone() {
200         return (RouteAdv) super.clone();
201     }
202
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     public String getAdvType() {
208         return getAdvertisementType();
209     }
210
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     protected boolean handleElement(Element raw) {
216
217         if (super.handleElement(raw)) {
218             return true;
219         }
220
221         XMLElement elem = (XMLElement) raw;
222
223         if (DEST_PID_TAG.equals(elem.getName())) {
224             try {
225                 URI pID = new URI(elem.getTextValue());
226
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");
232             }
233             return true;
234         }
235
236         if ("Dst".equals(elem.getName())) {
237             for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements();) {
238                 XMLElement aXpt = (XMLElement) eachXpt.nextElement();
239
240                 AccessPointAdvertisement xptAdv = (AccessPointAdvertisement)
241                         AdvertisementFactory.newAdvertisement(aXpt);
242
243                 setDest(xptAdv);
244             }
245             return true;
246         }
247
248         if ("Hops".equals(elem.getName())) {
249             Vector<AccessPointAdvertisement> hops = new Vector<AccessPointAdvertisement>();
250
251             for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements();) {
252                 XMLElement aXpt = (XMLElement) eachXpt.nextElement();
253
254                 AccessPointAdvertisement xptAdv = (AccessPointAdvertisement)
255                         AdvertisementFactory.newAdvertisement(aXpt);
256
257                 hops.addElement(xptAdv);
258             }
259             setHops(hops);
260             return true;
261         }
262
263         return false;
264     }
265
266     /**
267      * {@inheritDoc}
268      */
269     @Override
270     public Document getDocument(MimeMediaType encodeAs) {
271         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
272
273         if (hasALoop()) {
274             throw new IllegalStateException("I won't write a doc for a route with a loop");
275         }
276
277         PeerID pid = getDestPeerID();
278
279         if (null != pid) {
280             Element e0 = adv.createElement(DEST_PID_TAG, pid.toString());
281
282             adv.appendChild(e0);
283         }
284
285         Element e1 = adv.createElement("Dst");
286
287         adv.appendChild(e1);
288
289         AccessPointAdvertisement dest = getDest();
290
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);
296         }
297         
298         StructuredDocument xptDoc = (StructuredDocument) destAPA.getDocument(encodeAs);
299         StructuredDocumentUtils.copyElements(adv, e1, xptDoc);
300
301         Enumeration<AccessPointAdvertisement> eachHop = getHops();
302
303         // only include hops if we have some
304         if (eachHop.hasMoreElements()) {            
305             Element e2 = adv.createElement("Hops");
306
307             adv.appendChild(e2);
308
309             while (eachHop.hasMoreElements()) {
310                 AccessPointAdvertisement hop = eachHop.nextElement();
311                 
312                 if (null == hop.getPeerID()) {
313                     // Refuse to write illegal hops.
314                     continue;
315                 }
316
317                 xptDoc = (StructuredDocument) hop.getDocument(encodeAs);
318                     
319                 StructuredDocumentUtils.copyElements(adv, e2, xptDoc);
320             }
321         }
322         return adv;
323     }
324
325     /**
326      * {@inheritDoc}
327      */
328     @Override
329     public String[] getIndexFields() {
330         return INDEX_FIELDS;
331     }
332 }