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.*;
61 import net.jxta.id.IDFactory;
62 import net.jxta.platform.ModuleSpecID;
63 import net.jxta.protocol.ModuleSpecAdvertisement;
64 import net.jxta.protocol.PipeAdvertisement;
65 import java.util.logging.Level;
66 import net.jxta.logging.Logging;
67 import java.util.logging.Logger;
70 import java.net.URISyntaxException;
71 import java.util.Enumeration;
75 * Provides XML serialization support for ModuleSpecAdvertisement matching the
76 * schema defined by the JXTA Core Specification.
79 * <xs:complexType name="MSA">
81 * <xs:element name="MSID" type="jxta:JXTAID" />
82 * <xs:element name="Name" type="xs:string" minOccurs="0" />
83 * <xs:element name="Desc" type="xs:anyType" minOccurs="0" />
84 * <xs:element name="Crtr" type="xs:string" minOccurs="0" />
85 * <xs:element name="SURI" type="xs:anyURI" minOccurs="0" />
86 * <xs:element name="Vers" type="xs:string" />
87 * <xs:element name="Parm" type="xs:anyType" minOccurs="0" />
88 * <xs:element ref="jxta:PipeAdvertisement" minOccurs="0" />
89 * <xs:element name="Proxy" type="xs:anyURI" minOccurs="0" />
90 * <xs:element name="Auth" type="jxta:JXTAID" minOccurs="0" />
92 * </xs:complexType>
95 * @see net.jxta.document.Advertisement
96 * @see net.jxta.protocol.ModuleSpecAdvertisement
97 * @see <a href="https://jxta-spec.dev.java.net/nonav/JXTAProtocols.html#advert-msa> target='_blank'>JXTA Protocols Specification - Advertisements : Module Specification Advertisement</a>
99 public class ModuleSpecAdv extends ModuleSpecAdvertisement {
104 private static final Logger LOG = Logger.getLogger(ModuleSpecAdv.class.getName());
106 private static final String idTag = "MSID";
107 private static final String nameTag = "Name";
108 private static final String creatorTag = "Crtr";
109 private static final String uriTag = "SURI";
110 private static final String versTag = "Vers";
111 private static final String descTag = "Desc";
112 private static final String paramTag = "Parm";
113 private static final String proxyIdTag = "Proxy";
114 private static final String authIdTag = "Auth";
115 private static final String[] fields = { nameTag, idTag};
117 public static class Instantiator implements AdvertisementFactory.Instantiator {
123 public String getAdvertisementType() {
124 return ModuleSpecAdv.getAdvertisementType();
131 public Advertisement newInstance() {
132 return new ModuleSpecAdv();
139 public Advertisement newInstance(net.jxta.document.Element root) {
140 if (!XMLElement.class.isInstance(root)) {
141 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
144 return new ModuleSpecAdv((XMLElement) root);
149 * Private constructor for new instances. Use the instantiator.
151 private ModuleSpecAdv() {}
154 * Private constructor for xml serialized instances. Use the instantiator.
156 * @param doc The XML serialization of the advertisement.
158 private ModuleSpecAdv(XMLElement doc) {
159 String doctype = doc.getName();
161 String typedoctype = "";
162 Attribute itsType = doc.getAttribute("type");
164 if (null != itsType) {
165 typedoctype = itsType.getValue();
168 if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
169 throw new IllegalArgumentException(
170 "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
173 Enumeration elements = doc.getChildren();
175 while (elements.hasMoreElements()) {
176 XMLElement elem = (XMLElement) elements.nextElement();
178 if (!handleElement(elem)) {
179 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
180 LOG.fine("Unhandled Element: " + elem.toString());
186 if (null == getModuleSpecID()) {
187 throw new IllegalArgumentException("Module Spec Advertisement did not contain a module spec id.");
195 public String getAdvType() {
196 return getAdvertisementType();
203 protected boolean handleElement(Element raw) {
205 if (super.handleElement(raw)) {
209 XMLElement elem = (XMLElement) raw;
211 String nm = elem.getName();
213 if (nm.equals(nameTag)) {
214 setName(elem.getTextValue());
218 if (nm.equals(descTag)) {
223 if (nm.equals(idTag)) {
225 URI specID = new URI(elem.getTextValue());
227 setModuleSpecID((ModuleSpecID) IDFactory.fromURI(specID));
228 } catch (URISyntaxException badID) {
229 throw new IllegalArgumentException("Bad msid in advertisement");
230 } catch (ClassCastException badID) {
231 throw new IllegalArgumentException("Unusable msid in advertisement");
236 if (nm.equals(creatorTag)) {
237 setCreator(elem.getTextValue());
241 if (nm.equals(uriTag)) {
242 setSpecURI(elem.getTextValue());
246 if (nm.equals(versTag)) {
247 setVersion(elem.getTextValue());
251 if (nm.equals(paramTag)) {
252 // Copy the element into a complete new document
253 // which type matches the element name. There is no
254 // API Advertisement for it, each module implementation
255 // may have its own Advertisement subclass for its param.
260 if (nm.equals(proxyIdTag)) {
262 URI spID = new URI(elem.getTextValue());
264 setProxySpecID((ModuleSpecID) IDFactory.fromURI(spID));
265 } catch (URISyntaxException badID) {
266 throw new IllegalArgumentException("Bad proxy spec id in advertisement");
267 } catch (ClassCastException badID) {
268 throw new IllegalArgumentException("Unusable proxy spec id in advertisement");
273 if (nm.equals(authIdTag)) {
275 URI spID = new URI(elem.getTextValue());
277 setAuthSpecID((ModuleSpecID) IDFactory.fromURI(spID));
278 } catch (URISyntaxException badID) {
279 throw new IllegalArgumentException("Bad auth spec id in advertisement");
280 } catch (ClassCastException badID) {
281 throw new IllegalArgumentException("Unusable auth spec id in advertisement");
286 if (nm.equals(PipeAdvertisement.getAdvertisementType())) {
288 PipeAdvertisement pipeAdv = (PipeAdvertisement)
289 AdvertisementFactory.newAdvertisement(elem);
291 setPipeAdvertisement(pipeAdv);
292 } catch (ClassCastException wrongAdv) {
293 throw new IllegalArgumentException("Bad pipe advertisement in advertisement");
305 public Document getDocument(MimeMediaType encodeAs) {
306 if (null == getModuleSpecID()) {
307 throw new IllegalStateException("Module Spec Advertisement did not contain a module spec id.");
310 StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
314 e = adv.createElement(idTag, getModuleSpecID().toString());
317 if (null != getName()) {
318 e = adv.createElement(nameTag, getName());
323 StructuredDocument desc = getDesc();
326 StructuredDocumentUtils.copyElements(adv, adv, desc);
329 e = adv.createElement(creatorTag, getCreator());
332 e = adv.createElement(uriTag, getSpecURI());
335 e = adv.createElement(versTag, getVersion());
338 PipeAdvertisement pipeAdv = getPipeAdvertisement();
340 if (pipeAdv != null) {
341 StructuredTextDocument advDoc = (StructuredTextDocument)
342 pipeAdv.getDocument(encodeAs);
344 StructuredDocumentUtils.copyElements(adv, adv, advDoc);
346 ModuleSpecID tmpId = getProxySpecID();
349 e = adv.createElement(proxyIdTag, tmpId.toString());
352 tmpId = getAuthSpecID();
354 e = adv.createElement(authIdTag, tmpId.toString());
359 // Copy the param document as an element of adv.
361 // Force the element to be named "Parm" even if that is not
362 // the name of paramDoc.
363 StructuredDocumentUtils.copyElements(adv, adv, e, paramTag);
373 public String[] getIndexFields() {