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;
63 import java.net.URISyntaxException;
65 import java.util.logging.Level;
66 import net.jxta.logging.Logging;
67 import java.util.logging.Logger;
69 import net.jxta.document.Advertisement;
70 import net.jxta.document.AdvertisementFactory;
71 import net.jxta.document.Attribute;
72 import net.jxta.document.Document;
73 import net.jxta.document.Element;
74 import net.jxta.document.MimeMediaType;
75 import net.jxta.document.StructuredDocumentUtils;
76 import net.jxta.document.StructuredDocument;
77 import net.jxta.document.StructuredTextDocument;
78 import net.jxta.document.XMLElement;
79 import net.jxta.id.IDFactory;
80 import net.jxta.peer.PeerID;
81 import net.jxta.peergroup.PeerGroupID;
82 import net.jxta.protocol.RdvAdvertisement;
83 import net.jxta.protocol.RouteAdvertisement;
87 * This class implements the RdvAdvertisement.
90 * <xs:complexType name="RdvAdvertisement">
92 * <xs:element name="RdvGroupId" type="jxta:JXTAID"/>
93 * <xs:element name="RdvPeerId" type="jxta:JXTAID"/>
94 * <xs:element name="RdvServiceName" type="xs:string"/>
95 * <xs:element name="Name" type="xs:string" minOccurs="0"/>
96 * <!-- This should be a route -->
97 * <xs:element name="RdvRoute" type="xs:anyType" minOccurs="0"/>
99 * </xs:complexType>
102 public class RdvAdv extends RdvAdvertisement {
107 private final static transient Logger LOG = Logger.getLogger(RdvAdv.class.getName());
109 private static final String[] INDEX_FIELDS = { PeerIDTag, ServiceNameTag, GroupIDTag };
112 * Instantiator for our advertisement
114 public static class Instantiator implements AdvertisementFactory.Instantiator {
119 public String getAdvertisementType() {
120 return RdvAdv.getAdvertisementType();
126 public Advertisement newInstance() {
133 public Advertisement newInstance(Element root) {
134 if (!XMLElement.class.isInstance(root)) {
135 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
138 return new RdvAdv((XMLElement) root);
143 * Private constructor for new instances. Use the instantiator.
149 * Private constructor for xml serialized instances. Use the instantiator.
151 * @param doc The XML serialization of the advertisement.
153 private RdvAdv(XMLElement doc) {
154 String doctype = doc.getName();
156 String typedoctype = "";
157 Attribute itsType = doc.getAttribute("type");
159 if (null != itsType) {
160 typedoctype = itsType.getValue();
163 if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
164 throw new IllegalArgumentException(
165 "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
168 Enumeration elements = doc.getChildren();
170 while (elements.hasMoreElements()) {
171 XMLElement elem = (XMLElement) elements.nextElement();
173 if (!handleElement(elem)) {
174 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
175 LOG.fine("Unhandled Element: " + elem.toString());
181 if (null == getGroupID()) {
182 throw new IllegalArgumentException("Missing peer group ID");
185 if (null == getPeerID()) {
186 throw new IllegalArgumentException("Missing peer ID");
189 if (null == getServiceName()) {
190 throw new IllegalArgumentException("Missing service name");
198 public String getAdvType() {
199 return getAdvertisementType();
206 protected boolean handleElement(Element raw) {
208 if (super.handleElement(raw)) {
212 XMLElement elem = (XMLElement) raw;
214 if (elem.getName().equals(RdvAdvertisement.GroupIDTag)) {
216 URI groupID = new URI(elem.getTextValue().trim());
218 setGroupID((PeerGroupID) IDFactory.fromURI(groupID));
219 } catch (URISyntaxException badID) {
220 throw new IllegalArgumentException("Bad group ID in advertisement");
221 } catch (ClassCastException badID) {
222 throw new IllegalArgumentException("ID is not a group ID");
227 if (elem.getName().equals(RdvAdvertisement.PeerIDTag)) {
229 URI peerID = new URI(elem.getTextValue().trim());
231 setPeerID((PeerID) IDFactory.fromURI(peerID));
232 } catch (URISyntaxException badID) {
233 throw new IllegalArgumentException("Bad group ID in advertisement");
234 } catch (ClassCastException badID) {
235 throw new IllegalArgumentException("ID is not a group ID");
240 if (elem.getName().equals(RdvAdvertisement.ServiceNameTag)) {
241 setServiceName(elem.getTextValue());
245 if (elem.getName().equals(RdvAdvertisement.RouteTag)) {
246 for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements();) {
248 XMLElement aXpt = (XMLElement) eachXpt.nextElement();
250 RouteAdvertisement xptAdv = (RouteAdvertisement)
251 AdvertisementFactory.newAdvertisement(aXpt);
258 if (elem.getName().equals(RdvAdvertisement.NameTag)) {
259 setName(elem.getTextValue());
270 public Document getDocument(MimeMediaType encodeAs) {
273 if (null == getGroupID()) {
274 throw new IllegalStateException("Missing peer group ID");
277 if (null == getPeerID()) {
278 throw new IllegalStateException("Missing peer ID");
281 if (null == getServiceName()) {
282 throw new IllegalStateException("Missing service name");
285 StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
287 Element e = adv.createElement(RdvAdvertisement.GroupIDTag, getGroupID().toString());
291 e = adv.createElement(RdvAdvertisement.PeerIDTag, getPeerID().toString());
294 e = adv.createElement(RdvAdvertisement.ServiceNameTag, getServiceName());
297 String peerName = getName();
299 if (null != peerName) {
300 e = adv.createElement(RdvAdvertisement.NameTag, getName());
304 if (getRouteAdv() != null) {
305 Element el = adv.createElement(RdvAdvertisement.RouteTag);
309 StructuredTextDocument xptDoc = (StructuredTextDocument)
310 getRouteAdv().getDocument(encodeAs);
312 StructuredDocumentUtils.copyElements(adv, el, xptDoc);
322 public String[] getIndexFields() {