]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/PeerAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / PeerAdv.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 java.net.URI;
61 import java.util.Enumeration;
62 import java.util.Hashtable;
63
64 import java.net.URISyntaxException;
65
66 import java.util.logging.Level;
67 import net.jxta.logging.Logging;
68 import java.util.logging.Logger;
69
70 import net.jxta.document.Advertisement;
71 import net.jxta.document.AdvertisementFactory;
72 import net.jxta.document.Attribute;
73 import net.jxta.document.Document;
74 import net.jxta.document.Element;
75 import net.jxta.document.MimeMediaType;
76 import net.jxta.document.StructuredDocument;
77 import net.jxta.document.StructuredDocumentUtils;
78 import net.jxta.document.XMLElement;
79 import net.jxta.id.ID;
80 import net.jxta.id.IDFactory;
81 import net.jxta.peer.PeerID;
82 import net.jxta.peergroup.PeerGroupID;
83 import net.jxta.platform.ModuleClassID;
84 import net.jxta.protocol.PeerAdvertisement;
85
86
87 /**
88  * Implementation of {@link PeerAdvertisement} matching the standard JXTA
89  * Protocol Specification.
90  *
91  * It implements Peer Advertisement using the following schema:
92  *
93  * <pre><tt>
94  * &lt;xs:complexType name="PA">
95  *   &lt;xs:sequence>
96  *     &lt;xs:element name="PID" type="JXTAID"/>
97  *     &lt;xs:element name="GID" type="JXTAID"/>
98  *     &lt;xs:element name="Name" type="xs:string" minOccurs="0"/>
99  *     &lt;xs:element name="Desc" type="xs:anyType" minOccurs="0"/>
100  *     &lt;xs:element name="Svc" type="jxta:serviceParams" minOccurs="0" maxOccurs="unbounded"/>
101  *   &lt;xs:sequence>
102  * &lt;/xs:complexType>
103  * </tt></pre>
104  *
105  * @see net.jxta.protocol.PeerAdvertisement
106  * @see <a href="https://jxta-spec.dev.java.net/nonav/JXTAProtocols.html#advert-pa" target="_blank">JXTA Protocols Specification : Peer Advertisement</a>
107  **/
108 public class PeerAdv extends PeerAdvertisement {
109
110     /**
111      *  Logger
112      **/
113     private static final Logger LOG = Logger.getLogger(PeerAdv.class.getName());
114     
115     private static final String pidTag = "PID";
116     private static final String gidTag = "GID";
117     private static final String nameTag = "Name";
118     private static final String descTag = "Desc";
119     private static final String svcTag = "Svc";
120     private static final String mcidTag = "MCID";
121     private static final String paramTag = "Parm";
122     private static final String[] fields = { nameTag, pidTag };
123     
124     /**
125      *  Creates instances of PeerAdvertisement.
126      **/
127     public static class Instantiator implements AdvertisementFactory.Instantiator {
128
129         /**
130          *  {@inheritDoc}
131          **/
132         public String getAdvertisementType() {
133             return PeerAdvertisement.getAdvertisementType();
134         }
135         
136         /**
137          *  {@inheritDoc}
138          **/
139         public Advertisement newInstance() {
140             return new PeerAdv();
141         }
142         
143         /**
144          *  {@inheritDoc}
145          **/
146         public Advertisement newInstance(Element root) {
147             if (!XMLElement.class.isInstance(root)) {
148                 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
149             }
150         
151             return new PeerAdv((XMLElement) root);
152         }
153     }
154
155     /**
156      *  Private constructor for new instances. Use the instantiator.
157      */
158     private PeerAdv() {}
159
160     /**
161      *  Private constructor for xml serialized instances. Use the instantiator.
162      *  
163      *  @param doc The XML serialization of the advertisement.
164      */
165     private PeerAdv(XMLElement doc) {
166         String doctype = doc.getName();
167         
168         String typedoctype = "";
169         Attribute itsType = doc.getAttribute("type");
170
171         if (null != itsType) {
172             typedoctype = itsType.getValue();
173         }
174         
175         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
176             throw new IllegalArgumentException(
177                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
178         }
179         
180         Enumeration elements = doc.getChildren();
181         
182         while (elements.hasMoreElements()) {
183             XMLElement elem = (XMLElement) elements.nextElement();
184             
185             if (!handleElement(elem)) {
186                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
187                     LOG.fine("Unhandled Element: " + elem.toString());
188                 }
189             }
190         }
191         
192         // Sanity Check!!!
193         
194         // sanity check time!
195         if (null == getPeerID()) {
196             throw new IllegalArgumentException("Peer Advertisement did not contain a peer id.");
197         }
198         
199         if (null == getPeerGroupID()) {
200             throw new IllegalArgumentException("Peer Advertisement did not contain a peer group id.");
201         }
202     }
203     
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public String getAdvType() {
209         return getAdvertisementType();
210     }
211
212     /**
213      *  {@inheritDoc}
214      **/
215     @Override
216     protected boolean handleElement(Element raw) {
217         
218         if (super.handleElement(raw)) {
219             return true;
220         }
221         
222         XMLElement elem = (XMLElement) raw;
223         
224         if (elem.getName().equals(pidTag)) {
225             try {
226                 URI pID = new URI(elem.getTextValue());
227
228                 setPeerID((PeerID) IDFactory.fromURI(pID));
229             } catch (URISyntaxException badID) {
230                 throw new IllegalArgumentException("Bad PeerID ID in advertisement: " + elem.getTextValue());
231             } catch (ClassCastException badID) {
232                 throw new IllegalArgumentException("Id is not a peer id: " + elem.getTextValue());
233             }
234             return true;
235         }
236         
237         if (elem.getName().equals(gidTag)) {
238             try {
239                 URI gID = new URI(elem.getTextValue());
240
241                 setPeerGroupID((PeerGroupID) IDFactory.fromURI(gID));
242             } catch (URISyntaxException badID) {
243                 throw new IllegalArgumentException("Bad PeerGroupID in advertisement: " + elem.getTextValue());
244             } catch (ClassCastException badID) {
245                 throw new IllegalArgumentException("Id is not a group id: " + elem.getTextValue());
246             }
247             return true;
248         }
249
250         if (elem.getName().equals(nameTag)) {
251             setName(elem.getTextValue());
252             return true;
253         }
254         
255         if (elem.getName().equals(descTag)) {
256             setDesc(elem);
257             return true;
258         }
259         
260         if (elem.getName().equals(svcTag)) {
261             Enumeration elems = elem.getChildren();
262             ModuleClassID classID = null;
263             Element param = null;
264
265             while (elems.hasMoreElements()) {
266                 XMLElement e = (XMLElement) elems.nextElement();
267
268                 if (e.getName().equals(mcidTag)) {
269                     try {
270                         URI mcid = new URI(e.getTextValue());
271
272                         classID = (ModuleClassID) IDFactory.fromURI(mcid);
273                     } catch (URISyntaxException badID) {
274                         throw new IllegalArgumentException("Unusable ModuleClassID in advertisement: " + e.getTextValue());
275                     } catch (ClassCastException badID) {
276                         throw new IllegalArgumentException("Id is not a ModuleClassID: " + e.getTextValue());
277                     }
278                     continue;
279                 }
280                 if (e.getName().equals(paramTag)) {
281                     param = e;
282                 }
283             }
284             if (classID != null && param != null) {
285                 // Add this param to the table. putServiceParam()
286                 // clones param into a standalone document automatically.
287                 // (classID gets cloned too).
288                 putServiceParam(classID, param);
289             }
290             return true;
291         }
292         
293         // element was not handled
294         return false;
295     }
296     
297     /**
298      *  {@inheritDoc}
299      **/
300     @Override
301     public Document getDocument(MimeMediaType encodeAs) {
302         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
303         
304         PeerID peerID = getPeerID();
305
306         if ((null == peerID) || ID.nullID.equals(peerID)) {
307             throw new IllegalStateException("Cannot generate Peer Advertisement with no Peer ID!");
308         }
309         Element e = adv.createElement(pidTag, peerID.toString());
310
311         adv.appendChild(e);
312         
313         PeerGroupID groupID = getPeerGroupID();
314
315         if ((null == groupID) || ID.nullID.equals(groupID)) {
316             throw new IllegalStateException("Cannot generate Peer Advertisement with no group ID!");
317         } else {
318             e = adv.createElement(gidTag, groupID.toString());
319             adv.appendChild(e);
320         }
321         
322         // name is optional
323         if (getName() != null) {
324             e = adv.createElement(nameTag, getName());
325             adv.appendChild(e);
326         }
327         
328         // desc is optional
329         StructuredDocument desc = getDesc();
330
331         if (desc != null) {
332             StructuredDocumentUtils.copyElements(adv, adv, desc);
333         }
334         
335         // service params are optional
336         // FIXME: this is inefficient - we force our base class to make
337         // a deep clone of the table.
338         Hashtable serviceParams = getServiceParams();
339         Enumeration classIds = serviceParams.keys();
340
341         while (classIds.hasMoreElements()) {
342             ModuleClassID classId = (ModuleClassID) classIds.nextElement();
343             
344             Element s = adv.createElement(svcTag);
345
346             adv.appendChild(s);
347             
348             e = adv.createElement(mcidTag, classId.toString());
349             s.appendChild(e);
350             
351             e = (Element) serviceParams.get(classId);
352             StructuredDocumentUtils.copyElements(adv, s, e, paramTag);
353         }
354         return adv;
355     }
356     
357     /**
358      *  {@inheritDoc}
359      **/
360     @Override
361     public String[] getIndexFields() {
362         return fields;
363     }
364 }