]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/api/src/net/jxta/peergroup/PeerGroupFactory.java
bc182e5a670818393c1dc2f20552a857e47e08e1
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / api / src / net / jxta / peergroup / PeerGroupFactory.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.peergroup;
58
59
60 import net.jxta.document.Advertisement;
61 import net.jxta.document.MimeMediaType;
62 import net.jxta.document.StructuredDocumentFactory;
63 import net.jxta.document.XMLDocument;
64 import net.jxta.document.XMLElement;
65 import net.jxta.exception.ConfiguratorException;
66 import net.jxta.exception.JxtaError;
67 import net.jxta.exception.PeerGroupException;
68 import net.jxta.id.ID;
69 import net.jxta.logging.Logging;
70 import net.jxta.protocol.ConfigParams;
71
72 import java.io.File;
73 import java.io.IOException;
74 import java.lang.reflect.Constructor;
75 import java.lang.reflect.InvocationTargetException;
76 import java.net.URI;
77 import java.net.URLConnection;
78 import java.util.MissingResourceException;
79 import java.util.PropertyResourceBundle;
80 import java.util.ResourceBundle;
81 import java.util.logging.Level;
82 import java.util.logging.Logger;
83
84
85 /**
86  * A factory for instantiating the JXTA core peer groups.
87  * <p/>
88  * JXTA comes with two peergroup implementations:
89  * <p/>
90  * <dl>
91  * <DT><strong>Platform</strong></DT>
92  * <DD>Implements the world peer group. Every peer starts by instantiating this
93  * peer group and then other peer groups are instantiated as needed. The World
94  * Peer Group's ID is invariant.
95  * <p/>
96  * The world peer group provides the minimum core services needed to find
97  * and instantiate other groups on a peer. The <strong>Platform</strong>
98  * implementation will assign a new ID to the peer, if it does not already have
99  * one.</DD>
100  * <p/>
101  * <DT><strong>StdPeergroup</strong></DT>
102  * <DD>This is currently used to implement all other kinds of peer groups.
103  * The first such peer group that it is instantiated after starting is known as
104  * <em>The Net Peer Group</em>. When the <strong>Platform</strong> starts it may
105  * optionally search for <em>The Net Peer Group</em> on the local network and,
106  * if found, instantiate it. Otherwise a default built-in configuration of
107  * <em>The Net Peer Group</em> is instantiated.
108  * <p/>
109  * A non-default configuration of <em>The Net Peer Group</em> may be set-up
110  * by the administrator in charge of the network domain inside which the peer
111  * is starting. <em>The Net Peer Group</em> is discovered via the Discovery
112  * protocol. Many such groups may be configured by an administrator.<br>
113  * <p/>
114  * <strong>StdPeergroup</strong> may also be used to implement User-defined
115  * peer groups--Users can create new peer groups which use their own set of
116  * customized services.</DD>
117  * </dl>
118  *
119  * @see net.jxta.peergroup.PeerGroup
120  * @deprecated This factory has been deprecated in favour of {@link WorldPeerGroupFactory}
121  *             and {@link NetPeerGroupFactory}. See the deprecations for the individual
122  *             methods for the specific replacements/alternatives provided by the new
123  *             factory classes.
124  */
125 @Deprecated
126 public final class PeerGroupFactory {
127
128     /**
129      * Logger
130      */
131     private final static transient Logger LOG = Logger.getLogger(PeerGroupFactory.class.getName());
132
133     /**
134      * Constant for specifying no configurator. This configurator provides no
135      * configuration actions but does ensure that a valid configuration exists
136      * at the specified location.
137      */
138     public final static Class NULL_CONFIGURATOR = net.jxta.impl.peergroup.NullConfigurator.class;
139
140     /**
141      * Constant for specifying the default configurator. Currently this is the
142      * familiar AWT-based dialogue but in future is likely to become the
143      * UI-less automatic configurator.
144      */
145     public final static Class DEFAULT_CONFIGURATOR = net.jxta.impl.peergroup.DefaultConfigurator.class;
146
147     /**
148      * The class which will be instantiated as the World Peer Group.
149      */
150     private static Class worldGroupClass = null;
151
152     /**
153      * The ID of the network peer group.
154      */
155     private static PeerGroupID netPGID = null;
156
157     /**
158      * The name of the network peer group.
159      */
160     private static String netPGName = null;
161
162     /**
163      * The description of the network peer group.
164      */
165     private static String netPGDesc = null;
166
167     /**
168      * The class which will be instantiated to configure the World Peer
169      * Group.
170      */
171     private static Class configurator = DEFAULT_CONFIGURATOR;
172
173     /**
174      * the location which will serve as the parent for all stored items used
175      * by JXTA.
176      */
177     private static URI storeHome = null;
178
179     /**
180      * Static Method to initialize the world peer group class.
181      *
182      * @param c The Class which will be instantiated for the World Peer Group
183      * @deprecated Consider converting to use {@link WorldPeerGroupFactory#WorldPeerGroupFactory(Class,ConfigParams,URI)}.
184      */
185     @Deprecated
186     public static void setPlatformClass(Class c) {
187         worldGroupClass = c;
188     }
189
190     /**
191      * Static Method to initialize the std peer group class.
192      *
193      * @param c The Class which will be instantiated for most peer groups.
194      * @deprecated This method previously had no effect and has been removed with no alternatives.
195      */
196     @Deprecated
197     public static void setStdPeerGroupClass(Class c) {
198         throw new UnsupportedOperationException("This feature has been removed. (sorry)");
199     }
200
201     /**
202      * Sets the description which will be used for new net peer group instances.
203      *
204      * @param desc The description which will be used for new net peer group instances.
205      * @deprecated Consider converting to use {@link NetPeerGroupFactory#NetPeerGroupFactory(ConfigParams,URI,ID,String,XMLElement)}
206      *             or {@link NetPeerGroupFactory#NetPeerGroupFactory(PeerGroup,ID,String,XMLElement)}.
207      */
208     @Deprecated
209     public static void setNetPGDesc(String desc) {
210         netPGDesc = desc;
211     }
212
213     /**
214      * Sets the name which will be used for new net peer group instances.
215      *
216      * @param name The name which will be used for new net peer group instances.
217      * @deprecated Consider converting to use {@link NetPeerGroupFactory#NetPeerGroupFactory(ConfigParams,URI,ID,String,XMLElement)}
218      *             or {@link NetPeerGroupFactory#NetPeerGroupFactory(PeerGroup,ID,String,XMLElement)}.
219      */
220     @Deprecated
221     public static void setNetPGName(String name) {
222         netPGName = name;
223     }
224
225     /**
226      * Sets the ID which will be used for new net peer group instances.
227      *
228      * @param id The ID which will be used for new net peer group instances.
229      * @deprecated Consider converting to use {@link NetPeerGroupFactory#NetPeerGroupFactory(ConfigParams,URI,ID,String,XMLElement)}
230      *             or {@link NetPeerGroupFactory#NetPeerGroupFactory(PeerGroup,ID,String,XMLElement)}.
231      */
232     @Deprecated
233     public static void setNetPGID(PeerGroupID id) {
234         netPGID = id;
235     }
236
237     /**
238      * Get the optional configurator class for the world peer group.
239      *
240      * @return Class configurator class
241      * @deprecated Consider converting to use {@link NetPeerGroupFactory}.
242      */
243     @Deprecated
244     public static Class getConfiguratorClass() {
245         return configurator;
246     }
247
248     /**
249      * Set the optional configurator class for the World Peer Group. If present
250      * an instance of this class will be used to generate/update the
251      * configuration parameters for the World Peer Group whenever
252      * {@code newPlatform()} is invoked.
253      * <p/>
254      * All configuration actions for the World Peer Group may be completely
255      * disabled by specify {@code null} as the configurator class. The default
256      * configuration class is always initialized to {@code DEFAULT_CONFIGURATOR}.
257      *
258      * @param c The {@code Class} to use as a configurator for the World Peer
259      *          Group.
260      * @deprecated Consider converting to use {@link NetPeerGroupFactory} and/or {@link WorldPeerGroupFactory}.
261      */
262     @Deprecated
263     public static void setConfiguratorClass(Class c) {
264
265         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
266             LOG.fine("Setting configurator class to : " + c);
267         }
268
269         configurator = c;
270     }
271
272     /**
273      * Returns the location which will serve as the parent for all stored items
274      * used by JXTA. This method is intended for use by PeerGroup implementations
275      * and is not intended for use by applications. Applications and services
276      * should use the PeerGroup method with the same name.
277      *
278      * @return The location which will serve as the parent for all stored
279      *         items used by JXTA.
280      * @see PeerGroup#getStoreHome()
281      * @deprecated Consider converting to use {@link NetPeerGroupFactory} and/or {@link WorldPeerGroupFactory}.
282      */
283     @Deprecated
284     public static URI getStoreHome() {
285         if (null == storeHome) {
286             // Establish the default store location via long established hackery.
287             String jxta_path = System.getProperty("JXTA_HOME", ".jxta/");
288
289             File jxta_home = new File(jxta_path);
290
291             jxta_home.mkdirs();
292             URI defaultHome = jxta_home.toURI();
293
294             return defaultHome;
295         }
296
297         return storeHome;
298     }
299
300     /**
301      * Set the location which will serve as the parent for all stored items used by JXTA.
302      *
303      * @param newHome The absolute URI location which will serve as the parent
304      *                for all stored items used by JXTA. Currently this must be a non-opaque URI.
305      *                May also be {@code null} to restore the default value.
306      * @deprecated Consider converting to use {@link NetPeerGroupFactory} and/or {@link WorldPeerGroupFactory}.
307      */
308     @Deprecated
309     public static void setStoreHome(URI newHome) {
310
311         if (null != newHome) {
312             // Fail if the URI is not absolute.
313             if (!newHome.isAbsolute()) {
314                 throw new IllegalArgumentException("Only absolute URIs accepted for store home location.");
315             }
316
317             // Fail if the URI is Opaque.
318             if (newHome.isOpaque()) {
319                 throw new IllegalArgumentException("Only hierarchical URIs accepted for store home location.");
320             }
321
322             // Add a trailing slash if necessary. 
323             if (!newHome.toString().endsWith("/")) {
324                 newHome = URI.create(newHome.toString() + "/");
325             }
326         }
327
328         storeHome = newHome;
329     }
330
331     /**
332      * Static Method to create a new peer group instance.
333      * <p/>
334      * After being created the init() method needs to be called, and
335      * the startApp() method may be called, at the invoker's discretion.
336      *
337      * @return PeerGroup instance of a new PeerGroup
338      * @deprecated This method was previously unused and has been removed with no alternatives. (it wasn't useful)
339      */
340     @Deprecated
341     public static PeerGroup newPeerGroup() {
342         throw new UnsupportedOperationException("This feature has been removed. (sorry)");
343     }
344
345     /**
346      * Instantiates the World (Platform) Peer Group and can also optionally
347      * (re)configure the world peer group before instantiation using the
348      * configurator specified via {@link #setConfiguratorClass(Class)}.
349      * <p/>
350      * Only one instance of  the World Peer Group may be created within the
351      * context of the {@code PeerGroupFactory}'s class loader. Invoking this
352      * method amounts to creating an instance of JXTA.
353      * <p/>
354      * The {@link PeerGroup#init(PeerGroup,ID,Advertisement)} method is
355      * called automatically. The {@link PeerGroup#startApp(String[])} method
356      * is left for the invoker to call if appropriate.
357      *
358      * @return PeerGroup The World Peer Group instance.
359      * @throws JxtaError Thrown for all checked Exceptions which occur during
360      *                   construction of the World Peer Group.
361      * @deprecated Consider converting to use {@link WorldPeerGroupFactory#WorldPeerGroupFactory()}.
362      */
363     @Deprecated
364     public static PeerGroup newPlatform() {
365
366         Class c = PeerGroupFactory.getConfiguratorClass();
367
368         if (null == c) {
369             c = NULL_CONFIGURATOR;
370         }
371
372         Configurator configurator;
373
374         try {
375             Constructor config_constructor = c.getConstructor(URI.class);
376
377             configurator = (Configurator) config_constructor.newInstance(getStoreHome());
378         } catch (InvocationTargetException ie) {
379             LOG.log(Level.SEVERE, "Uninstantiatable configurator: " + c, ie);
380
381             throw new JxtaError("Uninstantiatable configurator: " + c, ie);
382         } catch (NoSuchMethodException ie) {
383             LOG.log(Level.SEVERE, "Uninstantiatable configurator: " + c, ie);
384
385             throw new JxtaError("Uninstantiatable configurator: " + c, ie);
386         } catch (InstantiationException ie) {
387             LOG.log(Level.SEVERE, "Uninstantiatable configurator: " + c, ie);
388
389             throw new JxtaError("Uninstantiatable configurator: " + c, ie);
390         } catch (IllegalAccessException iae) {
391             LOG.log(Level.SEVERE, "can\'t instantiate configurator: " + c, iae);
392
393             throw new JxtaError("Can't instantiate configurator: " + c, iae);
394         } catch (ClassCastException cce) {
395             LOG.log(Level.SEVERE, "Not a Configurator :" + c, cce);
396
397             throw new JxtaError("Not a Configurator :" + c, cce);
398         }
399
400         ConfigParams pc;
401
402         try {
403             pc = configurator.getConfigParams();
404         } catch (ConfiguratorException cce) {
405             LOG.log(Level.SEVERE, "Could not retrieve configuration", cce);
406
407             throw new JxtaError("Could not retrieve configuration", cce);
408         }
409
410         try {
411             WorldPeerGroupFactory wpgf;
412
413             if (null == worldGroupClass) {
414                 wpgf = new WorldPeerGroupFactory(pc, getStoreHome());
415             } else {
416                 wpgf = new WorldPeerGroupFactory(worldGroupClass, pc, getStoreHome());
417             }
418
419             configurator.setConfigParams(pc);
420             configurator.save();
421
422             // Forget about the configurator
423             configurator = null;
424
425             return wpgf.getInterface();
426         } catch (RuntimeException e) {
427             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
428                 LOG.log(Level.SEVERE, "newPlatform failed", e);
429             }
430             // rethrow
431             throw e;
432         } catch (Exception e) {
433             // should be all other checked exceptions
434             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
435                 LOG.log(Level.SEVERE, "newPlatform failed", e);
436             }
437
438             // Simplify exception scheme for caller: any sort of problem wrapped
439             // in a PeerGroupException.
440             throw new JxtaError("newPlatform failed", e);
441         } catch (Error e) {
442             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
443                 LOG.log(Level.SEVERE, "newPlatform failed", e);
444             }
445             // rethrow
446             throw e;
447         }
448     }
449
450     /**
451      * Instantiates the net peer group using the provided parent peer group.
452      *
453      * @param ppg The parent group.
454      * @return PeerGroup The default netPeerGroup
455      * @throws PeerGroupException For failures in constructing the Net Peer Group.
456      * @deprecated Consider converting to use {@link NetPeerGroupFactory#NetPeerGroupFactory(PeerGroup,ID,String,XMLElement)}.
457      */
458     @Deprecated
459     public static PeerGroup newNetPeerGroup(PeerGroup ppg) throws PeerGroupException {
460
461         try {
462             NetPeerGroupFactory npgf;
463
464             NetPeerGroupFactory.NetGroupTunables tunables;
465
466             if (null == netPGID) {
467                 // Determine net peer group configuration parameters if they
468                 // have not already been set.
469                 tunables = new NetPeerGroupFactory.NetGroupTunables(ResourceBundle.getBundle("net.jxta.impl.config")
470                         ,
471                         new NetPeerGroupFactory.NetGroupTunables());
472
473                 // load overides from "${JXTA_HOME}config.properties".
474                 URI configPropertiesLocation = getStoreHome().resolve("config.properties");
475
476                 try {
477                     URLConnection configProperties = configPropertiesLocation.toURL().openConnection();
478
479                     ResourceBundle rsrcs = new PropertyResourceBundle(configProperties.getInputStream());
480
481                     tunables = new NetPeerGroupFactory.NetGroupTunables(rsrcs, tunables);
482
483                     if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
484                         LOG.fine("Loaded defaults from " + rsrcs);
485                     }
486                 } catch (MissingResourceException ignored) {
487                     ;
488                 } catch (IOException ignored) {
489                     ;
490                 } catch (Exception ignored) {
491                     ;
492                 }
493             } else {
494                 tunables = new NetPeerGroupFactory.NetGroupTunables(netPGID, netPGName
495                         ,
496                         (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "desc", netPGDesc));
497             }
498
499             npgf = new NetPeerGroupFactory(ppg, tunables.id, tunables.name, tunables.desc);
500
501             PeerGroup newPg = npgf.getInterface();
502
503             return newPg;
504         } catch (PeerGroupException failed) {
505             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
506                 LOG.log(Level.SEVERE, "newNetPeerGroup failed", failed);
507             }
508             // rethrow
509             throw failed;
510         } catch (RuntimeException e) {
511             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
512                 LOG.log(Level.SEVERE, "newNetPeerGroup failed", e);
513             }
514             // rethrow
515             throw e;
516         } catch (Exception e) {
517             // should be all other checked exceptions
518             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
519                 LOG.log(Level.SEVERE, "newNetPeerGroup failed", e);
520             }
521             // Simplify exception scheme for caller: any sort of problem wrapped
522             // in a PeerGroupException.
523             throw new PeerGroupException("newNetPeerGroup failed", e);
524         } catch (Error e) {
525             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
526                 LOG.log(Level.SEVERE, "newNetPeerGroup failed", e);
527             }
528
529             // rethrow
530             throw e;
531         }
532     }
533
534     /**
535      * Instantiates the World Peer Group and then instantiates the Net Peer
536      * Group. This simplifies the method by which applications can start JXTA.
537      *
538      * @return The newly instantiated Net Peer Group.
539      * @deprecated Consider converting to use {@link NetPeerGroupFactory#NetPeerGroupFactory()}
540      *             or preferably one of the other {@code NetPeerGroupFactory} constructors.
541      */
542     @Deprecated
543     public static PeerGroup newNetPeerGroup() throws PeerGroupException {
544         // get/create the World Peer Group.
545         PeerGroup wpg = getWorldPeerGroup();
546
547         try {
548             PeerGroup npg = newNetPeerGroup(wpg);
549
550             return npg;
551         } finally {
552             wpg.unref();
553         }
554     }
555
556     /**
557      * Retrieves or constructs a new World Peer Group instance suitable for
558      * use as the parent for Net Peer Group instances. This implementation
559      * makes an important trade-off worth noting; it will use an existing
560      * world peer group instance if available and ignore any changes which have
561      * been made to the static configuration methods provided by this class.
562      *
563      * @return The World Peer Group.
564      * @throws PeerGroupException For failures in recovering the World Peer Group.
565      */
566     private static PeerGroup getWorldPeerGroup() throws PeerGroupException {
567         synchronized (PeerGroup.globalRegistry) {
568             PeerGroup result = PeerGroup.globalRegistry.lookupInstance(PeerGroupID.worldPeerGroupID);
569
570             if (null != result) {
571                 return result;
572             }
573
574             return newPlatform();
575         }
576     }
577 }