]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/api/src/net/jxta/protocol/PeerGroupAdvertisement.java
7049b0a7cf3577d93c18c1a7871351affe4482f4
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / api / src / net / jxta / protocol / PeerGroupAdvertisement.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.protocol;
58
59
60 import java.util.Hashtable;
61 import java.util.Map;
62 import java.util.HashMap;
63
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.id.ID;
71 import net.jxta.peergroup.PeerGroupID;
72 import net.jxta.platform.ModuleSpecID;
73
74
75 /**
76  *  Describes a peer group and references additional information required for
77  *  instantiating it. The PeerGroup method newGroup performs the task of
78  *  instantiating a PeerGroup given its advertisement (provided the required
79  *  subsequent documents can actually be found). This advertisement is indexed
80  *  on "Name", "GID", and "Desc"
81  *
82  *  @see    net.jxta.platform.ModuleSpecID
83  *  @see    net.jxta.protocol.ModuleImplAdvertisement
84  *  @see    net.jxta.peergroup.PeerGroup
85  */
86
87 public abstract class PeerGroupAdvertisement extends ExtendableAdvertisement implements Cloneable {
88     
89     private PeerGroupID gid = null;
90     private ModuleSpecID specId = null;
91     
92     /**
93      *  Informal, non-canonical name of this peer group
94      */
95     private String name = null;
96     
97     /**
98      * Descriptive meta-data about this peer group.
99      */
100     private Element description = null;
101     
102     // A table of structured documents to be interpreted by each service.
103     private final Map<ID, StructuredDocument> serviceParams = new HashMap<ID, StructuredDocument>();
104     
105     /**
106      *  Returns the identifying type of this Advertisement.
107      *
108      *@return    String the type of advertisement
109      */
110     public static String getAdvertisementType() {
111         return "jxta:PGA";
112     }
113     
114     /**
115      *  {@inheritDoc}
116      */
117     @Override
118     public final String getBaseAdvType() {
119         return getAdvertisementType();
120     }
121     
122     /**
123      *  Construct a new Peer Group Advertisement.
124      **/
125     public PeerGroupAdvertisement() {}
126     
127     /**
128      *  {@inheritDoc}
129      *
130      *@return An object of class PeerGroupAdvertisement that is a
131      *      deep-enough copy of this one.
132      */
133     @Override
134     public PeerGroupAdvertisement clone() {
135     
136         PeerGroupAdvertisement clone;
137
138         try {
139             clone = (PeerGroupAdvertisement) super.clone();
140         } catch (CloneNotSupportedException impossible) {
141             throw new Error("Object.clone() threw CloneNotSupportedException", impossible);
142         }  
143             
144         clone.setPeerGroupID(getPeerGroupID());
145         clone.setModuleSpecID(getModuleSpecID());
146         clone.setName(getName());
147         clone.setDesc(getDesc());
148         clone.setServiceParams(getServiceParams());
149
150         return clone;
151     }
152     
153     /**
154      *  Returns the name of the group or <tt>null</tt> if no name has been
155      *  assigned.
156      *
157      *@return    String name of the group.
158      */
159     
160     public String getName() {
161         return name;
162     }
163     
164     /**
165      *  sets the name of the group.
166      *
167      *@param  name  name of the group.
168      */
169     
170     public void setName(String name) {
171         this.name = name;
172     }
173
174     /**
175      *  Returns the id of the group spec that this uses.
176      *
177      *@return    ID the spec id
178      */
179     
180     public ModuleSpecID getModuleSpecID() {
181         return specId;
182     }
183     
184     /**
185      *  Sets the id of the group spec that this peer group uses.
186      *
187      *@param  sid  The id of the spec
188      */
189     
190     public void setModuleSpecID(ModuleSpecID sid) {
191         this.specId = sid;
192     }
193     
194     /**
195      *  Returns the id of the group.
196      *
197      *@return    ID the group id
198      */
199     
200     public PeerGroupID getPeerGroupID() {
201         return gid;
202     }
203     
204     /**
205      *  Sets the id of the group.
206      *
207      *@param  gid  The id of this group.
208      */
209     
210     public void setPeerGroupID(PeerGroupID gid) {
211         this.gid = gid;
212     }
213     
214     /**
215      *  Returns a unique ID for indexing purposes. We use the id of the group as
216      *  a plain ID.
217      *
218      *@return    ID a unique id for that advertisement.
219      */
220     
221     @Override
222     public ID getID() {
223         return gid;
224     }
225     
226     /**
227      * returns the description
228      *
229      * @return String the description
230      */
231     public String getDescription() {
232         if (null != description) {
233             return (String) description.getValue();
234         } else {
235             return null;
236         }
237     }
238     
239     /**
240      * sets the description
241      *
242      * @param description the description
243      */
244     public void setDescription(String description) {
245         
246         if (null != description) {
247             StructuredDocument newdoc = StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Desc", description);
248             
249             setDesc(newdoc);
250         } else {
251             this.description = null;
252         }
253     }
254     
255     /**
256      * returns the description
257      *
258      * @return the description
259      */
260     public StructuredDocument getDesc() {
261         if (null != description) {
262             StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(description);
263             
264             return newDoc;
265         } else {
266             return null;
267         }
268     }
269     
270     /**
271      * sets the description
272      *
273      * @since JXTA 1.0
274      *
275      * @param desc the description
276      *
277      */
278     public void setDesc(Element desc) {
279         
280         if (null != desc) {
281             this.description = StructuredDocumentUtils.copyAsDocument(desc);
282         } else {
283             this.description = null;
284         }
285     }
286     
287     /**
288      *  sets the sets of parameters for all services. This method first makes a
289      *  deep copy, in order to protect the active information from uncontrolled
290      *  sharing. This quite an expensive operation. If only a few of the
291      *  parameters need to be added, it is wise to use putServiceParam()
292      *  instead.
293      *
294      *@param  params  The whole set of parameters.
295      */
296     public void setServiceParams(Hashtable<ID, ? extends Element> params) {
297         serviceParams.clear();
298         
299         if (params == null) {
300             return;
301         }
302
303         for (Map.Entry<ID, ? extends Element> anEntry : params.entrySet()) {
304             Element e = anEntry.getValue();
305             StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(e);
306
307             serviceParams.put(anEntry.getKey(), newDoc);
308         }
309
310     }
311     
312     /**
313      *  Returns the sets of parameters for all services. 
314      *
315      *  <p/>This method returns a deep copy, in order to protect the real
316      *  information from uncontrolled sharing while keeping it shared as long as
317      *  it is safe. This quite an expensive operation. If only a few parameters
318      *  need to be accessed, it is wise to use getServiceParam() instead.
319      *
320      *@return    all of the parameters.
321      */
322     public Hashtable<ID, StructuredDocument> getServiceParams() {
323         Hashtable<ID, StructuredDocument> copy = new Hashtable<ID, StructuredDocument>();
324
325         for (Map.Entry<ID, StructuredDocument> anEntry : serviceParams.entrySet()) {
326             Element e = anEntry.getValue();
327             StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(e);
328
329             copy.put(anEntry.getKey(), newDoc);
330         }
331
332         return copy;
333     }
334     
335     /**
336      *  Puts a service parameter in the service parameters table under the given
337      *  key. The key is of a subclass of ID; usually a ModuleClassID. This
338      *  method makes a deep copy of the given element into an independent
339      *  document.
340      *
341      *@param  key    The key.
342      *@param  param  The parameter, as an element. What is stored is a copy as a
343      *      standalone StructuredDocument which type is the element's name.
344      */
345     public void putServiceParam(ID key, Element param) {
346         if (param == null) {
347             serviceParams.remove(key);
348             return;
349         }
350         
351         StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(param);
352
353         serviceParams.put(key, newDoc);        
354     }
355     
356     /**
357      *  Returns the parameter element that matches the given key from the
358      *  service parameters table. The key is of a subclass of ID; usually a
359      *  ModuleClassID.
360      *
361      *@param  key  The key.
362      *@return      StructuredDocument The matching parameter document or null if
363      *      none matched. The document type id "Param".
364      */
365     public StructuredDocument getServiceParam(ID key) {
366         StructuredDocument param = serviceParams.get(key);
367
368         if (param == null) {
369             return null;
370         }
371         
372         return  StructuredDocumentUtils.copyAsDocument(param);
373     }
374     
375     /**
376      *  Removes and returns the parameter element that matches the given key
377      *  from the service parameters table. The key is of a subclass of ID;
378      *  usually a ModuleClassID.
379      *
380      *@param  key  The key.
381      *@return      Element the removed parameter element or null if not found.
382      *      This is actually a StructureDocument of type "Param".
383      */
384     public StructuredDocument removeServiceParam(ID key) {
385         Element param = serviceParams.remove(key);
386
387         if (param == null) {
388             return null;
389         }
390                       
391         // It sound silly to clone it, but remember that we could be sharing
392         // this element with a clone of ours, so we have the duty to still
393         // protect it.
394         
395         return StructuredDocumentUtils.copyAsDocument(param);
396     }
397 }