]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/PeerGroupConfigAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / PeerGroupConfigAdv.java
1 /*
2  * Copyright (c) 2004-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.ExtendableAdvertisement;
66 import net.jxta.document.MimeMediaType;
67 import net.jxta.document.StructuredDocument;
68 import net.jxta.document.StructuredDocumentFactory;
69 import net.jxta.document.StructuredDocumentUtils;
70 import net.jxta.document.XMLDocument;
71 import net.jxta.document.XMLElement;
72 import net.jxta.id.ID;
73 import net.jxta.id.IDFactory;
74 import net.jxta.logging.Logging;
75
76 import java.net.URI;
77 import java.net.URISyntaxException;
78 import java.util.Enumeration;
79 import java.util.logging.Level;
80 import java.util.logging.Logger;
81
82
83 /**
84  * Defines Peer Group Runtime Configuration parameters.
85  * <p/>
86  * This typically includes the peer group ID to use, the peer group name (if any) to use, and optional descriptive
87  * meta-data.
88  * <p/>
89  * <pre><code>
90  *   NetPeerGroupID=uuid-59313231343132314A484431544E504702
91  *   PeerGroupName=Network Infrastructure PeerGroup
92  *   PeerGroupDesc=Infrastructure Group Description
93  * </code></pre>
94  */
95 public final class PeerGroupConfigAdv extends ExtendableAdvertisement implements Cloneable {
96
97     /**
98      * Logger
99      */
100     private static final Logger LOG = Logger.getLogger(PeerGroupConfigAdv.class.getName());
101
102     /**
103      *  The advertisement index fields. (currently none).
104      */
105     private final static String[] INDEX_FIELDS = {};
106
107     /**
108      * The DOCTYPE
109      */
110     private final static String advType = "jxta:PeerGroupConfigAdv";
111
112     private final static String PEERGROUP_ID_TAG = "PeerGroupID";
113     private final static String PEERGROUP_NAME_TAG = "PeerGroupName";
114     private final static String PEERGROUP_DESC_TAG = "PeerGroupDesc";
115
116     /**
117      * Instantiator for PeerGroupConfigAdv
118      */
119     public static class Instantiator implements AdvertisementFactory.Instantiator {
120
121         /**
122          * {@inheritDoc}
123          */
124         public String getAdvertisementType() {
125             return advType;
126         }
127
128         /**
129          * {@inheritDoc}
130          */
131         public Advertisement newInstance() {
132             return new PeerGroupConfigAdv();
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         public Advertisement newInstance(Element root) {
139             if (!XMLElement.class.isInstance(root)) {
140                 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
141             }
142
143             return new PeerGroupConfigAdv((XMLElement) root);
144         }
145     }
146
147     /**
148      * ID for the peer group.
149      */
150     private ID gid = null;
151
152     /**
153      * Informal, non-canonical name of this peer group
154      */
155     private String name = null;
156
157     /**
158      * Descriptive meta-data about this peer group.
159      */
160     private Element description = null;
161
162     /**
163      * Returns the identifying type of this Advertisement.
164      * <p/>
165      * <b>Note:</b> This is a static method. It cannot be used to determine
166      * the runtime type of an advertisement. ie.
167      * </p><code><pre>
168      *      Advertisement adv = module.getSomeAdv();
169      *      String advType = adv.getAdvertisementType();
170      *  </pre></code>
171      * <p/>
172      * <b>This is wrong and does not work the way you might expect.</b>
173      * This call is not polymorphic and calls
174      * Advertisement.getAdvertisementType() no matter what the real type of the
175      * advertisement.
176      *
177      * @return String the type of advertisement
178      */
179     public static String getAdvertisementType() {
180         return advType;
181     }
182
183     /**
184      * Use the Instantiator through the factory
185      */
186     private PeerGroupConfigAdv() {}
187
188     /**
189      * Use the Instantiator method to construct Peer Group Config Advs.
190      *
191      * @param doc the element
192      */
193     private PeerGroupConfigAdv(XMLElement doc) {
194         String doctype = doc.getName();
195
196         String typedoctype = "";
197         Attribute itsType = doc.getAttribute("type");
198
199         if (null != itsType) {
200             typedoctype = itsType.getValue();
201         }
202
203         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
204             throw new IllegalArgumentException(
205                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
206         }
207
208         Enumeration elements = doc.getChildren();
209
210         while (elements.hasMoreElements()) {
211             XMLElement elem = (XMLElement) elements.nextElement();
212
213             if (!handleElement(elem)) {
214                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
215                     LOG.fine("Unhandled Element: " + elem.toString());
216                 }
217             }
218         }
219
220         // Validate group id
221         if (null == getPeerGroupID()) {
222             throw new IllegalArgumentException("Peer Group Config Advertisement does not contain a peer group id.");
223         }
224     }
225
226     /**
227      * {@inheritDoc}
228      */
229     @Override
230     protected boolean handleElement(Element raw) {
231
232         if (super.handleElement(raw)) {
233             return true;
234         }
235
236         XMLElement elem = (XMLElement) raw;
237
238         if (PEERGROUP_ID_TAG.equals(elem.getName())) {
239             try {
240                 URI grID = new URI(elem.getTextValue());
241
242                 setPeerGroupID(IDFactory.fromURI(grID));
243             } catch (URISyntaxException badID) {
244                 throw new IllegalArgumentException("Invalid peer group ID in advertisement: " + elem.getTextValue());
245             } catch (ClassCastException badID) {
246                 throw new IllegalArgumentException("Invalid group ID: " + elem.getTextValue());
247             }
248             return true;
249         }
250
251         if (PEERGROUP_NAME_TAG.equals(elem.getName())) {
252             setName(elem.getTextValue());
253             return true;
254         }
255
256         if (PEERGROUP_DESC_TAG.equals(elem.getName())) {
257             setDesc(elem);
258             return true;
259         }
260
261         return false;
262     }
263
264     /**
265      * Make a safe clone of this PeerGroupConfigAdv.
266      *
267      * @return Object A copy of this PeerGroupConfigAdv
268      */
269     @Override
270     public PeerGroupConfigAdv clone() {
271         try {
272             PeerGroupConfigAdv clone = (PeerGroupConfigAdv) super.clone();
273             
274             clone.setPeerGroupID(getPeerGroupID());
275             clone.setName(getName());
276             clone.setDesc(getDesc());
277             
278             return clone;
279         } catch (CloneNotSupportedException impossible) {
280             throw new Error("Object.clone() threw CloneNotSupportedException", impossible);
281         }
282     }
283
284     /**
285      * {@inheritDoc}
286      */
287     @Override
288     public String getAdvType() {
289         return getAdvertisementType();
290     }
291
292     /**
293      * {@inheritDoc}
294      */
295     @Override
296     public final String getBaseAdvType() {
297         return getAdvertisementType();
298     }
299
300     /**
301      * {@inheritDoc}
302      */
303     @Override
304     public ID getID() {
305         return ID.nullID;
306     }
307
308     /**
309      * {@inheritDoc}
310      */
311     @Override
312     public Document getDocument(MimeMediaType encodeAs) {
313         if (null == getPeerGroupID()) {
314             throw new IllegalStateException("Peer Group Config Advertisement does not contain a peer group id.");
315         }
316
317         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
318
319         Element e = adv.createElement(PEERGROUP_ID_TAG, getPeerGroupID().toString());
320
321         adv.appendChild(e);
322
323         // name is optional
324         if (null != getName()) {
325             e = adv.createElement(PEERGROUP_NAME_TAG, getName());
326             adv.appendChild(e);
327         }
328
329         // desc is optional
330         StructuredDocument desc = getDesc();
331
332         if (desc != null) {
333             StructuredDocumentUtils.copyElements(adv, adv, desc);
334         }
335
336         return adv;
337     }
338
339     /**
340      * {@inheritDoc}
341      */
342     @Override
343     public String[] getIndexFields() {
344         return INDEX_FIELDS;
345     }
346
347     /**
348      * Returns the id of the peer group.
349      *
350      * @return ID the group id
351      */
352
353     public ID getPeerGroupID() {
354         return gid;
355     }
356
357     /**
358      * Sets the id of the peer group.
359      *
360      * @param gid The id of this group.
361      */
362
363     public void setPeerGroupID(ID gid) {
364         this.gid = gid;
365     }
366
367     /**
368      * Gets the name to use for the peer group.
369      *
370      * @return The name value
371      */
372     public String getName() {
373         return name;
374     }
375
376     /**
377      * Sets the name to use for the peer group.
378      *
379      * @param name The new name value
380      */
381     public void setName(String name) {
382         this.name = name;
383     }
384
385     /**
386      * returns the description
387      *
388      * @return String the description
389      */
390     public String getDescription() {
391         if (description != null) {
392             return (String) description.getValue();
393         } else {
394             return null;
395         }
396     }
397
398     /**
399      * sets the description
400      *
401      * @param description the description
402      */
403     public void setDescription(String description) {
404
405         if (null != description) {
406             XMLDocument newdoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8
407                     ,
408                     PEERGROUP_DESC_TAG, description);
409
410             setDesc(newdoc);
411         } else {
412             this.description = null;
413         }
414     }
415
416     /**
417      * returns the description
418      *
419      * @return the description
420      */
421     public XMLDocument getDesc() {
422         XMLDocument newDoc = null;
423
424         if (description != null) {
425             newDoc = (XMLDocument) StructuredDocumentUtils.copyAsDocument(description);
426         }
427         return newDoc;
428     }
429
430     /**
431      * Sets the description
432      *
433      * @param desc the description
434      */
435     public void setDesc(XMLElement desc) {
436
437         if (desc != null) {
438             this.description = StructuredDocumentUtils.copyAsDocument(desc);
439         } else {
440             this.description = null;
441         }
442     }
443 }