2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
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.
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.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
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.
48 * ====================================================================
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.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.protocol;
60 import java.util.Hashtable;
62 import java.util.HashMap;
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;
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"
82 * @see net.jxta.platform.ModuleSpecID
83 * @see net.jxta.protocol.ModuleImplAdvertisement
84 * @see net.jxta.peergroup.PeerGroup
87 public abstract class PeerGroupAdvertisement extends ExtendableAdvertisement implements Cloneable {
89 private PeerGroupID gid = null;
90 private ModuleSpecID specId = null;
93 * Informal, non-canonical name of this peer group
95 private String name = null;
98 * Descriptive meta-data about this peer group.
100 private Element description = null;
102 // A table of structured documents to be interpreted by each service.
103 private final Map<ID, StructuredDocument> serviceParams = new HashMap<ID, StructuredDocument>();
106 * Returns the identifying type of this Advertisement.
108 *@return String the type of advertisement
110 public static String getAdvertisementType() {
118 public final String getBaseAdvType() {
119 return getAdvertisementType();
123 * Construct a new Peer Group Advertisement.
125 public PeerGroupAdvertisement() {}
130 *@return An object of class PeerGroupAdvertisement that is a
131 * deep-enough copy of this one.
134 public PeerGroupAdvertisement clone() {
136 PeerGroupAdvertisement clone;
139 clone = (PeerGroupAdvertisement) super.clone();
140 } catch (CloneNotSupportedException impossible) {
141 throw new Error("Object.clone() threw CloneNotSupportedException", impossible);
144 clone.setPeerGroupID(getPeerGroupID());
145 clone.setModuleSpecID(getModuleSpecID());
146 clone.setName(getName());
147 clone.setDesc(getDesc());
148 clone.setServiceParams(getServiceParams());
154 * Returns the name of the group or <tt>null</tt> if no name has been
157 *@return String name of the group.
160 public String getName() {
165 * sets the name of the group.
167 *@param name name of the group.
170 public void setName(String name) {
175 * Returns the id of the group spec that this uses.
177 *@return ID the spec id
180 public ModuleSpecID getModuleSpecID() {
185 * Sets the id of the group spec that this peer group uses.
187 *@param sid The id of the spec
190 public void setModuleSpecID(ModuleSpecID sid) {
195 * Returns the id of the group.
197 *@return ID the group id
200 public PeerGroupID getPeerGroupID() {
205 * Sets the id of the group.
207 *@param gid The id of this group.
210 public void setPeerGroupID(PeerGroupID gid) {
215 * Returns a unique ID for indexing purposes. We use the id of the group as
218 *@return ID a unique id for that advertisement.
227 * returns the description
229 * @return String the description
231 public String getDescription() {
232 if (null != description) {
233 return (String) description.getValue();
240 * sets the description
242 * @param description the description
244 public void setDescription(String description) {
246 if (null != description) {
247 StructuredDocument newdoc = StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Desc", description);
251 this.description = null;
256 * returns the description
258 * @return the description
260 public StructuredDocument getDesc() {
261 if (null != description) {
262 StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(description);
271 * sets the description
275 * @param desc the description
278 public void setDesc(Element desc) {
281 this.description = StructuredDocumentUtils.copyAsDocument(desc);
283 this.description = null;
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()
294 *@param params The whole set of parameters.
296 public void setServiceParams(Hashtable<ID, ? extends Element> params) {
297 serviceParams.clear();
299 if (params == null) {
303 for (Map.Entry<ID, ? extends Element> anEntry : params.entrySet()) {
304 Element e = anEntry.getValue();
305 StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(e);
307 serviceParams.put(anEntry.getKey(), newDoc);
313 * Returns the sets of parameters for all services.
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.
320 *@return all of the parameters.
322 public Hashtable<ID, StructuredDocument> getServiceParams() {
323 Hashtable<ID, StructuredDocument> copy = new Hashtable<ID, StructuredDocument>();
325 for (Map.Entry<ID, StructuredDocument> anEntry : serviceParams.entrySet()) {
326 Element e = anEntry.getValue();
327 StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(e);
329 copy.put(anEntry.getKey(), newDoc);
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
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.
345 public void putServiceParam(ID key, Element param) {
347 serviceParams.remove(key);
351 StructuredDocument newDoc = StructuredDocumentUtils.copyAsDocument(param);
353 serviceParams.put(key, newDoc);
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
362 *@return StructuredDocument The matching parameter document or null if
363 * none matched. The document type id "Param".
365 public StructuredDocument getServiceParam(ID key) {
366 StructuredDocument param = serviceParams.get(key);
372 return StructuredDocumentUtils.copyAsDocument(param);
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.
381 *@return Element the removed parameter element or null if not found.
382 * This is actually a StructureDocument of type "Param".
384 public StructuredDocument removeServiceParam(ID key) {
385 Element param = serviceParams.remove(key);
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
395 return StructuredDocumentUtils.copyAsDocument(param);