]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/api/src/net/jxta/peergroup/PeerGroup.java
7e900561d94e08eb6358358316ba0cbc2b4df171
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / api / src / net / jxta / peergroup / PeerGroup.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.access.AccessService;
61 import net.jxta.discovery.DiscoveryService;
62 import net.jxta.document.Advertisement;
63 import net.jxta.document.Element;
64 import net.jxta.endpoint.EndpointService;
65 import net.jxta.exception.PeerGroupException;
66 import net.jxta.exception.ProtocolNotSupportedException;
67 import net.jxta.exception.ServiceNotFoundException;
68 import net.jxta.id.ID;
69 import net.jxta.membership.MembershipService;
70 import net.jxta.peer.PeerID;
71 import net.jxta.peer.PeerInfoService;
72 import net.jxta.pipe.PipeService;
73 import net.jxta.platform.JxtaLoader;
74 import net.jxta.platform.Module;
75 import net.jxta.platform.ModuleClassID;
76 import net.jxta.platform.ModuleSpecID;
77 import net.jxta.protocol.ConfigParams;
78 import net.jxta.protocol.ModuleImplAdvertisement;
79 import net.jxta.protocol.PeerAdvertisement;
80 import net.jxta.protocol.PeerGroupAdvertisement;
81 import net.jxta.rendezvous.RendezVousService;
82 import net.jxta.resolver.ResolverService;
83 import net.jxta.service.Service;
84
85 import java.io.IOException;
86 import java.lang.ref.Reference;
87 import java.lang.ref.WeakReference;
88 import java.net.URI;
89 import java.util.HashMap;
90 import java.util.Iterator;
91 import java.util.Map;
92
93
94 /**
95  * Peer groups are formed as a collection of peers that have agreed upon a
96  * common set of services. Each peer group is assigned a unique peer group ID
97  * and a peer group advertisement. The peer group advertisement contains a
98  * ModuleSpecID which refers to a module specification for this peer group.
99  * <p/>
100  * The peer group specification mandates each of the group services (membership,
101  * discovery, resolver, etc). Implementations of that specification are
102  * described by ModuleImplAdvertisements which are identified by the group's
103  * ModuleSpecID. Implementations are responsible for providing the services mandated
104  * by the specification.
105  * <p/>
106  * The java reference implementation achieves this by loading additional Modules
107  * which ModuleSpecIDs are listed by the group implementation advertisement.
108  * <p/>
109  * In order to fully participate in a group, a peer may need to authenticate
110  * with the group using the peer group membership service.
111  *
112  * @see net.jxta.peergroup.PeerGroupID
113  * @see net.jxta.service.Service
114  * @see net.jxta.peergroup.PeerGroupFactory
115  * @see net.jxta.protocol.PeerGroupAdvertisement
116  * @see net.jxta.protocol.ModuleImplAdvertisement
117  * @see net.jxta.platform.ModuleSpecID
118  * @see net.jxta.platform.ModuleClassID
119  */
120 public interface PeerGroup extends Service {
121
122     /**
123      * Look for needed ModuleImplAdvertisement in this group.
124      */
125     public final static int Here = 0;
126
127     /**
128      * Look for needed ModuleImplAdvertisement in the parent group of this group.
129      */
130     public final static int FromParent = 1;
131
132     /**
133      * Look for needed ModuleImplAdvertisement in both this group and its parent.
134      */
135     public final static int Both = 2;
136
137     /**
138      * Default life time for group advertisements in the publisher's cache.
139      * (a year)
140      */
141     // without casting to long we lose precision
142     public final static long DEFAULT_LIFETIME = (long) 1000 * (long) 3600 * (long) 24 * 365L;
143
144     /**
145      * Default expiration time for discovered group advertisements. (2 weeks)
146      */
147     // without casting to long we lose precision
148     public final static long DEFAULT_EXPIRATION = (long) 1000 * (long) 3600 * (long) 24 * 14L;
149
150     /**
151      * Global registry of instantiated peer groups. We allow only a single
152      * PeerGroup instance for a specific PeerGroupID within the context of the
153      * classloader JXTA is loaded into.
154      */
155     static class GlobalRegistry {
156
157         private final Map<ID, Reference<PeerGroup>> registry = new HashMap<ID, Reference<PeerGroup>>(8);
158
159         /**
160          * Registers a new instance.
161          *
162          * @param gid the ID of the group of which an instance is being registered.
163          * @param pg  the group instance being registered.
164          * @return false if the instance could not be registered because there
165          *         was already such an instance registered.
166          */
167         public synchronized boolean registerInstance(PeerGroupID gid, PeerGroup pg) {
168
169             Reference<PeerGroup> ref = registry.get(gid);
170
171             if ((ref != null) && (ref.get() != null)) {
172                 return false;
173             }
174
175             // If the ref is a dead instance, we can also replace it.
176             registry.put(gid, new WeakReference<PeerGroup>(pg));
177             return true;
178         }
179
180         /**
181          * Unregisters a group instance (normally because the group is being
182          * stopped.
183          *
184          * @param gid the ID of the group of which an instance is unregistered.
185          * @param pg  the group instance itself (serves as a credential).
186          * @return false if the group could not be unregistered because no such
187          *         registration (exact ID, exact object) was not found.
188          */
189         public synchronized boolean unRegisterInstance(PeerGroupID gid, PeerGroup pg) {
190
191             Reference<PeerGroup> ref = registry.get(gid);
192
193             if (ref == null) {
194                 return false;
195             }
196
197             PeerGroup found = ref.get();
198
199             if (found == null) {
200                 // Dead instance. Remove from table.
201                 registry.remove(gid);
202                 return false;
203             }
204
205             // Note the use of "!=", not "!equals()"
206             if (pg != found) {
207                 return false;
208             }
209
210             registry.remove(gid);
211             return true;
212         }
213
214         /**
215          * Returns a running instance of the peergroup with given ID if any
216          * exists. The instance should be {@link PeerGroup#unref()}ed when it is
217          * no longer needed.
218          *
219          * @param gid the id of the group of which an instance is wanted.
220          * @return the group, or {@code null} if no instance exists.
221          */
222         public synchronized PeerGroup lookupInstance(PeerGroupID gid) {
223
224             Reference<PeerGroup> ref = registry.get(gid);
225
226             if (ref == null) {
227                 return null;
228             }
229
230             PeerGroup pg = ref.get();
231
232             if (pg == null) {
233                 // Dead instance. remove from table.
234                 registry.remove(gid);
235                 return null;
236             }
237
238             // Returns an interface object. Therefore a module that got the
239             // peergroup through lookup cannot unregister it if the group
240             // protects itself by returning an interface object different from
241             // the group object. In general only the group itself can
242             // unregister when being torn down. Unregistration will also be
243             // automatic if the grp object is GC'ed (the references are weak
244             // references).
245             return (PeerGroup) pg.getInterface();
246         }
247
248         /**
249          * Returns a running instance of the peergroup with given ID if any
250          * exists.
251          *
252          * @param gid The id of the group of which an instance is wanted.
253          * @return The group, or {@code null} if no instance exists.
254          */
255         synchronized PeerGroup getInstance(PeerGroupID gid) {
256
257             Reference<PeerGroup> ref = registry.get(gid);
258
259             if (ref == null) {
260                 return null;
261             }
262
263             PeerGroup pg = ref.get();
264
265             if (pg == null) {
266                 // Dead instance. remove from table.
267                 registry.remove(gid);
268                 return null;
269             }
270
271             return pg;
272         }
273
274         /**
275          * Returns {@code true} if there is a registered peergroup of the
276          * specified ID.
277          *
278          * @param gid the id of the group of which an instance is wanted.
279          * @return {@code} true if the peergroup is currently registered
280          *         otherwise false;
281          */
282         public synchronized boolean registeredInstance(PeerGroupID gid) {
283
284             Reference<PeerGroup> ref = registry.get(gid);
285
286             if (ref == null) {
287                 return false;
288             }
289
290             PeerGroup pg = ref.get();
291
292             if (pg == null) {
293                 // Dead instance. remove from table.
294                 registry.remove(gid);
295                 return false;
296             }
297
298             return true;
299         }
300     }
301
302     /**
303      * Well known classes for the basic services.
304      *
305      * <p/>FIXME: we should make a "well-known ID" encoding implementation that
306      * has its own little name space of human readable names...later.
307      * To keep their string representation shorter, we put our small spec
308      * or role pseudo unique ID at the front of the second UUID string.
309      * Base classes do not need an explicit second UUID string because it is
310      * all 0.
311      *
312      * <p/>The type is always the last two characters, no-matter the total length.
313      */
314
315     /**
316      * Prefix string for all of the Well Known IDs declared in this interface.
317      */
318     static final String WK_ID_PREFIX = ID.URIEncodingName + ":" + ID.URNNamespace + ":uuid-DeadBeefDeafBabaFeedBabe";
319
320     /**
321      * Well known module class identifier: peer group
322      */
323     public final static ModuleClassID peerGroupClassID = 
324             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000105"));
325
326     /**
327      * Well known module class identifier: resolver service
328      */
329     public final static ModuleClassID resolverClassID = 
330             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000205"));
331
332     /**
333      * Well known module class identifier: discovery service
334      */
335     public final static ModuleClassID discoveryClassID = 
336             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000305"));
337
338     /**
339      * Well known module class identifier: pipe service
340      */
341     public final static ModuleClassID pipeClassID = 
342             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000405"));
343
344     /**
345      * Well known module class identifier: membership service
346      */
347     public final static ModuleClassID membershipClassID = 
348             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000505"));
349
350     /**
351      * Well known module class identifier: rendezvous service
352      */
353     public final static ModuleClassID rendezvousClassID = 
354             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000605"));
355
356     /**
357      * Well known module class identifier: peerinfo service
358      */
359     public final static ModuleClassID peerinfoClassID = 
360             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000705"));
361
362     /**
363      * Well known module class identifier: endpoint service
364      */
365     public final static ModuleClassID endpointClassID = 
366             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000805"));
367
368     // FIXME: EndpointProtocols should probably all be of the same class
369     // and of different specs and roles... But we'll take a shortcut for now.
370
371     /**
372      * Well known module class identifier: tcp protocol
373      */
374     public final static ModuleClassID tcpProtoClassID = 
375             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000905"));
376
377     /**
378      * Well known module class identifier: http protocol
379      */
380     public final static ModuleClassID httpProtoClassID = 
381             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000A05"));
382
383     /**
384      * Well known module class identifier: router protocol
385      */
386     public final static ModuleClassID routerProtoClassID = 
387             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000B05"));
388
389     /**
390      * Well known module class identifier: application
391      */
392     public final static ModuleClassID applicationClassID = 
393             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000C05"));
394
395     /**
396      * Well known module class identifier: tlsProtocol
397      */
398     public final static ModuleClassID tlsProtoClassID = 
399             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000D05"));
400
401     /**
402      * Well known module class identifier: ProxyService
403      */
404     public final static ModuleClassID proxyClassID = 
405             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000E05"));
406
407     /**
408      * Well known module class identifier: RelayProtocol
409      */
410     public final static ModuleClassID relayProtoClassID = 
411             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000000F05"));
412
413     /**
414      * Well known module class identifier: AccessService
415      */
416     public final static ModuleClassID accessClassID = 
417             ModuleClassID.create(URI.create(WK_ID_PREFIX + "0000001005"));
418
419     /**
420      * Well known group specification identifier: the platform
421      */
422     public final static ModuleSpecID refPlatformSpecID = 
423             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000010106"));
424
425     /**
426      * Well known group specification identifier: the Network Peer Group
427      */
428     public final static ModuleSpecID refNetPeerGroupSpecID = 
429             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000010206"));
430
431     /**
432      * Well known service specification identifier: the standard resolver
433      */
434     public final static ModuleSpecID refResolverSpecID = 
435             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000020106"));
436
437     /**
438      * Well known service specification identifier: the standard discovery
439      */
440     public final static ModuleSpecID refDiscoverySpecID = 
441             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000030106"));
442
443     /**
444      * Well known service specification identifier: the standard pipe service
445      */
446     public final static ModuleSpecID refPipeSpecID = 
447             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000040106"));
448
449     /**
450      * Well known service specification identifier: the standard membership
451      */
452     public final static ModuleSpecID refMembershipSpecID = 
453             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000050106"));
454
455     /**
456      * Well known service specification identifier: the standard rendezvous
457      */
458     public final static ModuleSpecID refRendezvousSpecID = 
459             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000060106"));
460
461     /**
462      * Well known service specification identifier: the standard peerinfo
463      */
464     public final static ModuleSpecID refPeerinfoSpecID = 
465             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000070106"));
466
467     /**
468      * Well known service specification identifier: the standard endpoint
469      */
470     public final static ModuleSpecID refEndpointSpecID =
471             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000080106"));
472
473     /**
474      * Well known endpoint protocol specification identifier: the standard
475      * tcp endpoint protocol
476      */
477     public final static ModuleSpecID refTcpProtoSpecID = 
478             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000090106"));
479
480     /**
481      * Well known endpoint protocol specification identifier: the standard
482      * http endpoint protocol
483      */
484     public final static ModuleSpecID refHttpProtoSpecID = 
485             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "0000000A0106"));
486
487     /**
488      * Well known endpoint protocol specification identifier: the standard
489      * router
490      */
491     public final static ModuleSpecID refRouterProtoSpecID = 
492             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "0000000B0106"));
493
494     /**
495      * Well known endpoint protocol specification identifier: the standard
496      * tls endpoint protocol
497      */
498     public final static ModuleSpecID refTlsProtoSpecID = 
499             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "0000000D0106"));
500
501     /**
502      * Well known group specification identifier: an all purpose peer group
503      * specification. The java reference implementation implements it with
504      * the StdPeerGroup class and all the standard platform services and no
505      * endpoint protocols.
506      */
507     public final static ModuleSpecID allPurposePeerGroupSpecID = 
508             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000010306"));
509
510     /**
511      * Well known application: the shell
512      */
513     public final static ModuleSpecID refShellSpecID = 
514             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "0000000C0206"));
515
516     /**
517      * Well known application: the Proxy
518      */
519     public final static ModuleSpecID refProxySpecID = 
520             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "0000000E0106"));
521
522     /**
523      * Well known endpoint protocol specification identifier: the standard
524      * relay endpoint protocol
525      */
526     public final static ModuleSpecID refRelayProtoSpecID = 
527             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "0000000F0106"));
528
529     /**
530      * Well known access specification identifier: the standard
531      * access service
532      */
533     public final static ModuleSpecID refAccessSpecID = 
534             ModuleSpecID.create(URI.create(WK_ID_PREFIX + "000000100106"));
535
536     /**
537      * The global registry of Peer Group instances. Operations involving the
538      * instantiation or orderly shutdown of Peer Groups should synchronize upon
539      * this object.
540      */
541     final static GlobalRegistry globalRegistry = new GlobalRegistry();
542
543     /**
544      * Returns the Thread Group in which threads for this peer group will live.
545      * This is currently used only for debugging purposes so that the source of
546      * a thread can be determined.
547      *
548      * @return ThreadGroup
549      */
550     public ThreadGroup getHomeThreadGroup();
551
552     /**
553      * Returns the class loader for this group.
554      *
555      * @return JxtaLoader The JXTA Class loader used by this group.
556      */
557     public JxtaLoader getLoader();
558
559     /**
560      * Returns the whether the group member is a Rendezvous peer for the group.
561      *
562      * @return boolean true if the peer is a rendezvous for the group.
563      */
564     public boolean isRendezvous();
565
566     /**
567      * Return the PeerGroupAdvertisement for this group.
568      *
569      * @return PeerGroupAdvertisement this Group's advertisement.
570      */
571     public PeerGroupAdvertisement getPeerGroupAdvertisement();
572
573     /**
574      * Return the PeerAdvertisement of the local Peer within this Peer Group.
575      *
576      * @return the PeerAdvertisement of the local Peer within this Peer Group.
577      */
578     public PeerAdvertisement getPeerAdvertisement();
579
580     /**
581      * Lookup for a service by name.
582      *
583      * @param name the service identifier.
584      * @return Service, the Service registered by that name
585      * @throws ServiceNotFoundException could not find the service requested
586      */
587     public Service lookupService(ID name) throws ServiceNotFoundException;
588
589     /**
590      * Lookup for a service by class ID and index in a map.
591      * <p/>
592      * More than one service in a group may be of a given ModuleClass.
593      * However each of them has a unique assigned ID which serves as the
594      * index in the map of services. In most cases, there is only one
595      * service of each given Module Class, and the ID of that Module Class
596      * is the assigned ID. Otherwise, the group may have a list of existing
597      * assigned ID per base class. This routine may be used to retrieve
598      * services of the given Module Class and index in that list.
599      * In the absence of a mapping, index 0 is still valid and
600      * corresponds to the service which assigned ID is exactly the
601      * given ID.
602      * Group objects with a map are normally wrappers tailored
603      * specially by the loader of a module (often the group itself) in order
604      * to provide a map appropriate for that module. Modules that do not use
605      * more than one service of a given base class normally never need to call
606      * this method; lookupService(ID) is equivalent to lookupService(ID, 0)
607      * and will transparently remap index 0 to whatever the group's
608      * structure defines as the default for the invoking service.
609      * <p/>
610      * Note: traditionally, the given ID is expected to be a base Module
611      * Class ID, and the assigned ID of a Module is a Class ID of the
612      * same base class with a role suffix to make it unique. If the given
613      * ID already contains a role suffix, there may exist an entry for
614      * it in the map anyway, if not (which is the expected use pattern),
615      * then only index 0 exists and the given ID is used whole and
616      * untranslated.
617      *
618      * @param name      the service identifier
619      * @param roleIndex the index in the list of assigned IDs that match
620      *                  that identifier.
621      * @return Service, the corresponding Service
622      * @throws ServiceNotFoundException Could not find the service requested.
623      * @since JXTA 2.3.1
624      */
625     public Service lookupService(ID name, int roleIndex) throws ServiceNotFoundException;
626
627     /**
628      * Returns the map of the assigned IDs currently associated with the given
629      * ModuleClassID by this PeerGroup object. The IDs are returned in the order
630      * of their index in the map. So the first ID returned will be identical to
631      * what would be returned by the lookup method for the given ID and index 0.
632      * If there is no explicit such map, this method will return a singleton
633      * containing the given ID as this is the default mapping.  There is no
634      * guarantee that any of the returned IDs correspond to an actually
635      * registered service. This method only maps IDs.
636      *
637      * @param name The ModuleClassID for which the map is desired.
638      * @return Iterator An iterator on a collection of the IDs to which the given ID maps.
639      * @since JXTA 2.3.1
640      */
641     public Iterator getRoleMap(ID name);
642
643     /**
644      * Return true if the provided compatibility statement is compatible with this group.
645      *
646      * @param compat compatibility element
647      * @return boolean True if the statement is compatible.
648      */
649     public boolean compatible(Element compat);
650
651     /**
652      * Load a Module from a ModuleImplAdv.
653      * <p/>
654      * Compatibility is checked and load is attempted. If compatible and
655      * loaded successfully, the resulting Module is initialized and returned.
656      * In most cases the other loadModule() method should be preferred, since
657      * unlike this one, it will seek many compatible implementation
658      * advertisements and try them all until one works. The home group of the new
659      * module (its' parent group if the new Module is a group) will be this group.
660      *
661      * @param assignedID Id to be assigned to that module (usually its ClassID).
662      * @param impl       An implementation advertisement for that module.
663      * @return Module the module loaded and initialized.
664      * @throws ProtocolNotSupportedException The implementation described by the
665      *                                       advertisement is incompatible with this peer. The module cannot be loaded.
666      * @throws PeerGroupException            The module could not be loaded or initialized
667      */
668     public Module loadModule(ID assignedID, Advertisement impl) throws ProtocolNotSupportedException, PeerGroupException;
669
670     /**
671      * Load a module from a ModuleSpecID
672      * <p/>
673      * Advertisement is sought, compatibility is checked on all candidates
674      * and load is attempted. The first one that is compatible and loads
675      * successfully is initialized and returned.
676      *
677      * @param assignedID Id to be assigned to that module (usually its ClassID).
678      * @param specID     The specID of this module.
679      * @param where      May be one of: {@code Here}, {@code FromParent}, or
680      *                   {@code Both}, meaning that the implementation advertisement will be
681      *                   searched in this group, its parent or both. As a general guideline, the
682      *                   implementation advertisements of a group should be searched in its
683      *                   prospective parent (that is {@code Here}), the implementation
684      *                   advertisements of a group standard service should be searched in the same
685      *                   group than where this group's advertisement was found (that is,
686      *                   {@code FromParent}), while applications may be sought more freely
687      *                   ({@code Both}).
688      * @return Module the new module, or null if no usable implementation was
689      *         found.
690      */
691     public Module loadModule(ID assignedID, ModuleSpecID specID, int where);
692
693     /**
694      * Publish this group's Peer Group Advertisement. The Advertisement will be
695      * published using the parent peer group's Discovery service.
696      * <p/>
697      * Calling this method is only useful if the group is being created
698      * from scratch and the PeerGroup advertisement has not been
699      * created beforehand. In such a case, the group has never been named or
700      * described. Therefore this information has to be supplied here.
701      *
702      * @param name        The name of this group.
703      * @param description The description of this group.
704      * @throws IOException The publication could not be accomplished
705      *                     because of a network or storage failure.
706      */
707     public void publishGroup(String name, String description) throws IOException;
708
709     /*
710      * Valuable application helpers: Various methods to instantiate
711      * groups.
712      */
713
714     /**
715      * Instantiate a peer group from the provided advertisement. This peer
716      * group will be the parent of the newly instantiated peer group.
717      * <p/>
718      * The pgAdv itself may be all new and unpublished. Therefore, the two
719      * typical uses of this routine are:
720      * <p/>
721      * <ul>
722      * <li>Creating an all new group with a new ID while using an existing
723      * and published implementation. (Possibly a new one published for
724      * that purpose). The information should first be gathered in a new
725      * PeerGroupAdvertisement which is then passed to this method.</li>
726      * <p/>
727      * <li>Instantiating a group which advertisement has already been
728      * discovered (therefore there is no need to find it by groupID
729      * again).</li>
730      * </ul>
731      *
732      * @param pgAdv The advertisement for the group to be instantiated.
733      * @return PeerGroup the initialized (but not started) peergroup.
734      * @throws PeerGroupException For problems instantiating the peer group.
735      */
736     public PeerGroup newGroup(Advertisement pgAdv) throws PeerGroupException;
737
738     /**
739      * Instantiates a peer group from its elementary pieces
740      * and publishes the corresponding PeerGroupAdvertisement.
741      * The pieces are: the groups implementation adv, the group id,
742      * the name and description.
743      * <p/>
744      * The typical use of this routine is creating a whole new group based
745      * on a newly created and possibly unpublished implementation adv.
746      * <p/>
747      * This is a convenience method equivalent to either:
748      * <p/>
749      * <pre>
750      * newGrp = thisGroup.loadModule(gid, impl);
751      * newGrp.publishGroup(name, description);
752      * </pre>
753      * or, but only if the implementation advertisement has been published:
754      * <p/>
755      * <pre>
756      * newPGAdv = AdvertisementFactory.newAdvertisement(
757      *                 PeerGroupAdvertisement.getAdvertisementType());
758      * newPGAdv.setPeerGroupID(gid);
759      * newPGAdv.setModuleSpecID(impl.getModuleSpecID());
760      * newPGAdv.setName(name);
761      * newPGAdv.setDescription(description);
762      * newGrp = thisGroup.newGroup(newPGAdv);
763      * </pre>
764      *
765      * @param gid         The ID of that group. If <code>null</code> then a new group ID
766      *                    will be chosen.
767      * @param impl        The advertisement of the implementation to be used.
768      * @param name        The name of the group.
769      * @param description A description of this group.
770      * @return PeerGroup the initialized (but not started) peergroup.
771      * @throws PeerGroupException Thrown if the group could not be instantiated.
772      */
773     public PeerGroup newGroup(PeerGroupID gid, Advertisement impl, String name, String description) throws PeerGroupException;
774
775     /**
776      * Instantiate a group from its Peer Group ID only. Use this when using a
777      * group that has already been published and discovered.
778      * <p/>
779      * The typical uses of this routine are therefore:
780      * <p/>
781      * <ul>
782      * <li>Instantiating a peer group which is assumed to exist and whose Peer
783      * Group ID is already known.</li>
784      * <p/>
785      * <li>Creating a new peer group instance using an already published
786      * Group advertisement, typically published for that purpose. All other
787      * referenced advertisements must also be available.</li>
788      * </ul>
789      * <p/>
790      * To create a group from a known implAdv, just use
791      * {@link #loadModule(ID,Advertisement)} or even:<p>
792      * <p/>
793      * <code>
794      * grp = new GroupSubClass();
795      * grp.init(parentGroup, gid, impladv);
796      * </code>
797      * <p/>
798      * then, <strong>REMEMBER TO PUBLISH THE GROUP IF IT IS ALL NEW.</strong>
799      *
800      * @param gid the groupID.
801      * @return PeerGroup the initialized (but not started) peergroup.
802      * @throws PeerGroupException Thrown if the group could not be instantiated.
803      */
804     public PeerGroup newGroup(PeerGroupID gid) throws PeerGroupException;
805
806     /*
807      * Shortcuts to the well-known services, in order to avoid calls to
808      * {@link #lookupService(ID)}.
809      */
810
811     /**
812      * Return the Rendezvous Service for this Peer Group. This service is
813      * optional and may not be present in all groups.
814      *
815      * @return The Rendezvous Service for this Peer Group or <code>null</code>
816      *         if there is no Rendezvous Service in this Peer Group.
817      */
818     public RendezVousService getRendezVousService();
819
820     /**
821      * Return the Endpoint Service for this Peer Group. This service is
822      * present in every Peer Group.
823      *
824      * @return EndpointService The Endpoint Service for this Peer Group.
825      */
826     public EndpointService getEndpointService();
827
828     /**
829      * Return the Resolver Service for this Peer Group. This service is
830      * present in every Peer Group.
831      *
832      * @return ResolverService The Resolver Service for this Peer Group.
833      */
834     public ResolverService getResolverService();
835
836     /**
837      * Return the Discovery Service for this Peer Group.
838      *
839      * @return The Discovery Service for this Peer Group or <code>null</code>
840      *         if there is no PeerInfo Service in this Peer Group.
841      */
842     public DiscoveryService getDiscoveryService();
843
844     /**
845      * Return the PeerInfo Service for this Peer Group.
846      *
847      * @return The PeerInfo Service for this Peer Group or <code>null</code>
848      *         if there is no PeerInfo Service in this Peer Group.
849      */
850     public PeerInfoService getPeerInfoService();
851
852     /**
853      * Return the Membership Service for this Peer Group. This service is
854      * present in every Peer Group.
855      *
856      * @return MembershipService The Membership Service for this Peer Group.
857      */
858     public MembershipService getMembershipService();
859
860     /**
861      * Return the Pipe Service for this Peer Group.
862      *
863      * @return The Pipe Service for this Peer Group or <code>null</code> if
864      *         there is no Pipe Service in this Peer Group.
865      */
866     public PipeService getPipeService();
867
868     /**
869      * Return the Access Service for this Peer Group. This service is
870      * present in every Peer Group.
871      *
872      * @return AccessService The Access Service for this Peer Group.
873      * @since JXTA 2.1
874      */
875     public AccessService getAccessService();
876
877     // A few convenience methods. This information is available from the peer and peergroup advertisement.
878
879     /**
880      * Return the Peer Group ID of this Peer Group.
881      *
882      * @return PeerGroupId The Peer Group ID of this Peer Group.
883      */
884     public PeerGroupID getPeerGroupID();
885
886     /**
887      * Return the Peer ID by which this Peer is known within this Peer Group.
888      *
889      * @return the Peer ID by which this Peer is known within this Peer Group.
890      */
891     public PeerID getPeerID();
892
893     /**
894      * Return the Name of this group. This name is not canonical, meaning that
895      * there may be other groups with the same name.
896      *
897      * @return String This groups's name or <code>null</code>  if no name was
898      *         specified.
899      */
900     public String getPeerGroupName();
901
902     /**
903      * Return the name of the local peer within this group. This name is not
904      * canonical, meaning that there may be other peers with the same name.
905      *
906      * @return String This peer's name or <code>null</code> if no name was
907      *         specified.
908      */
909     public String getPeerName();
910
911     /**
912      * Returns the config advertisement for this peer in this group (if any).
913      *
914      * @return The advertisement or <code>null</code> if none is available.
915      */
916     public ConfigParams getConfigAdvertisement();
917
918     /**
919      * Get an all purpose peerGroup ModuleImplAdvertisement that is compatible
920      * with this group. This impl adv can be used to create any group that
921      * relies only on the standard services. Or to derive other impl advs, using
922      * this impl advertisement as a basis.
923      * <p/>
924      * This defines a peergroup implementation that can be used for
925      * many purposes, and from which one may derive slightly different
926      * peergroup implementations.
927      * <p/>
928      * This definition is always the same and has a well known ModuleSpecID.
929      * It includes the basic service, no protocols and the shell for main
930      * application.
931      * <p/>
932      * The user must remember to change the specID if the set of services
933      * protocols or applications is altered before use.
934      *
935      * @return ModuleImplAdvertisement The new peergroup impl adv.
936      * @throws Exception if an error occurs while creating the implementation advertisement
937      */
938     public ModuleImplAdvertisement getAllPurposePeerGroupImplAdvertisement() throws Exception;
939
940     /**
941      * Explicitly notifies a group interface that it will no-longer be
942      * used (similar to dispose). Does nothing to a real group object,
943      * only has an effect on a group interface.
944      */
945     public void unref();
946
947     /**
948      * Returns a weak interface object that represents this
949      * group.
950      * <p/>
951      * A weak interface object has no life-cycle privileges over
952      * the group that it represents and therefore its users have
953      * no accountability. A weak interface object is safe to
954      * give away but holds no promise of sustained validity.
955      * <p/>
956      * Whatever code gave away a weak interface object retains
957      * the power of terminating the group object from which it
958      * was obtained, thereby making the weak interface
959      * object invalid.
960      * <p/>
961      * A weak interface object is immutable; its unref and stopApp
962      * methods do nothing. Its validity is exactly that of the
963      * group or interface object from which it was obtained.
964      * <p/>
965      * A weak interface object can be obtained from an interface
966      * object, or from a real group object, or from a weak interface
967      * object. In the later case, the object returned may be
968      * the original weak interface object since such objects
969      * are immutable.
970      * <p/>
971      * Whatever code obtains a weak interface object from a group object
972      * or regular interface object, remains entirely liable for invoking unref
973      * on the initial object before discarding it. Giving away a weak interface
974      * object is not equivalent to transferring ownership of the original.
975      *
976      * @return PeerGroup A weak interface object that represents this
977      *         PeerGroup object.
978      * @since JXTA 2.2
979      */
980     public PeerGroup getWeakInterface();
981
982     /**
983      * Returns the parent group of this group. Not all groups have parents and
984      * some implementations may not reveal their parents.
985      *
986      * @return PeerGroup the parent group or <code>null</code> if a parent group
987      *         if not available.
988      * @since JXTA 2.3
989      */
990     public PeerGroup getParentGroup();
991
992     /**
993      * Returns the location of the parent of all items that this peer group is
994      * using for persistently storing its preferences, cache, persistent store,
995      * properties, etc. May be {@code null} if the peergroup has no defined
996      * location for storing persistent data.
997      *
998      * @return The location of the parent of all persistent items stored by
999      *         this peer group.
1000      * @since JXTA 2.3.7
1001      */
1002     public URI getStoreHome();    
1003 }