2 * Copyright (c) 2004-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.impl.protocol;
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;
77 import java.net.URISyntaxException;
78 import java.util.Enumeration;
79 import java.util.logging.Level;
80 import java.util.logging.Logger;
84 * Defines Peer Group Runtime Configuration parameters.
86 * This typically includes the peer group ID to use, the peer group name (if any) to use, and optional descriptive
90 * NetPeerGroupID=uuid-59313231343132314A484431544E504702
91 * PeerGroupName=Network Infrastructure PeerGroup
92 * PeerGroupDesc=Infrastructure Group Description
95 public final class PeerGroupConfigAdv extends ExtendableAdvertisement implements Cloneable {
100 private static final Logger LOG = Logger.getLogger(PeerGroupConfigAdv.class.getName());
103 * The advertisement index fields. (currently none).
105 private final static String[] INDEX_FIELDS = {};
110 private final static String advType = "jxta:PeerGroupConfigAdv";
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";
117 * Instantiator for PeerGroupConfigAdv
119 public static class Instantiator implements AdvertisementFactory.Instantiator {
124 public String getAdvertisementType() {
131 public Advertisement newInstance() {
132 return new PeerGroupConfigAdv();
138 public Advertisement newInstance(Element root) {
139 if (!XMLElement.class.isInstance(root)) {
140 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
143 return new PeerGroupConfigAdv((XMLElement) root);
148 * ID for the peer group.
150 private ID gid = null;
153 * Informal, non-canonical name of this peer group
155 private String name = null;
158 * Descriptive meta-data about this peer group.
160 private Element description = null;
163 * Returns the identifying type of this Advertisement.
165 * <b>Note:</b> This is a static method. It cannot be used to determine
166 * the runtime type of an advertisement. ie.
168 * Advertisement adv = module.getSomeAdv();
169 * String advType = adv.getAdvertisementType();
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
177 * @return String the type of advertisement
179 public static String getAdvertisementType() {
184 * Use the Instantiator through the factory
186 private PeerGroupConfigAdv() {}
189 * Use the Instantiator method to construct Peer Group Config Advs.
191 * @param doc the element
193 private PeerGroupConfigAdv(XMLElement doc) {
194 String doctype = doc.getName();
196 String typedoctype = "";
197 Attribute itsType = doc.getAttribute("type");
199 if (null != itsType) {
200 typedoctype = itsType.getValue();
203 if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
204 throw new IllegalArgumentException(
205 "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
208 Enumeration elements = doc.getChildren();
210 while (elements.hasMoreElements()) {
211 XMLElement elem = (XMLElement) elements.nextElement();
213 if (!handleElement(elem)) {
214 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
215 LOG.fine("Unhandled Element: " + elem.toString());
221 if (null == getPeerGroupID()) {
222 throw new IllegalArgumentException("Peer Group Config Advertisement does not contain a peer group id.");
230 protected boolean handleElement(Element raw) {
232 if (super.handleElement(raw)) {
236 XMLElement elem = (XMLElement) raw;
238 if (PEERGROUP_ID_TAG.equals(elem.getName())) {
240 URI grID = new URI(elem.getTextValue());
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());
251 if (PEERGROUP_NAME_TAG.equals(elem.getName())) {
252 setName(elem.getTextValue());
256 if (PEERGROUP_DESC_TAG.equals(elem.getName())) {
265 * Make a safe clone of this PeerGroupConfigAdv.
267 * @return Object A copy of this PeerGroupConfigAdv
270 public PeerGroupConfigAdv clone() {
272 PeerGroupConfigAdv clone = (PeerGroupConfigAdv) super.clone();
274 clone.setPeerGroupID(getPeerGroupID());
275 clone.setName(getName());
276 clone.setDesc(getDesc());
279 } catch (CloneNotSupportedException impossible) {
280 throw new Error("Object.clone() threw CloneNotSupportedException", impossible);
288 public String getAdvType() {
289 return getAdvertisementType();
296 public final String getBaseAdvType() {
297 return getAdvertisementType();
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.");
317 StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
319 Element e = adv.createElement(PEERGROUP_ID_TAG, getPeerGroupID().toString());
324 if (null != getName()) {
325 e = adv.createElement(PEERGROUP_NAME_TAG, getName());
330 StructuredDocument desc = getDesc();
333 StructuredDocumentUtils.copyElements(adv, adv, desc);
343 public String[] getIndexFields() {
348 * Returns the id of the peer group.
350 * @return ID the group id
353 public ID getPeerGroupID() {
358 * Sets the id of the peer group.
360 * @param gid The id of this group.
363 public void setPeerGroupID(ID gid) {
368 * Gets the name to use for the peer group.
370 * @return The name value
372 public String getName() {
377 * Sets the name to use for the peer group.
379 * @param name The new name value
381 public void setName(String name) {
386 * returns the description
388 * @return String the description
390 public String getDescription() {
391 if (description != null) {
392 return (String) description.getValue();
399 * sets the description
401 * @param description the description
403 public void setDescription(String description) {
405 if (null != description) {
406 XMLDocument newdoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8
408 PEERGROUP_DESC_TAG, description);
412 this.description = null;
417 * returns the description
419 * @return the description
421 public XMLDocument getDesc() {
422 XMLDocument newDoc = null;
424 if (description != null) {
425 newDoc = (XMLDocument) StructuredDocumentUtils.copyAsDocument(description);
431 * Sets the description
433 * @param desc the description
435 public void setDesc(XMLElement desc) {
438 this.description = StructuredDocumentUtils.copyAsDocument(desc);
440 this.description = null;