]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/ModuleSpecAdv.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / ModuleSpecAdv.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.protocol;
58
59
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;
68
69 import java.net.URI;
70 import java.net.URISyntaxException;
71 import java.util.Enumeration;
72
73
74 /**
75  * Provides XML serialization support for ModuleSpecAdvertisement matching the
76  * schema defined by the JXTA Core Specification.
77  * <p/>
78  * <p/><pre>
79  *  &lt;xs:complexType name="MSA">
80  *    &lt;xs:sequence>
81  *      &lt;xs:element name="MSID" type="jxta:JXTAID" />
82  *      &lt;xs:element name="Name" type="xs:string" minOccurs="0" />
83  *      &lt;xs:element name="Desc" type="xs:anyType" minOccurs="0" />
84  *      &lt;xs:element name="Crtr" type="xs:string" minOccurs="0" />
85  *      &lt;xs:element name="SURI" type="xs:anyURI" minOccurs="0" />
86  *      &lt;xs:element name="Vers" type="xs:string" />
87  *      &lt;xs:element name="Parm" type="xs:anyType" minOccurs="0" />
88  *      &lt;xs:element ref="jxta:PipeAdvertisement" minOccurs="0" />
89  *      &lt;xs:element name="Proxy" type="xs:anyURI" minOccurs="0" />
90  *      &lt;xs:element name="Auth" type="jxta:JXTAID" minOccurs="0" />
91  *    &lt;/xs:sequence>
92  *  &lt;/xs:complexType>
93  * </pre>
94  *
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>
98  */
99 public class ModuleSpecAdv extends ModuleSpecAdvertisement {
100
101     /**
102      * Logger
103      */
104     private static final Logger LOG = Logger.getLogger(ModuleSpecAdv.class.getName());
105
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};
116
117     public static class Instantiator implements AdvertisementFactory.Instantiator {
118
119         /**
120          * {@inheritDoc}
121          */
122
123         public String getAdvertisementType() {
124             return ModuleSpecAdv.getAdvertisementType();
125         }
126
127         /**
128          * {@inheritDoc}
129          */
130
131         public Advertisement newInstance() {
132             return new ModuleSpecAdv();
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138
139         public Advertisement newInstance(net.jxta.document.Element root) {
140             if (!XMLElement.class.isInstance(root)) {
141                 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
142             }
143
144             return new ModuleSpecAdv((XMLElement) root);
145         }
146     }
147
148     /**
149      *  Private constructor for new instances. Use the instantiator.
150      */
151     private ModuleSpecAdv() {}
152
153     /**
154      *  Private constructor for xml serialized instances. Use the instantiator.
155      *  
156      *  @param doc The XML serialization of the advertisement.
157      */
158     private ModuleSpecAdv(XMLElement doc) {
159         String doctype = doc.getName();
160
161         String typedoctype = "";
162         Attribute itsType = doc.getAttribute("type");
163
164         if (null != itsType) {
165             typedoctype = itsType.getValue();
166         }
167
168         if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
169             throw new IllegalArgumentException(
170                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
171         }
172
173         Enumeration elements = doc.getChildren();
174
175         while (elements.hasMoreElements()) {
176             XMLElement elem = (XMLElement) elements.nextElement();
177
178             if (!handleElement(elem)) {
179                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
180                     LOG.fine("Unhandled Element: " + elem.toString());
181                 }
182             }
183         }
184
185         // Sanity Check!!!
186         if (null == getModuleSpecID()) {
187             throw new IllegalArgumentException("Module Spec Advertisement did not contain a module spec id.");
188         }
189     }
190
191     /**
192      * {@inheritDoc}
193      */
194     @Override
195     public String getAdvType() {
196         return getAdvertisementType();
197     }
198
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     protected boolean handleElement(Element raw) {
204
205         if (super.handleElement(raw)) {
206             return true;
207         }
208
209         XMLElement elem = (XMLElement) raw;
210
211         String nm = elem.getName();
212
213         if (nm.equals(nameTag)) {
214             setName(elem.getTextValue());
215             return true;
216         }
217
218         if (nm.equals(descTag)) {
219             setDesc(elem);
220             return true;
221         }
222
223         if (nm.equals(idTag)) {
224             try {
225                 URI specID = new URI(elem.getTextValue());
226
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");
232             }
233             return true;
234         }
235
236         if (nm.equals(creatorTag)) {
237             setCreator(elem.getTextValue());
238             return true;
239         }
240
241         if (nm.equals(uriTag)) {
242             setSpecURI(elem.getTextValue());
243             return true;
244         }
245
246         if (nm.equals(versTag)) {
247             setVersion(elem.getTextValue());
248             return true;
249         }
250
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.
256             setParam(elem);
257             return true;
258         }
259
260         if (nm.equals(proxyIdTag)) {
261             try {
262                 URI spID = new URI(elem.getTextValue());
263
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");
269             }
270             return true;
271         }
272
273         if (nm.equals(authIdTag)) {
274             try {
275                 URI spID = new URI(elem.getTextValue());
276
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");
282             }
283             return true;
284         }
285
286         if (nm.equals(PipeAdvertisement.getAdvertisementType())) {
287             try {
288                 PipeAdvertisement pipeAdv = (PipeAdvertisement)
289                         AdvertisementFactory.newAdvertisement(elem);
290
291                 setPipeAdvertisement(pipeAdv);
292             } catch (ClassCastException wrongAdv) {
293                 throw new IllegalArgumentException("Bad pipe advertisement in advertisement");
294             }
295             return true;
296         }
297
298         return false;
299     }
300
301     /**
302      * {@inheritDoc}
303      */
304     @Override
305     public Document getDocument(MimeMediaType encodeAs) {
306         if (null == getModuleSpecID()) {
307             throw new IllegalStateException("Module Spec Advertisement did not contain a module spec id.");
308         }
309
310         StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs);
311
312         Element e;
313
314         e = adv.createElement(idTag, getModuleSpecID().toString());
315         adv.appendChild(e);
316
317         if (null != getName()) {
318             e = adv.createElement(nameTag, getName());
319             adv.appendChild(e);
320         }
321
322         // desc is optional
323         StructuredDocument desc = getDesc();
324
325         if (desc != null) {
326             StructuredDocumentUtils.copyElements(adv, adv, desc);
327         }
328
329         e = adv.createElement(creatorTag, getCreator());
330         adv.appendChild(e);
331
332         e = adv.createElement(uriTag, getSpecURI());
333         adv.appendChild(e);
334
335         e = adv.createElement(versTag, getVersion());
336         adv.appendChild(e);
337
338         PipeAdvertisement pipeAdv = getPipeAdvertisement();
339
340         if (pipeAdv != null) {
341             StructuredTextDocument advDoc = (StructuredTextDocument)
342                     pipeAdv.getDocument(encodeAs);
343
344             StructuredDocumentUtils.copyElements(adv, adv, advDoc);
345         }
346         ModuleSpecID tmpId = getProxySpecID();
347
348         if (tmpId != null) {
349             e = adv.createElement(proxyIdTag, tmpId.toString());
350             adv.appendChild(e);
351         }
352         tmpId = getAuthSpecID();
353         if (tmpId != null) {
354             e = adv.createElement(authIdTag, tmpId.toString());
355             adv.appendChild(e);
356         }
357
358         e = getParamPriv();
359         // Copy the param document as an element of adv.
360         if (e != null) {
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);
364         }
365
366         return adv;
367     }
368
369     /**
370      * {@inheritDoc}
371      */
372     @Override
373     public String[] getIndexFields() {
374         return fields;
375     }
376 }