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;
61 import java.util.Enumeration;
62 import java.util.Hashtable;
64 import java.net.URISyntaxException;
66 import java.util.logging.Level;
67 import net.jxta.logging.Logging;
68 import java.util.logging.Logger;
70 import net.jxta.document.Advertisement;
71 import net.jxta.document.AdvertisementFactory;
72 import net.jxta.document.Attribute;
73 import net.jxta.document.Document;
74 import net.jxta.document.Element;
75 import net.jxta.document.MimeMediaType;
76 import net.jxta.document.StructuredDocument;
77 import net.jxta.document.StructuredDocumentUtils;
78 import net.jxta.document.XMLElement;
79 import net.jxta.id.ID;
80 import net.jxta.id.IDFactory;
81 import net.jxta.peer.PeerID;
82 import net.jxta.peergroup.PeerGroupID;
83 import net.jxta.platform.ModuleClassID;
84 import net.jxta.protocol.PeerAdvertisement;
88 * Implementation of {@link PeerAdvertisement} matching the standard JXTA
89 * Protocol Specification.
91 * It implements Peer Advertisement using the following schema:
94 * <xs:complexType name="PA">
96 * <xs:element name="PID" type="JXTAID"/>
97 * <xs:element name="GID" type="JXTAID"/>
98 * <xs:element name="Name" type="xs:string" minOccurs="0"/>
99 * <xs:element name="Desc" type="xs:anyType" minOccurs="0"/>
100 * <xs:element name="Svc" type="jxta:serviceParams" minOccurs="0" maxOccurs="unbounded"/>
102 * </xs:complexType>
105 * @see net.jxta.protocol.PeerAdvertisement
106 * @see <a href="https://jxta-spec.dev.java.net/nonav/JXTAProtocols.html#advert-pa" target="_blank">JXTA Protocols Specification : Peer Advertisement</a>
108 public class PeerAdv extends PeerAdvertisement {
113 private static final Logger LOG = Logger.getLogger(PeerAdv.class.getName());
115 private static final String pidTag = "PID";
116 private static final String gidTag = "GID";
117 private static final String nameTag = "Name";
118 private static final String descTag = "Desc";
119 private static final String svcTag = "Svc";
120 private static final String mcidTag = "MCID";
121 private static final String paramTag = "Parm";
122 private static final String[] fields = { nameTag, pidTag };
125 * Creates instances of PeerAdvertisement.
127 public static class Instantiator implements AdvertisementFactory.Instantiator {
132 public String getAdvertisementType() {
133 return PeerAdvertisement.getAdvertisementType();
139 public Advertisement newInstance() {
140 return new PeerAdv();
146 public Advertisement newInstance(Element root) {
147 if (!XMLElement.class.isInstance(root)) {
148 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
151 return new PeerAdv((XMLElement) root);
156 * Private constructor for new instances. Use the instantiator.
161 * Private constructor for xml serialized instances. Use the instantiator.
163 * @param doc The XML serialization of the advertisement.
165 private PeerAdv(XMLElement doc) {
166 String doctype = doc.getName();
168 String typedoctype = "";
169 Attribute itsType = doc.getAttribute("type");
171 if (null != itsType) {
172 typedoctype = itsType.getValue();
175 if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
176 throw new IllegalArgumentException(
177 "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
180 Enumeration elements = doc.getChildren();
182 while (elements.hasMoreElements()) {
183 XMLElement elem = (XMLElement) elements.nextElement();
185 if (!handleElement(elem)) {
186 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
187 LOG.fine("Unhandled Element: " + elem.toString());
194 // sanity check time!
195 if (null == getPeerID()) {
196 throw new IllegalArgumentException("Peer Advertisement did not contain a peer id.");
199 if (null == getPeerGroupID()) {
200 throw new IllegalArgumentException("Peer Advertisement did not contain a peer group id.");
208 public String getAdvType() {
209 return getAdvertisementType();
216 protected boolean handleElement(Element raw) {
218 if (super.handleElement(raw)) {
222 XMLElement elem = (XMLElement) raw;
224 if (elem.getName().equals(pidTag)) {
226 URI pID = new URI(elem.getTextValue());
228 setPeerID((PeerID) IDFactory.fromURI(pID));
229 } catch (URISyntaxException badID) {
230 throw new IllegalArgumentException("Bad PeerID ID in advertisement: " + elem.getTextValue());
231 } catch (ClassCastException badID) {
232 throw new IllegalArgumentException("Id is not a peer id: " + elem.getTextValue());
237 if (elem.getName().equals(gidTag)) {
239 URI gID = new URI(elem.getTextValue());
241 setPeerGroupID((PeerGroupID) IDFactory.fromURI(gID));
242 } catch (URISyntaxException badID) {
243 throw new IllegalArgumentException("Bad PeerGroupID in advertisement: " + elem.getTextValue());
244 } catch (ClassCastException badID) {
245 throw new IllegalArgumentException("Id is not a group id: " + elem.getTextValue());
250 if (elem.getName().equals(nameTag)) {
251 setName(elem.getTextValue());
255 if (elem.getName().equals(descTag)) {
260 if (elem.getName().equals(svcTag)) {
261 Enumeration elems = elem.getChildren();
262 ModuleClassID classID = null;
263 Element param = null;
265 while (elems.hasMoreElements()) {
266 XMLElement e = (XMLElement) elems.nextElement();
268 if (e.getName().equals(mcidTag)) {
270 URI mcid = new URI(e.getTextValue());
272 classID = (ModuleClassID) IDFactory.fromURI(mcid);
273 } catch (URISyntaxException badID) {
274 throw new IllegalArgumentException("Unusable ModuleClassID in advertisement: " + e.getTextValue());
275 } catch (ClassCastException badID) {
276 throw new IllegalArgumentException("Id is not a ModuleClassID: " + e.getTextValue());
280 if (e.getName().equals(paramTag)) {
284 if (classID != null && param != null) {
285 // Add this param to the table. putServiceParam()
286 // clones param into a standalone document automatically.
287 // (classID gets cloned too).
288 putServiceParam(classID, param);
293 // element was not handled
301 public Document getDocument(MimeMediaType encodeAs) {
302 StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
304 PeerID peerID = getPeerID();
306 if ((null == peerID) || ID.nullID.equals(peerID)) {
307 throw new IllegalStateException("Cannot generate Peer Advertisement with no Peer ID!");
309 Element e = adv.createElement(pidTag, peerID.toString());
313 PeerGroupID groupID = getPeerGroupID();
315 if ((null == groupID) || ID.nullID.equals(groupID)) {
316 throw new IllegalStateException("Cannot generate Peer Advertisement with no group ID!");
318 e = adv.createElement(gidTag, groupID.toString());
323 if (getName() != null) {
324 e = adv.createElement(nameTag, getName());
329 StructuredDocument desc = getDesc();
332 StructuredDocumentUtils.copyElements(adv, adv, desc);
335 // service params are optional
336 // FIXME: this is inefficient - we force our base class to make
337 // a deep clone of the table.
338 Hashtable serviceParams = getServiceParams();
339 Enumeration classIds = serviceParams.keys();
341 while (classIds.hasMoreElements()) {
342 ModuleClassID classId = (ModuleClassID) classIds.nextElement();
344 Element s = adv.createElement(svcTag);
348 e = adv.createElement(mcidTag, classId.toString());
351 e = (Element) serviceParams.get(classId);
352 StructuredDocumentUtils.copyElements(adv, s, e, paramTag);
361 public String[] getIndexFields() {