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.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.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;
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;
85 public class PeerGroupAdv extends PeerGroupAdvertisement {
90 private static final Logger LOG = Logger.getLogger(PeerGroupAdv.class.getName());
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};
101 public static class Instantiator implements AdvertisementFactory.Instantiator {
107 public String getAdvertisementType() {
108 return PeerGroupAdv.getAdvertisementType();
114 public Advertisement newInstance() {
115 return new PeerGroupAdv();
121 public Advertisement newInstance(net.jxta.document.Element root) {
122 return new PeerGroupAdv(root);
127 * Use the Instantiator method to construct Peer Group Advs.
129 private PeerGroupAdv() {}
132 * Use the Instantiator method to construct Peer Group Advs.
134 * @param root the element
136 private PeerGroupAdv(Element root) {
137 if (!XMLElement.class.isInstance(root)) {
138 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
141 XMLElement doc = (XMLElement) root;
143 String doctype = doc.getName();
145 String typedoctype = "";
146 Attribute itsType = doc.getAttribute("type");
148 if (null != itsType) {
149 typedoctype = itsType.getValue();
152 if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
153 throw new IllegalArgumentException(
154 "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
157 Enumeration elements = doc.getChildren();
159 while (elements.hasMoreElements()) {
160 XMLElement elem = (XMLElement) elements.nextElement();
162 if (!handleElement(elem)) {
163 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
164 LOG.fine("Unhandled Element: " + elem.toString());
170 if (null == getPeerGroupID()) {
171 throw new IllegalArgumentException("Peer Group Advertisement did not contain a peer group id.");
174 if (null == getModuleSpecID()) {
175 throw new IllegalArgumentException("Peer Group Advertisement did not contain a module spec id.");
183 public String getAdvType() {
184 return getAdvertisementType();
191 protected boolean handleElement(Element raw) {
193 if (super.handleElement(raw)) {
197 XMLElement elem = (XMLElement) raw;
199 if (elem.getName().equals(nameTag)) {
200 setName(elem.getTextValue());
204 if (elem.getName().equals(descTag)) {
209 if (elem.getName().equals(gidTag)) {
211 URI grID = new URI(elem.getTextValue());
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());
222 if (elem.getName().equals(msidTag)) {
224 URI specID = new URI(elem.getTextValue());
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());
235 if (elem.getName().equals(svcTag)) {
236 Enumeration elems = elem.getChildren();
237 String classID = null;
238 Element param = null;
240 while (elems.hasMoreElements()) {
241 TextElement e = (TextElement) elems.nextElement();
243 if (e.getName().equals(mcidTag)) {
244 classID = e.getTextValue();
247 if (e.getName().equals(paramTag)) {
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).
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());
273 public Document getDocument(MimeMediaType encodeAs) {
274 if (null == getPeerGroupID()) {
275 throw new IllegalStateException("Peer Group Advertisement did not contain a peer group id.");
278 if (null == getModuleSpecID()) {
279 throw new IllegalStateException("Peer Group Advertisement did not contain a module spec id.");
282 StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
286 e = adv.createElement(gidTag, getPeerGroupID().toString());
289 e = adv.createElement(msidTag, getModuleSpecID().toString());
293 if (null != getName()) {
294 e = adv.createElement(nameTag, getName());
299 StructuredDocument desc = getDesc();
302 StructuredDocumentUtils.copyElements(adv, adv, desc);
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();
310 while (classIds.hasMoreElements()) {
311 ModuleClassID classId = (ModuleClassID) classIds.nextElement();
313 Element s = adv.createElement(svcTag);
317 e = adv.createElement(mcidTag, classId.toString());
320 e = (Element) serviceParams.get(classId);
321 StructuredDocumentUtils.copyElements(adv, s, e, paramTag);
331 public String[] getIndexFields() {