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.peergroup;
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;
73 import java.io.IOException;
74 import java.lang.reflect.Constructor;
75 import java.lang.reflect.InvocationTargetException;
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;
86 * A factory for instantiating the JXTA core peer groups.
88 * JXTA comes with two peergroup implementations:
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.
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
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.
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>
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>
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
126 public final class PeerGroupFactory {
131 private final static transient Logger LOG = Logger.getLogger(PeerGroupFactory.class.getName());
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.
138 public final static Class NULL_CONFIGURATOR = net.jxta.impl.peergroup.NullConfigurator.class;
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.
145 public final static Class DEFAULT_CONFIGURATOR = net.jxta.impl.peergroup.DefaultConfigurator.class;
148 * The class which will be instantiated as the World Peer Group.
150 private static Class worldGroupClass = null;
153 * The ID of the network peer group.
155 private static PeerGroupID netPGID = null;
158 * The name of the network peer group.
160 private static String netPGName = null;
163 * The description of the network peer group.
165 private static String netPGDesc = null;
168 * The class which will be instantiated to configure the World Peer
171 private static Class configurator = DEFAULT_CONFIGURATOR;
174 * the location which will serve as the parent for all stored items used
177 private static URI storeHome = null;
180 * Static Method to initialize the world peer group class.
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)}.
186 public static void setPlatformClass(Class c) {
191 * Static Method to initialize the std peer group class.
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.
197 public static void setStdPeerGroupClass(Class c) {
198 throw new UnsupportedOperationException("This feature has been removed. (sorry)");
202 * Sets the description which will be used for new net peer group instances.
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)}.
209 public static void setNetPGDesc(String desc) {
214 * Sets the name which will be used for new net peer group instances.
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)}.
221 public static void setNetPGName(String name) {
226 * Sets the ID which will be used for new net peer group instances.
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)}.
233 public static void setNetPGID(PeerGroupID id) {
238 * Get the optional configurator class for the world peer group.
240 * @return Class configurator class
241 * @deprecated Consider converting to use {@link NetPeerGroupFactory}.
244 public static Class getConfiguratorClass() {
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.
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}.
258 * @param c The {@code Class} to use as a configurator for the World Peer
260 * @deprecated Consider converting to use {@link NetPeerGroupFactory} and/or {@link WorldPeerGroupFactory}.
263 public static void setConfiguratorClass(Class c) {
265 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
266 LOG.fine("Setting configurator class to : " + c);
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.
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}.
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/");
289 File jxta_home = new File(jxta_path);
292 URI defaultHome = jxta_home.toURI();
301 * Set the location which will serve as the parent for all stored items used by JXTA.
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}.
309 public static void setStoreHome(URI newHome) {
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.");
317 // Fail if the URI is Opaque.
318 if (newHome.isOpaque()) {
319 throw new IllegalArgumentException("Only hierarchical URIs accepted for store home location.");
322 // Add a trailing slash if necessary.
323 if (!newHome.toString().endsWith("/")) {
324 newHome = URI.create(newHome.toString() + "/");
332 * Static Method to create a new peer group instance.
334 * After being created the init() method needs to be called, and
335 * the startApp() method may be called, at the invoker's discretion.
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)
341 public static PeerGroup newPeerGroup() {
342 throw new UnsupportedOperationException("This feature has been removed. (sorry)");
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)}.
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.
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.
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()}.
364 public static PeerGroup newPlatform() {
366 Class c = PeerGroupFactory.getConfiguratorClass();
369 c = NULL_CONFIGURATOR;
372 Configurator configurator;
375 Constructor config_constructor = c.getConstructor(URI.class);
377 configurator = (Configurator) config_constructor.newInstance(getStoreHome());
378 } catch (InvocationTargetException ie) {
379 LOG.log(Level.SEVERE, "Uninstantiatable configurator: " + c, ie);
381 throw new JxtaError("Uninstantiatable configurator: " + c, ie);
382 } catch (NoSuchMethodException ie) {
383 LOG.log(Level.SEVERE, "Uninstantiatable configurator: " + c, ie);
385 throw new JxtaError("Uninstantiatable configurator: " + c, ie);
386 } catch (InstantiationException ie) {
387 LOG.log(Level.SEVERE, "Uninstantiatable configurator: " + c, ie);
389 throw new JxtaError("Uninstantiatable configurator: " + c, ie);
390 } catch (IllegalAccessException iae) {
391 LOG.log(Level.SEVERE, "can\'t instantiate configurator: " + c, iae);
393 throw new JxtaError("Can't instantiate configurator: " + c, iae);
394 } catch (ClassCastException cce) {
395 LOG.log(Level.SEVERE, "Not a Configurator :" + c, cce);
397 throw new JxtaError("Not a Configurator :" + c, cce);
403 pc = configurator.getConfigParams();
404 } catch (ConfiguratorException cce) {
405 LOG.log(Level.SEVERE, "Could not retrieve configuration", cce);
407 throw new JxtaError("Could not retrieve configuration", cce);
411 WorldPeerGroupFactory wpgf;
413 if (null == worldGroupClass) {
414 wpgf = new WorldPeerGroupFactory(pc, getStoreHome());
416 wpgf = new WorldPeerGroupFactory(worldGroupClass, pc, getStoreHome());
419 configurator.setConfigParams(pc);
422 // Forget about the configurator
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);
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);
438 // Simplify exception scheme for caller: any sort of problem wrapped
439 // in a PeerGroupException.
440 throw new JxtaError("newPlatform failed", e);
442 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
443 LOG.log(Level.SEVERE, "newPlatform failed", e);
451 * Instantiates the net peer group using the provided parent peer group.
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)}.
459 public static PeerGroup newNetPeerGroup(PeerGroup ppg) throws PeerGroupException {
462 NetPeerGroupFactory npgf;
464 NetPeerGroupFactory.NetGroupTunables tunables;
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")
471 new NetPeerGroupFactory.NetGroupTunables());
473 // load overides from "${JXTA_HOME}config.properties".
474 URI configPropertiesLocation = getStoreHome().resolve("config.properties");
477 URLConnection configProperties = configPropertiesLocation.toURL().openConnection();
479 ResourceBundle rsrcs = new PropertyResourceBundle(configProperties.getInputStream());
481 tunables = new NetPeerGroupFactory.NetGroupTunables(rsrcs, tunables);
483 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
484 LOG.fine("Loaded defaults from " + rsrcs);
486 } catch (MissingResourceException ignored) {
488 } catch (IOException ignored) {
490 } catch (Exception ignored) {
494 tunables = new NetPeerGroupFactory.NetGroupTunables(netPGID, netPGName
496 (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "desc", netPGDesc));
499 npgf = new NetPeerGroupFactory(ppg, tunables.id, tunables.name, tunables.desc);
501 PeerGroup newPg = npgf.getInterface();
504 } catch (PeerGroupException failed) {
505 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
506 LOG.log(Level.SEVERE, "newNetPeerGroup failed", failed);
510 } catch (RuntimeException e) {
511 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
512 LOG.log(Level.SEVERE, "newNetPeerGroup failed", 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);
521 // Simplify exception scheme for caller: any sort of problem wrapped
522 // in a PeerGroupException.
523 throw new PeerGroupException("newNetPeerGroup failed", e);
525 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
526 LOG.log(Level.SEVERE, "newNetPeerGroup failed", e);
535 * Instantiates the World Peer Group and then instantiates the Net Peer
536 * Group. This simplifies the method by which applications can start JXTA.
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.
543 public static PeerGroup newNetPeerGroup() throws PeerGroupException {
544 // get/create the World Peer Group.
545 PeerGroup wpg = getWorldPeerGroup();
548 PeerGroup npg = newNetPeerGroup(wpg);
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.
563 * @return The World Peer Group.
564 * @throws PeerGroupException For failures in recovering the World Peer Group.
566 private static PeerGroup getWorldPeerGroup() throws PeerGroupException {
567 synchronized (PeerGroup.globalRegistry) {
568 PeerGroup result = PeerGroup.globalRegistry.lookupInstance(PeerGroupID.worldPeerGroupID);
570 if (null != result) {
574 return newPlatform();