]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/api/src/net/jxta/util/AdvertisementUtilities.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / api / src / net / jxta / util / AdvertisementUtilities.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.util;
58
59
60 import net.jxta.discovery.DiscoveryService;
61 import net.jxta.document.Advertisement;
62 import net.jxta.document.AdvertisementFactory;
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.StructuredDocumentFactory;
68 import net.jxta.document.StructuredTextDocument;
69 import net.jxta.document.TextElement;
70 import net.jxta.exception.JxtaException;
71 import net.jxta.id.ID;
72 import net.jxta.id.IDFactory;
73 import net.jxta.peergroup.PeerGroup;
74 import net.jxta.pipe.PipeID;
75 import net.jxta.platform.ModuleClassID;
76 import net.jxta.platform.ModuleSpecID;
77 import net.jxta.protocol.ModuleClassAdvertisement;
78 import net.jxta.protocol.ModuleImplAdvertisement;
79 import net.jxta.protocol.ModuleSpecAdvertisement;
80 import net.jxta.protocol.PeerAdvertisement;
81 import net.jxta.protocol.PipeAdvertisement;
82
83 import java.io.ByteArrayInputStream;
84 import java.io.ByteArrayOutputStream;
85 import java.io.File;
86 import java.io.FileInputStream;
87 import java.io.FileOutputStream;
88 import java.io.IOException;
89 import java.io.InputStream;
90 import java.io.OutputStream;
91 import java.io.StringReader;
92 import java.io.StringWriter;
93 import java.net.URI;
94 import java.net.URISyntaxException;
95
96
97 /**
98  * @deprecated Will be deprecated soon. Do not use these methods. They contain
99  *             a number of incorrect assumption that cannot be corrected while maintaining
100  *             backwards compatibility with programs which already use them.
101  *             THIS CLASS IS SCHEDULED TO BE REMOVED AFTER 2.5
102  */
103 @Deprecated
104 public final class AdvertisementUtilities {
105
106     /**
107      * Logger
108      */
109     // private static final transient Logger LOG = Logger.getLogger(AdvertisementUtilities.class.getName());
110
111     public static final StructuredTextDocument STANDARD_COMPATABILITY = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(
112             MimeMediaType.XMLUTF8, "Comp");
113     public static final String STANDARD_URI = "http://www.jxta.org/download/jxta.jar";
114     public static final String STANDARD_PROVIDER = "jxta.org";
115
116     static {
117         Element element = STANDARD_COMPATABILITY.createElement("Efmt", "JDK1.4.1");
118
119         STANDARD_COMPATABILITY.appendChild(element);
120         element = STANDARD_COMPATABILITY.createElement("Bind", "V2.0 Ref Impl");
121         STANDARD_COMPATABILITY.appendChild(element);
122     }
123
124     private AdvertisementUtilities() {}
125
126     /**
127      * Read a JXTA Advertisement from a File
128      *
129      * @param fileName The file containing the Advertisement
130      * @return An polymorphic Advertisement object
131      * @throws JxtaException if Unable to parse the Advertisement
132      */
133     public static Advertisement readAdvertisementFromFile(String fileName) throws JxtaException {
134         return readAdvertisementFromFile(new File(fileName));
135     }
136
137     /**
138      * Read a JXTA Advertisement from a File
139      *
140      * @param file The file containing the Advertisement
141      * @return An polymorphic Advertisement object
142      * @throws JxtaException if Unable to parse the Advertisement
143      */
144     public static Advertisement readAdvertisementFromFile(File file) throws JxtaException {
145         FileInputStream in = null;
146
147         try {
148             in = new FileInputStream(file);
149
150             return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, in);
151         } catch (IOException e) {
152             throw new JxtaException("Advertisement Load Failed: " + file, e);
153         } catch (Exception e) {
154             throw new JxtaException("Advertisement Load Failed: " + file, e);
155         } finally {
156             if (in != null) {
157                 try {
158                     in.close();
159                 } catch (IOException ignored) {// ignored
160                 }
161             }
162         }
163     }
164
165     /**
166      * Save a JXTA Advertisement as an XML Document to a File
167      *
168      * @param adv      The Advertisement to be saved
169      * @param fileName The file to store the Advertisement
170      * @throws JxtaException if Unable to parse the Advertisement
171      */
172     public static void saveAdvertisementToFile(Advertisement adv, String fileName) throws JxtaException {
173         saveAdvertisementToFile(adv, new File(fileName));
174     }
175
176     /**
177      * Save a JXTA Advertisement as an XML Document to a File
178      *
179      * @param adv  The Advertisement to be saved
180      * @param file The file to store the Advertisement
181      * @throws JxtaException if Unable to parse the Advertisement
182      */
183     public static void saveAdvertisementToFile(Advertisement adv, File file) throws JxtaException {
184         OutputStream out = null;
185
186         try {
187             out = new FileOutputStream(file);
188             Document document = adv.getDocument(MimeMediaType.XML_DEFAULTENCODING);
189
190             document.sendToStream(out);
191         } catch (IOException e) {} finally {
192             if (out != null) {
193                 try {
194                     out.close();
195                 } catch (IOException ignored) {// ignored
196                 }
197             }
198         }
199     }
200
201     /**
202      * Save a JXTA Advertisement as an XML Document to an array of bytes
203      *
204      * @param advertisement The Advertisement to be saved
205      * @throws JxtaException if Unable to parse the Advertisement
206      * @deprecated This method should not be used because it produces a result
207      *             who's encoding is not predictable and may (will) differ from JVM to JVM.
208      */
209     @Deprecated
210     public static byte[] advertisementToBytes(Advertisement advertisement) throws JxtaException {
211         try {
212             Document document = advertisement.getDocument(MimeMediaType.XML_DEFAULTENCODING);
213             ByteArrayOutputStream bo = new ByteArrayOutputStream();
214
215             document.sendToStream(bo);
216
217             return bo.toByteArray();
218         } catch (IOException e) {
219             throw new JxtaException("Error converting a document to bytes", e);
220         }
221     }
222
223     /**
224      * Convert an array of bytes containing an XML encoded String to an JXTA Advertisement
225      *
226      * @param buf The source of the advertisement
227      * @return The Advertisement
228      * @deprecated This method should not be used because it interprets the
229      *             input using the local default encoding which is not predictable and
230      *             may (will) differ from JVM to JVM.
231      */
232     @Deprecated
233     public static Advertisement bytesToAdvertisement(byte buf[]) {
234         try {
235             InputStream in = new ByteArrayInputStream(buf);
236             Advertisement advertisement = AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, in);
237
238             return advertisement;
239         } catch (IOException e) {
240             return null;
241         } // This will never be thrown
242     }
243
244     /**
245      * Save a JXTA Advertisement to a String
246      *
247      * @param advertisement The Advertisement to be converted
248      * @param mimeType      Type of document to be created
249      */
250     public static String advertisementToText(Advertisement advertisement, String mimeType) {
251         try {
252             StructuredTextDocument doc = (StructuredTextDocument) advertisement.getDocument(new MimeMediaType(mimeType));
253             StringWriter stringWriter = new StringWriter();
254
255             doc.sendToWriter(stringWriter);
256
257             return stringWriter.toString();
258         } catch (IOException e) {
259             return null;
260         } // This will never be thrown
261     }
262
263     /**
264      * Save a JXTA Advertisement to a Plain Text String
265      *
266      * @param advertisement The Advertisement to be converted
267      */
268     public static String advertisementToPlainText(Advertisement advertisement) {
269         return advertisementToText(advertisement, MimeMediaType.TEXT_DEFAULTENCODING.toString());
270     }
271
272     /**
273      * Save a JXTA Advertisement to an XML String
274      *
275      * @param advertisement The Advertisement to be converted
276      * @deprecated Equivalent to Advertisement.toString()
277      */
278     @Deprecated
279     public static String advertisementToXmlText(Advertisement advertisement) {
280         return advertisementToText(advertisement, MimeMediaType.XMLUTF8.toString());
281     }
282
283     /**
284      * Convert an array of bytes containing an XML encoded String to an JXTA Advertisement
285      *
286      * @param xmlTextAsBytes The source of the advertisement
287      * @return The Advertisement
288      * @throws JxtaException if Unable to parse the Advertisement
289      * @deprecated This method should not be used because it produces a result
290      *             who's encoding is not predictable and may (will) differ from JVM to JVM.
291      */
292     @Deprecated
293     public static Advertisement newAdvertisementFromXml(byte xmlTextAsBytes[]) throws JxtaException {
294         try {
295             return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING
296                     ,
297                     new ByteArrayInputStream(xmlTextAsBytes));
298         } catch (Exception e) {
299             throw new JxtaException("Unable to create Advertisement from the provided XML", e);
300         }
301     }
302
303     /**
304      * Convert a String containing an XML encoded String to an JXTA Advertisement
305      *
306      * @param xmlText The source of the advertisement
307      * @return The Advertisement
308      * @throws JxtaException if Unable to parse the Advertisement
309      * @deprecated This method should not be used because it interprets the
310      *             input using the local default encoding which is not precidcatable and
311      *             may (will) differ from JVM to JVM.
312      */
313     @Deprecated
314     public static Advertisement newAdvertisementFromXml(String xmlText) throws JxtaException {
315         try {
316             return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, new StringReader(xmlText));
317         } catch (Exception e) {
318             throw new JxtaException("Unable to create Advertisement from the provided XML", e);
319         }
320     }
321
322     /**
323      * Create a Pipe Advertisement
324      *
325      * @return A new Pipe Advertisement
326      */
327     public static PipeAdvertisement createPipeAdvertisement() {
328         return (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
329     }
330
331     /**
332      * Create a Pipe Advertisement
333      *
334      * @param pipeId   The pipe ID
335      * @param pipeType The type of the Pipe
336      * @return A new Pipe Advertisement
337      */
338     public static PipeAdvertisement createPipeAdvertisement(PipeID pipeId, String pipeType) {
339         PipeAdvertisement pipeAdvertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(
340                 PipeAdvertisement.getAdvertisementType());
341
342         pipeAdvertisement.setPipeID(pipeId);
343         pipeAdvertisement.setType(pipeType);
344         return pipeAdvertisement;
345     }
346
347     /**
348      * Create a Pipe Advertisement
349      *
350      * @param pipeIdText The pipe ID
351      * @param pipeType   The type of the Pipe
352      * @return A new Pipe Advertisement
353      */
354     public static PipeAdvertisement createPipeAdvertisement(String pipeIdText, String pipeType) throws JxtaException {
355         PipeID pipeId;
356
357         try {
358             pipeId = (PipeID) IDFactory.fromURI(new URI(pipeIdText));
359         } catch (URISyntaxException failed) {
360             IllegalArgumentException failure = new IllegalArgumentException("Bad pipe id");
361
362             failure.initCause(failed);
363
364             throw failure;
365         }
366         PipeAdvertisement pipeAdvertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(
367                 PipeAdvertisement.getAdvertisementType());
368
369         pipeAdvertisement.setPipeID(pipeId);
370         pipeAdvertisement.setType(pipeType);
371         return pipeAdvertisement;
372     }
373
374     /**
375      * Create a Pipe Advertisement
376      *
377      * @param root Element containing a Pipe Advertisement
378      * @return A new Pipe Advertisement
379      */
380     public static PipeAdvertisement createPipeAdvertisement(Element root) {
381         TextElement pipeAdvElement = (TextElement) DocumentUtilities.getChild(root, PipeAdvertisement.getAdvertisementType());
382
383         if (pipeAdvElement == null) {
384             return null;
385         }
386
387         return (PipeAdvertisement) AdvertisementFactory.newAdvertisement(pipeAdvElement);
388     }
389
390     /**
391      * Create a Pipe Advertisement
392      *
393      * @param peerGroup The peerGroup
394      * @param pipeType  The pipeType
395      * @return A new Pipe Advertisement
396      */
397     public static PipeAdvertisement createPipeAdvertisement(PeerGroup peerGroup, String pipeType) {
398         PipeID pipeID = IDFactory.newPipeID(peerGroup.getPeerGroupID());
399
400         return createPipeAdvertisement(pipeID, pipeType);
401     }
402
403     /**
404      * Create a Pipe Advertisement
405      *
406      * @param pipeID   The pipeID
407      * @param pipeType The pipeType
408      * @return A new Pipe Advertisement
409      */
410     public static PipeAdvertisement createPipeAdvertisement(ID pipeID, String pipeType) {
411         PipeAdvertisement pipeAdvertisement = createPipeAdvertisement();
412
413         pipeAdvertisement.setPipeID(pipeID);
414         pipeAdvertisement.setType(pipeType);
415         return pipeAdvertisement;
416     }
417
418     /**
419      * Create a Pipe Advertisement
420      *
421      * @param peerGroup The peerGroup
422      * @param pipeType  The pipeType
423      * @param name      The Pime Name
424      * @return A new Pipe Advertisement
425      */
426     public static PipeAdvertisement createPipeAdvertisement(PeerGroup peerGroup, String sPipeID, String pipeType, String name) throws JxtaException {
427         PipeAdvertisement pipeAdvertisement = createPipeAdvertisement(peerGroup, pipeType);
428         PipeID pipeId;
429
430         try {
431             pipeId = (PipeID) IDFactory.fromURI(new URI(sPipeID));
432         } catch (URISyntaxException failed) {
433             IllegalArgumentException failure = new IllegalArgumentException("Bad pipe id");
434
435             failure.initCause(failed);
436
437             throw failure;
438         }
439         pipeAdvertisement.setPipeID(pipeId);
440         if (name != null) {
441             pipeAdvertisement.setName(name);
442         }
443
444         return pipeAdvertisement;
445     }
446
447     /**
448      * Create a Pipe Advertisement
449      *
450      * @param root The Root element containing the Advertisement
451      * @return A new Pipe Advertisement
452      * @deprecated These utilities are too specialized for general use.
453      */
454     @Deprecated
455     public static PipeAdvertisement getPipeAdvertisement(Element root) {
456         TextElement pipeAdvElement = (TextElement) DocumentUtilities.getChild(root, PipeAdvertisement.getAdvertisementType());
457
458         if (pipeAdvElement == null) {
459             return null;
460         }
461
462         return (PipeAdvertisement) AdvertisementFactory.newAdvertisement(pipeAdvElement);
463     }
464
465     /**
466      * Create a Peer Advertisement
467      *
468      * @param root The Root element containing the Advertisement
469      * @return A new Peer Advertisement
470      * @deprecated These utilities are too specialized for general use.
471      */
472     @Deprecated
473     public static PeerAdvertisement getPeerAdvertisement(Element root) {
474         TextElement peerAdvElement = (TextElement) DocumentUtilities.getChild(root, PeerAdvertisement.getAdvertisementType());
475
476         if (peerAdvElement == null) {
477             return null;
478         }
479
480         return (PeerAdvertisement) AdvertisementFactory.newAdvertisement(peerAdvElement);
481     }
482
483     /**
484      * Create a ModuleClassAdvertisement
485      *
486      * @param name        The name
487      * @param description The description
488      * @return An ModuleClassAdvertisement
489      */
490     public static ModuleClassAdvertisement createModuleClassAdvertisement(String name, String description) {
491         String moduleClassAdvertisementType = ModuleClassAdvertisement.getAdvertisementType();
492         ModuleClassAdvertisement moduleClassAdvertisement = (ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement(
493                 moduleClassAdvertisementType);
494
495         moduleClassAdvertisement.setName(name);
496         moduleClassAdvertisement.setDescription(description);
497
498         ModuleClassID mcID = IDFactory.newModuleClassID();
499
500         moduleClassAdvertisement.setModuleClassID(mcID);
501         return moduleClassAdvertisement;
502     }
503
504     /**
505      * Create a ModuleSpecAdvertisement
506      *
507      * @param name  The name
508      * @param param The param
509      * @return An ModuleSpecAdvertisement
510      * @deprecated This implementation incompletely initializes the module
511      *             spec advertisement. Consider creating Module Spec Advertisements without
512      *             this method.
513      */
514     @Deprecated
515     public static ModuleSpecAdvertisement createModuleSpecAdvertisement(String name, StructuredDocument param) {
516         return createModuleSpecAdvertisement(name, null, param);
517     }
518
519     /**
520      * Create a ModuleSpecAdvertisement
521      *
522      * @param name                     The name
523      * @param moduleClassAdvertisement The moduleClassAdvertisement
524      * @param param                    The param
525      * @return An ModuleSpecAdvertisement
526      * @deprecated This implementation incompletely initializes the module
527      *             spec advertisement. Consider creating Module Spec Advertisements without
528      *             this method.
529      */
530     @Deprecated
531     public static ModuleSpecAdvertisement createModuleSpecAdvertisement(String name, ModuleClassAdvertisement moduleClassAdvertisement, StructuredDocument param) {
532         String moduleSpecAdvertisementType = ModuleSpecAdvertisement.getAdvertisementType();
533         ModuleSpecAdvertisement moduleSpecAdvertisement = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement(
534                 moduleSpecAdvertisementType);
535
536         moduleSpecAdvertisement.setName(name);
537         moduleSpecAdvertisement.setVersion("Unknown");
538         moduleSpecAdvertisement.setCreator("Unknown");
539
540         if (moduleClassAdvertisement != null) {
541             ModuleClassID moduleClassID = moduleClassAdvertisement.getModuleClassID();
542
543             moduleSpecAdvertisement.setModuleSpecID(IDFactory.newModuleSpecID(moduleClassID));
544         }
545
546         moduleSpecAdvertisement.setSpecURI("Unknown");
547
548         if (param != null) {
549             moduleSpecAdvertisement.setParam(param);
550         }
551
552         return moduleSpecAdvertisement;
553     }
554
555     /**
556      * Publish and advertisement to the Cache
557      *
558      * @param peerGroup         The peerGroup
559      * @param peerAdvertisement The Advertisement
560      * @throws JxtaException if Unable to cache the Advertisement
561      */
562     public static void cachePeerAdvertisement(PeerGroup peerGroup, PeerAdvertisement peerAdvertisement) throws JxtaException {
563         cachePeerAdvertisement(peerGroup, peerAdvertisement, DiscoveryService.DEFAULT_EXPIRATION
564                 ,
565                 DiscoveryService.DEFAULT_EXPIRATION);
566     }
567
568     private static void cachePeerAdvertisement(PeerGroup peerGroup, PeerAdvertisement peerAdvertisement, long lifetime, long lifetimeForOthers) throws JxtaException {
569         try {
570             DiscoveryService discoveryService = peerGroup.getDiscoveryService();
571
572             if (peerAdvertisement.getPeerID().equals(peerGroup.getPeerID())) {
573                 return;
574             }            // no reason to persist our own peer ID
575
576             discoveryService.publish(peerAdvertisement, lifetime, lifetimeForOthers);
577         } catch (IOException e) {
578             throw new JxtaException("Unable to cache advertisement", e);
579         }
580     }
581
582     /**
583      * Create a ModuleImplAdvertisement
584      *
585      * @param specID      The specID
586      * @param code        The code
587      * @param description the advertisement description
588      * @return An ModuleImplAdvertisement
589      * @deprecated This implementation initializes some fields of the
590      *             resulting ModuleImplAdvertisement to constant values who's value may
591      *             not be correct for all circumstances. Consider creating ModuleImpl
592      *             Advertisements directly in your application.
593      */
594     @Deprecated
595     public static ModuleImplAdvertisement createModuleImplAdvertisement(ModuleSpecID specID, String code, String description) {
596
597         ModuleImplAdvertisement moduleImplAdvertisement = (ModuleImplAdvertisement)
598                 AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());
599
600         moduleImplAdvertisement.setModuleSpecID(specID);
601         moduleImplAdvertisement.setCompat(STANDARD_COMPATABILITY);
602         moduleImplAdvertisement.setCode(code);
603         moduleImplAdvertisement.setUri(STANDARD_URI);
604         moduleImplAdvertisement.setProvider(STANDARD_PROVIDER);
605         moduleImplAdvertisement.setDescription(description);
606         return moduleImplAdvertisement;
607     }
608 }