]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/PeerGroupAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / PeerGroupAdv.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.TextElement;
69 import net.jxta.document.XMLElement;
70 import net.jxta.id.IDFactory;
71 import net.jxta.logging.Logging;
72 import net.jxta.peergroup.PeerGroupID;
73 import net.jxta.platform.ModuleClassID;
74 import net.jxta.platform.ModuleSpecID;
75 import net.jxta.protocol.PeerGroupAdvertisement;
76
77 import java.net.URI;
78 import java.net.URISyntaxException;
79 import java.util.Enumeration;
80 import java.util.Hashtable;
81 import java.util.logging.Level;
82 import java.util.logging.Logger;
83
84
85 public class PeerGroupAdv extends PeerGroupAdvertisement {
86
87     /**
88      * Log4J Logger
89      */
90     private static final Logger LOG = Logger.getLogger(PeerGroupAdv.class.getName());
91
92     private static final String nameTag = "Name";
93     private static final String gidTag = "GID";
94     private static final String descTag = "Desc";
95     private static final String msidTag = "MSID";
96     private static final String svcTag = "Svc";
97     private static final String mcidTag = "MCID";
98     private static final String paramTag = "Parm";
99     private static final String[] fields = { nameTag, gidTag, descTag, msidTag};
100
101     public static class Instantiator implements AdvertisementFactory.Instantiator {
102
103         /**
104          * {@inheritDoc}
105          */
106
107         public String getAdvertisementType() {
108             return PeerGroupAdv.getAdvertisementType();
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         public Advertisement newInstance() {
115             return new PeerGroupAdv();
116         }
117
118         /**
119          * {@inheritDoc}
120          */
121         public Advertisement newInstance(net.jxta.document.Element root) {
122             return new PeerGroupAdv(root);
123         }
124     }
125
126     /**
127      * Use the Instantiator method to construct Peer Group Advs.
128      */
129     private PeerGroupAdv() {}
130
131     /**
132      * Use the Instantiator method to construct Peer Group Advs.
133      *
134      * @param root the element
135      */
136     private PeerGroupAdv(Element root) {
137         if (!XMLElement.class.isInstance(root)) {
138             throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
139         }
140
141         XMLElement doc = (XMLElement) root;
142
143         String doctype = doc.getName();
144
145         String typedoctype = "";
146         Attribute itsType = doc.getAttribute("type");
147
148         if (null != itsType) {
149             typedoctype = itsType.getValue();
150         }
151
152         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
153             throw new IllegalArgumentException(
154                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
155         }
156
157         Enumeration elements = doc.getChildren();
158
159         while (elements.hasMoreElements()) {
160             XMLElement elem = (XMLElement) elements.nextElement();
161
162             if (!handleElement(elem)) {
163                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
164                     LOG.fine("Unhandled Element: " + elem.toString());
165                 }
166             }
167         }
168
169         // Sanity Check!!!
170         if (null == getPeerGroupID()) {
171             throw new IllegalArgumentException("Peer Group Advertisement did not contain a peer group id.");
172         }
173
174         if (null == getModuleSpecID()) {
175             throw new IllegalArgumentException("Peer Group Advertisement did not contain a module spec id.");
176         }
177     }
178
179     /**
180      * {@inheritDoc}
181      */
182     @Override
183     public String getAdvType() {
184         return getAdvertisementType();
185     }
186
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     protected boolean handleElement(Element raw) {
192
193         if (super.handleElement(raw)) {
194             return true;
195         }
196
197         XMLElement elem = (XMLElement) raw;
198
199         if (elem.getName().equals(nameTag)) {
200             setName(elem.getTextValue());
201             return true;
202         }
203
204         if (elem.getName().equals(descTag)) {
205             setDesc(elem);
206             return true;
207         }
208
209         if (elem.getName().equals(gidTag)) {
210             try {
211                 URI grID = new URI(elem.getTextValue());
212
213                 setPeerGroupID((PeerGroupID) IDFactory.fromURI(grID));
214             } catch (URISyntaxException badID) {
215                 throw new IllegalArgumentException("Bad peer group ID in advertisement: " + elem.getTextValue());
216             } catch (ClassCastException badID) {
217                 throw new IllegalArgumentException("Id is not a group id: " + elem.getTextValue());
218             }
219             return true;
220         }
221
222         if (elem.getName().equals(msidTag)) {
223             try {
224                 URI specID = new URI(elem.getTextValue());
225
226                 setModuleSpecID((ModuleSpecID) IDFactory.fromURI(specID));
227             } catch (URISyntaxException badID) {
228                 throw new IllegalArgumentException("Bad msid in advertisement: " + elem.getTextValue());
229             } catch (ClassCastException badID) {
230                 throw new IllegalArgumentException("Id is not a module spec id: " + elem.getTextValue());
231             }
232             return true;
233         }
234
235         if (elem.getName().equals(svcTag)) {
236             Enumeration elems = elem.getChildren();
237             String classID = null;
238             Element param = null;
239
240             while (elems.hasMoreElements()) {
241                 TextElement e = (TextElement) elems.nextElement();
242
243                 if (e.getName().equals(mcidTag)) {
244                     classID = e.getTextValue();
245                     continue;
246                 }
247                 if (e.getName().equals(paramTag)) {
248                     param = e;
249                 }
250             }
251             if (classID != null && param != null) {
252                 // Add this param to the table. putServiceParam()
253                 // clones param into a standalone document automatically.
254                 // (classID gets cloned too).
255                 try {
256                     putServiceParam(IDFactory.fromURI(new URI(classID)), param);
257                 } catch (URISyntaxException badID) {
258                     throw new IllegalArgumentException("Bad mcid in advertisement: " + elem.getTextValue());
259                 } catch (ClassCastException badID) {
260                     throw new IllegalArgumentException("Id is not a module class id: " + elem.getTextValue());
261                 }
262             }
263             return true;
264         }
265
266         return false;
267     }
268
269     /**
270      * {@inheritDoc}
271      */
272     @Override
273     public Document getDocument(MimeMediaType encodeAs) {
274         if (null == getPeerGroupID()) {
275             throw new IllegalStateException("Peer Group Advertisement did not contain a peer group id.");
276         }
277
278         if (null == getModuleSpecID()) {
279             throw new IllegalStateException("Peer Group Advertisement did not contain a module spec id.");
280         }
281
282         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
283
284         Element e;
285
286         e = adv.createElement(gidTag, getPeerGroupID().toString());
287         adv.appendChild(e);
288
289         e = adv.createElement(msidTag, getModuleSpecID().toString());
290         adv.appendChild(e);
291
292         // name is optional
293         if (null != getName()) {
294             e = adv.createElement(nameTag, getName());
295             adv.appendChild(e);
296         }
297
298         // desc is optional
299         StructuredDocument desc = getDesc();
300
301         if (desc != null) {
302             StructuredDocumentUtils.copyElements(adv, adv, desc);
303         }
304
305         // FIXME: this is inefficient - we force our base class to make
306         // a deep clone of the table.
307         Hashtable serviceParams = getServiceParams();
308         Enumeration classIds = serviceParams.keys();
309
310         while (classIds.hasMoreElements()) {
311             ModuleClassID classId = (ModuleClassID) classIds.nextElement();
312
313             Element s = adv.createElement(svcTag);
314
315             adv.appendChild(s);
316
317             e = adv.createElement(mcidTag, classId.toString());
318             s.appendChild(e);
319
320             e = (Element) serviceParams.get(classId);
321             StructuredDocumentUtils.copyElements(adv, s, e, paramTag);
322
323         }
324         return adv;
325     }
326
327     /**
328      * {@inheritDoc}
329      */
330     @Override
331     public String[] getIndexFields() {
332         return fields;
333     }
334 }