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.util;
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;
83 import java.io.ByteArrayInputStream;
84 import java.io.ByteArrayOutputStream;
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;
94 import java.net.URISyntaxException;
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
104 public final class AdvertisementUtilities {
109 // private static final transient Logger LOG = Logger.getLogger(AdvertisementUtilities.class.getName());
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";
117 Element element = STANDARD_COMPATABILITY.createElement("Efmt", "JDK1.4.1");
119 STANDARD_COMPATABILITY.appendChild(element);
120 element = STANDARD_COMPATABILITY.createElement("Bind", "V2.0 Ref Impl");
121 STANDARD_COMPATABILITY.appendChild(element);
124 private AdvertisementUtilities() {}
127 * Read a JXTA Advertisement from a File
129 * @param fileName The file containing the Advertisement
130 * @return An polymorphic Advertisement object
131 * @throws JxtaException if Unable to parse the Advertisement
133 public static Advertisement readAdvertisementFromFile(String fileName) throws JxtaException {
134 return readAdvertisementFromFile(new File(fileName));
138 * Read a JXTA Advertisement from a File
140 * @param file The file containing the Advertisement
141 * @return An polymorphic Advertisement object
142 * @throws JxtaException if Unable to parse the Advertisement
144 public static Advertisement readAdvertisementFromFile(File file) throws JxtaException {
145 FileInputStream in = null;
148 in = new FileInputStream(file);
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);
159 } catch (IOException ignored) {// ignored
166 * Save a JXTA Advertisement as an XML Document to a File
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
172 public static void saveAdvertisementToFile(Advertisement adv, String fileName) throws JxtaException {
173 saveAdvertisementToFile(adv, new File(fileName));
177 * Save a JXTA Advertisement as an XML Document to a File
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
183 public static void saveAdvertisementToFile(Advertisement adv, File file) throws JxtaException {
184 OutputStream out = null;
187 out = new FileOutputStream(file);
188 Document document = adv.getDocument(MimeMediaType.XML_DEFAULTENCODING);
190 document.sendToStream(out);
191 } catch (IOException e) {} finally {
195 } catch (IOException ignored) {// ignored
202 * Save a JXTA Advertisement as an XML Document to an array of bytes
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.
210 public static byte[] advertisementToBytes(Advertisement advertisement) throws JxtaException {
212 Document document = advertisement.getDocument(MimeMediaType.XML_DEFAULTENCODING);
213 ByteArrayOutputStream bo = new ByteArrayOutputStream();
215 document.sendToStream(bo);
217 return bo.toByteArray();
218 } catch (IOException e) {
219 throw new JxtaException("Error converting a document to bytes", e);
224 * Convert an array of bytes containing an XML encoded String to an JXTA Advertisement
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.
233 public static Advertisement bytesToAdvertisement(byte buf[]) {
235 InputStream in = new ByteArrayInputStream(buf);
236 Advertisement advertisement = AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, in);
238 return advertisement;
239 } catch (IOException e) {
241 } // This will never be thrown
245 * Save a JXTA Advertisement to a String
247 * @param advertisement The Advertisement to be converted
248 * @param mimeType Type of document to be created
250 public static String advertisementToText(Advertisement advertisement, String mimeType) {
252 StructuredTextDocument doc = (StructuredTextDocument) advertisement.getDocument(new MimeMediaType(mimeType));
253 StringWriter stringWriter = new StringWriter();
255 doc.sendToWriter(stringWriter);
257 return stringWriter.toString();
258 } catch (IOException e) {
260 } // This will never be thrown
264 * Save a JXTA Advertisement to a Plain Text String
266 * @param advertisement The Advertisement to be converted
268 public static String advertisementToPlainText(Advertisement advertisement) {
269 return advertisementToText(advertisement, MimeMediaType.TEXT_DEFAULTENCODING.toString());
273 * Save a JXTA Advertisement to an XML String
275 * @param advertisement The Advertisement to be converted
276 * @deprecated Equivalent to Advertisement.toString()
279 public static String advertisementToXmlText(Advertisement advertisement) {
280 return advertisementToText(advertisement, MimeMediaType.XMLUTF8.toString());
284 * Convert an array of bytes containing an XML encoded String to an JXTA Advertisement
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.
293 public static Advertisement newAdvertisementFromXml(byte xmlTextAsBytes[]) throws JxtaException {
295 return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING
297 new ByteArrayInputStream(xmlTextAsBytes));
298 } catch (Exception e) {
299 throw new JxtaException("Unable to create Advertisement from the provided XML", e);
304 * Convert a String containing an XML encoded String to an JXTA Advertisement
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.
314 public static Advertisement newAdvertisementFromXml(String xmlText) throws JxtaException {
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);
323 * Create a Pipe Advertisement
325 * @return A new Pipe Advertisement
327 public static PipeAdvertisement createPipeAdvertisement() {
328 return (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
332 * Create a Pipe Advertisement
334 * @param pipeId The pipe ID
335 * @param pipeType The type of the Pipe
336 * @return A new Pipe Advertisement
338 public static PipeAdvertisement createPipeAdvertisement(PipeID pipeId, String pipeType) {
339 PipeAdvertisement pipeAdvertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(
340 PipeAdvertisement.getAdvertisementType());
342 pipeAdvertisement.setPipeID(pipeId);
343 pipeAdvertisement.setType(pipeType);
344 return pipeAdvertisement;
348 * Create a Pipe Advertisement
350 * @param pipeIdText The pipe ID
351 * @param pipeType The type of the Pipe
352 * @return A new Pipe Advertisement
354 public static PipeAdvertisement createPipeAdvertisement(String pipeIdText, String pipeType) throws JxtaException {
358 pipeId = (PipeID) IDFactory.fromURI(new URI(pipeIdText));
359 } catch (URISyntaxException failed) {
360 IllegalArgumentException failure = new IllegalArgumentException("Bad pipe id");
362 failure.initCause(failed);
366 PipeAdvertisement pipeAdvertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(
367 PipeAdvertisement.getAdvertisementType());
369 pipeAdvertisement.setPipeID(pipeId);
370 pipeAdvertisement.setType(pipeType);
371 return pipeAdvertisement;
375 * Create a Pipe Advertisement
377 * @param root Element containing a Pipe Advertisement
378 * @return A new Pipe Advertisement
380 public static PipeAdvertisement createPipeAdvertisement(Element root) {
381 TextElement pipeAdvElement = (TextElement) DocumentUtilities.getChild(root, PipeAdvertisement.getAdvertisementType());
383 if (pipeAdvElement == null) {
387 return (PipeAdvertisement) AdvertisementFactory.newAdvertisement(pipeAdvElement);
391 * Create a Pipe Advertisement
393 * @param peerGroup The peerGroup
394 * @param pipeType The pipeType
395 * @return A new Pipe Advertisement
397 public static PipeAdvertisement createPipeAdvertisement(PeerGroup peerGroup, String pipeType) {
398 PipeID pipeID = IDFactory.newPipeID(peerGroup.getPeerGroupID());
400 return createPipeAdvertisement(pipeID, pipeType);
404 * Create a Pipe Advertisement
406 * @param pipeID The pipeID
407 * @param pipeType The pipeType
408 * @return A new Pipe Advertisement
410 public static PipeAdvertisement createPipeAdvertisement(ID pipeID, String pipeType) {
411 PipeAdvertisement pipeAdvertisement = createPipeAdvertisement();
413 pipeAdvertisement.setPipeID(pipeID);
414 pipeAdvertisement.setType(pipeType);
415 return pipeAdvertisement;
419 * Create a Pipe Advertisement
421 * @param peerGroup The peerGroup
422 * @param pipeType The pipeType
423 * @param name The Pime Name
424 * @return A new Pipe Advertisement
426 public static PipeAdvertisement createPipeAdvertisement(PeerGroup peerGroup, String sPipeID, String pipeType, String name) throws JxtaException {
427 PipeAdvertisement pipeAdvertisement = createPipeAdvertisement(peerGroup, pipeType);
431 pipeId = (PipeID) IDFactory.fromURI(new URI(sPipeID));
432 } catch (URISyntaxException failed) {
433 IllegalArgumentException failure = new IllegalArgumentException("Bad pipe id");
435 failure.initCause(failed);
439 pipeAdvertisement.setPipeID(pipeId);
441 pipeAdvertisement.setName(name);
444 return pipeAdvertisement;
448 * Create a Pipe Advertisement
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.
455 public static PipeAdvertisement getPipeAdvertisement(Element root) {
456 TextElement pipeAdvElement = (TextElement) DocumentUtilities.getChild(root, PipeAdvertisement.getAdvertisementType());
458 if (pipeAdvElement == null) {
462 return (PipeAdvertisement) AdvertisementFactory.newAdvertisement(pipeAdvElement);
466 * Create a Peer Advertisement
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.
473 public static PeerAdvertisement getPeerAdvertisement(Element root) {
474 TextElement peerAdvElement = (TextElement) DocumentUtilities.getChild(root, PeerAdvertisement.getAdvertisementType());
476 if (peerAdvElement == null) {
480 return (PeerAdvertisement) AdvertisementFactory.newAdvertisement(peerAdvElement);
484 * Create a ModuleClassAdvertisement
486 * @param name The name
487 * @param description The description
488 * @return An ModuleClassAdvertisement
490 public static ModuleClassAdvertisement createModuleClassAdvertisement(String name, String description) {
491 String moduleClassAdvertisementType = ModuleClassAdvertisement.getAdvertisementType();
492 ModuleClassAdvertisement moduleClassAdvertisement = (ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement(
493 moduleClassAdvertisementType);
495 moduleClassAdvertisement.setName(name);
496 moduleClassAdvertisement.setDescription(description);
498 ModuleClassID mcID = IDFactory.newModuleClassID();
500 moduleClassAdvertisement.setModuleClassID(mcID);
501 return moduleClassAdvertisement;
505 * Create a ModuleSpecAdvertisement
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
515 public static ModuleSpecAdvertisement createModuleSpecAdvertisement(String name, StructuredDocument param) {
516 return createModuleSpecAdvertisement(name, null, param);
520 * Create a ModuleSpecAdvertisement
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
531 public static ModuleSpecAdvertisement createModuleSpecAdvertisement(String name, ModuleClassAdvertisement moduleClassAdvertisement, StructuredDocument param) {
532 String moduleSpecAdvertisementType = ModuleSpecAdvertisement.getAdvertisementType();
533 ModuleSpecAdvertisement moduleSpecAdvertisement = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement(
534 moduleSpecAdvertisementType);
536 moduleSpecAdvertisement.setName(name);
537 moduleSpecAdvertisement.setVersion("Unknown");
538 moduleSpecAdvertisement.setCreator("Unknown");
540 if (moduleClassAdvertisement != null) {
541 ModuleClassID moduleClassID = moduleClassAdvertisement.getModuleClassID();
543 moduleSpecAdvertisement.setModuleSpecID(IDFactory.newModuleSpecID(moduleClassID));
546 moduleSpecAdvertisement.setSpecURI("Unknown");
549 moduleSpecAdvertisement.setParam(param);
552 return moduleSpecAdvertisement;
556 * Publish and advertisement to the Cache
558 * @param peerGroup The peerGroup
559 * @param peerAdvertisement The Advertisement
560 * @throws JxtaException if Unable to cache the Advertisement
562 public static void cachePeerAdvertisement(PeerGroup peerGroup, PeerAdvertisement peerAdvertisement) throws JxtaException {
563 cachePeerAdvertisement(peerGroup, peerAdvertisement, DiscoveryService.DEFAULT_EXPIRATION
565 DiscoveryService.DEFAULT_EXPIRATION);
568 private static void cachePeerAdvertisement(PeerGroup peerGroup, PeerAdvertisement peerAdvertisement, long lifetime, long lifetimeForOthers) throws JxtaException {
570 DiscoveryService discoveryService = peerGroup.getDiscoveryService();
572 if (peerAdvertisement.getPeerID().equals(peerGroup.getPeerID())) {
574 } // no reason to persist our own peer ID
576 discoveryService.publish(peerAdvertisement, lifetime, lifetimeForOthers);
577 } catch (IOException e) {
578 throw new JxtaException("Unable to cache advertisement", e);
583 * Create a ModuleImplAdvertisement
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.
595 public static ModuleImplAdvertisement createModuleImplAdvertisement(ModuleSpecID specID, String code, String description) {
597 ModuleImplAdvertisement moduleImplAdvertisement = (ModuleImplAdvertisement)
598 AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());
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;