]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/RdvAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / RdvAdv.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
63 import java.net.URISyntaxException;
64
65 import java.util.logging.Level;
66 import net.jxta.logging.Logging;
67 import java.util.logging.Logger;
68
69 import net.jxta.document.Advertisement;
70 import net.jxta.document.AdvertisementFactory;
71 import net.jxta.document.Attribute;
72 import net.jxta.document.Document;
73 import net.jxta.document.Element;
74 import net.jxta.document.MimeMediaType;
75 import net.jxta.document.StructuredDocumentUtils;
76 import net.jxta.document.StructuredDocument;
77 import net.jxta.document.StructuredTextDocument;
78 import net.jxta.document.XMLElement;
79 import net.jxta.id.IDFactory;
80 import net.jxta.peer.PeerID;
81 import net.jxta.peergroup.PeerGroupID;
82 import net.jxta.protocol.RdvAdvertisement;
83 import net.jxta.protocol.RouteAdvertisement;
84
85
86 /**
87  * This class implements the RdvAdvertisement.
88  *
89  * <p/><pre>
90  *   &lt;xs:complexType name="RdvAdvertisement">
91  *     &lt;xs:sequence>
92  *       &lt;xs:element name="RdvGroupId" type="jxta:JXTAID"/>
93  *       &lt;xs:element name="RdvPeerId" type="jxta:JXTAID"/>
94  *       &lt;xs:element name="RdvServiceName" type="xs:string"/>
95  *       &lt;xs:element name="Name" type="xs:string" minOccurs="0"/>
96  *       &lt;!-- This should be a route -->
97  *       &lt;xs:element name="RdvRoute" type="xs:anyType" minOccurs="0"/>
98  *     &lt;/xs:sequence>
99  *   &lt;/xs:complexType>
100  * </pre>
101  **/
102 public class RdvAdv extends RdvAdvertisement {
103     
104     /**
105      *  Log4J Logger
106      **/
107     private final static transient Logger LOG = Logger.getLogger(RdvAdv.class.getName());   
108     
109     private static final String[] INDEX_FIELDS = { PeerIDTag, ServiceNameTag, GroupIDTag };
110     
111     /**
112      * Instantiator for our advertisement
113      **/
114     public static class Instantiator implements AdvertisementFactory.Instantiator {
115         
116         /**
117          * {@inheritDoc}
118          **/
119         public String getAdvertisementType() {
120             return RdvAdv.getAdvertisementType();
121         }
122         
123         /**
124          * {@inheritDoc}
125          **/
126         public Advertisement newInstance() {
127             return new RdvAdv();
128         }
129         
130         /**
131          * {@inheritDoc}
132          **/
133         public Advertisement newInstance(Element root) {
134             if (!XMLElement.class.isInstance(root)) {
135                 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
136             }
137         
138             return new RdvAdv((XMLElement) root);
139         }
140     }
141     
142     /**
143      *  Private constructor for new instances. Use the instantiator.
144      */
145     private RdvAdv() {
146     }
147
148     /**
149      *  Private constructor for xml serialized instances. Use the instantiator.
150      *  
151      *  @param doc The XML serialization of the advertisement.
152      */
153     private RdvAdv(XMLElement doc) {
154         String doctype = doc.getName();
155         
156         String typedoctype = "";
157         Attribute itsType = doc.getAttribute("type");
158
159         if (null != itsType) {
160             typedoctype = itsType.getValue();
161         }
162         
163         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
164             throw new IllegalArgumentException(
165                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
166         }
167         
168         Enumeration elements = doc.getChildren();
169         
170         while (elements.hasMoreElements()) {
171             XMLElement elem = (XMLElement) elements.nextElement();
172             
173             if (!handleElement(elem)) {
174                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
175                     LOG.fine("Unhandled Element: " + elem.toString());
176                 }
177             }
178         }
179         
180         // Sanity Check!!!
181         if (null == getGroupID()) {
182             throw new IllegalArgumentException("Missing peer group ID");
183         }
184         
185         if (null == getPeerID()) {
186             throw new IllegalArgumentException("Missing peer ID");
187         }
188         
189         if (null == getServiceName()) {
190             throw new IllegalArgumentException("Missing service name");
191         }
192     }
193     
194     /**
195      * {@inheritDoc}
196      */
197     @Override
198     public String getAdvType() {
199         return getAdvertisementType();
200     }
201
202     /**
203      *  {@inheritDoc}
204      **/
205     @Override
206     protected boolean handleElement(Element raw) {
207         
208         if (super.handleElement(raw)) {
209             return true;
210         }
211         
212         XMLElement elem = (XMLElement) raw;
213         
214         if (elem.getName().equals(RdvAdvertisement.GroupIDTag)) {
215             try {
216                 URI groupID = new URI(elem.getTextValue().trim());
217
218                 setGroupID((PeerGroupID) IDFactory.fromURI(groupID));
219             } catch (URISyntaxException badID) {
220                 throw new IllegalArgumentException("Bad group ID in advertisement");
221             } catch (ClassCastException badID) {
222                 throw new IllegalArgumentException("ID is not a group ID");
223             }
224             return true;
225         }
226         
227         if (elem.getName().equals(RdvAdvertisement.PeerIDTag)) {
228             try {
229                 URI peerID = new URI(elem.getTextValue().trim());
230
231                 setPeerID((PeerID) IDFactory.fromURI(peerID));
232             } catch (URISyntaxException badID) {
233                 throw new IllegalArgumentException("Bad group ID in advertisement");
234             } catch (ClassCastException badID) {
235                 throw new IllegalArgumentException("ID is not a group ID");
236             }
237             return true;
238         }
239         
240         if (elem.getName().equals(RdvAdvertisement.ServiceNameTag)) {
241             setServiceName(elem.getTextValue());
242             return true;
243         }
244         
245         if (elem.getName().equals(RdvAdvertisement.RouteTag)) {
246             for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements();) {
247                 
248                 XMLElement aXpt = (XMLElement) eachXpt.nextElement();
249                 
250                 RouteAdvertisement xptAdv = (RouteAdvertisement)
251                         AdvertisementFactory.newAdvertisement(aXpt);
252
253                 setRouteAdv(xptAdv);
254             }
255             return true;
256         }
257         
258         if (elem.getName().equals(RdvAdvertisement.NameTag)) {
259             setName(elem.getTextValue());
260             return true;
261         }
262         
263         return false;
264     }
265     
266     /**
267      *  {@inheritDoc}
268      **/
269     @Override
270     public Document getDocument(MimeMediaType encodeAs) {
271         
272         // Sanity Check!!!
273         if (null == getGroupID()) {
274             throw new IllegalStateException("Missing peer group ID");
275         }
276         
277         if (null == getPeerID()) {
278             throw new IllegalStateException("Missing peer ID");
279         }
280         
281         if (null == getServiceName()) {
282             throw new IllegalStateException("Missing service name");
283         }
284
285         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
286         
287         Element e = adv.createElement(RdvAdvertisement.GroupIDTag, getGroupID().toString());
288
289         adv.appendChild(e);
290         
291         e = adv.createElement(RdvAdvertisement.PeerIDTag, getPeerID().toString());
292         adv.appendChild(e);
293         
294         e = adv.createElement(RdvAdvertisement.ServiceNameTag, getServiceName());
295         adv.appendChild(e);
296         
297         String peerName = getName();
298
299         if (null != peerName) {
300             e = adv.createElement(RdvAdvertisement.NameTag, getName());
301             adv.appendChild(e);
302         }
303         
304         if (getRouteAdv() != null) {
305             Element el = adv.createElement(RdvAdvertisement.RouteTag);
306
307             adv.appendChild(el);
308             
309             StructuredTextDocument xptDoc = (StructuredTextDocument)
310                     getRouteAdv().getDocument(encodeAs);
311
312             StructuredDocumentUtils.copyElements(adv, el, xptDoc);
313         }
314         
315         return adv;
316     }
317     
318     /**
319      *  {@inheritDoc}
320      **/
321     @Override
322     public String[] getIndexFields() {
323         return INDEX_FIELDS;
324     }
325 }